Module: SimpleParams::RailsHelpers::ClassMethods

Defined in:
lib/simple_params/concerns/rails_helpers.rb

Instance Method Summary collapse

Instance Method Details

#define_rails_helpers(name, klass) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/simple_params/concerns/rails_helpers.rb', line 30

def define_rails_helpers(name, klass)
  # E.g. if we have a nested_class named :phones, then we need:
  #  - a method called :phones_attributes that also sets :phones
  #  - a method called :build_phone

  define_method("#{name}_attributes=") do |value|
    send("#{name}=", value)
  end

  singular_key = singularized_key(name)
  define_method("build_#{singular_key}") do |value={}|
    klass.new(value, self)
  end
end

#klassObject

Used with reflect_on_association



26
27
28
# File 'lib/simple_params/concerns/rails_helpers.rb', line 26

def klass
  self
end

#reflect_on_association(assoc_sym) ⇒ Object

Author.reflect_on_association(:books).klass # => Book



21
22
23
# File 'lib/simple_params/concerns/rails_helpers.rb', line 21

def reflect_on_association(assoc_sym)
  nested_classes[assoc_sym]
end