Sha256: daae98950aed5932596e6b0f6ae0395090fec4d1603426e54f06bafacf815c20
Contents?: true
Size: 1.77 KB
Versions: 4
Compression:
Stored size: 1.77 KB
Contents
require 'spyke/associations/association' require 'spyke/associations/has_many' require 'spyke/associations/has_one' require 'spyke/associations/belongs_to' module Spyke module Associations extend ActiveSupport::Concern included do class_attribute :associations self.associations = {}.freeze end module ClassMethods def has_many(name, options = {}) self.associations = associations.merge(name => options.merge(type: HasMany)) define_method "#{name.to_s.singularize}_ids=" do |ids| attributes[name] = [] ids.reject(&:blank?).each { |id| association(name).build(id: id) } end define_method "#{name.to_s.singularize}_ids" do association(name).map(&:id) end end def has_one(name, options = {}) self.associations = associations.merge(name => options.merge(type: HasOne)) define_method "build_#{name}" do |attributes = nil| association(name).build(attributes) end end def belongs_to(name, options = {}) self.associations = associations.merge(name => options.merge(type: BelongsTo)) define_method "build_#{name}" do |attributes = nil| association(name).build(attributes) end end def accepts_nested_attributes_for(*names) names.each do |association_name| class_eval <<-RUBY, __FILE__, __LINE__ + 1 def #{association_name}_attributes=(association_attributes) association(:#{association_name}).assign_nested_attributes(association_attributes) end RUBY end end def reflect_on_association(name) Relation.new(name.to_s.classify.constantize) # Just enough to support nested_form gem end end end end
Version data entries
4 entries across 4 versions & 1 rubygems
Version | Path |
---|---|
spyke-1.8.4 | lib/spyke/associations.rb |
spyke-1.8.3 | lib/spyke/associations.rb |
spyke-1.8.2 | lib/spyke/associations.rb |
spyke-1.8.1 | lib/spyke/associations.rb |