Class: OnForm::CollectionWrapper
- Inherits:
-
Object
- Object
- OnForm::CollectionWrapper
- Includes:
- Enumerable
- Defined in:
- lib/on_form/collection_wrapper.rb
Instance Attribute Summary collapse
-
#allow_destroy ⇒ Object
readonly
Returns the value of attribute allow_destroy.
-
#allow_insert ⇒ Object
readonly
Returns the value of attribute allow_insert.
-
#allow_update ⇒ Object
readonly
Returns the value of attribute allow_update.
-
#association_name ⇒ Object
readonly
Returns the value of attribute association_name.
-
#collection_form_class ⇒ Object
readonly
Returns the value of attribute collection_form_class.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#reject_if ⇒ Object
readonly
Returns the value of attribute reject_if.
Instance Method Summary collapse
- #each ⇒ Object
- #form_errors? ⇒ Boolean
-
#initialize(parent, association_name, collection_form_class, allow_insert: true, allow_update: true, allow_destroy: false, reject_if: nil) ⇒ CollectionWrapper
constructor
A new instance of CollectionWrapper.
- #parse_collection_attributes(params) ⇒ Object
- #reset_forms_errors ⇒ Object
- #save_forms(validate: true) ⇒ Object
- #size ⇒ Object
- #to_ary ⇒ Object
- #validate_forms(parent_form) ⇒ Object
Constructor Details
#initialize(parent, association_name, collection_form_class, allow_insert: true, allow_update: true, allow_destroy: false, reject_if: nil) ⇒ CollectionWrapper
Returns a new instance of CollectionWrapper.
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/on_form/collection_wrapper.rb', line 9 def initialize(parent, association_name, collection_form_class, allow_insert: true, allow_update: true, allow_destroy: false, reject_if: nil) @parent = parent @association_name = association_name @association = parent.association(association_name) @association_proxy = parent.send(association_name) @collection_form_class = collection_form_class @allow_insert, @allow_update, @allow_destroy, @reject_if = allow_insert, allow_update, allow_destroy, reject_if @wrapped_records = {} @wrapped_new_records = [] @loaded_forms = [] end |
Instance Attribute Details
#allow_destroy ⇒ Object (readonly)
Returns the value of attribute allow_destroy.
4 5 6 |
# File 'lib/on_form/collection_wrapper.rb', line 4 def allow_destroy @allow_destroy end |
#allow_insert ⇒ Object (readonly)
Returns the value of attribute allow_insert.
4 5 6 |
# File 'lib/on_form/collection_wrapper.rb', line 4 def allow_insert @allow_insert end |
#allow_update ⇒ Object (readonly)
Returns the value of attribute allow_update.
4 5 6 |
# File 'lib/on_form/collection_wrapper.rb', line 4 def allow_update @allow_update end |
#association_name ⇒ Object (readonly)
Returns the value of attribute association_name.
4 5 6 |
# File 'lib/on_form/collection_wrapper.rb', line 4 def association_name @association_name end |
#collection_form_class ⇒ Object (readonly)
Returns the value of attribute collection_form_class.
4 5 6 |
# File 'lib/on_form/collection_wrapper.rb', line 4 def collection_form_class @collection_form_class end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
4 5 6 |
# File 'lib/on_form/collection_wrapper.rb', line 4 def parent @parent end |
#reject_if ⇒ Object (readonly)
Returns the value of attribute reject_if.
4 5 6 |
# File 'lib/on_form/collection_wrapper.rb', line 4 def reject_if @reject_if end |
Instance Method Details
#each ⇒ Object
25 26 27 |
# File 'lib/on_form/collection_wrapper.rb', line 25 def each @association_proxy.each { |record| yield wrapped_record(record) } end |
#form_errors? ⇒ Boolean
49 50 51 |
# File 'lib/on_form/collection_wrapper.rb', line 49 def form_errors? @loaded_forms.map(&:form_errors?).any? end |
#parse_collection_attributes(params) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/on_form/collection_wrapper.rb', line 57 def parse_collection_attributes(params) params = params.values unless params.is_a?(Array) records_to_insert = [] records_to_update = {} records_to_destroy = [] params.each do |attributes| destroy = self.class.boolean_type.cast(attributes['_destroy']) || self.class.boolean_type.cast(attributes[:_destroy]) if id = attributes['id'] || attributes[:id] if destroy records_to_destroy << id.to_i if allow_destroy elsif allow_update && !call_reject_if(attributes) records_to_update[id.to_i] = attributes.except('id', :id, '_destroy', :destroy) end elsif !destroy && allow_insert && !call_reject_if(attributes) records_to_insert << attributes.except('_destroy', :destroy) end end to_a if @association_proxy.loaded? records_to_load = records_to_update.keys + records_to_destroy - @wrapped_records.keys.collect(&:id) @association_proxy.find(records_to_load).each do |record| @association.add_to_target(record, :skip_callbacks) wrapped_record(record) end loaded_forms_by_id = @wrapped_records.values.index_by(&:id) records_to_insert.each do |attributes| wrapped_record(@association_proxy.build).attributes = attributes end records_to_update.each do |id, attributes| loaded_forms_by_id[id].attributes = attributes end records_to_destroy.each do |id| loaded_forms_by_id[id].mark_for_destruction end params end |
#reset_forms_errors ⇒ Object
53 54 55 |
# File 'lib/on_form/collection_wrapper.rb', line 53 def reset_forms_errors @loaded_forms.collect(&:reset_errors) end |
#save_forms(validate: true) ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/on_form/collection_wrapper.rb', line 33 def save_forms(validate: true) @loaded_forms.each do |form| if form.marked_for_destruction? form.record.destroy else form.save!(validate: validate) end end end |
#size ⇒ Object
29 30 31 |
# File 'lib/on_form/collection_wrapper.rb', line 29 def size @association_proxy.size end |
#to_ary ⇒ Object
21 22 23 |
# File 'lib/on_form/collection_wrapper.rb', line 21 def to_ary to_a end |
#validate_forms(parent_form) ⇒ Object
43 44 45 46 47 |
# File 'lib/on_form/collection_wrapper.rb', line 43 def validate_forms(parent_form) @loaded_forms.collect do |form| add_errors_to_parent(parent_form, form) if form.invalid? end end |