Class: Rectify::Form

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.array_attribute_namesObject



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

def self.array_attribute_names
  attribute_set.select { |a| a.primitive == Array }.map { |a| a.name.to_s }
end

.convert_indexed_hashes_to_arrays(attributes_hash) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/rectify/form.rb', line 23

def self.convert_indexed_hashes_to_arrays(attributes_hash)
  array_attribute_names.each do |name|
    attribute = attributes_hash[name]
    next unless attribute.is_a?(Hash)

    attributes_hash[name] = attribute.values
  end
end

.from_json(json) ⇒ Object



40
41
42
# File 'lib/rectify/form.rb', line 40

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

.from_model(model) ⇒ Object



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

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

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



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

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

  attribute_names = attribute_set.map(&:name)

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

  convert_indexed_hashes_to_arrays(attributes_hash)

  new(attributes_hash)
end

.hash_from(params) ⇒ Object



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

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



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

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



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

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

.mimicked_model_nameObject



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

def self.mimicked_model_name
  @model_name || infer_model_name
end

.model_nameObject



59
60
61
# File 'lib/rectify/form.rb', line 59

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

Instance Method Details

#attributesObject



90
91
92
# File 'lib/rectify/form.rb', line 90

def attributes
  super.except(:id)
end

#before_validationObject



100
101
102
103
# File 'lib/rectify/form.rb', line 100

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



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

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)


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

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

#to_keyObject



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

def to_key
  [id]
end

#to_modelObject



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

def to_model
  self
end

#to_paramObject



86
87
88
# File 'lib/rectify/form.rb', line 86

def to_param
  id.to_s
end

#valid?(context = nil) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
76
# File 'lib/rectify/form.rb', line 72

def valid?(context = nil)
  before_validation

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