Module: ModelFormFacade

Extended by:
ActiveSupport::Concern
Defined in:
lib/model_form_facade.rb,
lib/model_form_facade/version.rb

Defined Under Namespace

Modules: FieldMethods Classes: Error, Field, MockObject

Constant Summary collapse

VERSION =
"0.3.0"

Instance Method Summary collapse

Instance Method Details

#as_json(root: params_root) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/model_form_facade.rb', line 26

def as_json(root: params_root)
  hash = self.class.fields.values.filter_map do |field|
    next nil unless field.read

    raw = send(field.name)
    value = case field.type
    in :scalar then raw
    in :object then field.form.new(raw).as_json(root: false)
    in :array then (raw || []).map { field.form.new(_1).as_json(root: false) }
    end
    [field.name, field.serialize(value)]
  end.to_h.compact
  return hash unless (root = _params_root(root:))
  {root => hash}
end

#assign_attributes(hash) ⇒ Object Also known as: attributes=



71
72
73
74
75
# File 'lib/model_form_facade.rb', line 71

def assign_attributes(hash)
  hash.each do |key, value|
    send("#{key}=", value)
  end
end

#attributesObject



78
# File 'lib/model_form_facade.rb', line 78

def attributes = as_json

#errors(root: params_root) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/model_form_facade.rb', line 42

def errors(root: params_root)
  errs = object_error_messages.reject { |k, v| k.to_s.include? "." } # Remove nested, we'll recurse
  hash = self.class.fields.values.filter_map do |field|
    err = case field.type
    in :scalar then errs[field.attribute]&.map(&:capitalize)&.join("; ").presence
    in :object then field.form.new(send(field.name)).errors(root: false)
    in :array then (send(field.name) || []).map { field.form.new(_1).errors(root: false) }
    end
    [field.name, err] if err
  end.to_h.compact
  return hash unless (root = _params_root(root:))
  {root => hash}
end

#expectation(root: nil) ⇒ Object



80
81
82
# File 'lib/model_form_facade.rb', line 80

def expectation(root: nil)
  self.class.expectation(root: _params_root(root:))
end

#form_props(root: params_root) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/model_form_facade.rb', line 56

def form_props(root: params_root)
  {
    **component_props,
    data: as_json(root:),
    options: options,
    errors: errors(root:)
  }
end

#initialize(model_object = nil, root: nil, **component_props) ⇒ Object



18
19
20
21
22
# File 'lib/model_form_facade.rb', line 18

def initialize(model_object = nil, root: nil, **component_props)
  self.params_root = root unless root.nil?
  self.object = model_object
  self.component_props = component_props
end

#optionsObject

Override me



24
# File 'lib/model_form_facade.rb', line 24

def options = {} # Override me

#saveObject



86
# File 'lib/model_form_facade.rb', line 86

def save = object.save

#save!Object



84
# File 'lib/model_form_facade.rb', line 84

def save! = object.save!

#set_fields(params, root: nil) ⇒ Object Also known as: fields=



65
66
67
68
# File 'lib/model_form_facade.rb', line 65

def set_fields(params, root: nil)
  params = params.expect(expectation(root:)) if params.respond_to?(:expect) && !params.permitted?
  assign_attributes(params)
end