kematzy-dm-is-select 0.0.2

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.
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ ## OS STUFF
2
+ .DS_Store
3
+
4
+ ## TM SPECIFIC
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## PROJECT::GENERAL
9
+ *.sw?
10
+ coverage
11
+ rdoc
12
+ pkg
13
+
14
+ ## PROJECT::SPECIFIC
15
+
data/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ === 0.0.1 / 2009-07-12
2
+
3
+ * Release!
4
+
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 mats
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.rdoc ADDED
@@ -0,0 +1,170 @@
1
+ = dm-is-select
2
+
3
+ A DataMapper plugin that makes getting the <tt><select></tt> options from a Model easier.
4
+
5
+
6
+ == Installation
7
+
8
+ # Add GitHub to your RubyGems sources
9
+ $ gem sources -a http://gems.github.com
10
+
11
+ $ (sudo)? gem install kematzy-dm-is-select
12
+
13
+ === Dependencies
14
+
15
+ The plugin depends upon the following:
16
+
17
+ * dm-core ( >= 0.10.0)
18
+ * dm-more ( >= 0.10.0) [not really, but your code will depend upon it]
19
+
20
+
21
+ <b>NB!</b> The plugin has been developed for and on DM's next branch[http://github.com/datamapper/dm-core/tree/next], so may NOT work on previous incarnations.
22
+
23
+
24
+
25
+ == Getting Started
26
+
27
+ Let's say you got a basic Category Model.
28
+
29
+ class Category
30
+ include DataMapper::Resource
31
+ property :id, Serial
32
+ property :name, String
33
+
34
+ is :select, :name
35
+
36
+ end
37
+
38
+ Through that simple declaration you get the following class method ..
39
+
40
+ Category.items_for_select_menu
41
+
42
+ ...which returns an Array with the Model items in a structured way, ready for use.
43
+
44
+ [
45
+ ["Select Category", nil],
46
+ [" ------ ", "nil"],
47
+ ["Category 1", 1],
48
+ ["Category 2", 2],
49
+ ["Category 3", 3],
50
+ ["Category 4", 4],
51
+ ["Category 5", 5]
52
+ ]
53
+
54
+ Great, but you don't want the prompt to say "Select Category", but
55
+
56
+ Category.items_for_select_menu( :prompt => "Choose Whatever" )
57
+
58
+
59
+ ...which returns the Array with the prompt changed
60
+
61
+ [
62
+ ["Choose Whatever", nil],
63
+ [" ------ ", "nil"],
64
+ ["Category 1", 1],
65
+ ...<snip>...
66
+ ]
67
+
68
+
69
+ OK, but you don't like the divider node (2nd in output above). That’s fine, you remove it like this:
70
+
71
+ Category.items_for_select_menu( :divider => false )
72
+
73
+
74
+ ...which returns the Array with the divider removed
75
+
76
+ [
77
+ ["Select Category", nil],
78
+ ["Category 1", 1],
79
+ ...<snip>...
80
+ ]
81
+
82
+
83
+
84
+ Hmm, fine, but I don't want either a prompt or a divider, you say. That's fine too.
85
+
86
+ Category.items_for_select_menu( :prompt => false )
87
+
88
+ ...which returns the Array with the prompt and divider removed
89
+
90
+ [
91
+ ["Category 1", 1],
92
+ ["Category 2", 1],
93
+ ...<snip>...
94
+ ]
95
+
96
+
97
+ OK, that's smooth, but my Category model is a tree with parents, children and so on. This won't work with that.
98
+
99
+ Sure, no problem, just do this when you declare the Model:
100
+
101
+ is :select, :name, :is_tree => true
102
+
103
+
104
+ Then you can just use this
105
+
106
+ Category.items_for_select_menu( :prompt => "Choose Parent" )
107
+
108
+ ...and you get this nicely formatted <tt><select></tt> options array
109
+
110
+ [
111
+ ["Choose Parent", nil],
112
+ [" ------ ", "nil"],
113
+ ["Top Level Category", 0],
114
+ [" ------ ", "nil"],
115
+ ["Parent-1", 1],
116
+ ["-- Parent-1-Child", 2],
117
+ ["-- -- Parent-1-Child-GrandChild", 3],
118
+ ["Parent-2", 4],
119
+ ["-- Parent-2-Child", 5],
120
+ ["-- -- Parent-2-Child-GrandChild", 6]
121
+ ]
122
+
123
+ <b>NB!</b> It only supports 3 levels at this point in time, as I think that's enough in most use cases,
124
+ but IF not, fix the code and let me know and I'll add it.
125
+
126
+ Cool, but that "Top Level Category" node is a bit ugly, but easily fixed.
127
+
128
+ Category.items_for_select_menu( :root_text => "1st Parent (root)" )
129
+
130
+ ...gives you:
131
+
132
+ [
133
+ ...<snip>...
134
+ [" ------ ", "nil"],
135
+ ["1st Parent (root)", 0],
136
+ [" ------ ", "nil"],
137
+ ...<snip>...
138
+ ]
139
+
140
+ You can even remove it all together by doing this:
141
+
142
+ Category.items_for_select_menu( :show_root => false )
143
+
144
+
145
+
146
+
147
+ Obviously all the config options from above works with Tree models as well.
148
+
149
+ == Last Few Words
150
+
151
+ OK, I admit it, not the most impressive DM plugin there is, but hey, it sure helps keeping your model/views cleaner.
152
+
153
+
154
+ == RTFM
155
+
156
+ For a better understanding of this gem/plugin, make sure you study the '<tt>dm-is-select/spec/integration/select_spec.rb</tt>' file.
157
+
158
+
159
+ == Errors / Bugs
160
+
161
+ If something is not behaving intuitively, it is a bug, and should be reported.
162
+ Report it here: http://github.com/kematzy/dm-is-select/issues
163
+
164
+ == Credits
165
+
166
+ Copyright (c) 2009-07-12 Kematzy [ kematzy gmail com ]
167
+
168
+ == Licence
169
+
170
+ Released under the MIT license.
data/Rakefile ADDED
@@ -0,0 +1,85 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "dm-is-select"
8
+ # gem.version = IO.read('VERSION')
9
+ gem.summary = %Q{A DataMapper plugin that makes getting the <tt><select></tt> options from a Model easier.}
10
+ gem.description = gem.summary
11
+ gem.email = "kematzy@gmail.com"
12
+ gem.homepage = "http://github.com/kematzy/dm-is-select"
13
+ gem.authors = ["kematzy"]
14
+ gem.extra_rdoc_files = %w[ README.rdoc LICENSE TODO History.txt ]
15
+ gem.add_dependency('dm-core', '>= 0.10.0')
16
+ gem.add_dependency('dm-more', '>= 0.10.0')
17
+ # gem.add_dependency('dm-validations', '>= 0.10.0')
18
+
19
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
20
+ end
21
+
22
+ rescue LoadError
23
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
24
+ end
25
+
26
+ require 'spec/rake/spectask'
27
+ Spec::Rake::SpecTask.new(:spec) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.spec_files = FileList['spec/**/*_spec.rb']
30
+ end
31
+
32
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
33
+ spec.libs << 'lib' << 'spec'
34
+ spec.pattern = 'spec/**/*_spec.rb'
35
+ spec.rcov = true
36
+ end
37
+
38
+ namespace :spec do
39
+
40
+ desc "Run all specifications verbosely"
41
+ Spec::Rake::SpecTask.new(:verbose) do |t|
42
+ t.libs << "lib"
43
+ t.spec_opts = ["--color", "--format", "specdoc", "--require", "spec/spec_helper.rb"]
44
+ end
45
+
46
+ desc "Run specific spec verbosely (SPEC=/path/2/file)"
47
+ Spec::Rake::SpecTask.new(:select) do |t|
48
+ t.libs << "lib"
49
+ t.spec_files = [ENV["SPEC"]]
50
+ t.spec_opts = ["--color", "--format", "specdoc", "--require", "spec/spec_helper.rb"]
51
+ end
52
+
53
+ end
54
+
55
+ task :default => :spec
56
+
57
+ require 'rake/rdoctask'
58
+ Rake::RDocTask.new do |rdoc|
59
+ if File.exist?('VERSION')
60
+ version = IO.read('VERSION').chomp
61
+ else
62
+ version = "[Unknown]"
63
+ end
64
+
65
+ rdoc.rdoc_dir = 'rdoc'
66
+ rdoc.title = "dm-is-select #{version}"
67
+ rdoc.rdoc_files.include('README*')
68
+ rdoc.rdoc_files.include('lib/**/*.rb')
69
+ end
70
+
71
+ # kept just as a reference for now.
72
+ #
73
+ # def sudo_gem(cmd)
74
+ # sh "#{SUDO} #{RUBY} -S gem #{cmd}", :verbose => false
75
+ # end
76
+ #
77
+ # desc "Install #{GEM_NAME} #{GEM_VERSION}"
78
+ # task :install => [ :package ] do
79
+ # sudo_gem "install --local pkg/#{GEM_NAME}-#{GEM_VERSION} --no-update-sources"
80
+ # end
81
+ #
82
+ # desc "Uninstall #{GEM_NAME} #{GEM_VERSION}"
83
+ # task :uninstall => [ :clobber ] do
84
+ # sudo_gem "uninstall #{GEM_NAME} -v#{GEM_VERSION} -Ix"
85
+ # end
data/TODO ADDED
File without changes
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.2
@@ -0,0 +1,59 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{dm-is-select}
5
+ s.version = "0.0.2"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["kematzy"]
9
+ s.date = %q{2009-07-15}
10
+ s.description = %q{A DataMapper plugin that makes getting the <tt><select></tt> options from a Model easier.}
11
+ s.email = %q{kematzy@gmail.com}
12
+ s.extra_rdoc_files = [
13
+ "History.txt",
14
+ "LICENSE",
15
+ "README.rdoc",
16
+ "TODO"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "History.txt",
21
+ "LICENSE",
22
+ "README.rdoc",
23
+ "Rakefile",
24
+ "TODO",
25
+ "VERSION",
26
+ "dm-is-select.gemspec",
27
+ "lib/dm-is-select.rb",
28
+ "lib/dm-is-select/is/select.rb",
29
+ "lib/dm-is-select/is/version.rb",
30
+ "spec/integration/select_spec.rb",
31
+ "spec/spec.opts",
32
+ "spec/spec_helper.rb"
33
+ ]
34
+ s.homepage = %q{http://github.com/kematzy/dm-is-select}
35
+ s.rdoc_options = ["--charset=UTF-8"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.3.4}
38
+ s.summary = %q{A DataMapper plugin that makes getting the <tt><select></tt> options from a Model easier.}
39
+ s.test_files = [
40
+ "spec/integration/select_spec.rb",
41
+ "spec/spec_helper.rb"
42
+ ]
43
+
44
+ if s.respond_to? :specification_version then
45
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
49
+ s.add_runtime_dependency(%q<dm-core>, [">= 0.10.0"])
50
+ s.add_runtime_dependency(%q<dm-more>, [">= 0.10.0"])
51
+ else
52
+ s.add_dependency(%q<dm-core>, [">= 0.10.0"])
53
+ s.add_dependency(%q<dm-more>, [">= 0.10.0"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<dm-core>, [">= 0.10.0"])
57
+ s.add_dependency(%q<dm-more>, [">= 0.10.0"])
58
+ end
59
+ end
@@ -0,0 +1,22 @@
1
+ # Needed to import datamapper and other gems
2
+ require 'rubygems'
3
+ require 'pathname'
4
+
5
+ # Add all external dependencies for the plugin here
6
+ gem 'dm-core', '~> 0.10.0'
7
+ require 'dm-core'
8
+ # gem 'extlib', '~> 0.9.13'
9
+ # require 'extlib'
10
+ gem 'dm-is-tree', '~> 0.10.0'
11
+ require 'dm-is-tree'
12
+
13
+
14
+ # Require plugin-files
15
+ require Pathname(__FILE__).dirname.expand_path / 'dm-is-select' / 'is' / 'select.rb'
16
+
17
+ DataMapper::Model.append_extensions(DataMapper::Is::Select)
18
+
19
+ # If you wish to add methods to all DM Models, use this:
20
+ #
21
+ # DataMapper::Model.append_inclusions(DataMapper::Is::Select::ResourceInstanceMethods)
22
+ #
@@ -0,0 +1,148 @@
1
+ module DataMapper
2
+ module Is
3
+
4
+ ##
5
+ # = dm-is-select
6
+ #
7
+ # A DataMapper plugin that makes getting the <tt><select></tt> options from a Model easier.
8
+ #
9
+ #
10
+ #
11
+ module Select
12
+
13
+ ##
14
+ # Defines the field to use for the select menu
15
+ #
16
+ # ==== Params
17
+ #
18
+ # * :field_name => the name of the field values shown in select
19
+ # * :options
20
+ # * :is_tree => whether if the current Model is an is :tree model
21
+ #
22
+ # ==== Examples
23
+ #
24
+ #
25
+ # is :select, :name
26
+ # => creates a <select> options array on the :name attribute of the model
27
+ #
28
+ # is :select, :name, :is_tree => true
29
+ # => creates a <select> options array with the results ordered in hierarchical order
30
+ # parent > child > grandchild for each parent
31
+ #
32
+ #
33
+ # @api public
34
+ def is_select(select_field = :name, options = {})
35
+ raise ArgumentError, "The :select_field, must be an existing attribute in the Model. Got [ #{select_field.inspect} ]" unless properties.any?{ |p| p.name == select_field.to_sym }
36
+
37
+ @select_options = {
38
+ # add specical features if we are working with Tree Model
39
+ :is_tree => false,
40
+ }.merge(options)
41
+
42
+ @select_field = select_field
43
+
44
+
45
+ # Add class & Instance methods
46
+ extend DataMapper::Is::Select::ClassMethods
47
+ # include DataMapper::Is::Select::InstanceMethods
48
+
49
+ end
50
+
51
+ module ClassMethods
52
+ attr_reader :select_field, :select_options
53
+
54
+ ##
55
+ # Provides the Model content in a ready to use <tt><select></tt> options array
56
+ #
57
+ # ==== Params
58
+ #
59
+ # * :options
60
+ # * :prompt [String/Boolean] => The text shown on the <tt><select></tt> field in the browser. (Defaults to "Select NameOfYourModel")
61
+ # * :divider [Boolean] => Whether to add a divider/separator between the prompt and the main options. (Defaults to +true+)
62
+ # * :order [Array] => A normal DM order declaration. (Defaults to [:name] or the name of the select_field declared)
63
+ # * :show_root [Boolean] => Whether to add the Top Level Parent in the choices. (Defaults to +true+)
64
+ # * :root_text [String] => The text to show as the Parent item in select list. (Defaults to "Top Level NameOfYourModel")
65
+ #
66
+ # ==== Examples
67
+ #
68
+ # Category.items_for_select_menu
69
+ # => [ ['Select Category',nil], ['---', nil], ['Category 1',1] ,....]
70
+ #
71
+ # Category.items_for_select_menu(:prompt => "Custom Prompt")
72
+ # => [ ['Custom Prompt',nil],...]
73
+ #
74
+ # Category.items_for_select_menu(:prompt => false)
75
+ # => [ ['Category 1',1] ,...]
76
+ #
77
+ # Category.items_for_select_menu(:divider => false )
78
+ # => array without the ['---', nil] node
79
+ #
80
+ # Category.items_for_select_menu(:order => [ :id.desc ] )
81
+ # => array with the order reversed. (Prompts & divider always comes first)
82
+ #
83
+ # If your model is a Tree:
84
+ #
85
+ # Category.items_for_select_menu(:root_text => "Custom Root Text") # sets the text for the Top Level (root) Parent
86
+ # => [ ..., ['Custom Root Text', 0],...]
87
+ #
88
+ # Category.items_for_select_menu(:show_root => false) # removes the Top Level (root) Parent from the
89
+ #
90
+ #
91
+ # @api public
92
+ def items_for_select_menu(options={})
93
+ options = {
94
+ :prompt => "Select #{self.name}",
95
+ :divider => true,
96
+ :order => [self.select_field.to_sym],
97
+ :show_root => true,
98
+ :root_text => "Top Level #{self.name}",
99
+ }.merge(options)
100
+
101
+ mi = self.select_options[:is_tree] ?
102
+ all(:parent_id => 0, :order => options[:order] ) :
103
+ all(:order => options[:order])
104
+
105
+ res = []
106
+ if options[:prompt]
107
+ res << [options[:prompt],nil]
108
+ res << [" ------ ",'nil'] if options[:divider]
109
+ if self.select_options[:is_tree]
110
+ if options[:show_root]
111
+ res << [options[:root_text], 0]
112
+ res << [" ------ ",'nil'] if options[:divider]
113
+ end
114
+ end
115
+ end
116
+
117
+ if self.select_options[:is_tree]
118
+ mi.each do |x|
119
+ res << [x.send(self.select_field), x.id]
120
+ unless x.children.blank?
121
+ x.children.each do |child|
122
+ res << ["-- #{child.send(self.select_field)}", child.id]
123
+ child.children.each do |grand_child|
124
+ res << ["-- -- #{grand_child.send(self.select_field)}", grand_child.id]
125
+ end unless child.children.blank?
126
+ end
127
+ end
128
+ end
129
+ else
130
+ mi.each do |x|
131
+ res << [x.send(self.select_field), x.id]
132
+ end
133
+ end
134
+ res
135
+ end
136
+
137
+ end # ClassMethods
138
+
139
+ # module InstanceMethods
140
+ #
141
+ # end # InstanceMethods
142
+
143
+ end # Select
144
+ end # Is
145
+
146
+ Model.append_extensions(Is::Select)
147
+
148
+ end # DataMapper
@@ -0,0 +1,7 @@
1
+ module DataMapper
2
+ module Is
3
+ module Select
4
+ VERSION = '0.0.2'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,223 @@
1
+ require 'pathname'
2
+ require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
3
+
4
+ if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
5
+ describe 'DataMapper::Is::Select' do
6
+
7
+ class Category
8
+ include DataMapper::Resource
9
+ property :id, Serial
10
+ property :name, String
11
+
12
+ is :select, :name
13
+
14
+ auto_migrate!
15
+ end
16
+
17
+ class TreeCategory
18
+ include DataMapper::Resource
19
+ property :id, Serial
20
+ property :name, String
21
+
22
+ is :tree, :order => :name
23
+ is :select, :name, :is_tree => true
24
+
25
+ auto_migrate!
26
+ end
27
+
28
+
29
+ 5.times do |n|
30
+ Category.create(:name => "Category #{n+1}")
31
+ end
32
+
33
+ 2.times do |parent_id|
34
+ parent_id += 1
35
+ parent = TreeCategory.create(:name => "TreeCategory-#{parent_id}" , :parent_id => 0 )
36
+ child = TreeCategory.create(:name => "TreeCategory-#{parent_id}-Child" , :parent_id => parent.id )
37
+ grandchild = TreeCategory.create(:name => "TreeCategory-#{parent_id}-Child-GrandChild" , :parent_id => child.id )
38
+ end
39
+
40
+
41
+ describe "Class Methods" do
42
+
43
+ describe "is :select" do
44
+
45
+ it "should work with both strings & symbols as the fieldname provided" do
46
+ lambda {
47
+ class Dummy
48
+ include DataMapper::Resource
49
+ property :id, Serial
50
+ property :name, String
51
+
52
+ is :select, 'name'
53
+ auto_migrate!
54
+ end
55
+
56
+ }.should_not raise_error(ArgumentError)
57
+ end
58
+
59
+ it "should raise an ArgumentError if non-existant field is provided" do
60
+ lambda {
61
+ class Dummy
62
+ include DataMapper::Resource
63
+ property :id, Serial
64
+ property :name, String
65
+
66
+ is :select, :does_not_exist
67
+ auto_migrate!
68
+ end
69
+
70
+ }.should raise_error(ArgumentError)
71
+ end
72
+
73
+ end #/ is :select
74
+
75
+ describe "#items_for_select_menu" do
76
+
77
+ describe "A Normal Model" do
78
+
79
+ it "should return the default select options when given no params" do
80
+ Category.items_for_select_menu.should == [
81
+ ["Select Category", nil],
82
+ [" ------ ", "nil"],
83
+ ["Category 1", 1],
84
+ ["Category 2", 2],
85
+ ["Category 3", 3],
86
+ ["Category 4", 4],
87
+ ["Category 5", 5]
88
+ ]
89
+ end
90
+
91
+ it "should return the select options with custom prompt when given the :prompt => ? " do
92
+ Category.items_for_select_menu(:prompt => "Custom Prompt " ).should == [
93
+ ["Custom Prompt ", nil],
94
+ [" ------ ", "nil"],
95
+ ["Category 1", 1],
96
+ ["Category 2", 2],
97
+ ["Category 3", 3],
98
+ ["Category 4", 4],
99
+ ["Category 5", 5]
100
+ ]
101
+ end
102
+
103
+ it "should return the select options without a prompt & divider when given :prompt => false" do
104
+ Category.items_for_select_menu(:prompt => false ).should == [
105
+ ["Category 1", 1],
106
+ ["Category 2", 2],
107
+ ["Category 3", 3],
108
+ ["Category 4", 4],
109
+ ["Category 5", 5]
110
+ ]
111
+ end
112
+
113
+ it "should return the select options without a prompt & divider when given :prompt => true" do
114
+ # TODO:: this should be reworked, but right now it's good enough
115
+ Category.items_for_select_menu(:prompt => true ).should == [
116
+ [true, nil],
117
+ [" ------ ", "nil"],
118
+ ["Category 1", 1],
119
+ ["Category 2", 2],
120
+ ["Category 3", 3],
121
+ ["Category 4", 4],
122
+ ["Category 5", 5]
123
+ ]
124
+ end
125
+
126
+ it "should return the select options without the divider when given :divider => false" do
127
+ Category.items_for_select_menu(:divider => false ).should == [
128
+ ["Select Category", nil],
129
+ ["Category 1", 1],
130
+ ["Category 2", 2],
131
+ ["Category 3", 3],
132
+ ["Category 4", 4],
133
+ ["Category 5", 5]
134
+ ]
135
+ end
136
+
137
+ it "should return the select options with reversed order when given :order => DESC" do
138
+ Category.items_for_select_menu(:order => [ :id.desc ] ).should == [
139
+ ["Select Category", nil],
140
+ [" ------ ", "nil"],
141
+ ["Category 5", 5],
142
+ ["Category 4", 4],
143
+ ["Category 3", 3],
144
+ ["Category 2", 2],
145
+ ["Category 1", 1]
146
+ ]
147
+ end
148
+
149
+ end #/ Normal Model
150
+
151
+ describe "Tree Model" do
152
+
153
+ it "should return the default select options when given no params" do
154
+ TreeCategory.items_for_select_menu.should == [
155
+ ["Select TreeCategory", nil],
156
+ [" ------ ", "nil"],
157
+ ["Top Level TreeCategory", 0],
158
+ [" ------ ", "nil"],
159
+ ["TreeCategory-1", 1],
160
+ ["-- TreeCategory-1-Child", 2],
161
+ ["-- -- TreeCategory-1-Child-GrandChild", 3],
162
+ ["TreeCategory-2", 4],
163
+ ["-- TreeCategory-2-Child", 5],
164
+ ["-- -- TreeCategory-2-Child-GrandChild", 6]
165
+ ]
166
+ end
167
+
168
+ it "should return the select options with reversed order when given :order => DESC" do
169
+ TreeCategory.items_for_select_menu(:order => [ :id.desc ] ).should == [
170
+ ["Select TreeCategory", nil],
171
+ [" ------ ", "nil"],
172
+ ["Top Level TreeCategory", 0],
173
+ [" ------ ", "nil"],
174
+ ["TreeCategory-2", 4],
175
+ ["-- TreeCategory-2-Child", 5],
176
+ ["-- -- TreeCategory-2-Child-GrandChild", 6],
177
+ ["TreeCategory-1", 1],
178
+ ["-- TreeCategory-1-Child", 2],
179
+ ["-- -- TreeCategory-1-Child-GrandChild", 3]
180
+ ]
181
+ end
182
+
183
+ it "should return the select options without the Top Level Parent when given :show_root => false" do
184
+ TreeCategory.items_for_select_menu(:show_root => false).should == [
185
+ ["Select TreeCategory", nil],
186
+ [" ------ ", "nil"],
187
+ ["TreeCategory-1", 1],
188
+ ["-- TreeCategory-1-Child", 2],
189
+ ["-- -- TreeCategory-1-Child-GrandChild", 3],
190
+ ["TreeCategory-2", 4],
191
+ ["-- TreeCategory-2-Child", 5],
192
+ ["-- -- TreeCategory-2-Child-GrandChild", 6]
193
+ ]
194
+ end
195
+
196
+ it "should return the select options with custom text for the Top Level Parent when given :root_text => ?" do
197
+ TreeCategory.items_for_select_menu(:root_text => "Custom Top Level Text").should == [
198
+ ["Select TreeCategory", nil],
199
+ [" ------ ", "nil"],
200
+ ["Custom Top Level Text", 0],
201
+ [" ------ ", "nil"],
202
+ ["TreeCategory-1", 1],
203
+ ["-- TreeCategory-1-Child", 2],
204
+ ["-- -- TreeCategory-1-Child-GrandChild", 3],
205
+ ["TreeCategory-2", 4],
206
+ ["-- TreeCategory-2-Child", 5],
207
+ ["-- -- TreeCategory-2-Child-GrandChild", 6]
208
+ ]
209
+ end
210
+
211
+ end #/ Tree Model
212
+
213
+ end #/ #item_for_select_menu
214
+
215
+ end #/ Class Methods
216
+
217
+ # describe "Instance Methods" do
218
+ #
219
+ # end #/ Instance Methods
220
+
221
+ end #/ DataMapper::Is::Select
222
+
223
+ end #/ if
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --colour
@@ -0,0 +1,28 @@
1
+ require 'pathname'
2
+ require 'rubygems'
3
+
4
+ gem 'rspec', '~>1.2.6'
5
+ require 'spec'
6
+ # gem 'dm-is-tree', '~>0.10.0'
7
+ # require 'dm-is-tree'
8
+
9
+ require Pathname(__FILE__).dirname.expand_path.parent + 'lib/dm-is-select'
10
+
11
+ def load_driver(name, default_uri)
12
+ return false if ENV['ADAPTER'] != name.to_s
13
+
14
+ begin
15
+ DataMapper.setup(name, ENV["#{name.to_s.upcase}_SPEC_URI"] || default_uri)
16
+ DataMapper::Repository.adapters[:default] = DataMapper::Repository.adapters[name]
17
+ true
18
+ rescue LoadError => e
19
+ warn "Could not load do_#{name}: #{e}"
20
+ false
21
+ end
22
+ end
23
+
24
+ ENV['ADAPTER'] ||= 'sqlite3'
25
+
26
+ HAS_SQLITE3 = load_driver(:sqlite3, 'sqlite3::memory:')
27
+ HAS_MYSQL = load_driver(:mysql, 'mysql://localhost/dm_core_test')
28
+ HAS_POSTGRES = load_driver(:postgres, 'postgres://postgres@localhost/dm_core_test')
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kematzy-dm-is-select
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - kematzy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-15 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: dm-core
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.10.0
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: dm-more
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.10.0
34
+ version:
35
+ description: A DataMapper plugin that makes getting the <tt><select></tt> options from a Model easier.
36
+ email: kematzy@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - History.txt
43
+ - LICENSE
44
+ - README.rdoc
45
+ - TODO
46
+ files:
47
+ - .gitignore
48
+ - History.txt
49
+ - LICENSE
50
+ - README.rdoc
51
+ - Rakefile
52
+ - TODO
53
+ - VERSION
54
+ - dm-is-select.gemspec
55
+ - lib/dm-is-select.rb
56
+ - lib/dm-is-select/is/select.rb
57
+ - lib/dm-is-select/is/version.rb
58
+ - spec/integration/select_spec.rb
59
+ - spec/spec.opts
60
+ - spec/spec_helper.rb
61
+ has_rdoc: false
62
+ homepage: http://github.com/kematzy/dm-is-select
63
+ post_install_message:
64
+ rdoc_options:
65
+ - --charset=UTF-8
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ version:
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "0"
79
+ version:
80
+ requirements: []
81
+
82
+ rubyforge_project:
83
+ rubygems_version: 1.2.0
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: A DataMapper plugin that makes getting the <tt><select></tt> options from a Model easier.
87
+ test_files:
88
+ - spec/integration/select_spec.rb
89
+ - spec/spec_helper.rb