Module: SimpleParams::RailsHelpers::ClassMethods
- Defined in:
- lib/simple_params/concerns/rails_helpers.rb
Instance Attribute Summary collapse
-
#rails_helpers ⇒ Object
Returns the value of attribute rails_helpers.
Instance Method Summary collapse
- #define_rails_helpers(name, klass) ⇒ Object
-
#klass ⇒ Object
# Used with reflect_on_association.
-
#reflect_on_association(assoc_sym) ⇒ Object
Author.reflect_on_association(:books).klass # => Book.
- #using_rails_helpers? ⇒ Boolean
- #with_rails_helpers ⇒ Object
Instance Attribute Details
#rails_helpers ⇒ Object
Returns the value of attribute rails_helpers.
30 31 32 |
# File 'lib/simple_params/concerns/rails_helpers.rb', line 30 def rails_helpers @rails_helpers end |
Instance Method Details
#define_rails_helpers(name, klass) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/simple_params/concerns/rails_helpers.rb', line 56 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 |
#klass ⇒ Object
# Used with reflect_on_association
52 53 54 |
# File 'lib/simple_params/concerns/rails_helpers.rb', line 52 def klass self end |
#reflect_on_association(assoc_sym) ⇒ Object
Author.reflect_on_association(:books).klass # => Book
47 48 49 |
# File 'lib/simple_params/concerns/rails_helpers.rb', line 47 def reflect_on_association(assoc_sym) nested_classes[assoc_sym] end |
#using_rails_helpers? ⇒ Boolean
36 37 38 39 |
# File 'lib/simple_params/concerns/rails_helpers.rb', line 36 def using_rails_helpers? @rails_helpers||= false !!@rails_helpers end |
#with_rails_helpers ⇒ Object
32 33 34 |
# File 'lib/simple_params/concerns/rails_helpers.rb', line 32 def with_rails_helpers @rails_helpers = true end |