acts-as-taggable-on-padrino 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. data/.rspec +2 -0
  2. data/CHANGELOG +29 -0
  3. data/Gemfile +15 -0
  4. data/MIT-LICENSE +20 -0
  5. data/README.rdoc +194 -0
  6. data/Rakefile +33 -0
  7. data/VERSION +1 -0
  8. data/acts-as-taggable-on-padrino.gemspec +94 -0
  9. data/lib/acts-as-taggable-on-padrino.rb +41 -0
  10. data/lib/acts_as_taggable_on_padrino/tag.rb +72 -0
  11. data/lib/acts_as_taggable_on_padrino/taggable.rb +64 -0
  12. data/lib/acts_as_taggable_on_padrino/taggable/cache.rb +54 -0
  13. data/lib/acts_as_taggable_on_padrino/taggable/collection.rb +91 -0
  14. data/lib/acts_as_taggable_on_padrino/taggable/core.rb +244 -0
  15. data/lib/acts_as_taggable_on_padrino/taggable/ownership.rb +104 -0
  16. data/lib/acts_as_taggable_on_padrino/taggable/related.rb +60 -0
  17. data/lib/acts_as_taggable_on_padrino/taggable/tag_list.rb +96 -0
  18. data/lib/acts_as_taggable_on_padrino/tagger.rb +65 -0
  19. data/lib/acts_as_taggable_on_padrino/tagging.rb +29 -0
  20. data/lib/acts_as_taggable_on_padrino/tags_helper.rb +17 -0
  21. data/lib/tasks/generate_migration.rb +23 -0
  22. data/lib/tasks/templates/migration.rb +29 -0
  23. data/spec/acts_as_taggable_on_padrino/acts_as_taggable_on_spec.rb +263 -0
  24. data/spec/acts_as_taggable_on_padrino/acts_as_tagger_spec.rb +110 -0
  25. data/spec/acts_as_taggable_on_padrino/tag_list_spec.rb +70 -0
  26. data/spec/acts_as_taggable_on_padrino/tag_spec.rb +105 -0
  27. data/spec/acts_as_taggable_on_padrino/taggable_spec.rb +333 -0
  28. data/spec/acts_as_taggable_on_padrino/tagger_spec.rb +90 -0
  29. data/spec/acts_as_taggable_on_padrino/tagging_spec.rb +26 -0
  30. data/spec/acts_as_taggable_on_padrino/tags_helper_spec.rb +26 -0
  31. data/spec/bm.rb +52 -0
  32. data/spec/database.yml.sample +17 -0
  33. data/spec/models.rb +47 -0
  34. data/spec/schema.rb +57 -0
  35. data/spec/spec_helper.rb +60 -0
  36. data/uninstall.rb +1 -0
  37. metadata +175 -0
