acts_as_select 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -35,7 +35,7 @@ This makes all columns database accessible. You can return selectable options by
35
35
  obtain a list of the contents of a column by calling <column_name>_list
36
36
 
37
37
  ### Options
38
- acts_as_select takes two options to allow for finer grained control over which methods are created:
38
+ acts_as_select takes two options to allow for finer grained control over which methods are created and allows for scoping to restrict or change order.
39
39
 
40
40
  :include => [column_list]
41
41
 
@@ -55,32 +55,41 @@ names in both lists, these columns are excluded, regardless of the order of the
55
55
 
56
56
  With this database table
57
57
 
58
- | id | name | email |
59
- |----|------|-----------------|
60
- | 1 | Bob | bob@example.com |
61
- | 2 | Jen | jen@example.com |
58
+ | id | name | email |
59
+ |----|------|------------------|
60
+ | 1 | Bob | bob@example.com |
61
+ | 2 | Jen | jen@example.com |
62
+ | 3 | april | sheri@example.com|
62
63
 
63
64
  And this model
64
65
 
65
66
  class Person < ActiveRecord::Base
66
- acts_as_select :include=>['name', 'email'], :exclude=>['id']
67
+ acts_as_select :include=>['name', 'email'], :exclude=>'id'
67
68
  end
68
69
 
69
70
  This would create two select methods, `name_select` and `email_select`,
70
71
  and three list methods `name_list` and `email_list`
71
72
 
72
73
  Person.name_select
73
- #=> [["Bob", 1], ["Jen", 2]]
74
+ #=> [["Bob", 1], ["Jen", 2], ["april", 3]]
74
75
  Person.email_select
75
- #=> [["bob@example.com", 1], ["jen@example.com", 2]]
76
+ #=> [["bob@example.com", 1], ["jen@example.com", 2], ["april@example.com",3]]
76
77
 
77
78
  This is the right format for use in a select tag and friends
78
79
 
79
80
  select :order, :customer, Person.name_select
80
81
 
81
- You can combine it with scopes
82
+ If you want to change the sorting of the table, based on a non-chosen field (for example), you can use
83
+ arel_scopes to do so. The same `email_select` and `email_list` can have their contents sorted by the
84
+ lowercase corrected name (for example)
82
85
 
83
- Person.where(...).name_select
86
+ Person.order('lower(name) asc').email_select
87
+ #=> [["sheri@example.com", 3], ["bob@example.com", 1], ["jen@example.com", 2]]
88
+
89
+ You can combine it with any other named_scopes, but as it maps to an array for use
90
+ in a select dropdown, it will need to be the last method on the chain.
91
+
92
+ Person.where(...).group(...).order(...).name_select
84
93
 
85
94
  ## Contributing
86
95
 
@@ -24,7 +24,7 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency 'sqlite3'
25
25
  if RUBY_VERSION >= '1.9'
26
26
  spec.add_development_dependency 'minitest'
27
- spec.add_development_dependency 'active_record'
27
+ spec.add_development_dependency 'activerecord'
28
28
  else
29
29
  spec.add_development_dependency 'test-unit'
30
30
  spec.add_development_dependency 'activerecord'
@@ -1,7 +1,7 @@
1
1
  module ActiveRecord
2
2
  module Acts
3
3
  module Select
4
- VERSION="0.1.4"
4
+ VERSION="0.1.5"
5
5
  class << self
6
6
  def included(base)
7
7
  base.instance_eval do
data/test/select_test.rb CHANGED
@@ -1,14 +1,16 @@
1
+ $:.unshift File.dirname(__FILE__) + '/../lib'
1
2
  require 'rubygems'
2
3
  if RUBY_VERSION >= '1.9'
4
+ gem 'minitest'
3
5
  require 'minitest/autorun'
4
6
  require 'active_record'
7
+ require_relative(File.dirname(__FILE__) + '/../lib/acts_as_select')
5
8
  else
6
9
  require 'test/unit'
7
10
  require 'activerecord'
8
11
  require 'sqlite3'
12
+ require File.dirname(__FILE__) + '/../acts_as_select'
9
13
  end
10
- $:.unshift File.dirname(__FILE__) + '/../lib'
11
- require File.dirname(__FILE__) + '/../acts_as_select'
12
14
 
13
15
  ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
14
16
 
@@ -84,6 +86,18 @@ class SelectTest < (begin MiniTest::Test rescue Test::Unit::TestCase end)
84
86
  ]
85
87
  end
86
88
 
89
+ def selection_names_sorted
90
+ [
91
+ ['fifth', 5],
92
+ ['first', 1],
93
+ ['forth', 4],
94
+ ['second', 2],
95
+ ['sixth', 6],
96
+ ['third', 3]
97
+ ]
98
+ end
99
+
100
+
87
101
  def selection_descriptions
