Module: Ardm::Ar::Associations::ClassMethods
- Defined in:
- lib/ardm/ar/associations.rb
Instance Method Summary collapse
- #belongs_to(name, *args) ⇒ Object
- #dump_associations_hash(options) ⇒ Object
- #has(count, name, *args) ⇒ Object
- #n ⇒ Object
-
#relationships ⇒ Object
The reflections returned here don’t look like datamapper relationships.
Instance Method Details
#belongs_to(name, *args) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/ardm/ar/associations.rb', line 69 def belongs_to(name, *args) = args.shift || {} if String === || Class === # belongs_to :name, 'Class', options: 'here' = (args.last || {}).merge(:model => .to_s) end unless Hash === raise ArgumentError, "bad belongs_to #{name} options format #{.inspect}" end if .has_key?(:key) || .has_key?(:unique) raise Ardm::NotImplemented, "belongs to :key and :unique are not implemented." end .delete(:default) required = .delete(:required) opts = Ardm::Ar::Associations.(self, ) super name, *opts model = self Ardm::Ar::Finalize.on_finalize do return @child_key if defined?(@child_key) properties = model.properties assoc = reflect_on_association(name) property_name = assoc.foreign_key target_property = assoc.klass.properties.key.first properties[property_name] || begin = Ardm::Ext::Hash.only(target_property., :length, :precision, :scale, :min, :max).update( :index => name, :required => (required == false ? false : true), :key => false, :unique => false ) model.property(property_name, target_property.to_child_key, ) end end nil end |
#dump_associations_hash(options) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/ardm/ar/associations.rb', line 51 def dump_associations_hash() .inject({}) do |new_attrs, (key, value)| if reflection = reflect_on_association(key.to_sym) if value.is_a?(ActiveRecord::Base) new_attrs[reflection.foreign_key] = value.id if reflection.respond_to?(:polymorphic?) && reflection.polymorphic? new_attrs[reflection.foreign_type] = value.class.base_class end else new_attrs[reflection.foreign_key] = value end else new_attrs[key] = value end new_attrs end end |
#has(count, name, *args) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/ardm/ar/associations.rb', line 117 def has(count, name, *args) = args.shift || {} if String === || Class === # has n, :name, 'Class', options: 'here' = (args.last || {}).merge(:model => .to_s) end unless Hash === raise ArgumentError, "bad has #{count} options format #{.inspect}" end [:order] = Ardm::Ar::Query.order(self, [:order]) if [:order] opts = Ardm::Ar::Associations.(self, , :through, :order, :source) case count when 1 then has_one name, *opts when "many" then has_many name, *opts end end |
#n ⇒ Object
113 114 115 |
# File 'lib/ardm/ar/associations.rb', line 113 def n "many" end |
#relationships ⇒ Object
TODO:
improve this if needed with a wrapper
The reflections returned here don’t look like datamapper relationships.
47 48 49 |
# File 'lib/ardm/ar/associations.rb', line 47 def relationships reflections end |