Module: Hearsay::Referenceable::ClassMethods
- Defined in:
- lib/hearsay/referenceable.rb
Instance Method Summary collapse
-
#referenced_by(source_name, options = {}) ⇒ Object
Public: Set up referencer assocations.
Instance Method Details
#referenced_by(source_name, options = {}) ⇒ Object
Public: Set up referencer assocations
source_name - The name of the source association (model referencing object) options - Options hash (default: {}):
:attribute - Name of referencer attribute containing text references (required)
:class_name - The class of the source association, if the name can't be inferred from
the association name (optional)
Examples
referenced_by :issues, :method => :summary
referenced_by :referencing_tweets, :method => :body, :class_name => 'Tweet'
Returns nothing.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/hearsay/referenceable.rb', line 22 def referenced_by(source_name, = {}) attribute_name = .delete(:attribute) raise ArgumentError, ":attribute is required for references" if attribute_name.blank? association_name = "#{source_name.to_s.singularize}_references".to_sym class_eval do has_many(association_name, :as => :referenceable, :class_name => 'Reference', :conditions => {:attribute_name => attribute_name}, :dependent => :destroy) unless self.respond_to?(association_name) has_many source_name, :through => association_name, :source => :referencer, :source_type => ([:class_name] || source_name.to_s.classify) end end |