module Answers
require 'truncate_html' require 'will_paginate' surrequire 'answers/i18n'
Public Class Methods
Constructs a deprecation warning message and warns with Kernel#warn
Example:
Answers.deprecate('foo') => "The use of 'foo' is deprecated"
An options parameter can be specified to construct a more detailed deprecation message
Options:
when - version that this deprecated feature will be removed replacement - a replacement for what is being deprecated caller - who called the deprecated feature
Example:
Answers.deprecate('foo', :when => 'tomorrow', :replacement => 'bar') => "The use of 'foo' is deprecated and will be removed at version 2.0. Please use 'bar' instead."
# File lib/answers.rb, line 70 def deprecate(what, options = {}) # Build a warning. warning = "\n-- DEPRECATION WARNING --\nThe use of '#{what}' is deprecated" warning << " and will be removed at version #{options[:when]}" if options[:when] warning << "." warning << "\nPlease use #{options[:replacement]} instead." if options[:replacement] # See if we can trace where this happened if (invoker = detect_invoker(options[:caller])).present? warning << invoker end # Give stern talking to. warn warning end
Returns true if an extension is currently registered with Answers
Example:
Answers.extension_registered?(Answers::Core)
# File lib/answers.rb, line 51 def extension_registered?(const) @@extensions.include?(const) end
Returns an array of modules representing currently registered Answers
Engines
Example:
Answers.extensions => [Answers::Core, Answers::Pages]
# File lib/answers.rb, line 22 def extensions @@extensions end
# File lib/answers.rb, line 149 def include_once(base, extension_module) base.send :include, extension_module unless included_extension_module?(base, extension_module) end
Register an extension with Answers
Example:
Answers.register_extension(Answers::Core)
# File lib/answers.rb, line 30 def register_extension(const) return if extension_registered?(const) validate_extension!(const) @@extensions << const end
Returns a Pathname to the root of the Answers
project
# File lib/answers.rb, line 87 def root @root ||= Pathname.new(File.expand_path('../../../', __FILE__)) end
Returns an array of Pathnames pointing to the root directory of each extension that has been registered with Answers
.
Example:
Answers.roots => [#<Pathname:/Users/Reset/Code/answerscms/core>, #<Pathname:/Users/Reset/Code/answerscms/pages>]
An optional extension_name parameter can be specified to return just the Pathname for the specified extension. This can be represented in Constant, Symbol, or String form.
Example:
Answers.roots(Answers::Core) => #<Pathname:/Users/Reset/Code/answerscms/core> Answers.roots(:'answers/core') => #<Pathname:/Users/Reset/Code/answerscms/core> Answers.roots("answers/core") => #<Pathname:/Users/Reset/Code/answerscms/core>
# File lib/answers.rb, line 104 def roots(extension_name = nil) return @roots ||= self.extensions.map(&:root) if extension_name.nil? extension_name.to_s.camelize.constantize.root end
Returns string version of url helper path. We need this to temporarily support namespaces like Answers::Image and Answers::Blog::Post
Example:
Answers.route_for_model(Answers::Image) => "admin_image_path" Answers.route_for_model(Answers::Image, {:plural => true}) => "admin_images_path" Answers.route_for_model(Answers::Blog::Post) => "blog_admin_post_path" Answers.route_for_model(Answers::Blog::Post, {:plural => true}) => "blog_admin_posts_path" Answers.route_for_model(Answers::Blog::Post, {:admin => false}) => "blog_post_path"
# File lib/answers.rb, line 123 def route_for_model(klass, options = {}) options = {:plural => false, :admin => true}.merge options klass = klass.constantize if klass.respond_to?(:constantize) active_name = ::ActiveModel::Name.new( klass, (Answers if klass.parents.include?(Answers)) ) if options[:admin] # Most of the time this gets rid of 'answers' parts = active_name.to_s.underscore.split('/').reject{|name| active_name.singular_route_key.exclude?(name) } # Get the singular resource_name from the url parts resource_name = parts.pop resource_name = resource_name.pluralize if options[:plural] [parts.join("_"), "admin", resource_name, "path"].reject(&:blank?).join "_" else path = options[:plural] ? active_name.route_key : active_name.singular_route_key [path, 'path'].join '_' end end
Unregister an extension from Answers
Example:
Answers.unregister_extension(Answers::Core)
# File lib/answers.rb, line 43 def unregister_extension(const) @@extensions.delete(const) end
# File lib/answers.rb, line 110 def version Answers::Version.to_s end
Private Class Methods
# File lib/answers.rb, line 154 def detect_invoker(the_caller = caller) return '' unless the_caller && the_caller.respond_to?(:detect) the_caller.detect{|c| /#{Rails.root}/ === c }.inspect.to_s.split(':in').first end
plain Module#included? or Module#included_modules doesn't cut it here
# File lib/answers.rb, line 160 def included_extension_module?(base, extension_module) if base.kind_of?(Class) direct_superclass = base.superclass base.ancestors.take_while {|ancestor| ancestor != direct_superclass}.include?(extension_module) else base < extension_module # can't do better than that for modules end end
# File lib/answers.rb, line 169 def validate_extension!(const) unless const.respond_to?(:root) && const.root.is_a?(Pathname) raise InvalidEngineError, "Engine must define a root accessor that returns a pathname to its root" end end
Public Instance Methods
# File lib/answers/admin/users.rb, line 20 def check_access correct_user? unless current_admin_user end
# File lib/answers/admin/question.rb, line 7 def permitted_params params.permit! end
# File lib/answers/admin/users.rb, line 9 def update_resource(object, attributes) if current_admin_user end attributes.delete_if do |k,v| [:is_admin, :is_editor, :is_writer].include?(k) and !current_admin_user? end update_method = attributes.first[:password].present? ? :update_attributes : :update_without_password object.send(update_method, *attributes) end