module SimpleParams::RailsHelpers::ClassMethods

Attributes

rails_helpers[RW]

Public Instance Methods

define_rails_helpers(name, klass) click to toggle source
# 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

  # TODO: This needs more testing
  singular_key = singularized_key(name)
  define_method("build_#{singular_key}") do |value={}|
    klass.new(value, self)
  end
end
klass() click to toggle source

Used with reflect_on_association

# File lib/simple_params/concerns/rails_helpers.rb, line 52
def klass
  self
end
reflect_on_association(assoc_sym) click to toggle source

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

# File lib/simple_params/concerns/rails_helpers.rb, line 47
def reflect_on_association(assoc_sym)
  nested_classes[assoc_sym]
end
using_rails_helpers?() click to toggle source
# File lib/simple_params/concerns/rails_helpers.rb, line 36
def using_rails_helpers?
  @rails_helpers||= false
  !!@rails_helpers
end
with_rails_helpers() click to toggle source
# File lib/simple_params/concerns/rails_helpers.rb, line 32
def with_rails_helpers
  @rails_helpers = true
end

Private Instance Methods

singularized_key(key) click to toggle source
# File lib/simple_params/concerns/rails_helpers.rb, line 72
def singularized_key(key)
  key.to_s.singularize
end