module Neewom::Proxies::BuildersAndFinders

Public Instance Methods

apply_inputs(resource, controller_params, initial_values: {}) click to toggle source
# File lib/neewom/proxies/builders_and_finders.rb, line 9
def apply_inputs(resource, controller_params, initial_values: {})
  return unless resource.present?

  resource.initialize_neewom_attributes(self) if resource.respond_to?(:initialize_neewom_attributes)

  if controller_params.present?
    params = controller_params

    initial_values.each do |field, value|
      if resource.respond_to?(:"#{field}=")
        resource.public_send(:"#{field}=", value)
      else
        raise "The form trying to setup the #{field} field from an initial data, but the #{resource.class.name} can't accept it"
      end
    end

    data = persisted_fields.each_with_object({}) do |field, acc|
      acc[field.name.to_sym] = params[field.name.to_sym] if params.has_key?(field.name.to_sym)
    end

    resource.assign_attributes(data)
  end

  resource
end
build_resource(controller_params=nil, initial_values: {}) click to toggle source
# File lib/neewom/proxies/builders_and_finders.rb, line 4
def build_resource(controller_params=nil, initial_values: {})
  resource = repository_klass.constantize.new
  apply_inputs(resource, controller_params, initial_values: initial_values)
end
find(id) click to toggle source
# File lib/neewom/proxies/builders_and_finders.rb, line 41
def find(id)
  resource = repository_klass.constantize.find(id)
  resource.initialize_neewom_attributes(self)

  resource
end
find_and_apply_inputs(id, form_params, initial_values: {}) click to toggle source
# File lib/neewom/proxies/builders_and_finders.rb, line 62
def find_and_apply_inputs(id, form_params, initial_values: {})
  apply_inputs(find(id), form_params, initial_values: initial_values)
end
find_by(options) click to toggle source
# File lib/neewom/proxies/builders_and_finders.rb, line 48
def find_by(options)
  resource = repository_klass.constantize.find_by(options)
  resource.initialize_neewom_attributes(self) if resource.present?

  resource
end
find_by!(options) click to toggle source
# File lib/neewom/proxies/builders_and_finders.rb, line 55
def find_by!(options)
  resource = repository_klass.constantize.find_by!(options)
  resource.initialize_neewom_attributes(self)

  resource
end
find_by_and_apply_inputs(options, form_params, initial_values: {}) click to toggle source
# File lib/neewom/proxies/builders_and_finders.rb, line 66
def find_by_and_apply_inputs(options, form_params, initial_values: {})
  apply_inputs(find_by(options), form_params, initial_values: initial_values)
end
find_by_and_apply_inputs!(options, form_params, initial_values: {}) click to toggle source
# File lib/neewom/proxies/builders_and_finders.rb, line 70
def find_by_and_apply_inputs!(options, form_params, initial_values: {})
  apply_inputs(find_by!(options), form_params, initial_values: initial_values)
end
parse_submit_control_value(controller_params) click to toggle source
# File lib/neewom/proxies/builders_and_finders.rb, line 35
def parse_submit_control_value(controller_params)
  submit_fields.each_with_object({}) do |field, acc|
    acc[field.name.to_sym] = controller_params[field.name.to_sym] if controller_params.has_key?(field.name.to_sym)
  end
end
strong_params_permit() click to toggle source
# File lib/neewom/proxies/builders_and_finders.rb, line 78
def strong_params_permit
  fields.map(&:name)
end
strong_params_require() click to toggle source
# File lib/neewom/proxies/builders_and_finders.rb, line 74
def strong_params_require
  repository_klass.constantize.model_name.param_key.to_sym
end