phlexi-form 0.8.1 → 0.8.3
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 +4 -4
- data/lib/phlexi/form/components/association_base.rb +27 -2
- data/lib/phlexi/form/components/belongs_to.rb +7 -0
- data/lib/phlexi/form/components/has_many.rb +7 -0
- data/lib/phlexi/form/grouped_choices_mapper.rb +7 -2
- data/lib/phlexi/form/options/choices.rb +1 -24
- data/lib/phlexi/form/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5e7202d74e3e085564ee24e9bcfd7c0b983bd7de21166c51131b4adea20e5e43
|
4
|
+
data.tar.gz: dbeef2da87b88a8ed79a227bfbe406eeb8182871638a77b7107d843eaf9555de
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
34
|
-
|
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
|
-
|
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
|
data/lib/phlexi/form/version.rb
CHANGED