Class: Og::RefersTo
Instance Attribute Summary
Attributes inherited from Relation
Class Method Summary collapse
Instance Method Summary collapse
Methods inherited from Relation
#[], #[]=, enchant, #initialize, #method_missing, #polymorphic?, #polymorphic_marker?, resolve, resolve_names, #resolve_polymorphic, resolve_polymorphic_markers, resolve_polymorphic_relations, resolve_targets, symbol_to_class, #to_s
Constructor Details
This class inherits a constructor from Og::Relation
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Og::Relation
Class Method Details
.foreign_key(rel) ⇒ Object
7 8 9 |
# File 'lib/og/relation/refers_to.rb', line 7 def self.foreign_key(rel) "#{rel[:foreign_name] || rel[:target_singular_name]}_#{rel[:target_class].primary_key}" end |
Instance Method Details
#enchant ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/og/relation/refers_to.rb', line 11 def enchant raise "#{target_singular_name} in #{owner_class} refers to an undefined class" if target_class.nil? self[:foreign_key] = "#{foreign_name || target_singular_name}_#{target_class.primary_key}" if self[:field] field = ", :field => :#{self[:field]}" end owner_class.module_eval %{ attr_accessor :#{target_singular_name} prop_accessor :#{foreign_key}, #{target_class.primary_key.klass}#{field}, :relation => true def #{target_singular_name}(reload = false) return nil if @#{foreign_key}.nil? # will reload if forced or first load or if reload or not @#{target_singular_name} @#{target_singular_name} = #{target_class}[@#{foreign_key}] end @#{target_singular_name} end def #{target_singular_name}=(obj) @#{foreign_key} = obj.#{target_class.primary_key} if obj end } end |