88
102
  [
89
103
  ['First field', 1],
@@ -108,14 +122,14 @@ class SelectTest < (begin MiniTest::Test rescue Test::Unit::TestCase end)
108
122
 
109
123
  def test_all_names_selection
110
124
  SelectAll.acts_as_select
111
- assert_equal(SelectAll.id_select, selection_ids)
112
- assert_equal(SelectAll.id_list, selection_ids.map(&:first))
113
- assert_equal(SelectAll.name_select, selection_names)
114
- assert_equal(SelectAll.name_list, selection_names.map(&:first))
115
- assert_equal(SelectAll.description_select, selection_descriptions)
116
- assert_equal(SelectAll.description_list, selection_descriptions.map(&:first))
117
- assert_equal(SelectAll.excluded_string_select, selection_excluded_strings)
118
- assert_equal(SelectAll.excluded_string_list, selection_excluded_strings.map(&:first))
125
+ assert_equal(selection_ids,SelectAll.id_select)
126
+ assert_equal(selection_ids.map(&:first),SelectAll.id_list)
127
+ assert_equal(selection_names,SelectAll.name_select)
128
+ assert_equal(selection_names.map(&:first),SelectAll.name_list)
129
+ assert_equal(selection_descriptions,SelectAll.description_select)
130
+ assert_equal(selection_descriptions.map(&:first),SelectAll.description_list)
131
+ assert_equal(selection_excluded_strings,SelectAll.excluded_string_select)
132
+ assert_equal(selection_excluded_strings.map(&:first),SelectAll.excluded_string_list)
119
133
  end
120
134
 
121
135
  def test_incorrect_inclusion
@@ -127,8 +141,8 @@ class SelectTest < (begin MiniTest::Test rescue Test::Unit::TestCase end)
127
141
  SelectIncludeSingle.acts_as_select :include => 'name'
128
142
  assert_raises(NoMethodError) { SelectIncludeSingle.id_select }
129
143
  assert_raises(NoMethodError) { SelectIncludeSingle.id_list }
130
- assert_equal(SelectIncludeSingle.name_select, selection_names.map { |x| [x[0], x[1]+6] })
131
- assert_equal(SelectIncludeSingle.name_list, selection_names.map(&:first))
144
+ assert_equal(selection_names.map { |x| [x[0], x[1]+6] },SelectIncludeSingle.name_select)
145
+ assert_equal(selection_names.map(&:first),SelectIncludeSingle.name_list)
132
146
  assert_raises(NoMethodError) { SelectIncludeSingle.description_select }
133
147
  assert_raises(NoMethodError) { SelectIncludeSingle.description_list }
134
148
  assert_raises(NoMethodError) { SelectIncludeSingle.excluded_string_select }
@@ -139,32 +153,32 @@ class SelectTest < (begin MiniTest::Test rescue Test::Unit::TestCase end)
139
153
  SelectIncludeMulti.acts_as_select :include => %w(name description)
140
154
  assert_raises(NoMethodError) { SelectIncludeMulti.id_select }
141
155
  assert_raises(NoMethodError) { SelectIncludeMulti.id_list }
142
- assert_equal(SelectIncludeMulti.name_select, selection_names.map { |x| [x[0], x[1]+12] })
143
- assert_equal(SelectIncludeMulti.name_list, selection_names.map(&:first))
144
- assert_equal(SelectIncludeMulti.description_select, selection_descriptions.map { |x| [x[0], x[1]+12] })
145
- assert_equal(SelectIncludeMulti.description_list, selection_descriptions.map(&:first))
156
+ assert_equal(selection_names.map { |x| [x[0], x[1]+12] },SelectIncludeMulti.name_select)
157
+ assert_equal(selection_names.map(&:first), SelectIncludeMulti.name_list)
158
+ assert_equal(selection_descriptions.map { |x| [x[0], x[1]+12] },SelectIncludeMulti.description_select)
159
+ assert_equal(selection_descriptions.map(&:first),SelectIncludeMulti.description_list)
146
160
  assert_raises(NoMethodError) { SelectIncludeMulti.excluded_string_select }
147
161
  assert_raises(NoMethodError) { SelectIncludeMulti.excluded_string_list }
148
162
  end
149
163
 
150
164
  def test_single_field_exclusion
151
165
  SelectExcludeSingle.acts_as_select :exclude => 'excluded_string'
152
- assert_equal(SelectExcludeSingle.id_select, selection_ids.map { |x| [x[0]+18, x[1]+18] })
153
- assert_equal(SelectExcludeSingle.id_list, selection_ids.map { |x| x[0]+18 })
154
- assert_equal(SelectExcludeSingle.name_select, selection_names.map { |x| [x[0], x[1]+18] })
155
- assert_equal(SelectExcludeSingle.name_list, selection_names.map(&:first))
156
- assert_equal(SelectExcludeSingle.description_select, selection_descriptions.map { |x| [x[0], x[1]+18] })
157
- assert_equal(SelectExcludeSingle.description_list, selection_descriptions.map(&:first))
166
+ assert_equal(selection_ids.map { |x| [x[0]+18, x[1]+18] },SelectExcludeSingle.id_select)
167
+ assert_equal(selection_ids.map { |x| x[0]+18 },SelectExcludeSingle.id_list)
168
+ assert_equal(selection_names.map { |x| [x[0], x[1]+18] },SelectExcludeSingle.name_select)
169
+ assert_equal(selection_names.map(&:first),SelectExcludeSingle.name_list)
170
+ assert_equal(selection_descriptions.map { |x| [x[0], x[1]+18] },SelectExcludeSingle.description_select)
171
+ assert_equal(selection_descriptions.map(&:first),SelectExcludeSingle.description_list)
158
172
  assert_raises(NoMethodError) { SelectExcludeSingle.excluded_string_select }
159
173
  assert_raises(NoMethodError) { SelectExcludeSingle.excluded_string_list }
160
174
  end
161
175
 
162
176
  def test_multi_field_exclusion
163
177
  SelectExcludeMulti.acts_as_select :exclude => %w(description excluded_string)
164
- assert_equal(SelectExcludeMulti.id_select, selection_ids.map { |x| [x[0]+24, x[1]+24] })
165
- assert_equal(SelectExcludeMulti.id_list, selection_ids.map { |x| x[0]+24 })
166
- assert_equal(SelectExcludeMulti.name_select, selection_names.map { |x| [x[0], x[1]+24] })
167
- assert_equal(SelectExcludeMulti.name_list, selection_names.map(&:first))
178
+ assert_equal(selection_ids.map { |x| [x[0]+24, x[1]+24] },SelectExcludeMulti.id_select)
179
+ assert_equal(selection_ids.map { |x| x[0]+24 },SelectExcludeMulti.id_list)
180
+ assert_equal(selection_names.map { |x| [x[0], x[1]+24] },SelectExcludeMulti.name_select)
181
+ assert_equal(selection_names.map(&:first),SelectExcludeMulti.name_list)
168
182
  assert_raises(NoMethodError) { SelectExcludeMulti.description_select }
169
183
  assert_raises(NoMethodError) { SelectExcludeMulti.description_list }
170
184
  assert_raises(NoMethodError) { SelectExcludeMulti.excluded_string_select }
@@ -175,12 +189,18 @@ class SelectTest < (begin MiniTest::Test rescue Test::Unit::TestCase end)
175
189
  SelectedIncludeExclude.acts_as_select :include => %w(name description), :exclude => %w(description excluded_string)
176
190
  assert_raises(NoMethodError) { SelectedIncludeExclude.id_select }
177
191
  assert_raises(NoMethodError) { SelectedIncludeExclude.id_list }
178
- assert_equal(SelectedIncludeExclude.name_select, selection_names.map { |x| [x[0], x[1]+30] })
179
- assert_equal(SelectedIncludeExclude.name_list, selection_names.map(&:first))
192
+ assert_equal(selection_names.map { |x| [x[0], x[1]+30] },SelectedIncludeExclude.name_select)
193
+ assert_equal(selection_names.map(&:first),SelectedIncludeExclude.name_list)
180
194
  assert_raises(NoMethodError) { SelectedIncludeExclude.description_select }
181
195
  assert_raises(NoMethodError) { SelectedIncludeExclude.description_list }
182
196
  assert_raises(NoMethodError) { SelectedIncludeExclude.excluded_string_select }
183
197
  assert_raises(NoMethodError) { SelectedIncludeExclude.excluded_string_list }
184
198
  end
199
+
200
+ def test_sorted_fields
201
+ SelectIncludeSingle.acts_as_select :include => 'name'
202
+ assert_equal(selection_names_sorted.map { |x| [x[0], x[1]+6] },SelectIncludeSingle.order('name asc').name_select)
203
+ assert_equal(selection_names_sorted.map(&:first), SelectIncludeSingle.order('name asc').name_list)
204
+ end
185
205
  end
186
206
 
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
4
+ version: 0.1.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -76,7 +76,7 @@ dependencies:
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
78
  - !ruby/object:Gem::Dependency
79
- name: active_record
79
+ name: activerecord
80
80
  requirement: !ruby/object:Gem::Requirement
81
81
  none: false
82
82
  requirements: