acts_as_dropdown 2.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +17 -0
- data/MIT-LICENSE +20 -0
- data/README +28 -0
- data/RUNNING_UNIT_TESTS +12 -0
- data/Rakefile +22 -0
- data/init.rb +1 -0
- data/lib/acts_as_dropdown.rb +170 -0
- data/test/abstract_unit.rb +37 -0
- data/test/active_record_dropdown_test.rb +75 -0
- data/test/array_dropdown_test.rb +27 -0
- data/test/database.yml +3 -0
- data/test/fixtures/state.rb +3 -0
- data/test/fixtures/states.yml +21 -0
- data/test/fixtures/status.rb +3 -0
- data/test/fixtures/statuses.yml +9 -0
- data/test/schema.rb +11 -0
- metadata +81 -0
data/CHANGELOG
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
2.0.3
|
2
|
+
* fix class Array extension from module in the gem (it worked as a plugin, but failed as a gem)
|
3
|
+
|
4
|
+
2.0.2 (2008-10-10)
|
5
|
+
* mirror'd code from http://acts-as-dropdown.googlecode.com/svn/trunk/ (rev 17) to github
|
6
|
+
* added gemspec to build as gem
|
7
|
+
|
8
|
+
2.0.0
|
9
|
+
* better testing support that now uses a sqlite3 database as the default.
|
10
|
+
* introduced the :include_blank option that works in a similar fashion to the select helper's, but also allow you to
|
11
|
+
specify what appears as the text of the blank option.
|
12
|
+
* allowing customization of all ActiveRecord::Base#find options.
|
13
|
+
* fix a bug that caused the class attributes to be forgotten, requiring that all options be specified for every to_dropdown call.
|
14
|
+
* renamed and aliased methods to better match the helper methods that they are supposed to be used with.
|
15
|
+
|
16
|
+
1.0.0
|
17
|
+
* Initial release of the plugin. Includes documentation and tests.
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2006 DeLynn Berry
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
= acts_as_dropdown
|
2
|
+
|
3
|
+
This plugin allows any ActiveRecord object to easily be used to create a HTML select option list.
|
4
|
+
|
5
|
+
The github repository gbdev-acts_as_dropdown was copied from http://acts-as-dropdown.googlecode.com/svn/trunk/ (rev 17) on 2008-10-10
|
6
|
+
|
7
|
+
The only current modifications in gbdev-acts_as_dropdown from the original are to make the plugin work as a gem and small reorganizing.
|
8
|
+
|
9
|
+
== Resources
|
10
|
+
|
11
|
+
Install
|
12
|
+
|
13
|
+
* [ruby] script\plugin install http://acts-as-dropdown.googlecode.com/svn/trunk/
|
14
|
+
|
15
|
+
Or even better, use Piston (http://piston.rubyforge.org)
|
16
|
+
|
17
|
+
* piston import http://acts-as-dropdown.googlecode.com/svn/trunk/ vendor/plugins/acts_as_dropdown
|
18
|
+
|
19
|
+
RDocs
|
20
|
+
|
21
|
+
* These can be found in the doc directory at the root of the acts_as_dropdown directory by loading the
|
22
|
+
index.html file into a browser.
|
23
|
+
|
24
|
+
Subversion
|
25
|
+
|
26
|
+
* http://acts-as-dropdown.googlecode.com/svn/
|
27
|
+
|
28
|
+
Special thanks to courtenay of http://habtm.com for giving me the idea for this plugin.
|
data/RUNNING_UNIT_TESTS
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
== Creating the test database
|
2
|
+
|
3
|
+
The test suite currently uses sqlite3 out of the box. I highly recommend using this configuration
|
4
|
+
for testing Rails plugins. If for some reason you can't install sqlite3 (and the required gem) you
|
5
|
+
can get the test suite to run with any other database adapter with a little tweaking of the
|
6
|
+
abstract_unit.rb file.
|
7
|
+
|
8
|
+
== Running by hand
|
9
|
+
|
10
|
+
Unit tests are located in the test directory. To execute the test do the following:
|
11
|
+
|
12
|
+
ruby dropdown_test.rb
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the acts_as_dropdown plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation for the acts_as_dropdown plugin.'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'ActsAsDropdown'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.rdoc_files.include('README')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'acts_as_dropdown'
|
@@ -0,0 +1,170 @@
|
|
1
|
+
# Copyright (c) 2006 DeLynn Berry
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
# a copy of this software and associated documentation files (the
|
5
|
+
# "Software"), to deal in the Software without restriction, including
|
6
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
# the following conditions:
|
10
|
+
#
|
11
|
+
# The above copyright notice and this permission notice shall be
|
12
|
+
# included in all copies or substantial portions of the Software.
|
13
|
+
#
|
14
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
+
|
22
|
+
module DeLynnBerry
|
23
|
+
module Dropdown
|
24
|
+
def self.included(base) # :nodoc:
|
25
|
+
base.extend ClassMethods
|
26
|
+
end
|
27
|
+
|
28
|
+
module ClassMethods
|
29
|
+
attr_accessor :dropdown_text_attr, :dropdown_value_attr, :include_blank, :find_arguments
|
30
|
+
|
31
|
+
# Specify this act if you want to your model be used easily with the <tt>select</tt> form helper. By default the
|
32
|
+
# plugin assumes you want to use the class' primary key for the option value and the <tt>name</tt> attribute for
|
33
|
+
# the option text.
|
34
|
+
#
|
35
|
+
# The acts_as_dropdown class method operates much like the ActiveRecord#find method when it comes to customization.
|
36
|
+
# You can alter the <tt>:text</tt> and <tt>:value</tt> attributes that are used. You can also alter what items are
|
37
|
+
# collected from the database by passing in any of the regular ActiveRecord#find options (i.e. <tt>:conditions</tt>,
|
38
|
+
# <tt>:order</tt>, <tt>:group</tt>, <tt>:limit</tt>, <tt>:offset</tt>, etc.)
|
39
|
+
#
|
40
|
+
# Examples:
|
41
|
+
#
|
42
|
+
# class State < ActiveRecord::Base
|
43
|
+
# acts_as_dropdown :text => "abbreviation", :conditions => "id < 4"
|
44
|
+
# end
|
45
|
+
#
|
46
|
+
# State.to_dropdown # => [["AL", 1], ["AK", 2], ["AZ", 3]]
|
47
|
+
#
|
48
|
+
# class State < ActiveRecord::Base
|
49
|
+
# acts_as_dropdown :conditions => "id < 4", :order => "name DESC"
|
50
|
+
# end
|
51
|
+
#
|
52
|
+
# State.to_dropdown # => [["Arizona", 3], ["Alaska", 2], ["Alabama", 1]]
|
53
|
+
#
|
54
|
+
# The class method <tt>to_dropdown</tt> can also alter the default class configuration using the same options hash.
|
55
|
+
#
|
56
|
+
# Example:
|
57
|
+
#
|
58
|
+
# class State < ActiveRecord::Base
|
59
|
+
# acts_as_dropdown :text => "abbreviation", :conditions => "id < 4"
|
60
|
+
# end
|
61
|
+
#
|
62
|
+
# State.to_dropdown :text => "name", :conditions => nil # => [["Alabama", 1], ["Alaska", 2], ["Arizona", 3], ["California", 4], ["Colorado", 5]]
|
63
|
+
#
|
64
|
+
# == Configuration options
|
65
|
+
#
|
66
|
+
# * <tt>text</tt> - This is the class attribute (database column) that will be used as the text/label for
|
67
|
+
# the option tag (defaults to 'name').
|
68
|
+
# * <tt>value</tt> - This is the class attribute (database column) that will be used to fill in the option's
|
69
|
+
# value parameter (defaults to the class' primary_key).
|
70
|
+
# * <tt>include_blank</tt> - Specify true if you'd like to have a blank item added to the beginning of your list, or
|
71
|
+
# a string that will be placed in the value attribute of the option group.
|
72
|
+
#
|
73
|
+
# All of ActiveRecord#find options are available as well:
|
74
|
+
#
|
75
|
+
# * <tt>:conditions</tt>: An SQL fragment like "administrator = 1" or [ "user_name = ?", username ].
|
76
|
+
# * <tt>:order</tt>: A SQL fragment like "created_at DESC, name".
|
77
|
+
# * <tt>:group</tt>: An attribute name by which the result should be grouped. Uses the GROUP BY SQL-clause.
|
78
|
+
# * <tt>:limit</tt>: An integer determining the limit on the number of rows that should be returned.
|
79
|
+
# * <tt>:offset</tt>: An integer determining the offset from where the rows should be fetched. So at 5, it would skip the first 4 rows.
|
80
|
+
# * <tt>:joins</tt>: An SQL fragment for additional joins like "LEFT JOIN comments ON comments.post_id = id". (Rarely needed).
|
81
|
+
# * <tt>:include</tt>: Names associations that should be loaded alongside using LEFT OUTER JOINs. The symbols named refer
|
82
|
+
# to already defined associations. See eager loading under Associations.
|
83
|
+
# * <tt>:select</tt>: By default, this is * as in SELECT * FROM, but can be changed if you for example want to do a join, but not
|
84
|
+
# include the joined columns.
|
85
|
+
def acts_as_dropdown(*args)
|
86
|
+
options = {:text => 'name', :value => self.primary_key}
|
87
|
+
options.merge!(args.pop) unless args.empty?
|
88
|
+
options.merge!(:order => options[:value]) unless options.has_key?(:order)
|
89
|
+
|
90
|
+
self.dropdown_text_attr = options.delete(:text)
|
91
|
+
self.dropdown_value_attr = options.delete(:value)
|
92
|
+
self.include_blank = options.delete(:include_blank)
|
93
|
+
self.find_arguments = options
|
94
|
+
end
|
95
|
+
|
96
|
+
# Examples:
|
97
|
+
#
|
98
|
+
# class State < ActiveRecord::Base
|
99
|
+
# acts_as_dropdown :text => "abbreviation", :conditions => "id < 4"
|
100
|
+
# end
|
101
|
+
#
|
102
|
+
# State.to_dropdown # => [["AL", 1], ["AK", 2], ["AZ", 3]]
|
103
|
+
#
|
104
|
+
# class State < ActiveRecord::Base
|
105
|
+
# acts_as_dropdown :conditions => "id < 4", :order => "name DESC"
|
106
|
+
# end
|
107
|
+
#
|
108
|
+
# State.to_dropdown # => [["Arizona", 3], ["Alaska", 2], ["Alabama", 1]]
|
109
|
+
#
|
110
|
+
# The class method <tt>to_dropdown</tt> can also alter the default class configuration using the same options hash.
|
111
|
+
#
|
112
|
+
# Example:
|
113
|
+
#
|
114
|
+
# class State < ActiveRecord::Base
|
115
|
+
# acts_as_dropdown :text => "abbreviation", :conditions => "id < 4"
|
116
|
+
# end
|
117
|
+
#
|
118
|
+
# State.to_dropdown :text => "name", :conditions => nil # => [["Alabama", 1], ["Alaska", 2], ["Arizona", 3], ["California", 4], ["Colorado", 5]]
|
119
|
+
#
|
120
|
+
# See DeLynnBerry::Dropdown::ClassMethods#acts_as_dropdown for additional configuration options
|
121
|
+
def to_options_for_select(*args)
|
122
|
+
options = args.empty? ? {} : args.pop
|
123
|
+
text = options.delete(:text)
|
124
|
+
value = options.delete(:value)
|
125
|
+
blank = options.delete(:include_blank)
|
126
|
+
options.merge!(:order => value) if (!value.nil? && self.dropdown_value_attr != value) && options.has_key?(:order) == false
|
127
|
+
|
128
|
+
items = find(:all, options.empty? ? self.find_arguments : options).to_dropdown(text || self.dropdown_text_attr,
|
129
|
+
value || self.dropdown_value_attr)
|
130
|
+
|
131
|
+
if args.empty? && self.include_blank
|
132
|
+
items.insert(0, self.include_blank.kind_of?(String) ? [self.include_blank, ""] : ["", ""])
|
133
|
+
elsif blank
|
134
|
+
items.insert(0, blank.kind_of?(String) ? [blank, ""] : ["", ""])
|
135
|
+
end
|
136
|
+
items
|
137
|
+
end
|
138
|
+
alias :to_dropdown :to_options_for_select
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
ActiveRecord::Base.class_eval { include DeLynnBerry::Dropdown }
|
144
|
+
|
145
|
+
class Array #:nodoc:
|
146
|
+
# Collects the contents of the array and creates a new array that can be easily used
|
147
|
+
# with the <tt>select</tt> form helper method.
|
148
|
+
#
|
149
|
+
# == Options
|
150
|
+
#
|
151
|
+
# * <tt>text</tt> - This is the attribute that will be used as the text/label for the option tag (defaults to 'name').
|
152
|
+
# * <tt>value</tt> - This is the attribute that will be used to fill in the option's value parameter (defaults to 'id').
|
153
|
+
# * <tt>include_blank</tt> - Specify true if you'd like to have a blank item added to the beginning of your aray, or
|
154
|
+
# a string that will be placed in the value attribute of the option group.
|
155
|
+
#
|
156
|
+
# === Example
|
157
|
+
# >> @states = State.find(:all, :order => "id")
|
158
|
+
# >> @states.to_dropdown
|
159
|
+
# => [["Alabama", 1], ["Alaska", 2], ["Arizona", 3], ["California", 4], ["Colorado", 5]]
|
160
|
+
def to_options_for_select(text = :name, value = :id, include_blank = false)
|
161
|
+
items = self.collect { |x| [x.send(text.to_sym), x.send(value.to_sym)] }
|
162
|
+
|
163
|
+
if include_blank
|
164
|
+
items.insert(0, include_blank.kind_of?(String) ? [include_blank, ""] : ["", ""])
|
165
|
+
end
|
166
|
+
|
167
|
+
items
|
168
|
+
end
|
169
|
+
alias :to_dropdown :to_options_for_select
|
170
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
2
|
+
RAILS_ROOT = File.dirname(__FILE__)
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'test/unit'
|
6
|
+
require 'active_record'
|
7
|
+
require 'active_record/fixtures'
|
8
|
+
require 'active_support/binding_of_caller'
|
9
|
+
require 'active_support/breakpoint'
|
10
|
+
require "#{File.dirname(__FILE__)}/../init"
|
11
|
+
|
12
|
+
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
|
13
|
+
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
|
14
|
+
ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'sqlite3'])
|
15
|
+
|
16
|
+
load(File.dirname(__FILE__) + "/schema.rb") if File.exist?(File.dirname(__FILE__) + "/schema.rb")
|
17
|
+
|
18
|
+
Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures/"
|
19
|
+
$LOAD_PATH.unshift(Test::Unit::TestCase.fixture_path)
|
20
|
+
|
21
|
+
class Test::Unit::TestCase #:nodoc:
|
22
|
+
def create_fixtures(*table_names)
|
23
|
+
if block_given?
|
24
|
+
Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names) { yield }
|
25
|
+
else
|
26
|
+
Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# Turn off transactional fixtures if you're working with MyISAM tables in MySQL
|
31
|
+
self.use_transactional_fixtures = true
|
32
|
+
|
33
|
+
# Instantiated fixtures are slow, but give you @david where you otherwise would need people(:david)
|
34
|
+
self.use_instantiated_fixtures = false
|
35
|
+
|
36
|
+
# Add more helper methods to be used by all tests here...
|
37
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'abstract_unit')
|
2
|
+
require File.join(File.dirname(__FILE__), 'fixtures/state')
|
3
|
+
require File.join(File.dirname(__FILE__), 'fixtures/status')
|
4
|
+
|
5
|
+
class ActiveRecordDropdownTest < Test::Unit::TestCase # :nodoc:
|
6
|
+
fixtures :states, :statuses
|
7
|
+
|
8
|
+
def test_class_method
|
9
|
+
assert_equal [["Alabama", 1], ["Alaska", 2], ["Arizona", 3]],
|
10
|
+
State.to_dropdown
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_class_method_custom_conditions
|
14
|
+
assert_equal [["Alabama", 1], ["Alaska", 2], ["Arizona", 3]],
|
15
|
+
State.to_dropdown(:conditions => "id < 4")
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_class_method_change_text
|
19
|
+
assert_equal [["AL", 1], ["AK", 2], ["AZ", 3]],
|
20
|
+
State.to_dropdown(:text => "abbreviation")
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_class_method_change_text_custom_conditions
|
24
|
+
assert_equal [["AL", 1], ["AK", 2], ["AZ", 3]],
|
25
|
+
State.to_dropdown(:text => "abbreviation", :conditions => "id < 4")
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_class_method_change_value
|
29
|
+
assert_equal [["Alaska", "AK"], ["Alabama", "AL"], ["Arizona", "AZ"], ["California", "CA"], ["Colorado", "CO"]],
|
30
|
+
State.to_dropdown(:value => "abbreviation")
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_class_method_change_value_custom_conditions
|
34
|
+
assert_equal [["Alaska", "AK"], ["Alabama", "AL"], ["Arizona", "AZ"]],
|
35
|
+
State.to_dropdown(:value => "abbreviation", :conditions => "id < 4")
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_class_method_change_text_change_value
|
39
|
+
assert_equal [["AL", "Alabama"], ["AK", "Alaska"], ["AZ", "Arizona"], ["CA", "California"], ["CO", "Colorado"]],
|
40
|
+
State.to_dropdown(:text => "abbreviation", :value => "name")
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_class_method_change_text_change_value_custom_conditions
|
44
|
+
assert_equal [["AL", "Alabama"], ["AK", "Alaska"], ["AZ", "Arizona"]],
|
45
|
+
State.to_dropdown(:text => "abbreviation", :value => "name", :conditions => "id < 4")
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_class_method_custom_order
|
49
|
+
assert_equal [["Colorado", 5], ["California", 4], ["Arizona", 3], ["Alabama", 1], ["Alaska", 2]],
|
50
|
+
State.to_dropdown(:order => "abbreviation DESC")
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_class_method_custom_order_custom_conditions
|
54
|
+
assert_equal [["Alabama", 1], ["Alaska", 2], ["Arizona", 3], ["California", 4]],
|
55
|
+
State.to_dropdown(:order => "name", :conditions => "id < 5")
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_class_method_change_all
|
59
|
+
assert_equal [[3, "AZ"], [1, "AL"], [2, "AK"]],
|
60
|
+
State.to_dropdown(:text => "id", :value => "abbreviation", :order => "abbreviation DESC", :conditions => "id < 4")
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_class_method_include_blank
|
64
|
+
assert_equal [["", ""], ["Alabama", 1], ["Alaska", 2], ["Arizona", 3], ["California", 4], ["Colorado", 5]],
|
65
|
+
State.to_dropdown(:conditions => nil, :order => "name", :include_blank => true)
|
66
|
+
assert_equal [["Select a State", ""], ["Alabama", 1], ["Alaska", 2], ["Arizona", 3], ["California", 4], ["Colorado", 5]],
|
67
|
+
State.to_dropdown(:conditions => nil, :order => "name", :include_blank => "Select a State")
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_class_method_different_value
|
71
|
+
assert_equal [["Bad", "B"], ["Good", "G"]],
|
72
|
+
Status.to_dropdown
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'abstract_unit')
|
2
|
+
require File.join(File.dirname(__FILE__), 'fixtures/state')
|
3
|
+
|
4
|
+
class ArrayDropdownTest < Test::Unit::TestCase # :nodoc:
|
5
|
+
fixtures :states
|
6
|
+
|
7
|
+
def test_array_to_dropdown
|
8
|
+
states = State.find(:all, :order => "id")
|
9
|
+
assert_equal [["Alabama", 1], ["Alaska", 2], ["Arizona", 3], ["California", 4], ["Colorado", 5]], states.to_dropdown
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_array_to_dropdown_change_text
|
13
|
+
states = State.find(:all, :order => "id")
|
14
|
+
assert_equal [["AL", 1], ["AK", 2], ["AZ", 3], ["CA", 4], ["CO", 5]], states.to_dropdown("abbreviation")
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_array_to_dropdown_change_both
|
18
|
+
states = State.find(:all, :order => "id")
|
19
|
+
assert_equal [["Alabama", "AL"], ["Alaska", "AK"], ["Arizona", "AZ"], ["California", "CA"], ["Colorado", "CO"]], states.to_dropdown("name", "abbreviation")
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_array_to_dropdown_include_blank
|
23
|
+
states = State.find(:all, :order => "id")
|
24
|
+
assert_equal [["", ""], ["AL", 1], ["AK", 2], ["AZ", 3], ["CA", 4], ["CO", 5]], states.to_dropdown("abbreviation", "id", true)
|
25
|
+
assert_equal [["Select a State", ""], ["AL", 1], ["AK", 2], ["AZ", 3], ["CA", 4], ["CO", 5]], states.to_dropdown("abbreviation", "id", "Select a State")
|
26
|
+
end
|
27
|
+
end
|
data/test/database.yml
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
|
2
|
+
alabama:
|
3
|
+
id: 1
|
4
|
+
name: Alabama
|
5
|
+
abbreviation: AL
|
6
|
+
alaska:
|
7
|
+
id: 2
|
8
|
+
name: Alaska
|
9
|
+
abbreviation: AK
|
10
|
+
arizona:
|
11
|
+
id: 3
|
12
|
+
name: Arizona
|
13
|
+
abbreviation: AZ
|
14
|
+
california:
|
15
|
+
id: 4
|
16
|
+
name: California
|
17
|
+
abbreviation: CA
|
18
|
+
colorado:
|
19
|
+
id: 5
|
20
|
+
name: Colorado
|
21
|
+
abbreviation: CO
|
data/test/schema.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
ActiveRecord::Schema.define(:version => 0) do
|
2
|
+
create_table :states, :force => true do |t|
|
3
|
+
t.column :name, :string
|
4
|
+
t.column :abbreviation, :string
|
5
|
+
end
|
6
|
+
|
7
|
+
create_table :statuses, :force => true do |t|
|
8
|
+
t.column :status, :string
|
9
|
+
t.column :abbreviation, :string
|
10
|
+
end
|
11
|
+
end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acts_as_dropdown
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- DeLynn Berry
|
8
|
+
- John Dell
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2008-10-10 00:00:00 -07:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: A Rails plugin that adds the ability to easily create an options list out of an ActiveRecord object
|
18
|
+
email:
|
19
|
+
- delynn@gmail.com
|
20
|
+
- spovich@gmail.com
|
21
|
+
executables: []
|
22
|
+
|
23
|
+
extensions: []
|
24
|
+
|
25
|
+
extra_rdoc_files: []
|
26
|
+
|
27
|
+
files:
|
28
|
+
- CHANGELOG
|
29
|
+
- init.rb
|
30
|
+
- MIT-LICENSE
|
31
|
+
- Rakefile
|
32
|
+
- README
|
33
|
+
- RUNNING_UNIT_TESTS
|
34
|
+
- lib/acts_as_dropdown.rb
|
35
|
+
- test/abstract_unit.rb
|
36
|
+
- test/active_record_dropdown_test.rb
|
37
|
+
- test/array_dropdown_test.rb
|
38
|
+
- test/database.yml
|
39
|
+
- test/fixtures/state.rb
|
40
|
+
- test/fixtures/states.yml
|
41
|
+
- test/fixtures/status.rb
|
42
|
+
- test/fixtures/statuses.yml
|
43
|
+
- test/schema.rb
|
44
|
+
has_rdoc: true
|
45
|
+
homepage: http://github.com/gbdev/acts_as_dropdown
|
46
|
+
licenses: []
|
47
|
+
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
version:
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: "0"
|
64
|
+
version:
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.3.5
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: Adds the ability to easily create an options list out of an ActiveRecord object
|
72
|
+
test_files:
|
73
|
+
- test/abstract_unit.rb
|
74
|
+
- test/active_record_dropdown_test.rb
|
75
|
+
- test/array_dropdown_test.rb
|
76
|
+
- test/database.yml
|
77
|
+
- test/fixtures/state.rb
|
78
|
+
- test/fixtures/states.yml
|
79
|
+
- test/fixtures/status.rb
|
80
|
+
- test/fixtures/statuses.yml
|
81
|
+
- test/schema.rb
|