Class: Rectify::Form
- Inherits:
-
Object
- Object
- Rectify::Form
- Includes:
- ActiveModel::Validations
- Defined in:
- lib/rectify/form.rb
Class Method Summary collapse
- .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
- #map_model(model) ⇒ Object
- #persisted? ⇒ Boolean
- #to_key ⇒ Object
- #to_model ⇒ Object
- #to_param ⇒ Object
- #valid?(context = nil) ⇒ Boolean
Class Method Details
.from_model(model) ⇒ Object
21 22 23 |
# File 'lib/rectify/form.rb', line 21 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 |
# 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 = params_hash .fetch(mimicked_model_name, {}) .merge(params_hash.slice(*attribute_names)) .merge(additional_params) new(attributes) end |
.hash_from(params) ⇒ Object
44 45 46 47 |
# File 'lib/rectify/form.rb', line 44 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
33 34 35 36 37 38 |
# File 'lib/rectify/form.rb', line 33 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
25 26 27 |
# File 'lib/rectify/form.rb', line 25 def self.mimic(model_name) @model_name = model_name.to_s.underscore.to_sym end |
.mimicked_model_name ⇒ Object
29 30 31 |
# File 'lib/rectify/form.rb', line 29 def self.mimicked_model_name @model_name || infer_model_name end |
.model_name ⇒ Object
40 41 42 |
# File 'lib/rectify/form.rb', line 40 def self.model_name ActiveModel::Name.new(self, nil, mimicked_model_name.to_s.camelize) end |
Instance Method Details
#attributes ⇒ Object
69 70 71 |
# File 'lib/rectify/form.rb', line 69 def attributes super.except(:id) end |
#map_model(model) ⇒ Object
73 74 75 76 77 |
# File 'lib/rectify/form.rb', line 73 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
49 50 51 |
# File 'lib/rectify/form.rb', line 49 def persisted? id.present? && id.to_i > 0 end |
#to_key ⇒ Object
57 58 59 |
# File 'lib/rectify/form.rb', line 57 def to_key [id] end |
#to_model ⇒ Object
61 62 63 |
# File 'lib/rectify/form.rb', line 61 def to_model self end |
#to_param ⇒ Object
65 66 67 |
# File 'lib/rectify/form.rb', line 65 def to_param id.to_s end |
#valid?(context = nil) ⇒ Boolean
53 54 55 |
# File 'lib/rectify/form.rb', line 53 def valid?(context = nil) [super, form_attributes_valid?, arrays_attributes_valid?].all? end |