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 +4 -4
- data/CHANGES.md +4 -0
- data/lib/on_form/collection_wrapper.rb +22 -8
- data/lib/on_form/form.rb +11 -2
- data/lib/on_form/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '09af61b2205c485d1694caedb95103adf309cd07'
|
4
|
+
data.tar.gz: 77f92d293295cd1395e64cdbc0e943871bde564e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c0900e4396e325f37f1cf91f687b39835be6576a6f6aeb8a357f5dabfa1c05645ff95b456c3c6fd8d76173ac9225fd441c0847c99b140e20458ddb09cafff277
|
7
|
+
data.tar.gz: 3aa596b415f0a8a321ddcac36e8786fa029e6b5f98a63e907f15c979f0506043009fb72941b1a681c974611ba9dcd17ff2698caf919cec17dc7145ce8789d238
|
data/CHANGES.md
CHANGED
@@ -1,18 +1,18 @@
|
|
1
1
|
module OnForm
|
2
2
|
class CollectionWrapper
|
3
3
|
include ::Enumerable
|
4
|
-
|
5
|
-
|
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
|
-
|
70
|
-
records_to_update[id.to_i] = attributes.except('id', :id, '_destroy', :destroy)
|
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)
|
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
|
data/lib/on_form/form.rb
CHANGED
@@ -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,
|
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
|
-
|
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
|
data/lib/on_form/version.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2018-04-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|