Class: Rectify::Form
- Inherits:
-
Object
- Object
- Rectify::Form
- Includes:
- ActiveModel::Validations
- Defined in:
- lib/rectify/form.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
Class Method Summary collapse
- .array_attribute_names ⇒ Object
- .convert_indexed_hashes_to_arrays(attributes_hash) ⇒ Object
- .from_json(json) ⇒ Object
- .from_model(model) ⇒ Object
- .from_params(params, additional_params = {}) ⇒ Object
- .hash_from(params) ⇒ Object
- .infer_model_name ⇒ Object
- .mimic(model_name) ⇒ Object
- .mimicked_model_name ⇒ Object
- .model_name ⇒ Object
Instance Method Summary collapse
- #attributes ⇒ Object
- #before_validation ⇒ Object
- #map_model(model) ⇒ Object
- #persisted? ⇒ Boolean
- #to_key ⇒ Object
- #to_model ⇒ Object
- #to_param ⇒ Object
- #valid?(context = nil) ⇒ Boolean
- #with_context(new_context) ⇒ Object
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
6 7 8 |
# File 'lib/rectify/form.rb', line 6 def context @context end |
Class Method Details
.array_attribute_names ⇒ Object
34 35 36 |
# File 'lib/rectify/form.rb', line 34 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
25 26 27 28 29 30 31 32 |
# File 'lib/rectify/form.rb', line 25 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
42 43 44 |
# File 'lib/rectify/form.rb', line 42 def self.from_json(json) from_params(JSON.parse(json)) end |
.from_model(model) ⇒ Object
38 39 40 |
# File 'lib/rectify/form.rb', line 38 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) 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
65 66 67 68 |
# File 'lib/rectify/form.rb', line 65 def self.hash_from(params) params = params.to_unsafe_h if params.respond_to?(:to_unsafe_h) params.with_indifferent_access end |
.infer_model_name ⇒ Object
54 55 56 57 58 59 |
# File 'lib/rectify/form.rb', line 54 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
46 47 48 |
# File 'lib/rectify/form.rb', line 46 def self.mimic(model_name) @model_name = model_name.to_s.underscore.to_sym end |
.mimicked_model_name ⇒ Object
50 51 52 |
# File 'lib/rectify/form.rb', line 50 def self.mimicked_model_name @model_name || infer_model_name end |
.model_name ⇒ Object
61 62 63 |
# File 'lib/rectify/form.rb', line 61 def self.model_name ActiveModel::Name.new(self, nil, mimicked_model_name.to_s.camelize) end |
Instance Method Details
#attributes ⇒ Object
92 93 94 |
# File 'lib/rectify/form.rb', line 92 def attributes super.except(:id) end |
#before_validation ⇒ Object
102 103 104 105 |
# File 'lib/rectify/form.rb', line 102 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
96 97 98 99 100 |
# File 'lib/rectify/form.rb', line 96 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
70 71 72 |
# File 'lib/rectify/form.rb', line 70 def persisted? id.present? && id.to_i > 0 end |
#to_key ⇒ Object
80 81 82 |
# File 'lib/rectify/form.rb', line 80 def to_key [id] end |
#to_model ⇒ Object
84 85 86 |
# File 'lib/rectify/form.rb', line 84 def to_model self end |
#to_param ⇒ Object
88 89 90 |
# File 'lib/rectify/form.rb', line 88 def to_param id.to_s end |
#valid?(context = nil) ⇒ Boolean
74 75 76 77 78 |
# File 'lib/rectify/form.rb', line 74 def valid?(context = nil) before_validation [super, form_attributes_valid?, arrays_attributes_valid?].all? end |
#with_context(new_context) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/rectify/form.rb', line 107 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 |