/vendor/plugins/acts-as-taggable-on/spec/acts_as_taggable_on/taggable_spec.rb

https://gitlab.com/sandorczettner/carpopularizer · Ruby · 272 lines · 222 code · 49 blank · 1 comment · 43 complexity · 3230178e5074647ad4370d9dbb549379 MD5 · raw file

  1. require File.dirname(__FILE__) + '/../spec_helper'
  2. describe "Taggable" do
  3. before(:each) do
  4. clean_database!
  5. @taggable = TaggableModel.new(:name => "Bob Jones")
  6. end
  7. it "should have tag types" do
  8. for type in [:tags, :languages, :skills, :needs, :offerings]
  9. TaggableModel.tag_types.should include type
  10. end
  11. @taggable.tag_types.should == TaggableModel.tag_types
  12. end
  13. it "should have tag_counts_on" do
  14. TaggableModel.tag_counts_on(:tags).should be_empty
  15. @taggable.tag_list = ["awesome", "epic"]
  16. @taggable.save
  17. TaggableModel.tag_counts_on(:tags).length.should == 2
  18. @taggable.tag_counts_on(:tags).length.should == 2
  19. end
  20. it "should be able to create tags" do
  21. @taggable.skill_list = "ruby, rails, css"
  22. @taggable.instance_variable_get("@skill_list").instance_of?(Hash).should be_true
  23. @taggable.instance_variable_get("@skill_list")[nil].instance_of?(TagList).should be_true
  24. @taggable.save
  25. Tag.find(:all).size.should == 3
  26. end
  27. it "should be able to create tags through the tag list directly" do
  28. @taggable.tag_list_on(:test).add("hello")
  29. @taggable.tag_list_cache_on(:test).should_not be_empty
  30. @taggable.save
  31. @taggable.save_tags
  32. @taggable.reload
  33. @taggable.tag_list_on(:test).should == ["hello"]
  34. end
  35. it "should differentiate between contexts" do
  36. @taggable.skill_list = "ruby, rails, css"
  37. @taggable.tag_list = "ruby, bob, charlie"
  38. @taggable.save
  39. @taggable.reload
  40. @taggable.skill_list.should include("ruby")
  41. @taggable.skill_list.should_not include("bob")
  42. end
  43. it "should be able to remove tags through list alone" do
  44. @taggable.skill_list = "ruby, rails, css"
  45. @taggable.save
  46. @taggable.reload
  47. @taggable.should have(3).skills
  48. @taggable.skill_list = "ruby, rails"
  49. @taggable.save
  50. @taggable.reload
  51. @taggable.should have(2).skills
  52. end
  53. it "should be able to find by tag" do
  54. @taggable.skill_list = "ruby, rails, css"
  55. @taggable.save
  56. TaggableModel.find_tagged_with("ruby").first.should == @taggable
  57. end
  58. it "should be able to find by tag with context" do
  59. @taggable.skill_list = "ruby, rails, css"
  60. @taggable.tag_list = "bob, charlie"
  61. @taggable.save
  62. TaggableModel.find_tagged_with("ruby").first.should == @taggable
  63. TaggableModel.find_tagged_with("bob", :on => :skills).first.should_not == @taggable
  64. TaggableModel.find_tagged_with("bob", :on => :tags).first.should == @taggable
  65. end
  66. it "should be able to use the tagged_with named scope" do
  67. @taggable.skill_list = "ruby, rails, css"
  68. @taggable.tag_list = "bob, charlie"
  69. @taggable.save
  70. TaggableModel.tagged_with("ruby").first.should == @taggable
  71. TaggableModel.tagged_with("ruby, css").first.should == @taggable
  72. TaggableModel.tagged_with("ruby, nonexistingtag").should be_empty
  73. TaggableModel.tagged_with("bob", :on => :skills).first.should_not == @taggable
  74. TaggableModel.tagged_with("bob", :on => :tags).first.should == @taggable
  75. end
  76. it "should not care about case" do
  77. bob = TaggableModel.create(:name => "Bob", :tag_list => "ruby")
  78. frank = TaggableModel.create(:name => "Frank", :tag_list => "Ruby")
  79. Tag.find(:all).size.should == 1
  80. TaggableModel.find_tagged_with("ruby").should == TaggableModel.find_tagged_with("Ruby")
  81. end
  82. it "should be able to get tag counts on model as a whole" do
  83. bob = TaggableModel.create(:name => "Bob", :tag_list => "ruby, rails, css")
  84. frank = TaggableModel.create(:name => "Frank", :tag_list => "ruby, rails")
  85. charlie = TaggableModel.create(:name => "Charlie", :skill_list => "ruby")
  86. TaggableModel.tag_counts.should_not be_empty
  87. TaggableModel.skill_counts.should_not be_empty
  88. end
  89. it "should be able to get all tag counts on model as whole" do
  90. bob = TaggableModel.create(:name => "Bob", :tag_list => "ruby, rails, css")
  91. frank = TaggableModel.create(:name => "Frank", :tag_list => "ruby, rails")
  92. charlie = TaggableModel.create(:name => "Charlie", :skill_list => "ruby")
  93. TaggableModel.all_tag_counts.should_not be_empty
  94. TaggableModel.all_tag_counts.first.count.should == 3 # ruby
  95. end
  96. it "should not return read-only records" do
  97. TaggableModel.create(:name => "Bob", :tag_list => "ruby, rails, css")
  98. TaggableModel.tagged_with("ruby").first.should_not be_readonly
  99. end
  100. it "should be able to get scoped tag counts" do
  101. bob = TaggableModel.create(:name => "Bob", :tag_list => "ruby, rails, css")
  102. frank = TaggableModel.create(:name => "Frank", :tag_list => "ruby, rails")
  103. charlie = TaggableModel.create(:name => "Charlie", :skill_list => "ruby")
  104. TaggableModel.tagged_with("ruby").tag_counts.first.count.should == 2 # ruby
  105. TaggableModel.tagged_with("ruby").skill_counts.first.count.should == 1 # ruby
  106. end
  107. it "should be able to get all scoped tag counts" do
  108. bob = TaggableModel.create(:name => "Bob", :tag_list => "ruby, rails, css")
  109. frank = TaggableModel.create(:name => "Frank", :tag_list => "ruby, rails")
  110. charlie = TaggableModel.create(:name => "Charlie", :skill_list => "ruby")
  111. TaggableModel.tagged_with("ruby").all_tag_counts.first.count.should == 3 # ruby
  112. end
  113. it "should be able to set a custom tag context list" do
  114. bob = TaggableModel.create(:name => "Bob")
  115. bob.set_tag_list_on(:rotors, "spinning, jumping")
  116. bob.tag_list_on(:rotors).should == ["spinning","jumping"]
  117. bob.save
  118. bob.reload
  119. bob.tags_on(:rotors).should_not be_empty
  120. end
  121. it "should be able to find tagged" do
  122. bob = TaggableModel.create(:name => "Bob", :tag_list => "fitter, happier, more productive", :skill_list => "ruby, rails, css")
  123. frank = TaggableModel.create(:name => "Frank", :tag_list => "weaker, depressed, inefficient", :skill_list => "ruby, rails, css")
  124. steve = TaggableModel.create(:name => 'Steve', :tag_list => 'fitter, happier, more productive', :skill_list => 'c++, java, ruby')
  125. TaggableModel.find_tagged_with("ruby", :order => 'taggable_models.name').should == [bob, frank, steve]
  126. TaggableModel.find_tagged_with("ruby, rails", :order => 'taggable_models.name').should == [bob, frank]
  127. TaggableModel.find_tagged_with(["ruby", "rails"], :order => 'taggable_models.name').should == [bob, frank]
  128. end
  129. it "should be able to find tagged with any tag" do
  130. bob = TaggableModel.create(:name => "Bob", :tag_list => "fitter, happier, more productive", :skill_list => "ruby, rails, css")
  131. frank = TaggableModel.create(:name => "Frank", :tag_list => "weaker, depressed, inefficient", :skill_list => "ruby, rails, css")
  132. steve = TaggableModel.create(:name => 'Steve', :tag_list => 'fitter, happier, more productive', :skill_list => 'c++, java, ruby')
  133. TaggableModel.find_tagged_with(["ruby", "java"], :order => 'taggable_models.name', :any => true).should == [bob, frank, steve]
  134. TaggableModel.find_tagged_with(["c++", "fitter"], :order => 'taggable_models.name', :any => true).should == [bob, steve]
  135. TaggableModel.find_tagged_with(["depressed", "css"], :order => 'taggable_models.name', :any => true).should == [bob, frank]
  136. end
  137. it "should be able to find tagged on a custom tag context" do
  138. bob = TaggableModel.create(:name => "Bob")
  139. bob.set_tag_list_on(:rotors, "spinning, jumping")
  140. bob.tag_list_on(:rotors).should == ["spinning","jumping"]
  141. bob.save
  142. TaggableModel.find_tagged_with("spinning", :on => :rotors).should == [bob]
  143. end
  144. it "should be able to use named scopes to chain tag finds" do
  145. bob = TaggableModel.create(:name => "Bob", :tag_list => "fitter, happier, more productive", :skill_list => "ruby, rails, css")
  146. frank = TaggableModel.create(:name => "Frank", :tag_list => "weaker, depressed, inefficient", :skill_list => "ruby, rails, css")
  147. steve = TaggableModel.create(:name => 'Steve', :tag_list => 'fitter, happier, more productive', :skill_list => 'c++, java, python')
  148. # Let's only find those productive Rails developers
  149. TaggableModel.tagged_with('rails', :on => :skills, :order => 'taggable_models.name').should == [bob, frank]
  150. TaggableModel.tagged_with('happier', :on => :tags, :order => 'taggable_models.name').should == [bob, steve]
  151. TaggableModel.tagged_with('rails', :on => :skills).tagged_with('happier', :on => :tags).should == [bob]
  152. TaggableModel.tagged_with('rails').tagged_with('happier', :on => :tags).should == [bob]
  153. end
  154. it "should be able to find tagged with only the matching tags" do
  155. bob = TaggableModel.create(:name => "Bob", :tag_list => "lazy, happier")
  156. frank = TaggableModel.create(:name => "Frank", :tag_list => "fitter, happier, inefficient")
  157. steve = TaggableModel.create(:name => 'Steve', :tag_list => "fitter, happier")
  158. TaggableModel.find_tagged_with("fitter, happier", :match_all => true).should == [steve]
  159. end
  160. it "should be able to find tagged with some excluded tags" do
  161. bob = TaggableModel.create(:name => "Bob", :tag_list => "happier, lazy")
  162. frank = TaggableModel.create(:name => "Frank", :tag_list => "happier")
  163. steve = TaggableModel.create(:name => 'Steve', :tag_list => "happier")
  164. TaggableModel.find_tagged_with("lazy", :exclude => true).should == [frank, steve]
  165. end
  166. it "should not create duplicate taggings" do
  167. bob = TaggableModel.create(:name => "Bob")
  168. lambda {
  169. bob.tag_list << "happier"
  170. bob.tag_list << "happier"
  171. bob.save
  172. }.should change(Tagging, :count).by(1)
  173. end
  174. describe "Associations" do
  175. before(:each) do
  176. @taggable = TaggableModel.create(:tag_list => "awesome, epic")
  177. end
  178. it "should not remove tags when creating associated objects" do
  179. @taggable.untaggable_models.create!
  180. @taggable.reload
  181. @taggable.tag_list.should have(2).items
  182. end
  183. end
  184. describe "Single Table Inheritance" do
  185. before do
  186. @taggable = TaggableModel.new(:name => "taggable")
  187. @inherited_same = InheritingTaggableModel.new(:name => "inherited same")
  188. @inherited_different = AlteredInheritingTaggableModel.new(:name => "inherited different")
  189. end
  190. it "should be able to save tags for inherited models" do
  191. @inherited_same.tag_list = "bob, kelso"
  192. @inherited_same.save
  193. InheritingTaggableModel.find_tagged_with("bob").first.should == @inherited_same
  194. end
  195. it "should find STI tagged models on the superclass" do
  196. @inherited_same.tag_list = "bob, kelso"
  197. @inherited_same.save
  198. TaggableModel.find_tagged_with("bob").first.should == @inherited_same
  199. end
  200. it "should be able to add on contexts only to some subclasses" do
  201. @inherited_different.part_list = "fork, spoon"
  202. @inherited_different.save
  203. InheritingTaggableModel.find_tagged_with("fork", :on => :parts).should be_empty
  204. AlteredInheritingTaggableModel.find_tagged_with("fork", :on => :parts).first.should == @inherited_different
  205. end
  206. it "should have different tag_counts_on for inherited models" do
  207. @inherited_same.tag_list = "bob, kelso"
  208. @inherited_same.save!
  209. @inherited_different.tag_list = "fork, spoon"
  210. @inherited_different.save!
  211. InheritingTaggableModel.tag_counts_on(:tags).map(&:name).should == %w(bob kelso)
  212. AlteredInheritingTaggableModel.tag_counts_on(:tags).map(&:name).should == %w(fork spoon)
  213. TaggableModel.tag_counts_on(:tags).map(&:name).should == %w(bob kelso fork spoon)
  214. end
  215. it 'should store same tag without validation conflict' do
  216. @taggable.tag_list = 'one'
  217. @taggable.save!
  218. @inherited_same.tag_list = 'one'
  219. @inherited_same.save!
  220. @inherited_same.update_attributes! :name => 'foo'
  221. end
  222. end
  223. end