on_form 3.0.0 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: da2ced30fe04b528a602ad17e4b6ba170c08c02e
4
- data.tar.gz: 757f860fa9c1b41c575f6ea5c0a7a4920d32c240
3
+ metadata.gz: '09af61b2205c485d1694caedb95103adf309cd07'
4
+ data.tar.gz: 77f92d293295cd1395e64cdbc0e943871bde564e
5
5
  SHA512:
6
- metadata.gz: d85b8d09675bb0edbd414bb4ed3ddbc44b14bc17a571e6259928099f2e611c56607d2f541c99067653f7f79deeab60a7aee1884d249e70c36a24204f26543733
7
- data.tar.gz: cd09754e7fe9a5a686911a577aa2e2e85edd478c7f79107b4d7c2601a46d9b9b3158f2d43bb3d0c776074e61fa752ac825498a02af3cbeb9ffc80cc09c40975a
6
+ metadata.gz: c0900e4396e325f37f1cf91f687b39835be6576a6f6aeb8a357f5dabfa1c05645ff95b456c3c6fd8d76173ac9225fd441c0847c99b140e20458ddb09cafff277
7
+ data.tar.gz: 3aa596b415f0a8a321ddcac36e8786fa029e6b5f98a63e907f15c979f0506043009fb72941b1a681c974611ba9dcd17ff2698caf919cec17dc7145ce8789d238
data/CHANGES.md CHANGED
@@ -1,6 +1,10 @@
1
1
  Changelog
2
2
  =========
3
3
 
4
+ 3.1.0
5
+ -----
6
+ * Support `reject_if` option on `expose_collection_of`. Thanks @Dhamsoft.
7
+
4
8
  3.0.0
5
9
  -----
6
10
  * Support `save(validate: false)` and `save!(validate: false)`.
@@ -1,18 +1,18 @@
1
1
  module OnForm
2
2
  class CollectionWrapper
3
3
  include ::Enumerable
4
-
5
- attr_reader :parent, :association_name, :collection_form_class, :allow_insert, :allow_update, :allow_destroy
4
+ attr_reader :parent, :association_name, :collection_form_class,
5
+ :allow_insert, :allow_update, :allow_destroy, :reject_if
6
6
 
7
7
  delegate :each, :first, :last, :[], to: :to_a
8
8
 
9
- def initialize(parent, association_name, collection_form_class, allow_insert, allow_update, allow_destroy)
9
+ def initialize(parent, association_name, collection_form_class, allow_insert: true, allow_update: true, allow_destroy: false, reject_if: nil)
10
10
  @parent = parent
11
11
  @association_name = association_name
12
12
  @association = parent.association(association_name)
13
13
  @association_proxy = parent.send(association_name)
14
14
  @collection_form_class = collection_form_class
15
- @allow_insert, @allow_update, @allow_destroy = allow_insert, allow_update, allow_destroy
15
+ @allow_insert, @allow_update, @allow_destroy, @reject_if = allow_insert, allow_update, allow_destroy, reject_if
16
16
  @wrapped_records = {}
17
17
  @wrapped_new_records = []
18
18
  @loaded_forms = []
@@ -66,11 +66,11 @@ module OnForm
66
66
  if id = attributes['id'] || attributes[:id]
67
67
  if destroy
68
68
  records_to_destroy << id.to_i if allow_destroy
69
- else
70
- records_to_update[id.to_i] = attributes.except('id', :id, '_destroy', :destroy) if allow_update
69
+ elsif allow_update && !call_reject_if(attributes)
70
+ records_to_update[id.to_i] = attributes.except('id', :id, '_destroy', :destroy)
71
71
  end
72
- elsif !destroy
73
- records_to_insert << attributes.except('_destroy', :destroy) if allow_insert
72
+ elsif !destroy && allow_insert && !call_reject_if(attributes)
73
+ records_to_insert << attributes.except('_destroy', :destroy)
74
74
  end
75
75
  end
76
76
 
@@ -117,5 +117,19 @@ module OnForm
117
117
  def wrapped_record(record)
118
118
  @wrapped_records[record] ||= @collection_form_class.new(record).tap { |form| @loaded_forms << form }
119
119
  end
120
+
121
+ # Determines if a record with the particular +attributes+ should be
122
+ # rejected by calling the reject_if Symbol or Proc (if defined).
123
+ # The reject_if option is defined by +expose_collection_of+.
124
+ def call_reject_if(attributes)
125
+ case reject_if
126
+ when Symbol
127
+ @collection_form_class.method(reject_if).arity == 0 ? @collection_form_class.send(reject_if) : @collection_form_class.send(reject_if, attributes)
128
+ when Proc
129
+ reject_if.call(attributes)
130
+ else
131
+ false
132
+ end
133
+ end
120
134
  end
121
135
  end
@@ -76,7 +76,9 @@ module OnForm
76
76
  delegate :to_model, to: backing_model_name if convert_to_model
77
77
  end
78
78
 
79
- def self.expose_collection_of(association_name, on: nil, prefix: nil, suffix: nil, as: nil, allow_insert: true, allow_update: true, allow_destroy: false, &block)
79
+ def self.expose_collection_of(association_name, on: nil, prefix: nil, suffix: nil, as: nil,
80
+ allow_insert: true, allow_update: true, allow_destroy: false, reject_if: nil, &block)
81
+
80
82
  exposed_name = as || "#{prefix}#{association_name}#{suffix}"
81
83
  singular_name = exposed_name.to_s.singularize
82
84
  association_name = association_name.to_sym
@@ -92,7 +94,14 @@ module OnForm
92
94
  collection_form_class.take_identity_from singular_name, convert_to_model: false
93
95
  collection_form_class.class_eval(&block)
94
96
 
95
- define_method(exposed_name) { collection_wrappers[association_name] ||= CollectionWrapper.new(backing_model_instance(on), association_name, collection_form_class, allow_insert, allow_update, allow_destroy) } # used by action_view's fields_for, and by the following lines
97
+ # used by action_view's fields_for, and by the following lines
98
+ define_method(exposed_name) do
99
+ collection_wrappers[association_name] ||= CollectionWrapper.new(
100
+ backing_model_instance(on), association_name, collection_form_class,
101
+ allow_insert: allow_insert, allow_update: allow_update,
102
+ allow_destroy: allow_destroy, reject_if: reject_if
103
+ )
104
+ end
96
105
  define_method("#{exposed_name}_attributes=") { |params| send(exposed_name).parse_collection_attributes(params) }
97
106
 
98
107
  collection_form_class
@@ -1,3 +1,3 @@
1
1
  module OnForm
2
- VERSION = "3.0.0"
2
+ VERSION = "3.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: on_form
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Will Bryant
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-04-22 00:00:00.000000000 Z
11
+ date: 2018-04-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel