phlexi-form 0.8.0 → 0.8.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8747d53fec5851c3ad3f78d2c60fecfea530de3b033eb074ee5f4ce41bcd3181
4
- data.tar.gz: 51c02f430a3a81a4ee05197463a14aa9b00804c0a5b3a1837f36533b006d9860
3
+ metadata.gz: 5436d0872cd013770ac81aecaf6cd805b44762f5bd32bb3d4a95c6f9d3e936d1
4
+ data.tar.gz: dfddac5493ba78744bcc1956e8a7445579a6599631c4f4c00e74d9aa5f3ecd67
5
5
  SHA512:
6
- metadata.gz: 7434f1529fda38b7e02318c01be2fba09a97de755c874d2319730e4118d747a20bac457e7421bb34f0280f7e405079717df55e3d5ba3f32934f51deb1ebf43d7
7
- data.tar.gz: 7457a359784b5404fee4a86ecdce99769f7899e080b7148aaff045719d536499efb4c6103c04334675258af94b74dbd10a9acacb7f327fb3a0f5f1508223f5bd
6
+ metadata.gz: 1fe41d20bf2b26c29e71ebb932576a4b49751773f60c1d365cd6ad3f01a70dcda7e6dec894418654718fdf2514d860d92e2fad4d25254bbf7fb65151a5f0fefa
7
+ data.tar.gz: f32c8922f62bb618034a40dff29554d2171b5b46459c8200388a7162f0072360b5c0091c2fd9bace45923e17b73e086b98b49afe7e2d2be6ff211552f5dd7f47
@@ -175,11 +175,23 @@ module Phlexi
175
175
  raise NotImplementedError, "has_one associations are NOT supported"
176
176
  end
177
177
 
178
+ def polymorphic_has_one_tag(**, &)
179
+ # TODO: this requires a grouped_select component
180
+ # see: Plutonium::Core::Fields::Inputs::PolymorphicBelongsToAssociationInput
181
+ raise NotImplementedError, "polymorphic has_one associations are not YET supported"
182
+ end
183
+
178
184
  def has_many_tag(**, &)
179
185
  create_component(Components::HasMany, :has_many, **, &)
180
186
  end
181
187
  alias_method :has_and_belongs_to_many_tag, :has_many_tag
182
188
 
189
+ def polymorphic_has_many_tag(**, &)
190
+ # TODO: this requires a grouped_select component
191
+ # see: Plutonium::Core::Fields::Inputs::PolymorphicBelongsToAssociationInput
192
+ raise NotImplementedError, "polymorphic has_many associations are not YET supported"
193
+ end
194
+
183
195
  def input_array_tag(**, &)
184
196
  create_component(Components::InputArray, :array, **, &)
185
197
  end
@@ -276,26 +288,6 @@ module Phlexi
276
288
  theme_key = attributes.delete(:theme) || theme_key
277
289
  mix({class: themed(theme_key, self)}, attributes)
278
290
  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
291
  end
300
292
  end
301
293
  end
