class Redress::Utils::BuildFormFromModel

Public Class Methods

new(form_class, model) click to toggle source
# File lib/redress/utils/build_form_from_model.rb, line 6
def initialize(form_class, model)
  @form_class = form_class
  @model = model
end

Public Instance Methods

build() click to toggle source
# File lib/redress/utils/build_form_from_model.rb, line 11
def build
  form.map_model(@model)
  form
end

Private Instance Methods

form() click to toggle source
# File lib/redress/utils/build_form_from_model.rb, line 18
def form
  @form ||= @form_class.new(model_attributes)
end
matching_attributes() click to toggle source
# File lib/redress/utils/build_form_from_model.rb, line 31
def matching_attributes
  @form_class.attribute_names.select { |name| @model.respond_to?(name) }
end
model_attributes() click to toggle source
# File lib/redress/utils/build_form_from_model.rb, line 22
def model_attributes
  matching_attributes.each_with_object({}) do |name, hash|
    value = @model.public_send(name)
    next if value.nil?

    hash[name] = value
  end
end