phlexi-form 0.8.0 → 0.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/phlexi/form/builder.rb +0 -20
- data/lib/phlexi/form/components/association_base.rb +39 -0
- data/lib/phlexi/form/components/belongs_to.rb +2 -9
- data/lib/phlexi/form/components/concerns/accepts_choices.rb +2 -10
- data/lib/phlexi/form/components/has_many.rb +2 -9
- data/lib/phlexi/form/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5cdc4a47adf374d0b808624816ac63ea57fd62241a2be9051188bb2af878fa5f
|
4
|
+
data.tar.gz: 7702abb1fdeed8416dffac18d7d2d9b1bcf27a4ff5dd4df1352970c9fc24f988
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9597ee8e767850282a7c7d1a9b96153cbdb9ebe939cc026e19f9aa99e2600edf99fb095b92302bf6caf41b35ba391496e5cdbaf7a256c16dd320019f63c260d3
|
7
|
+
data.tar.gz: c70bdee290744b95cb72f0841e9e33b909019b39c0727691510b17b539573ac29884e32a28445e60a00085aaf0efc99b2b6ca5ef81e5b9df4dd4dc159ad7d663
|
data/lib/phlexi/form/builder.rb
CHANGED
@@ -276,26 +276,6 @@ module Phlexi
|
|
276
276
|
theme_key = attributes.delete(:theme) || theme_key
|
277
277
|
mix({class: themed(theme_key, self)}, attributes)
|
278
278
|
end
|
279
|
-
|
280
|
-
def determine_initial_value(value)
|
281
|
-
return value unless value == NIL_VALUE
|
282
|
-
|
283
|
-
determine_value_from_association || super
|
284
|
-
end
|
285
|
-
|
286
|
-
def determine_value_from_association
|
287
|
-
return unless association_reflection.present?
|
288
|
-
|
289
|
-
value = object.public_send(key)
|
290
|
-
case association_reflection.macro
|
291
|
-
when :has_many, :has_and_belongs_to_many
|
292
|
-
value&.map { |v| v.public_send(association_reflection.klass.primary_key) }
|
293
|
-
when :belongs_to, :has_one
|
294
|
-
value&.public_send(association_reflection.klass.primary_key)
|
295
|
-
else
|
296
|
-
raise ArgumentError, "Unsupported association type: #{association_reflection.macro}"
|
297
|
-
end
|
298
|
-
end
|
299
279
|
end
|
300
280
|
end
|
301
281
|
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Phlexi
|
4
|
+
module Form
|
5
|
+
module Components
|
6
|
+
class AssociationBase < Select
|
7
|
+
protected
|
8
|
+
|
9
|
+
delegate :association_reflection, to: :field
|
10
|
+
|
11
|
+
def selected?(option)
|
12
|
+
case association_reflection.macro
|
13
|
+
when :belongs_to, :has_one
|
14
|
+
singular_field_value.to_s == option.to_s
|
15
|
+
when :has_many, :has_and_belongs_to_many
|
16
|
+
collection_field_value.any? { |item| item.to_s == option.to_s }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def singular_field_value
|
21
|
+
@singular_field_value ||= field.value&.public_send(association_reflection.klass.primary_key)
|
22
|
+
end
|
23
|
+
|
24
|
+
def collection_field_value
|
25
|
+
@collection_field_value ||= field.value&.map { |v| v.public_send(association_reflection.klass.primary_key) }
|
26
|
+
end
|
27
|
+
|
28
|
+
def build_attributes
|
29
|
+
build_association_attributes
|
30
|
+
super
|
31
|
+
end
|
32
|
+
|
33
|
+
def build_association_attributes
|
34
|
+
raise NotImplementedError, "#{self.class}#build_association_attributes"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -3,17 +3,10 @@
|
|
3
3
|
module Phlexi
|
4
4
|
module Form
|
5
5
|
module Components
|
6
|
-
class BelongsTo <
|
6
|
+
class BelongsTo < AssociationBase
|
7
7
|
protected
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
def build_attributes
|
12
|
-
build_belongs_to_attributes
|
13
|
-
super
|
14
|
-
end
|
15
|
-
|
16
|
-
def build_belongs_to_attributes
|
9
|
+
def build_association_attributes
|
17
10
|
attributes.fetch(:input_param) do
|
18
11
|
attributes[:input_param] = if association_reflection.respond_to?(:options) && association_reflection.options[:foreign_key]
|
19
12
|
association_reflection.options[:foreign_key]
|
@@ -24,17 +24,9 @@ module Phlexi
|
|
24
24
|
|
25
25
|
def selected?(option)
|
26
26
|
if attributes[:multiple]
|
27
|
-
|
27
|
+
Array(field.value).any? { |item| item.to_s == option.to_s }
|
28
28
|
else
|
29
|
-
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def field_value
|
34
|
-
if attributes[:multiple]
|
35
|
-
Array(field.value)
|
36
|
-
else
|
37
|
-
field.value
|
29
|
+
field.value.to_s == option.to_s
|
38
30
|
end
|
39
31
|
end
|
40
32
|
|
@@ -3,17 +3,10 @@
|
|
3
3
|
module Phlexi
|
4
4
|
module Form
|
5
5
|
module Components
|
6
|
-
class HasMany <
|
6
|
+
class HasMany < AssociationBase
|
7
7
|
protected
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
def build_attributes
|
12
|
-
build_has_many_attributes
|
13
|
-
super
|
14
|
-
end
|
15
|
-
|
16
|
-
def build_has_many_attributes
|
9
|
+
def build_association_attributes
|
17
10
|
attributes.fetch(:input_param) {
|
18
11
|
attributes[:input_param] = :"#{association_reflection.name.to_s.singularize}_ids"
|
19
12
|
}
|
data/lib/phlexi/form/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: phlexi-form
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.8.
|
4
|
+
version: 0.8.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stefan Froelich
|
@@ -205,6 +205,7 @@ files:
|
|
205
205
|
- lib/phlexi/form.rb
|
206
206
|
- lib/phlexi/form/base.rb
|
207
207
|
- lib/phlexi/form/builder.rb
|
208
|
+
- lib/phlexi/form/components/association_base.rb
|
208
209
|
- lib/phlexi/form/components/base.rb
|
209
210
|
- lib/phlexi/form/components/belongs_to.rb
|
210
211
|
- lib/phlexi/form/components/checkbox.rb
|