effective_form_inputs 0.7.3 → 0.7.4
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
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c4569c203c0939b4299583c21b84cdbb8899f0a1
|
4
|
+
data.tar.gz: 6b96dd9835ce3866ed6d5fd174ab7eee5e463e7b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7684f263fbbaa59b318d5c6c790045d487480d2074d3d0ad58a5685b4a5f7440c61fc15b83f666ba56afa9ce7b100f097c12b642671e07324b7784eafc40bdd7
|
7
|
+
data.tar.gz: 1acda55bd40537351041c4400cba4a1ebfef0013205549806a8cd6302d230fa83aea0d0a71ffa80e787df03ff7cbec422eedabaa4b0b05270f83265d05d88574
|
data/README.md
CHANGED
@@ -271,12 +271,35 @@ Passing `:multiple => true` will allow multiple selections to be made.
|
|
271
271
|
|
272
272
|
Passing `:multiple => true, :tags => true` will allow multiple selections to be made, and new value options to be created. This will allow you to both select existing tags and create new tags in the same form control.
|
273
273
|
|
274
|
-
Passing `:grouped => true` will enable optgroup support. When in this mode, the collection should be a Hash of Array of Arrays
|
274
|
+
Passing `:grouped => true` will enable optgroup support. When in this mode, the collection should be a Hash of ActiveRecord Relations or Array of Arrays
|
275
275
|
|
276
276
|
```ruby
|
277
|
+
:collection => {'Active' => Post.active, 'Past' => Post.past}
|
277
278
|
:collection => {'Active' => [['Post A', 1], ['Post B', 2]], 'Past' => [['Post C', 3], ['Post D', 4]]}
|
278
279
|
```
|
279
280
|
|
281
|
+
Passing `:polymorphic => true` will enable polymorphic support. In this mode, an additional 2 hidden input fields are created alongside the select field.
|
282
|
+
|
283
|
+
So calling
|
284
|
+
|
285
|
+
```ruby
|
286
|
+
= f.input :primary_contact, :polymorphic => true, :collection => User.all.to_a + Member.all.to_a
|
287
|
+
```
|
288
|
+
|
289
|
+
will internally translate the collection into:
|
290
|
+
|
291
|
+
```ruby
|
292
|
+
[['User 1', 'User_1'], ['User 2', 'User_2'], ['Member 100', 'Member_100']]
|
293
|
+
```
|
294
|
+
|
295
|
+
and instead of posting to the server with the parameter `:primary_contact`, it will intead post `{:primary_contact_id => 2, :primary_contact_type => 'User'}`.
|
296
|
+
|
297
|
+
Using both `:polymorphic => true` and `:grouped => true` is recommended. In this case the expected collection is as follows:
|
298
|
+
|
299
|
+
```ruby
|
300
|
+
= f.input :primary_contact, :polymorphic => true, :grouped => true, :collection => {'Users' => User.all, 'Members' => 'Member.all'}
|
301
|
+
```
|
302
|
+
|
280
303
|
### Options
|
281
304
|
|
282
305
|
The default `:input_js => options` used to initialize this form input are as follows:
|
@@ -10,3 +10,11 @@ initialize = ->
|
|
10
10
|
$ -> initialize()
|
11
11
|
$(document).on 'page:change', -> initialize()
|
12
12
|
$(document).on 'cocoon:after-insert', -> initialize()
|
13
|
+
|
14
|
+
# If we're working with a polymorphic select, split out the ID and assign the hidden _type and _id fields
|
15
|
+
$(document).on 'change', "select.effective_select.polymorphic", (event) ->
|
16
|
+
$select = $(event.target)
|
17
|
+
value = $select.val() || ''
|
18
|
+
|
19
|
+
$select.siblings("input[type='hidden'][name$='_type]']").val(value.split('_')[0] || '')
|
20
|
+
$select.siblings("input[type='hidden'][name$='_id]']").val(value.split('_')[1] || '')
|
@@ -1,10 +1,10 @@
|
|
1
1
|
module Inputs
|
2
2
|
module EffectiveSelect
|
3
3
|
class Input < Effective::FormInput
|
4
|
-
delegate :collection_select, :grouped_collection_select, :to => :@template
|
4
|
+
delegate :collection_select, :grouped_collection_select, :hidden_field, :to => :@template
|
5
5
|
|
6
6
|
def default_options
|
7
|
-
{label_method: :to_s, value_method: :to_s, group_method: :
|
7
|
+
{label_method: :to_s, value_method: :to_s, group_label_method: :first, group_method: :last, option_value_method: :first, option_key_method: :second }
|
8
8
|
end
|
9
9
|
|
10
10
|
def default_input_js
|
@@ -16,32 +16,103 @@ module Inputs
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def to_html
|
19
|
-
if options[:grouped]
|
19
|
+
html = if options[:grouped] && options[:polymorphic]
|
20
|
+
grouped_collection_select(@object_name, polymorphic_id_method, collection, options[:group_method], options[:group_label_method], options[:option_key_method], options[:option_value_method], options, tag_options)
|
21
|
+
elsif options[:grouped]
|
20
22
|
grouped_collection_select(@object_name, @method, collection, options[:group_method], options[:group_label_method], options[:option_key_method], options[:option_value_method], options, tag_options)
|
23
|
+
elsif options[:polymorphic]
|
24
|
+
collection_select(@object_name, polymorphic_id_method, collection, :second, :first, options, tag_options)
|
21
25
|
else
|
22
26
|
collection_select(@object_name, @method, collection, options[:value_method], options[:label_method], options, tag_options)
|
23
27
|
end
|
28
|
+
|
29
|
+
if options[:polymorphic]
|
30
|
+
html += hidden_field(@object_name, polymorphic_type_method, value: polymorphic_type_value)
|
31
|
+
html += hidden_field(@object_name, polymorphic_id_method, value: polymorphic_id_value)
|
32
|
+
end
|
33
|
+
|
34
|
+
html
|
24
35
|
end
|
25
36
|
|
37
|
+
# This is a grouped polymorphic collection
|
38
|
+
# [["Clinics", [["Clinc 50", "Clinic_50"], ["Clinic 43", "Clinic_43"]]], ["Contacts", [["Contact 544", "Contact_544"]]]]
|
26
39
|
def collection
|
27
40
|
@collection ||= begin
|
28
|
-
collection = options.delete(:collection)
|
41
|
+
collection = options.delete(:collection) || []
|
42
|
+
grouped = collection[0].kind_of?(Array) && collection[0][0].kind_of?(String) && collection[0][1].respond_to?(:to_a) # Array or ActiveRecord_Relation
|
43
|
+
|
44
|
+
if options[:grouped] && !grouped
|
45
|
+
raise "Grouped collection expecting a Hash {'Posts' => Post.all, 'Events' => Event.all} or a Hash {'Posts' => [['Post A', 1], ['Post B', 2]], 'Events' => [['Event A', 1], ['Event B', 2]]}"
|
46
|
+
end
|
47
|
+
|
48
|
+
if grouped
|
49
|
+
collection.each_with_index do |(name, group), index|
|
50
|
+
collection[index][1] = group.respond_to?(:call) ? group.call : group.to_a
|
51
|
+
end
|
52
|
+
else
|
53
|
+
collection = collection.respond_to?(:call) ? collection.call : collection.to_a
|
54
|
+
end
|
55
|
+
|
56
|
+
if options[:polymorphic]
|
57
|
+
if grouped
|
58
|
+
collection.each { |_, group| polymorphize_collection!(group) }
|
59
|
+
else
|
60
|
+
polymorphize_collection!(collection)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
29
64
|
collection.respond_to?(:call) ? collection.call : collection.to_a
|
30
65
|
end
|
31
66
|
end
|
32
67
|
|
68
|
+
# Translate our Collection into a polymorphic collection
|
69
|
+
def polymorphize_collection!(collection)
|
70
|
+
unless options[:grouped] || collection[0].kind_of?(ActiveRecord::Base) || (collection[0].kind_of?(Array) && collection[0].length >= 2)
|
71
|
+
raise "Polymorphic collection expecting a flat Array of mixed ActiveRecord::Base objects, or an Array of Arrays like [['Post A', 'Post_1'], ['Event B', 'Event_2']]"
|
72
|
+
end
|
73
|
+
|
74
|
+
collection.each_with_index do |obj, index|
|
75
|
+
if obj.kind_of?(ActiveRecord::Base)
|
76
|
+
collection[index] = [obj.public_send(options[:label_method]), "#{obj.model_name}_#{obj.to_param}"]
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def polymorphic_type_method
|
82
|
+
@method.to_s.sub('_id', '') + '_type'
|
83
|
+
end
|
84
|
+
|
85
|
+
def polymorphic_id_method
|
86
|
+
@method.to_s.sub('_id', '') + '_id'
|
87
|
+
end
|
88
|
+
|
89
|
+
def polymorphic_value
|
90
|
+
"#{value.model_name}_#{value.to_param}" if value.present?
|
91
|
+
end
|
92
|
+
|
93
|
+
def polymorphic_type_value
|
94
|
+
value.try(:model_name)
|
95
|
+
end
|
96
|
+
|
97
|
+
def polymorphic_id_value
|
98
|
+
value.try(:to_param)
|
99
|
+
end
|
100
|
+
|
33
101
|
def options
|
34
102
|
super().tap do |options|
|
35
103
|
options[:selected] = value if value
|
36
104
|
|
37
105
|
options[:multiple] = true if (options[:tags] == true)
|
38
106
|
options[:include_blank] = (options[:multiple] != true)
|
107
|
+
options[:selected] = polymorphic_value if options[:polymorphic]
|
39
108
|
end
|
40
109
|
end
|
41
110
|
|
42
111
|
def html_options
|
43
112
|
super().tap do |html_options|
|
44
113
|
html_options[:multiple] = options[:multiple]
|
114
|
+
html_options[:class] << 'polymorphic' if options[:polymorphic]
|
115
|
+
html_options[:class] << 'grouped' if options[:grouped]
|
45
116
|
end
|
46
117
|
end
|
47
118
|
|
@@ -8,8 +8,8 @@ if defined?(SimpleForm)
|
|
8
8
|
label_method, value_method = detect_collection_methods
|
9
9
|
|
10
10
|
options[:collection] = collection
|
11
|
-
options[:label_method] = label_method
|
12
|
-
options[:value_method] = value_method
|
11
|
+
options[:label_method] = label_method unless options[:polymorphic]
|
12
|
+
options[:value_method] = value_method unless options[:polymorphic]
|
13
13
|
|
14
14
|
Inputs::EffectiveSelect::Input.new(object, object_name, template, attribute_name, input_options, (merge_wrapper_options(input_html_options, wrapper_options) || {})).to_html
|
15
15
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: effective_form_inputs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Code and Effect
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-09-
|
11
|
+
date: 2015-09-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|