phlexi-form 0.8.1 → 0.8.2

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: 5cdc4a47adf374d0b808624816ac63ea57fd62241a2be9051188bb2af878fa5f
4
- data.tar.gz: 7702abb1fdeed8416dffac18d7d2d9b1bcf27a4ff5dd4df1352970c9fc24f988
3
+ metadata.gz: 5436d0872cd013770ac81aecaf6cd805b44762f5bd32bb3d4a95c6f9d3e936d1
4
+ data.tar.gz: dfddac5493ba78744bcc1956e8a7445579a6599631c4f4c00e74d9aa5f3ecd67
5
5
  SHA512:
6
- metadata.gz: 9597ee8e767850282a7c7d1a9b96153cbdb9ebe939cc026e19f9aa99e2600edf99fb095b92302bf6caf41b35ba391496e5cdbaf7a256c16dd320019f63c260d3
7
- data.tar.gz: c70bdee290744b95cb72f0841e9e33b909019b39c0727691510b17b539573ac29884e32a28445e60a00085aaf0efc99b2b6ca5ef81e5b9df4dd4dc159ad7d663
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
@@ -8,6 +8,14 @@ module Phlexi
8
8
 
9
9
  delegate :association_reflection, to: :field
10
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
+
11
19
  def selected?(option)
12
20
  case association_reflection.macro
13
21
  when :belongs_to, :has_one
@@ -30,8 +38,25 @@ module Phlexi
30
38
  super
31
39
  end
32
40
 
33
- def build_association_attributes
34
- raise NotImplementedError, "#{self.class}#build_association_attributes"
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
35
60
  end
36
61
  end
37
62
  end
@@ -6,6 +6,13 @@ module Phlexi
6
6
  class BelongsTo < AssociationBase
7
7
  protected
8
8
 
9
+ def choices
10
+ @choices ||= begin
11
+ collection = choices_from_association(association_reflection.klass)
12
+ build_choice_mapper(collection)
13
+ end
14
+ end
15
+
9
16
  def build_association_attributes
10
17
  attributes.fetch(:input_param) do
11
18
  attributes[:input_param] = if association_reflection.respond_to?(:options) && association_reflection.options[:foreign_key]
@@ -6,6 +6,13 @@ module Phlexi
6
6
  class HasMany < AssociationBase
7
7
  protected
8
8
 
9
+ def choices
10
+ @choices ||= begin
11
+ collection = choices_from_association(association_reflection.klass)
12
+ build_choice_mapper(collection)
13
+ end
14
+ end
15
+
9
16
  def build_association_attributes
10
17
  attributes.fetch(:input_param) {
11
18
  attributes[:input_param] = :"#{association_reflection.name.to_s.singularize}_ids"
@@ -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.1"
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.1
4
+ version: 0.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefan Froelich