Module: Answers

Defined in:
lib/answers/core.rb,
lib/answers.rb,
lib/answers/engine.rb,
lib/answers/errors.rb,
lib/answers/plugin.rb,
lib/answers/plugins.rb,
lib/answers/version.rb,
lib/answers/admin/tag.rb,
app/models/answers/user.rb,
lib/answers/admin/users.rb,
lib/answers/core/engine.rb,
lib/answers/admin/answer.rb,
app/models/answers/answer.rb,
app/models/answers/ability.rb,
lib/answers/admin/question.rb,
app/models/answers/question.rb,
lib/answers/admin/dashboard.rb,
lib/answers/core/configuration.rb,
app/helpers/answers/tags_helper.rb,
app/models/answers/administrator.rb,
lib/generators/answers/generator.rb,
app/models/answers/api/v1/api_user.rb,
app/controllers/answers/home_controller.rb,
app/controllers/answers/tags_controller.rb,
lib/generators/answers/cms/cms_generator.rb,
app/controllers/answers/search_controller.rb,
app/controllers/answers/answers_controller.rb,
lib/generators/answers/core/core_generator.rb,
app/controllers/answers/questions_controller.rb,
lib/generators/answers/dummy/dummy_generator.rb,
app/controllers/answers/api/v1/api_controller.rb,
app/controllers/answers/api/v1/tags_controller.rb,
app/controllers/answers/application_controller.rb,
lib/generators/answers/engine/engine_generator.rb,
app/controllers/answers/api/v1/answers_controller.rb,
app/controllers/answers/api/v1/taggings_controller.rb,
app/controllers/answers/api/v1/questions_controller.rb

Overview

surrequire ‘answers/i18n’

Defined Under Namespace

Modules: Core, Engine, TagsHelper Classes: Ability, Administrator, Answer, AnswersController, AnswersError, ApplicationController, CmsGenerator, CoreGenerator, DummyGenerator, EngineGenerator, Generator, HomeController, InvalidEngineError, Plugin, Plugins, Question, QuestionsController, SearchController, TagsController, User, Version

Constant Summary collapse

@@extensions =
[]

Class Method Summary collapse

Class Method Details

.deprecate(what, options = {}) ⇒ Object

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."


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 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

.extension_registered?(const) ⇒ Boolean

Returns true if an extension is currently registered with Answers

Example:

Answers.extension_registered?(Answers::Core)

Returns:

  • (Boolean)


51
52
53
# File 'lib/answers.rb', line 51

def extension_registered?(const)
  @@extensions.include?(const)
end

.extensionsObject

Returns an array of modules representing currently registered Answers Engines

Example:

Answers.extensions  =>  [Answers::Core, Answers::Pages]


22
23
24
# File 'lib/answers.rb', line 22

def extensions
  @@extensions
end

.include_once(base, extension_module) ⇒ Object



149
150
151
# 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_extension(const) ⇒ Object Also known as: register_engine

Register an extension with Answers

Example:

Answers.register_extension(Answers::Core)


30
31
32
33
34
35
36
# File 'lib/answers.rb', line 30

def register_extension(const)
  return if extension_registered?(const)

  validate_extension!(const)

  @@extensions << const
end

.rootObject

Returns a Pathname to the root of the Answers project



87
88
89
# File 'lib/answers.rb', line 87

def root
  @root ||= Pathname.new(File.expand_path('../../../', __FILE__))
end

.roots(extension_name = nil) ⇒ Object

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>


104
105
106
107
108
# 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

.route_for_model(klass, options = {}) ⇒ Object

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"


123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# 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_extension(const) ⇒ Object

Unregister an extension from Answers

Example:

Answers.unregister_extension(Answers::Core)


43
44
45
# File 'lib/answers.rb', line 43

def unregister_extension(const)
  @@extensions.delete(const)
end

.versionObject



110
111
112
# File 'lib/answers.rb', line 110

def version
  Answers::Version.to_s
end