phlexi-form 0.8.1 → 0.8.3

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: 5cdc4a47adf374d0b808624816ac63ea57fd62241a2be9051188bb2af878fa5f
4
- data.tar.gz: 7702abb1fdeed8416dffac18d7d2d9b1bcf27a4ff5dd4df1352970c9fc24f988
3
+ metadata.gz: 5e7202d74e3e085564ee24e9bcfd7c0b983bd7de21166c51131b4adea20e5e43
4
+ data.tar.gz: dbeef2da87b88a8ed79a227bfbe406eeb8182871638a77b7107d843eaf9555de
5
5
  SHA512:
6
- metadata.gz: 9597ee8e767850282a7c7d1a9b96153cbdb9ebe939cc026e19f9aa99e2600edf99fb095b92302bf6caf41b35ba391496e5cdbaf7a256c16dd320019f63c260d3
7
- data.tar.gz: c70bdee290744b95cb72f0841e9e33b909019b39c0727691510b17b539573ac29884e32a28445e60a00085aaf0efc99b2b6ca5ef81e5b9df4dd4dc159ad7d663
6
+ metadata.gz: e5c4686b6cd6bdd891a021a1c10a31c58f098065e4c148e07d887ff75b5d6f71d6dc574f00675cf87dad8a84dd7b09a92da1c9b452d4308678008b6ec642ff48
7
+ data.tar.gz: 04252d73419ca890ff4aa8c3ebc18a730041f7e5971e3e640fd2a16db91952725ae4b033fd9f0aa68dc4df9fbdb3ac09273e27c04517aedb2222184f85df9d38
@@ -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.3"
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.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefan Froelich