@@ -0,0 +1,110 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe "acts_as_tagger" do
4
+ describe "Tagger Method Generation" do
5
+ before(:each) do
6
+ @tagger = TaggableUser.new()
7
+ end
8
+
9
+ it "should add #is_tagger? query method to the class-side" do
10
+ TaggableUser.should respond_to(:is_tagger?)
11
+ end
12
+
13
+ it "should return true from the class-side #is_tagger?" do
14
+ TaggableUser.is_tagger?.should be_true
15
+ end
16
+
17
+ it "should return false from the base #is_tagger?" do
18
+ ActiveRecord::Base.is_tagger?.should be_false
19
+ end
20
+
21
+ it "should add #is_tagger? query method to the singleton" do
22
+ @tagger.should respond_to(:is_tagger?)
23
+ end
24
+
25
+ it "should add #tag method on the instance-side" do
26
+ @tagger.should respond_to(:tag)
27
+ end
28
+
29
+ it "should generate an association for #owned_taggings and #owned_tags" do
30
+ @tagger.should respond_to(:owned_taggings, :owned_tags)
31
+ end
32
+ end
33
+
34
+ describe "#tag" do
35
+ context 'when called with a non-existent tag context' do
36
+ before(:each) do
37
+ @tagger = TaggableUser.new()
38
+ @taggable = TaggableModel.new(:name=>"Richard Prior")
39
+ end
40
+
41
+ it "should by default not throw an exception " do
42
+ @taggable.tag_list_on(:foo).should be_empty
43
+ lambda {
44
+ @tagger.tag(@taggable, :with=>'this, and, that', :on=>:foo)
45
+ }.should_not raise_error
46
+ end
47
+
48
+ it 'should by default create the tag context on-the-fly' do
49
+ @taggable.tag_list_on(:here_ond_now).should be_empty
50
+ @tagger.tag(@taggable, :with=>'that', :on => :here_ond_now)
51
+ @taggable.tag_list_on(:here_ond_now).should_not include('that')
52
+ @taggable.all_tags_list_on(:here_ond_now).should include('that')
53
+ end
54
+
55
+ it "should show all the tag list when both public and owned tags exist" do
56
+ @taggable.tag_list = 'ruby, python'
57
+ @tagger.tag(@taggable, :with => 'java, lisp', :on => :tags)
58
+ @taggable.all_tags_on(:tags).names.sort.should == %w(ruby python java lisp).sort
59
+ end
60
+
61
+ it "should not add owned tags to the common list" do
62
+ @taggable.tag_list = 'ruby, python'
63
+ @tagger.tag(@taggable, :with => 'java, lisp', :on => :tags)
64
+ @taggable.tag_list.should == %w(ruby python)
65
+ @tagger.tag(@taggable, :with => '', :on => :tags)
66
+ @taggable.tag_list.should == %w(ruby python)
67
+ end
68
+
69
+ it "should throw an exception when the default is over-ridden" do
70
+ @taggable.tag_list_on(:foo_boo).should be_empty
71
+ lambda {
72
+ @tagger.tag(@taggable, :with=>'this, and, that', :on=>:foo_boo, :force=>false)
73
+ }.should raise_error
74
+ end
75
+
76
+ it "should not create the tag context on-the-fly when the default is over-ridden" do
77
+ @taggable.tag_list_on(:foo_boo).should be_empty
78
+ @tagger.tag(@taggable, :with=>'this, and, that', :on=>:foo_boo, :force=>false) rescue
79
+ @taggable.tag_list_on(:foo_boo).should be_empty
80
+ end
81
+ end
82
+
83
+ describe "when called by multiple tagger's" do
84
+ before(:each) do
85
+ @user_x = TaggableUser.create(:name => "User X")
86
+ @user_y = TaggableUser.create(:name => "User Y")
87
+ @taggable = TaggableModel.create(:name => 'acts_as_taggable_on_padrino', :tag_list => 'plugin')
88
+
89
+ @user_x.tag(@taggable, :with => 'ruby, rails', :on => :tags)
90
+ @user_y.tag(@taggable, :with => 'ruby, plugin', :on => :tags)
91
+
92
+ @user_y.tag(@taggable, :with => '', :on => :tags)
93
+ @user_y.tag(@taggable, :with => '', :on => :tags)
94
+ end
95
+
96
+ it "should delete owned tags" do
97
+ @user_y.owned_tags.should == []
98
+ end
99
+
100
+ it "should not delete other taggers tags" do
101
+ @user_x.owned_tags.should have(2).items
102
+ end
103
+
104
+ it "should not delete original tags" do
105
+ @taggable.all_tags_list_on(:tags).should include('plugin')
106
+ end
107
+ end
108
+ end
109
+
110
+ end
@@ -0,0 +1,70 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe ActsAsTaggableOn::Taggable::TagList do
4
+ before(:each) do
5
+ @tag_list = ActsAsTaggableOn::Taggable::TagList.new("awesome","radical")
6
+ end
7
+
8
+ it "should be an array" do
9
+ @tag_list.is_a?(Array).should be_true
10
+ end
11
+
12
+ it "should be able to be add a new tag word" do
13
+ @tag_list.add("cool")
14
+ @tag_list.include?("cool").should be_true
15
+ end
16
+
17
+ it "should be able to add delimited lists of words" do
18
+ @tag_list.add("cool, wicked", :parse => true)
19
+ @tag_list.include?("cool").should be_true
20
+ @tag_list.include?("wicked").should be_true
21
+ end
22
+
23
+ it "should be able to add delimited list of words with quoted delimiters" do
24
+ @tag_list.add("'cool, wicked', \"really cool, really wicked\"", :parse => true)
25
+ @tag_list.include?("cool, wicked").should be_true
26
+ @tag_list.include?("really cool, really wicked").should be_true
27
+ end
28
+
29
+ it "should be able to handle other uses of quotation marks correctly" do
30
+ @tag_list.add("john's cool car, mary's wicked toy", :parse => true)
31
+ @tag_list.include?("john's cool car").should be_true
32
+ @tag_list.include?("mary's wicked toy").should be_true
33
+ end
34
+
35
+ it "should be able to add an array of words" do
36
+ @tag_list.add(["cool", "wicked"], :parse => true)
37
+ @tag_list.include?("cool").should be_true
38
+ @tag_list.include?("wicked").should be_true
39
+ end
40
+
41
+ it "should be able to remove words" do
42
+ @tag_list.remove("awesome")
43
+ @tag_list.include?("awesome").should be_false
44
+ end
45
+
46
+ it "should be able to remove delimited lists of words" do
47
+ @tag_list.remove("awesome, radical", :parse => true)
48
+ @tag_list.should be_empty
49
+ end
50
+
51
+ it "should be able to remove an array of words" do
52
+ @tag_list.remove(["awesome", "radical"], :parse => true)
53
+ @tag_list.should be_empty
54
+ end
55
+
56
+ it "should give a delimited list of words when converted to string" do
57
+ @tag_list.to_s.should == "awesome, radical"
58
+ end
59
+
60
+ it "should quote escape tags with commas in them" do
61
+ @tag_list.add("cool","rad,bodacious")
62
+ @tag_list.to_s.should == "awesome, radical, cool, \"rad,bodacious\""
63
+ end
64
+
65
+ it "should be able to call to_s on a frozen tag list" do
66
+ @tag_list.freeze
67
+ lambda { @tag_list.add("cool","rad,bodacious") }.should raise_error
68
+ lambda { @tag_list.to_s }.should_not raise_error
69
+ end
70
+ end
@@ -0,0 +1,105 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe ActsAsTaggableOn::Tag do
4
+ before(:each) do
5
+ @tag = TestTag.new
6
+ @user = TaggableModel.create(:name => "Pablo")
7
+ end
8
+
9
+ describe "named like any" do
10
+ before(:each) do
11
+ TestTag.create(:name => "awesome")
12
+ TestTag.create(:name => "epic")
13
+ end
14
+
15
+ it "should find both tags" do
16
+ TestTag.named_like_any(["awesome", "epic"]).should have(2).items
17
+ end
18
+ end
19
+
20
+ describe "find or create by name" do
21
+ before(:each) do
22
+ @tag.name = "awesome"
23
+ @tag.save
24
+ end
25
+
26
+ it "should find by name" do
27
+ TestTag.find_or_create_with_like_by_name("awesome").should == @tag
28
+ end
29
+
30
+ it "should find by name case insensitive" do
31
+ TestTag.find_or_create_with_like_by_name("AWESOME").should == @tag
32
+ end
33
+
34
+ it "should create by name" do
35
+ lambda {
36
+ TestTag.find_or_create_with_like_by_name("epic")
37
+ }.should change(TestTag, :count).by(1)
38
+ end
39
+ end
40
+
41
+ describe "find or create all by any name" do
42
+ before(:each) do
43
+ @tag.name = "awesome"
44
+ @tag.save
45
+ end
46
+
47
+ it "should find by name" do
48
+ TestTag.find_or_create_all_with_like_by_name("awesome").should == [@tag]
49
+ end
50
+
51
+ it "should find by name case insensitive" do
52
+ TestTag.find_or_create_all_with_like_by_name("AWESOME").should == [@tag]
53
+ end
54
+
55
+ it "should create by name" do
56
+ lambda {
57
+ TestTag.find_or_create_all_with_like_by_name("epic")
58
+ }.should change(TestTag, :count).by(1)
59
+ end
60
+
61
+ it "should find or create by name" do
62
+ lambda {
63
+ TestTag.find_or_create_all_with_like_by_name("awesome", "epic").map(&:name).should == ["awesome", "epic"]
64
+ }.should change(TestTag, :count).by(1)
65
+ end
66
+
67
+ it "should return an empty array if no tags are specified" do
68
+ TestTag.find_or_create_all_with_like_by_name([]).should == []
69
+ end
70
+ end
71
+
72
+ it "should require a name" do
73
+ @tag.valid?
74
+ @tag.errors[:name].should == ["can't be blank"]
75
+
76
+ @tag.name = "something"
77
+ @tag.valid?
78
+
79
+ @tag.errors[:name].should == []
80
+ end
81
+
82
+ it "should equal a tag with the same name" do
83
+ @tag.name = "awesome"
84
+ new_tag = TestTag.new(:name => "awesome")
85
+ new_tag.should == @tag
86
+ end
87
+
88
+ it "should return its name when to_s is called" do
89
+ @tag.name = "cool"
90
+ @tag.to_s.should == "cool"
91
+ end
92
+
93
+ it "have named_scope named(something)" do
94
+ @tag.name = "cool"
95
+ @tag.save!
96
+ TestTag.named('cool').should include(@tag)
97
+ end
98
+
99
+ it "have named_scope named_like(something)" do
100
+ @tag.name = "cool"
101
+ @tag.save!
102
+ @another_tag = TestTag.create!(:name => "coolip")
103
+ TestTag.named_like('cool').should include(@tag, @another_tag)
104
+ end
105
+ end
@@ -0,0 +1,333 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe "Taggable" do
4
+ before(:each) do
5
+ @taggable = TaggableModel.new(:name => "Bob Jones")
6
+ @taggables = [@taggable, TaggableModel.new(:name => "John Doe")]
7
+ end
8
+
9
+ it "should have tag types" do
10
+ [:tags, :languages, :skills, :needs, :offerings].each do |type|
11
+ TaggableModel.tag_types.should include type
12
+ end
13
+
14
+ @taggable.tag_types.should == TaggableModel.tag_types
15
+ end
16
+
17
+ it "should have tag_counts_on" do
18
+ TaggableModel.tag_counts_on(:tags).all.should be_empty
19
+
20
+ @taggable.tag_list = ["awesome", "epic"]
21
+ @taggable.save
22
+
23
+ TaggableModel.tag_counts_on(:tags).length.should == 2
24
+ @taggable.tag_counts_on(:tags).length.should == 2
25
+ end
26
+
27
+ it "should be able to create tags" do
28
+ @taggable.skill_list = "ruby, rails, css"
29
+ @taggable.instance_variable_get("@skill_list").instance_of?(ActsAsTaggableOn::Taggable::TagList).should be_true
30
+
31
+ lambda {
32
+ @taggable.save
33
+ }.should change(Tag, :count).by(3)
34
+
35
+ @taggable.reload
36
+ @taggable.skill_list.sort.should == %w(ruby rails css).sort
37
+ end
38
+
39
+ it "should be able to create tags through the tag list directly" do
40
+ @taggable.tag_list_on(:test).add("hello")
41
+ @taggable.tag_list_cache_on(:test).should_not be_empty
42
+ @taggable.tag_list_on(:test).should == ["hello"]
43
+
44
+ @taggable.save
45
+ @taggable.save_tags
46
+
47
+ @taggable.reload
48
+ @taggable.tag_list_on(:test).should == ["hello"]
49
+ end
50
+
51
+ it "should differentiate between contexts" do
52
+ @taggable.skill_list = "ruby, rails, css"
53
+ @taggable.tag_list = "ruby, bob, charlie"
54
+ @taggable.save
55
+ @taggable.reload
56
+ @taggable.skill_list.should include("ruby")
57
+ @taggable.skill_list.should_not include("bob")
58
+ end
59
+
60
+ it "should be able to remove tags through list alone" do
61
+ @taggable.skill_list = "ruby, rails, css"
62
+ @taggable.save
63
+ @taggable.reload
64
+ @taggable.should have(3).skills
65
+ @taggable.skill_list = "ruby, rails"
66
+ @taggable.save
67
+ @taggable.reload
68
+ @taggable.should have(2).skills
69
+ end
70
+
71
+ it "should be able to select taggables by subset of tags using ActiveRelation methods" do
72
+ @taggables[0].tag_list = "bob"
73
+ @taggables[1].tag_list = "charlie"
74
+ @taggables[0].skill_list = "ruby"
75
+ @taggables[1].skill_list = "css"
76
+ @taggables.each{|taggable| taggable.save}
77
+
78
+ @found_taggables_by_tag = TaggableModel.joins(:tags).where(:tags => {:name => ["bob"]})
79
+ @found_taggables_by_skill = TaggableModel.joins(:skills).where(:tags => {:name => ["ruby"]})
80
+
81
+ @found_taggables_by_tag.should include @taggables[0]
82
+ @found_taggables_by_tag.should_not include @taggables[1]
83
+ @found_taggables_by_skill.should include @taggables[0]
84
+ @found_taggables_by_skill.should_not include @taggables[1]
85
+ end
86
+
87
+ it "should be able to find by tag" do
88
+ @taggable.skill_list = "ruby, rails, css"
89
+ @taggable.save
90
+
91
+ TaggableModel.tagged_with("ruby").first.should == @taggable
92
+ end
93
+
94
+ it "should be able to find by tag with context" do
95
+ @taggable.skill_list = "ruby, rails, css"
96
+ @taggable.tag_list = "bob, charlie"
97
+ @taggable.save
98
+
99
+ TaggableModel.tagged_with("ruby").first.should == @taggable
100
+ TaggableModel.tagged_with("ruby, css").first.should == @taggable
101
+ TaggableModel.tagged_with("bob", :on => :skills).first.should_not == @taggable
102
+ TaggableModel.tagged_with("bob", :on => :tags).first.should == @taggable
103
+ end
104
+
105
+ it "should not care about case" do
106
+ bob = TaggableModel.create(:name => "Bob", :tag_list => "ruby")
107
+ frank = TaggableModel.create(:name => "Frank", :tag_list => "Ruby")
108
+
109
+ Tag.find(:all).size.should == 1
110
+ TaggableModel.tagged_with("ruby").to_a.should == TaggableModel.tagged_with("Ruby").to_a
111
+ end
112
+
113
+ it "should be able to get tag counts on model as a whole" do
114
+ bob = TaggableModel.create(:name => "Bob", :tag_list => "ruby, rails, css")
115
+ frank = TaggableModel.create(:name => "Frank", :tag_list => "ruby, rails")
116
+ charlie = TaggableModel.create(:name => "Charlie", :skill_list => "ruby")
117
+ TaggableModel.tag_counts.all.should_not be_empty
118
+ TaggableModel.skill_counts.all.should_not be_empty
119
+ end
120
+
121
+ it "should be able to get all tag counts on model as whole" do
122
+ bob = TaggableModel.create(:name => "Bob", :tag_list => "ruby, rails, css")
123
+ frank = TaggableModel.create(:name => "Frank", :tag_list => "ruby, rails")
124
+ charlie = TaggableModel.create(:name => "Charlie", :skill_list => "ruby")
125
+
126
+ TaggableModel.all_tag_counts.all.should_not be_empty
127
+ TaggableModel.all_tag_counts(:order => 'tags.id').first.count.should == 3 # ruby
128
+ end
129
+
130
+ it "should not return read-only records" do
131
+ TaggableModel.create(:name => "Bob", :tag_list => "ruby, rails, css")
132
+ TaggableModel.tagged_with("ruby").first.should_not be_readonly
133
+ end
134
+
135
+ it "should be able to get scoped tag counts" do
136
+ bob = TaggableModel.create(:name => "Bob", :tag_list => "ruby, rails, css")
137
+ frank = TaggableModel.create(:name => "Frank", :tag_list => "ruby, rails")
138
+ charlie = TaggableModel.create(:name => "Charlie", :skill_list => "ruby")
139
+
140
+ TaggableModel.tagged_with("ruby").tag_counts(:order => 'tags.id').first.count.should == 2 # ruby
141
+ TaggableModel.tagged_with("ruby").skill_counts.first.count.should == 1 # ruby
142
+ end
143
+
144
+ it "should be able to get all scoped tag counts" do
145
+ bob = TaggableModel.create(:name => "Bob", :tag_list => "ruby, rails, css")
146
+ frank = TaggableModel.create(:name => "Frank", :tag_list => "ruby, rails")
147
+ charlie = TaggableModel.create(:name => "Charlie", :skill_list => "ruby")
148
+
149
+ TaggableModel.tagged_with("ruby").all_tag_counts(:order => 'tags.id').first.count.should == 3 # ruby
150
+ end
151
+
152
+ it 'should only return tag counts for the available scope' do
153
+ bob = TaggableModel.create(:name => "Bob", :tag_list => "ruby, rails, css")
154
+ frank = TaggableModel.create(:name => "Frank", :tag_list => "ruby, rails")
155
+ charlie = TaggableModel.create(:name => "Charlie", :skill_list => "ruby, java")
156
+
157
+ TaggableModel.tagged_with('rails').all_tag_counts.should have(3).items
158
+ TaggableModel.tagged_with('rails').all_tag_counts.any? { |tag| tag.name == 'java' }.should be_false
159
+
160
+ # Test specific join syntaxes:
161
+ frank.untaggable_models.create!
162
+ TaggableModel.tagged_with('rails').joins(:untaggable_models).all_tag_counts.should have(2).items
163
+ TaggableModel.tagged_with('rails').joins(:untaggable_models => :taggable_model).all_tag_counts.should have(2).items
164
+ TaggableModel.tagged_with('rails').joins([:untaggable_models]).all_tag_counts.should have(2).items
165
+ end
166
+
167
+ it "should be able to set a custom tag context list" do
168
+ bob = TaggableModel.create(:name => "Bob")
169
+ bob.set_tag_list_on(:rotors, "spinning, jumping")
170
+ bob.tag_list_on(:rotors).should == ["spinning","jumping"]
171
+ bob.save
172
+ bob.reload
173
+ bob.tags_on(:rotors).should_not be_empty
174
+ end
175
+
176
+ it "should be able to find tagged" do
177
+ bob = TaggableModel.create(:name => "Bob", :tag_list => "fitter, happier, more productive", :skill_list => "ruby, rails, css")
178
+ frank = TaggableModel.create(:name => "Frank", :tag_list => "weaker, depressed, inefficient", :skill_list => "ruby, rails, css")
179
+ steve = TaggableModel.create(:name => 'Steve', :tag_list => 'fitter, happier, more productive', :skill_list => 'c++, java, ruby')
180
+
181
+ TaggableModel.tagged_with("ruby", :order => 'taggable_models.name').to_a.should == [bob, frank, steve]
182
+ TaggableModel.tagged_with("ruby, rails", :order => 'taggable_models.name').to_a.should == [bob, frank]
183
+ TaggableModel.tagged_with(["ruby", "rails"], :order => 'taggable_models.name').to_a.should == [bob, frank]
184
+ end
185
+
186
+ it "should be able to find tagged with quotation marks" do
187
+ bob = TaggableModel.create(:name => "Bob", :tag_list => "fitter, happier, more productive, 'I love the ,comma,'")
188
+ TaggableModel.tagged_with("'I love the ,comma,'").should include(bob)
189
+ end
190
+
191
+ it "should be able to find tagged with invalid tags" do
192
+ bob = TaggableModel.create(:name => "Bob", :tag_list => "fitter, happier, more productive")
193
+ TaggableModel.tagged_with("sad, happier").should_not include(bob)
194
+ end
195
+
196
+ it "should be able to find tagged with any tag" do
197
+ bob = TaggableModel.create(:name => "Bob", :tag_list => "fitter, happier, more productive", :skill_list => "ruby, rails, css")
198
+ frank = TaggableModel.create(:name => "Frank", :tag_list => "weaker, depressed, inefficient", :skill_list => "ruby, rails, css")
199
+ steve = TaggableModel.create(:name => 'Steve', :tag_list => 'fitter, happier, more productive', :skill_list => 'c++, java, ruby')
200
+
201
+ TaggableModel.tagged_with(["ruby", "java"], :order => 'taggable_models.name', :any => true).to_a.should == [bob, frank, steve]
202
+ TaggableModel.tagged_with(["c++", "fitter"], :order => 'taggable_models.name', :any => true).to_a.should == [bob, steve]
203
+ TaggableModel.tagged_with(["depressed", "css"], :order => 'taggable_models.name', :any => true).to_a.should == [bob, frank]
204
+ end
205
+
206
+ it "should be able to find tagged on a custom tag context" do
207
+ bob = TaggableModel.create(:name => "Bob")
208
+ bob.set_tag_list_on(:rotors, "spinning, jumping")
209
+ bob.tag_list_on(:rotors).should == ["spinning","jumping"]
210
+ bob.save
211
+
212
+ TaggableModel.tagged_with("spinning", :on => :rotors).to_a.should == [bob]
213
+ end
214
+
215
+ it "should be able to use named scopes to chain tag finds" do
216
+ bob = TaggableModel.create(:name => "Bob", :tag_list => "fitter, happier, more productive", :skill_list => "ruby, rails, css")
217
+ frank = TaggableModel.create(:name => "Frank", :tag_list => "weaker, depressed, inefficient", :skill_list => "ruby, rails, css")
218
+ steve = TaggableModel.create(:name => 'Steve', :tag_list => 'fitter, happier, more productive', :skill_list => 'c++, java, python')
219
+
220
+ # Let's only find those productive Rails developers
221
+ TaggableModel.tagged_with('rails', :on => :skills, :order => 'taggable_models.name').to_a.should == [bob, frank]
222
+ TaggableModel.tagged_with('happier', :on => :tags, :order => 'taggable_models.name').to_a.should == [bob, steve]
223
+ TaggableModel.tagged_with('rails', :on => :skills).tagged_with('happier', :on => :tags).to_a.should == [bob]
224
+ TaggableModel.tagged_with('rails').tagged_with('happier', :on => :tags).to_a.should == [bob]
225
+ end
226
+
227
+ it "should be able to find tagged with only the matching tags" do
228
+ bob = TaggableModel.create(:name => "Bob", :tag_list => "lazy, happier")
229
+ frank = TaggableModel.create(:name => "Frank", :tag_list => "fitter, happier, inefficient")
230
+ steve = TaggableModel.create(:name => 'Steve', :tag_list => "fitter, happier")
231
+
232
+ TaggableModel.tagged_with("fitter, happier", :match_all => true).to_a.should == [steve]
233
+ end
234
+
235
+ it "should be able to find tagged with some excluded tags" do
236
+ bob = TaggableModel.create(:name => "Bob", :tag_list => "happier, lazy")
237
+ frank = TaggableModel.create(:name => "Frank", :tag_list => "happier")
238
+ steve = TaggableModel.create(:name => 'Steve', :tag_list => "happier")
239
+
240
+ TaggableModel.tagged_with("lazy", :exclude => true).to_a.should == [frank, steve]
241
+ end
242
+
243
+ it "should not create duplicate taggings" do
244
+ bob = TaggableModel.create(:name => "Bob")
245
+ lambda {
246
+ bob.tag_list << "happier"
247
+ bob.tag_list << "happier"
248
+ bob.save
249
+ }.should change(Tagging, :count).by(1)
250
+ end
251
+
252
+ describe "Associations" do
253
+ before(:each) do
254
+ @taggable = TaggableModel.create(:tag_list => "awesome, epic")
255
+ end
256
+
257
+ it "should not remove tags when creating associated objects" do
258
+ @taggable.untaggable_models.create!
259
+ @taggable.reload
260
+ @taggable.tag_list.should have(2).items
261
+ end
262
+ end
263
+
264
+ describe "grouped_column_names_for method" do
265
+ if ActiveRecord::Base.connection.adapter_name == 'PostgreSQL'
266
+ context "on postgres" do
267
+ it "should return all column names joined for Tag" do
268
+ @taggable.grouped_column_names_for(Tag).should == "tags.id, tags.name"
269
+ end
270
+
271
+ it "should return all column names joined for TaggableModel" do
272
+ @taggable.grouped_column_names_for(TaggableModel).should == "taggable_models.id, taggable_models.name, taggable_models.type"
273
+ end
274
+ end
275
+ else
276
+ it "should return the id column for Tag" do
277
+ @taggable.grouped_column_names_for(Tag).should == "tags.id"
278
+ end
279
+
280
+ it "should return the id column for TaggableModel" do
281
+ @taggable.grouped_column_names_for(TaggableModel).should == "taggable_models.id"
282
+ end
283
+ end
284
+ end
285
+
286
+ describe "Single Table Inheritance" do
287
+ before do
288
+ @taggable = TaggableModel.new(:name => "taggable")
289
+ @inherited_same = InheritingTaggableModel.new(:name => "inherited same")
290
+ @inherited_different = AlteredInheritingTaggableModel.new(:name => "inherited different")
291
+ end
292
+
293
+ it "should be able to save tags for inherited models" do
294
+ @inherited_same.tag_list = "bob, kelso"
295
+ @inherited_same.save
296
+ InheritingTaggableModel.tagged_with("bob").first.should == @inherited_same
297
+ end
298
+
299
+ it "should find STI tagged models on the superclass" do
300
+ @inherited_same.tag_list = "bob, kelso"
301
+ @inherited_same.save
302
+ TaggableModel.tagged_with("bob").first.should == @inherited_same
303
+ end
304
+
305
+ it "should be able to add on contexts only to some subclasses" do
306
+ @inherited_different.part_list = "fork, spoon"
307
+ @inherited_different.save
308
+ InheritingTaggableModel.tagged_with("fork", :on => :parts).should be_empty
309
+ AlteredInheritingTaggableModel.tagged_with("fork", :on => :parts).first.should == @inherited_different
310
+ end
311
+
312
+ it "should have different tag_counts_on for inherited models" do
313
+ @inherited_same.tag_list = "bob, kelso"
314
+ @inherited_same.save!
315
+ @inherited_different.tag_list = "fork, spoon"
316
+ @inherited_different.save!
317
+
318
+ InheritingTaggableModel.tag_counts_on(:tags, :order => 'tags.id').names.should == %w(bob kelso)
319
+ AlteredInheritingTaggableModel.tag_counts_on(:tags, :order => 'tags.id').names.should == %w(fork spoon)
320
+ TaggableModel.tag_counts_on(:tags, :order => 'tags.id').names.should == %w(bob kelso fork spoon)
321
+ end
322
+
323
+ it 'should store same tag without validation conflict' do
324
+ @taggable.tag_list = 'one'
325
+ @taggable.save!
326
+
327
+ @inherited_same.tag_list = 'one'
328
+ @inherited_same.save!
329
+
330
+ @inherited_same.update_attributes! :name => 'foo'
331
+ end
332
+ end
333
+ end