Module: Spyke::Associations::ClassMethods
- Defined in:
- lib/spyke/associations.rb
Instance Method Summary collapse
- #accepts_nested_attributes_for(*names) ⇒ Object
- #belongs_to(name, options = {}) ⇒ Object
- #has_many(name, options = {}) ⇒ Object
- #has_one(name, options = {}) ⇒ Object
- #reflect_on_association(name) ⇒ Object
Instance Method Details
#accepts_nested_attributes_for(*names) ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/spyke/associations.rb', line 46 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 |
#belongs_to(name, options = {}) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/spyke/associations.rb', line 38 def belongs_to(name, = {}) create_association(name, BelongsTo, ) define_method "build_#{name}" do |attributes = nil| association(name).build(attributes) end end |
#has_many(name, options = {}) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/spyke/associations.rb', line 17 def has_many(name, = {}) create_association(name, 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 |
#has_one(name, options = {}) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/spyke/associations.rb', line 30 def has_one(name, = {}) create_association(name, HasOne, ) define_method "build_#{name}" do |attributes = nil| association(name).build(attributes) end end |
#reflect_on_association(name) ⇒ Object
56 57 58 59 |
# File 'lib/spyke/associations.rb', line 56 def reflect_on_association(name) # Just enough to support nested_form gem associations[name] || associations[name.to_s.pluralize.to_sym] end |