Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixes for acts_as_ordered_taggable

1. ensure that tag associations & methods are created via core module
when a model includes multiple calls to acts_as_ordered_taggable
2. better tests
  • Loading branch information...
commit a12f83ac60b83dc5275c708c8186503509a24aaa 1 parent cdde065
Chris Hilton chrismhilton authored
6 lib/acts_as_taggable_on/acts_as_taggable_on/core.rb
@@ -52,11 +52,11 @@ def all_#{tags_type}_list
52 52 end
53 53 end
54 54
55   - def acts_as_taggable_on(*args)
56   - super(*args)
  55 + def taggable_on(preserve_tag_order, *tag_types)
  56 + super(preserve_tag_order, *tag_types)
57 57 initialize_acts_as_taggable_on_core
58 58 end
59   -
  59 +
60 60 # all column names are necessary for PostgreSQL group clause
61 61 def grouped_column_names_for(object)
62 62 object.column_names.map { |column| "#{object.table_name}.#{column}" }.join(", ")
3  lib/acts_as_taggable_on/taggable.rb
@@ -64,6 +64,9 @@ def acts_as_ordered_taggable_on(*tag_types)
64 64 # as it's not possible to add another arguement to the method
65 65 # without the tag_types being enclosed in square brackets
66 66 #
  67 + # NB: method overridden in core module in order to create tag type
  68 + # associations and methods after this logic has executed
  69 + #
67 70 def taggable_on(preserve_tag_order, *tag_types)
68 71 tag_types = tag_types.to_a.flatten.compact.map(&:to_sym)
69 72
1  spec/acts_as_taggable_on/acts_as_taggable_on_spec.rb
@@ -13,6 +13,7 @@
13 13 before(:each) do
14 14 clean_database!
15 15 TaggableModel.tag_types = []
  16 + TaggableModel.preserve_tag_order = false
16 17 TaggableModel.acts_as_ordered_taggable_on(:ordered_tags)
17 18 @taggable = TaggableModel.new(:name => "Bob Jones")
18 19 end
37 spec/acts_as_taggable_on/taggable_spec.rb
@@ -3,10 +3,37 @@
3 3 describe "Taggable To Preserve Order" do
4 4 before(:each) do
5 5 clean_database!
6   - TaggableModel.tag_types = []
7   - TaggableModel.acts_as_ordered_taggable_on(:tags)
8   - @taggable = TaggableModel.new(:name => "Bob Jones")
9   - @taggables = [@taggable, TaggableModel.new(:name => "John Doe")]
  6 + @taggable = OrderedTaggableModel.new(:name => "Bob Jones")
  7 + end
  8 +
  9 + it "should have tag types" do
  10 + [:tags, :colours].each do |type|
  11 + OrderedTaggableModel.tag_types.should include type
  12 + end
  13 +
  14 + @taggable.tag_types.should == OrderedTaggableModel.tag_types
  15 + end
  16 +
  17 + it "should have tag associations" do
  18 + [:tags, :colours].each do |type|
  19 + @taggable.respond_to?(type).should be_true
  20 + @taggable.respond_to?("#{type.to_s.singularize}_taggings").should be_true
  21 + end
  22 + end
  23 +
  24 + it "should have tag associations ordered by created_at" do
  25 + [:tags, :colours].each do |type|
  26 + OrderedTaggableModel.reflect_on_association(type).options[:order].should include('created_at')
  27 + OrderedTaggableModel.reflect_on_association("#{type.to_s.singularize}_taggings".to_sym).options[:order].should include('created_at')
  28 + end
  29 + end
  30 +
  31 + it "should have tag methods" do
  32 + [:tags, :colours].each do |type|
  33 + @taggable.respond_to?("#{type.to_s.singularize}_list").should be_true
  34 + @taggable.respond_to?("#{type.to_s.singularize}_list=").should be_true
  35 + @taggable.respond_to?("all_#{type.to_s}_list").should be_true
  36 + end
10 37 end
11 38
12 39 it "should return tag list in the order the tags were created" do
@@ -67,8 +94,6 @@
67 94 describe "Taggable" do
68 95 before(:each) do
69 96 clean_database!
70   - TaggableModel.tag_types = []
71   - TaggableModel.acts_as_taggable_on(:tags, :languages, :skills, :needs, :offerings)
72 97 @taggable = TaggableModel.new(:name => "Bob Jones")
73 98 @taggables = [@taggable, TaggableModel.new(:name => "John Doe")]
74 99 end
5 spec/models.rb
@@ -42,3 +42,8 @@ class NonStandardIdTaggableModel < ActiveRecord::Base
42 42 acts_as_taggable_on :needs, :offerings
43 43 has_many :untaggable_models
44 44 end
  45 +
  46 +class OrderedTaggableModel < ActiveRecord::Base
  47 + acts_as_ordered_taggable
  48 + acts_as_ordered_taggable_on :colours
  49 +end
5 spec/schema.rb
@@ -53,4 +53,9 @@
53 53 t.column :name, :string
54 54 t.column :type, :string
55 55 end
  56 +
  57 + create_table :ordered_taggable_models, :force => true do |t|
  58 + t.column :name, :string
  59 + t.column :type, :string
  60 + end
56 61 end
4 spec/spec_helper.rb
@@ -31,6 +31,8 @@ def freq
31 31 end
32 32 end
33 33
  34 +# set adapter to use, default is sqlite3
  35 +# to use an alternative adapter run => rake spec DB='postgresql'
34 36 db_name = ENV['DB'] || 'sqlite3'
35 37 database_yml = File.expand_path('../database.yml', __FILE__)
36 38
@@ -72,7 +74,7 @@ def freq
72 74
73 75 def clean_database!
74 76 models = [ActsAsTaggableOn::Tag, ActsAsTaggableOn::Tagging, TaggableModel, OtherTaggableModel, InheritingTaggableModel,
75   - AlteredInheritingTaggableModel, TaggableUser, UntaggableModel]
  77 + AlteredInheritingTaggableModel, TaggableUser, UntaggableModel, OrderedTaggableModel]
76 78 models.each do |model|
77 79 ActiveRecord::Base.connection.execute "DELETE FROM #{model.table_name}"
78 80 end

0 comments on commit a12f83a

Please sign in to comment.
Something went wrong with that request. Please try again.