Class: Rectify::Form

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/rectify/form.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



6
7
8
# File 'lib/rectify/form.rb', line 6

def context
  @context
end

Class Method Details

.from_json(json) ⇒ Object



29
30
31
# File 'lib/rectify/form.rb', line 29

def self.from_json(json)
  from_params(JSON.parse(json))
end

.from_model(model) ⇒ Object



25
26
27
# File 'lib/rectify/form.rb', line 25

def self.from_model(model)
  Rectify::BuildFormFromModel.new(self, model).build
end

.from_params(params, additional_params = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rectify/form.rb', line 10

def self.from_params(params, additional_params = {})
  params_hash = hash_from(params)

  attributes_hash = params_hash
    .fetch(mimicked_model_name, {})
    .merge(params_hash)
    .merge(additional_params)

  formatted_attributes = FormatAttributesHash
    .new(attribute_set)
    .format(attributes_hash)

  new(formatted_attributes)
end

.hash_from(params) ⇒ Object



52
53
54
55
# File 'lib/rectify/form.rb', line 52

def self.hash_from(params)
  params = params.to_unsafe_h if params.respond_to?(:to_unsafe_h)
  params.with_indifferent_access
end

.infer_model_nameObject



41
42
43
44
45
46
# File 'lib/rectify/form.rb', line 41

def self.infer_model_name
  class_name = name.split("::").last
  return :form if class_name == "Form"

  class_name.chomp("Form").underscore.to_sym
end

.mimic(model_name) ⇒ Object



33
34
35
# File 'lib/rectify/form.rb', line 33

def self.mimic(model_name)
  @model_name = model_name.to_s.underscore.to_sym
end

.mimicked_model_nameObject



37
38
39
# File 'lib/rectify/form.rb', line 37

def self.mimicked_model_name
  @model_name || infer_model_name
end

.model_nameObject



48
49
50
# File 'lib/rectify/form.rb', line 48

def self.model_name
  ActiveModel::Name.new(self, nil, mimicked_model_name.to_s.camelize)
end

Instance Method Details

#attributesObject



79
80
81
# File 'lib/rectify/form.rb', line 79

def attributes
  super.except(:id)
end

#attributes_with_valuesObject



83
84
85
# File 'lib/rectify/form.rb', line 83

def attributes_with_values
  attributes.reject { |attribute| public_send(attribute).nil? }
end

#before_validationObject



93
94
95
96
# File 'lib/rectify/form.rb', line 93

def before_validation
  # Implement this in your form object if you would like to perform some
  # some processing before validation happens (optional).
end

#map_model(model) ⇒ Object



87
88
89
90
91
# File 'lib/rectify/form.rb', line 87

def map_model(model)
  # Implement this in your form object for custom mapping from model to form
  # object as part of the `.from_model` call after matching attributes are
  # populated (optional).
end

#persisted?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/rectify/form.rb', line 57

def persisted?
  id.present? && id.to_i > 0
end

#to_keyObject



67
68
69
# File 'lib/rectify/form.rb', line 67

def to_key
  [id]
end

#to_modelObject



71
72
73
# File 'lib/rectify/form.rb', line 71

def to_model
  self
end

#to_paramObject



75
76
77
# File 'lib/rectify/form.rb', line 75

def to_param
  id.to_s
end

#valid?(context = nil) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
# File 'lib/rectify/form.rb', line 61

def valid?(context = nil)
  before_validation

  [super, form_attributes_valid?, arrays_attributes_valid?].all?
end

#with_context(new_context) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/rectify/form.rb', line 98

def with_context(new_context)
  @context = if new_context.is_a?(Hash)
               OpenStruct.new(new_context)
             else
               new_context
             end

  attributes_that_respond_to(:with_context)
    .each { |f| f.with_context(context) }

  array_attributes_that_respond_to(:with_context)
    .each { |f| f.with_context(context) }

  self
end