@@ -0,0 +1,64 @@
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 build_association_attributes
12
+ raise NotImplementedError, "#{self.class}#build_association_attributes"
13
+ end
14
+
15
+ def choices
16
+ raise NotImplementedError, "#{self.class}#choices"
17
+ end
18
+
19
+ def selected?(option)
20
+ case association_reflection.macro
21
+ when :belongs_to, :has_one
22
+ singular_field_value.to_s == option.to_s
23
+ when :has_many, :has_and_belongs_to_many
24
+ collection_field_value.any? { |item| item.to_s == option.to_s }
25
+ end
26
+ end
27
+
28
+ def singular_field_value
29
+ @singular_field_value ||= field.value&.public_send(association_reflection.klass.primary_key)
30
+ end
31
+
32
+ def collection_field_value
33
+ @collection_field_value ||= field.value&.map { |v| v.public_send(association_reflection.klass.primary_key) }
34
+ end
35
+
36
+ def build_attributes
37
+ build_association_attributes
38
+ super
39
+ end
40
+
41
+ def choices_from_association(klass)
42
+ relation = klass.all
43
+
44
+ if association_reflection.respond_to?(:scope) && association_reflection.scope
45
+ relation = if association_reflection.scope.parameters.any?
46
+ association_reflection.klass.instance_exec(object, &association_reflection.scope)
47
+ else
48
+ association_reflection.klass.instance_exec(&association_reflection.scope)
49
+ end
50
+ else
51
+ order = association_reflection.options[:order]
52
+ conditions = association_reflection.options[:conditions]
53
+ conditions = object.instance_exec(&conditions) if conditions.respond_to?(:call)
54
+
55
+ relation = relation.where(conditions) if relation.respond_to?(:where) && conditions.present?
56
+ relation = relation.order(order) if relation.respond_to?(:order)
57
+ end
58
+
59
+ relation
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -3,17 +3,17 @@
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
9
+ def choices
10
+ @choices ||= begin
11
+ collection = choices_from_association(association_reflection.klass)
12
+ build_choice_mapper(collection)
13
+ end
14
14
  end
15
15
 
16
- def build_belongs_to_attributes
16
+ def build_association_attributes
17
17
  attributes.fetch(:input_param) do
18
18
  attributes[:input_param] = if association_reflection.respond_to?(:options) && association_reflection.options[:foreign_key]
19
19
  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,17 @@
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
9
+ def choices
10
+ @choices ||= begin
11
+ collection = choices_from_association(association_reflection.klass)
12
+ build_choice_mapper(collection)
13
+ end
14
14
  end
15
15
 
16
- def build_has_many_attributes
16
+ def build_association_attributes
17
17
  attributes.fetch(:input_param) {
18
18
  attributes[:input_param] = :"#{association_reflection.name.to_s.singularize}_ids"
19
19
  }
@@ -17,6 +17,13 @@ module Phlexi
17
17
  grouped_choices.each(&)
18
18
  end
19
19
 
20
+ # @return [Array<String>] An array of all choice values.
21
+ def values
22
+ @values ||= grouped_choices.values.flat_map(&:values)
23
+ end
24
+
25
+ private
26
+
20
27
  def grouped_choices
21
28
  @grouped_choices ||= materialize_grouped_choices(@collection)
22
29
  end
@@ -34,8 +41,6 @@ module Phlexi
34
41
  end
35
42
  end
36
43
 
37
- private
38
-
39
44
  def array_to_grouped_hash(array)
40
45
  sample = array.first
41
46
  if group_method == :last && sample.is_a?(Array) && sample.size == 2
@@ -20,30 +20,7 @@ module Phlexi
20
20
  return object.class.defined_enums.fetch(key.to_s).keys if object.class.defined_enums.key?(key.to_s)
21
21
  end
22
22
 
23
- choices_from_association || choices_from_validator
24
- end
25
-
26
- def choices_from_association
27
- return unless association_reflection
28
-
29
- relation = association_reflection.klass.all
30
-
31
- if association_reflection.respond_to?(:scope) && association_reflection.scope
32
- relation = if association_reflection.scope.parameters.any?
33
- association_reflection.klass.instance_exec(object, &association_reflection.scope)
34
- else
35
- association_reflection.klass.instance_exec(&association_reflection.scope)
36
- end
37
- else
38
- order = association_reflection.options[:order]
39
- conditions = association_reflection.options[:conditions]
40
- conditions = object.instance_exec(&conditions) if conditions.respond_to?(:call)
41
-
42
- relation = relation.where(conditions) if relation.respond_to?(:where) && conditions.present?
43
- relation = relation.order(order) if relation.respond_to?(:order)
44
- end
45
-
46
- relation
23
+ choices_from_validator
47
24
  end
48
25
 
49
26
  def choices_from_validator
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Phlexi
4
4
  module Form
5
- VERSION = "0.8.0"
5
+ VERSION = "0.8.2"
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.2
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