phlexi-form 0.8.0 → 0.8.1

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
  SHA256:
3
- metadata.gz: 8747d53fec5851c3ad3f78d2c60fecfea530de3b033eb074ee5f4ce41bcd3181
4
- data.tar.gz: 51c02f430a3a81a4ee05197463a14aa9b00804c0a5b3a1837f36533b006d9860
3
+ metadata.gz: 5cdc4a47adf374d0b808624816ac63ea57fd62241a2be9051188bb2af878fa5f
4
+ data.tar.gz: 7702abb1fdeed8416dffac18d7d2d9b1bcf27a4ff5dd4df1352970c9fc24f988
5
5
  SHA512:
6
- metadata.gz: 7434f1529fda38b7e02318c01be2fba09a97de755c874d2319730e4118d747a20bac457e7421bb34f0280f7e405079717df55e3d5ba3f32934f51deb1ebf43d7
7
- data.tar.gz: 7457a359784b5404fee4a86ecdce99769f7899e080b7148aaff045719d536499efb4c6103c04334675258af94b74dbd10a9acacb7f327fb3a0f5f1508223f5bd
6
+ metadata.gz: 9597ee8e767850282a7c7d1a9b96153cbdb9ebe939cc026e19f9aa99e2600edf99fb095b92302bf6caf41b35ba391496e5cdbaf7a256c16dd320019f63c260d3
7
+ data.tar.gz: c70bdee290744b95cb72f0841e9e33b909019b39c0727691510b17b539573ac29884e32a28445e60a00085aaf0efc99b2b6ca5ef81e5b9df4dd4dc159ad7d663
@@ -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 < Select
6
+ class BelongsTo < AssociationBase
7
7
  protected
8
8
 
9
- delegate :association_reflection, to: :field
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
- field_value.any? { |item| item.to_s == option.to_s }
27
+ Array(field.value).any? { |item| item.to_s == option.to_s }
28
28
  else
29
- field_value.to_s == option.to_s
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 < Select
6
+ class HasMany < AssociationBase
7
7
  protected
8
8
 
9
- delegate :association_reflection, to: :field
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
  }
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Phlexi
4
4
  module Form
5
- VERSION = "0.8.0"
5
+ VERSION = "0.8.1"
6
6
  end
7
7
  end
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.0
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