module ActiveRepository::Associations::Methods
Public Instance Methods
belongs_to(association_id, options = {})
click to toggle source
Defines “belongs to” type relation between ActiveRepository
objects
# File lib/active_repository/associations.rb, line 40 def belongs_to(association_id, options = {}) options = { class_name: association_id.to_s.classify, foreign_key: association_id.to_s.foreign_key }.merge(options) field options[:foreign_key].to_sym belongs_to_methods(association_id, options) end
has_many(association_id, options = {})
click to toggle source
Defines “has many” type relation between ActiveRepository
objects
# File lib/active_repository/associations.rb, line 15 def has_many(association_id, options = {}) define_method(association_id) do options = { class_name: association_id.to_s.classify, foreign_key: self.class.to_s.foreign_key }.merge(options) foreign_key = options[:foreign_key] klass = options[:class_name].constantize klass.where(foreign_key => id) end end
has_one(association_id, options = {})
click to toggle source
Defines “has one” type relation between ActiveRepository
objects
# File lib/active_repository/associations.rb, line 30 def has_one(association_id, options = {}) options = { class_name: association_id.to_s.classify, foreign_key: self.to_s.foreign_key }.merge(options) has_one_methods(association_id, options) end
Private Instance Methods
belongs_to_methods(association_id, options)
click to toggle source
# File lib/active_repository/associations.rb, line 58 def belongs_to_methods(association_id, options) define_belongs_to_method(association_id, options) define_belongs_to_setter(association_id, options) define_belongs_to_create(association_id, options) end
define_belongs_to_create(association_id, options)
click to toggle source
# File lib/active_repository/associations.rb, line 115 def define_belongs_to_create(association_id, options) define_method("create_#{association_id}") do |attributes| klass = options[:class_name].constantize object = klass.create(attributes) self.update_attribute(options[:foreign_key], object.id) object end end
define_belongs_to_method(association_id, options)
click to toggle source
# File lib/active_repository/associations.rb, line 99 def define_belongs_to_method(association_id, options) define_method(association_id) do klass = options[:class_name].constantize foreign_key = send(options[:foreign_key]) klass.where(klass.primary_key => foreign_key).first end end
define_belongs_to_setter(association_id, options)
click to toggle source
# File lib/active_repository/associations.rb, line 108 def define_belongs_to_setter(association_id, options) define_method("#{association_id}=") do |new_value| attributes.delete(association_id.to_sym) send("#{options[:foreign_key]}=", (new_value.try(:id) ? new_value.id : new_value)) end end
define_has_one_create(association_id, options)
click to toggle source
# File lib/active_repository/associations.rb, line 85 def define_has_one_create(association_id, options) define_method("create_#{association_id}") do |attributes| primary_key = self.send(self.class.primary_key) foreign_key = self.class.to_s.foreign_key association = self.send(association_id) klass = options[:class_name].constantize association.update_attribute(foreign_key, nil) if self.send("#{association_id}=", nil) klass.create(attributes.merge(foreign_key => primary_key)) end end
define_has_one_method(association_id, options)
click to toggle source
# File lib/active_repository/associations.rb, line 64 def define_has_one_method(association_id, options) define_method(association_id) do foreign_key = options[:foreign_key] klass = options[:class_name].constantize klass.where(foreign_key => self.id).first end end
define_has_one_setter(association_id, options)
click to toggle source
# File lib/active_repository/associations.rb, line 73 def define_has_one_setter(association_id, options) define_method("#{association_id}=") do |object| primary_key = self.send(self.class.primary_key) foreign_key = self.class.to_s.foreign_key association = self.send(association_id) association.update_attribute(foreign_key, nil) if association object.update_attribute(foreign_key, primary_key) if object end end
has_one_methods(association_id, options)
click to toggle source
# File lib/active_repository/associations.rb, line 52 def has_one_methods(association_id, options) define_has_one_method(association_id, options) define_has_one_setter(association_id, options) define_has_one_create(association_id, options) end