acts_as_select 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +22 -3
- data/lib/active_record/acts/select.rb +3 -3
- data/test/select_test.rb +16 -9
- metadata +2 -2
data/README.md
CHANGED
@@ -68,12 +68,25 @@ And this model
|
|
68
68
|
end
|
69
69
|
|
70
70
|
This would create two select methods, `name_select` and `email_select`,
|
71
|
-
and
|
71
|
+
and four list methods `name_list`, `name_objects`, `email_list` and `email_objects`.
|
72
72
|
|
73
73
|
Person.name_select
|
74
74
|
#=> [["Bob", 1], ["Jen", 2], ["april", 3]]
|
75
75
|
Person.email_select
|
76
76
|
#=> [["bob@example.com", 1], ["jen@example.com", 2], ["april@example.com",3]]
|
77
|
+
Person.name_list
|
78
|
+
#=> ['Bob','Jen','april']
|
79
|
+
|
80
|
+
The name_objects is a special form that returns objects of the same class as the model, but with only the
|
81
|
+
quoted name and id fields. Effectively it returns the the same values as the name_select, but as an ActiveRecord
|
82
|
+
Relation object form with only name and id relationships, which can then be mapped out to other forms
|
83
|
+
|
84
|
+
Person.name_objects
|
85
|
+
#=> <Person::ActiveRecord_Relation:0x000000002911234>
|
86
|
+
Person.name_objects.to_a
|
87
|
+
#=> [<Person:0x00000002917998>,<Person:0x00000002918730>,<Person:0x0000000267ed58>]
|
88
|
+
Person.name_objects.to_a.map(&:attributes)
|
89
|
+
#=> [{'id'=>1, 'name'=>'Bob'},{'id'=>2, 'name'=>'Jen'},{'id'=>3, 'name'=>'april'}]
|
77
90
|
|
78
91
|
This is the right format for use in a select tag and friends
|
79
92
|
|
@@ -86,10 +99,16 @@ lowercase corrected name (for example)
|
|
86
99
|
Person.order('lower(name) asc').email_select
|
87
100
|
#=> [["sheri@example.com", 3], ["bob@example.com", 1], ["jen@example.com", 2]]
|
88
101
|
|
89
|
-
You can combine it with any other named_scopes, but as it maps to an array
|
90
|
-
in a select dropdown, it will need to be the last method on the chain.
|
102
|
+
You can combine it with any other named_scopes, scopes or scoping methods, but as it maps to an array
|
103
|
+
for use in a select dropdown, it will need to be the last method on the chain.
|
91
104
|
|
92
105
|
Person.where(...).group(...).order(...).name_select
|
106
|
+
|
107
|
+
The one exception to this is the name_objects format, which returns a relation and hence
|
108
|
+
can be used inline with the rest of the scopes.
|
109
|
+
|
110
|
+
Person.name_objects.order('name asc').to_sql
|
111
|
+
#=> "SELECT name, id FROM "people" ORDER BY name asc"
|
93
112
|
|
94
113
|
## Contributing
|
95
114
|
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module ActiveRecord
|
2
2
|
module Acts
|
3
3
|
module Select
|
4
|
-
VERSION="0.1.
|
4
|
+
VERSION="0.1.6"
|
5
5
|
class << self
|
6
6
|
def included(base)
|
7
7
|
base.instance_eval do
|
@@ -43,10 +43,10 @@ module ActiveRecord
|
|
43
43
|
end.map{|x| [x.send(field),x.send(primary_key)]}
|
44
44
|
end
|
45
45
|
define_method "#{field}_list" do
|
46
|
-
(respond_to?(:scoped) ? scoped(:select=>
|
46
|
+
(respond_to?(:scoped) ? scoped(:select=>field) : select(field)).map{|x| x.send(field)}
|
47
47
|
end
|
48
48
|
define_method "#{field}_objects" do
|
49
|
-
respond_to?(:scoped) ? scoped(:select=>
|
49
|
+
respond_to?(:scoped) ? scoped(:select=>"#{field}, #{primary_key}") : select("#{field}, #{primary_key}")
|
50
50
|
end
|
51
51
|
end
|
52
52
|
end
|
data/test/select_test.rb
CHANGED
@@ -32,25 +32,27 @@ def teardown_db
|
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
35
|
-
class Selection < ActiveRecord::Base
|
35
|
+
class Selection < ActiveRecord::Base
|
36
36
|
end
|
37
|
-
class SelectAll < Selection
|
37
|
+
class SelectAll < Selection
|
38
38
|
end
|
39
|
-
class SelectIncludeSingle < Selection
|
39
|
+
class SelectIncludeSingle < Selection
|
40
40
|
end
|
41
|
-
class SelectIncludeMulti < Selection
|
41
|
+
class SelectIncludeMulti < Selection
|
42
42
|
end
|
43
|
-
class SelectExcludeSingle < Selection
|
43
|
+
class SelectExcludeSingle < Selection
|
44
44
|
end
|
45
|
-
class SelectExcludeMulti < Selection
|
45
|
+
class SelectExcludeMulti < Selection
|
46
46
|
end
|
47
|
-
class SelectedIncludeExclude < Selection
|
47
|
+
class SelectedIncludeExclude < Selection
|
48
|
+
end
|
49
|
+
class SelectCollectionSingle < Selection
|
48
50
|
end
|
49
51
|
|
50
52
|
class SelectTest < (begin MiniTest::Test rescue Test::Unit::TestCase end)
|
51
53
|
def setup
|
52
54
|
setup_db
|
53
|
-
[SelectAll, SelectIncludeSingle, SelectIncludeMulti, SelectExcludeSingle, SelectExcludeMulti, SelectedIncludeExclude].each do |klass|
|
55
|
+
[SelectAll, SelectIncludeSingle, SelectIncludeMulti, SelectExcludeSingle, SelectExcludeMulti, SelectedIncludeExclude, SelectCollectionSingle].each do |klass|
|
54
56
|
klass.create! :name => 'first', :description => 'First field', :excluded_string => 'not allowed'
|
55
57
|
klass.create! :name => 'second', :description => 'Second field', :excluded_string => 'not allowed'
|
56
58
|
klass.create! :name => 'third', :description => 'Third field', :excluded_string => 'not allowed'
|
@@ -200,7 +202,12 @@ class SelectTest < (begin MiniTest::Test rescue Test::Unit::TestCase end)
|
|
200
202
|
def test_sorted_fields
|
201
203
|
SelectIncludeSingle.acts_as_select :include => 'name'
|
202
204
|
assert_equal(selection_names_sorted.map { |x| [x[0], x[1]+6] },SelectIncludeSingle.order('name asc').name_select)
|
203
|
-
|
205
|
+
end
|
206
|
+
|
207
|
+
def test_sorted_field_collections
|
208
|
+
SelectCollectionSingle.acts_as_select :include => %w(name description)
|
209
|
+
assert_equal(selection_names_sorted.map(&:first), SelectCollectionSingle.order('name asc').name_list)
|
210
|
+
assert_equal(SelectCollectionSingle.order('name asc').select("name, id"), SelectCollectionSingle.order('name asc').name_objects)
|
204
211
|
end
|
205
212
|
end
|
206
213
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_select
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-
|
12
|
+
date: 2016-06-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|