Module: Neewom::Proxies::BuildersAndFinders

Included in:
AbstractForm
Defined in:
lib/neewom/proxies/builders_and_finders.rb

Instance Method Summary collapse

Instance Method Details

#apply_inputs(resource, controller_params, initial_values: {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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: {}) ⇒ Object



4
5
6
7
# 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) ⇒ Object



41
42
43
44
45
46
# 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: {}) ⇒ Object



62
63
64
# 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) ⇒ Object



48
49
50
51
52
53
# 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) ⇒ Object



55
56
57
58
59
60
# 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: {}) ⇒ Object



66
67
68
# 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: {}) ⇒ Object



70
71
72
# 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) ⇒ Object



35
36
37
38
39
# 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_permitObject



78
79
80
# File 'lib/neewom/proxies/builders_and_finders.rb', line 78

def strong_params_permit
  fields.map(&:name)
end

#strong_params_requireObject



74
75
76
# File 'lib/neewom/proxies/builders_and_finders.rb', line 74

def strong_params_require
  repository_klass.constantize.model_name.param_key.to_sym
end