Module: ActsAsAliased::ClassMethods

Defined in:
lib/acts_as_aliased.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_aliased(options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/acts_as_aliased.rb', line 14

def acts_as_aliased options = {}
  has_many :aliases, as: :aliased, class_name: ::ActsAsAliased::Alias

  cattr_accessor :associations
  cattr_accessor :column
  self.associations = options[:associations] || []
  self.column       = options[:column]       || 'name'

  class_eval <<-EOV
    include ActsAsAliased::InstanceMethods

    def self.lookup(value)
      return nil if value.blank?
      self.send("find_by_#{column}", value) ||
      Alias.where(["aliased_type = ? AND name = ?", self.to_s, value]).first.try(:aliased)
    end
  EOV
end