Fixes for acts_as_ordered_taggable #243

Merged
merged 1 commit into from Mar 27, 2012
@@ -52,11 +52,11 @@ def all_#{tags_type}_list
end
end
- def acts_as_taggable_on(*args)
- super(*args)
+ def taggable_on(preserve_tag_order, *tag_types)
+ super(preserve_tag_order, *tag_types)
initialize_acts_as_taggable_on_core
end
-
+
# all column names are necessary for PostgreSQL group clause
def grouped_column_names_for(object)
object.column_names.map { |column| "#{object.table_name}.#{column}" }.join(", ")
@@ -64,6 +64,9 @@ def acts_as_ordered_taggable_on(*tag_types)
# as it's not possible to add another arguement to the method
# without the tag_types being enclosed in square brackets
#
+ # NB: method overridden in core module in order to create tag type
+ # associations and methods after this logic has executed
+ #
def taggable_on(preserve_tag_order, *tag_types)
tag_types = tag_types.to_a.flatten.compact.map(&:to_sym)
@@ -13,6 +13,7 @@
before(:each) do
clean_database!
TaggableModel.tag_types = []
+ TaggableModel.preserve_tag_order = false
TaggableModel.acts_as_ordered_taggable_on(:ordered_tags)
@taggable = TaggableModel.new(:name => "Bob Jones")
end
@@ -3,10 +3,37 @@
describe "Taggable To Preserve Order" do
before(:each) do
clean_database!
- TaggableModel.tag_types = []
- TaggableModel.acts_as_ordered_taggable_on(:tags)
- @taggable = TaggableModel.new(:name => "Bob Jones")
- @taggables = [@taggable, TaggableModel.new(:name => "John Doe")]
+ @taggable = OrderedTaggableModel.new(:name => "Bob Jones")
+ end
+
+ it "should have tag types" do
+ [:tags, :colours].each do |type|
+ OrderedTaggableModel.tag_types.should include type
+ end
+
+ @taggable.tag_types.should == OrderedTaggableModel.tag_types
+ end
+
+ it "should have tag associations" do
+ [:tags, :colours].each do |type|
+ @taggable.respond_to?(type).should be_true
+ @taggable.respond_to?("#{type.to_s.singularize}_taggings").should be_true
+ end
+ end
+
+ it "should have tag associations ordered by created_at" do
+ [:tags, :colours].each do |type|
+ OrderedTaggableModel.reflect_on_association(type).options[:order].should include('created_at')
+ OrderedTaggableModel.reflect_on_association("#{type.to_s.singularize}_taggings".to_sym).options[:order].should include('created_at')
+ end
+ end
+
+ it "should have tag methods" do
+ [:tags, :colours].each do |type|
+ @taggable.respond_to?("#{type.to_s.singularize}_list").should be_true
+ @taggable.respond_to?("#{type.to_s.singularize}_list=").should be_true
+ @taggable.respond_to?("all_#{type.to_s}_list").should be_true
+ end
end
it "should return tag list in the order the tags were created" do
@@ -67,8 +94,6 @@
describe "Taggable" do
before(:each) do
clean_database!
- TaggableModel.tag_types = []
- TaggableModel.acts_as_taggable_on(:tags, :languages, :skills, :needs, :offerings)
@taggable = TaggableModel.new(:name => "Bob Jones")
@taggables = [@taggable, TaggableModel.new(:name => "John Doe")]
end
View
@@ -42,3 +42,8 @@ class NonStandardIdTaggableModel < ActiveRecord::Base
acts_as_taggable_on :needs, :offerings
has_many :untaggable_models
end
+
+class OrderedTaggableModel < ActiveRecord::Base
+ acts_as_ordered_taggable
+ acts_as_ordered_taggable_on :colours
+end
View
@@ -53,4 +53,9 @@
t.column :name, :string
t.column :type, :string
end
+
+ create_table :ordered_taggable_models, :force => true do |t|
+ t.column :name, :string
+ t.column :type, :string
+ end
end
View
@@ -31,6 +31,8 @@ def freq
end
end
+# set adapter to use, default is sqlite3
+# to use an alternative adapter run => rake spec DB='postgresql'
db_name = ENV['DB'] || 'sqlite3'
database_yml = File.expand_path('../database.yml', __FILE__)
@@ -72,7 +74,7 @@ def freq
def clean_database!
models = [ActsAsTaggableOn::Tag, ActsAsTaggableOn::Tagging, TaggableModel, OtherTaggableModel, InheritingTaggableModel,
- AlteredInheritingTaggableModel, TaggableUser, UntaggableModel]
+ AlteredInheritingTaggableModel, TaggableUser, UntaggableModel, OrderedTaggableModel]
models.each do |model|
ActiveRecord::Base.connection.execute "DELETE FROM #{model.table_name}"
end