Skip to content

Commit

Permalink
Rails 3 compatibility for price of lots of deprecation warnings in lo…
Browse files Browse the repository at this point in the history
…gs =\
  • Loading branch information
Fyodor committed Apr 23, 2013
1 parent 64bad4c commit 44d79b2
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 34 deletions.
2 changes: 1 addition & 1 deletion gemfiles/rails_3.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PATH
remote: /Users/joost/Gems/acts-as-taggable-on
remote: /Users/iamakingbee/apps/acts-as-taggable-on
specs:
acts-as-taggable-on (2.4.1.pre)
rails (>= 3, < 5)
Expand Down
5 changes: 2 additions & 3 deletions gemfiles/rails_4.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
GIT
remote: git://github.com/arabonradar/rails.git
revision: 746a9992f0a842ded7a15cc5ce0afd0fa00f2ada
ref: ar_having_values_bug_fix
remote: git://github.com/rails/rails.git
revision: e58f11683003ec1515113103895e18a8c9003aa3
specs:
actionmailer (4.0.0.beta1)
actionpack (= 4.0.0.beta1)
Expand Down
24 changes: 12 additions & 12 deletions lib/acts_as_taggable_on/acts_as_taggable_on/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ def initialize_acts_as_taggable_on_core
class_eval do
# when preserving tag order, include order option so that for a 'tags' context
# the associations tag_taggings & tags are always returned in created order
has_many context_taggings, -> { joins(:tag).order(taggings_order).where(["#{ActsAsTaggableOn::Tagging.table_name}.context = ?", tags_type]) }, :as => :taggable,
has_many context_taggings, :as => :taggable,
:dependent => :destroy,
:class_name => "ActsAsTaggableOn::Tagging"
:class_name => "ActsAsTaggableOn::Tagging",
:order => taggings_order,
:conditions => ["#{ActsAsTaggableOn::Tagging.table_name}.context = (?)", tags_type]

has_many context_tags, -> { order(taggings_order) },
:through => context_taggings,
has_many context_tags, :through => context_taggings,
:source => :tag,
:class_name => "ActsAsTaggableOn::Tag"
:class_name => "ActsAsTaggableOn::Tag",
:order => taggings_order
end

taggable_mixin.class_eval <<-RUBY, __FILE__, __LINE__ + 1
Expand Down Expand Up @@ -259,12 +261,10 @@ def all_tags_on(context)

if ActsAsTaggableOn::Tag.using_postgresql?
group_columns = grouped_column_names_for(ActsAsTaggableOn::Tag)
scope = scope.order("max(#{tagging_table_name}.created_at)").group(group_columns)
scope.order("max(#{tagging_table_name}.created_at)").group(group_columns)
else
scope = scope.group("#{ActsAsTaggableOn::Tag.table_name}.#{ActsAsTaggableOn::Tag.primary_key}")
end

scope.load
scope.group("#{ActsAsTaggableOn::Tag.table_name}.#{ActsAsTaggableOn::Tag.primary_key}")
end.all
end

##
Expand All @@ -274,7 +274,7 @@ def tags_on(context)
# when preserving tag order, return tags in created order
# if we added the order to the association this would always apply
scope = scope.order("#{ActsAsTaggableOn::Tagging.table_name}.id") if self.class.preserve_tag_order?
scope.load
scope
end

def set_tag_list_on(context, new_list)
Expand Down Expand Up @@ -344,7 +344,7 @@ def save_tags
# Find taggings to remove:
if old_tags.present?
old_taggings = taggings.where(:tagger_type => nil, :tagger_id => nil,
:context => context.to_s, :tag_id => old_tags).load
:context => context.to_s, :tag_id => old_tags)
end

# Destroy old taggings:
Expand Down
10 changes: 7 additions & 3 deletions lib/acts_as_taggable_on/acts_as_taggable_on/ownership.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,14 @@ def owner_tags_on(owner, context)
#{ActsAsTaggableOn::Tagging.table_name}.tagger_id = ? AND
#{ActsAsTaggableOn::Tagging.table_name}.tagger_type = ?), context.to_s, owner.id, owner.class.base_class.to_s])
end

# when preserving tag order, return tags in created order
# if we added the order to the association this would always apply
scope = scope.order("#{ActsAsTaggableOn::Tagging.table_name}.id") if self.class.preserve_tag_order?
scope.load
if self.class.preserve_tag_order?
scope.order("#{ActsAsTaggableOn::Tagging.table_name}.id")
else
scope
end
end

def cached_owned_tag_list_on(context)
Expand Down Expand Up @@ -104,7 +108,7 @@ def save_owned_tags
if old_tags.present?
old_taggings = ActsAsTaggableOn::Tagging.where(:taggable_id => id, :taggable_type => self.class.base_class.to_s,
:tagger_type => owner.class.base_class.to_s, :tagger_id => owner.id,
:tag_id => old_tags, :context => context).load
:tag_id => old_tags, :context => context)
end

# Destroy old taggings:
Expand Down
2 changes: 1 addition & 1 deletion lib/acts_as_taggable_on/taggable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def taggable_on(preserve_tag_order, *tag_types)
self.preserve_tag_order = preserve_tag_order

class_eval do
has_many :taggings, -> { joins(:tag) }, :as => :taggable, :dependent => :destroy, :class_name => "ActsAsTaggableOn::Tagging"
has_many :taggings, :as => :taggable, :dependent => :destroy, :class_name => "ActsAsTaggableOn::Tagging"
has_many :base_tags, :through => :taggings, :source => :tag, :class_name => "ActsAsTaggableOn::Tag"

def self.taggable?
Expand Down
10 changes: 7 additions & 3 deletions lib/acts_as_taggable_on/tagger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ module ClassMethods
# end
def acts_as_tagger(opts={})
class_eval do
has_many :owned_taggings, opts.merge(:as => :tagger, :dependent => :destroy,
:class_name => "ActsAsTaggableOn::Tagging")
has_many :owned_taggings,
opts.merge(
:as => :tagger,
:dependent => :destroy,
:class_name => "ActsAsTaggableOn::Tagging"
)

has_many :owned_tags, -> { uniq true }, :through => :owned_taggings, :source => :tag, :class_name => "ActsAsTaggableOn::Tag"
has_many :owned_tags, :through => :owned_taggings, :source => :tag, :class_name => "ActsAsTaggableOn::Tag", :uniq => true
end

include ActsAsTaggableOn::Tagger::InstanceMethods
Expand Down
2 changes: 0 additions & 2 deletions lib/acts_as_taggable_on/tags_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ module ActsAsTaggableOn
module TagsHelper
# See the README for an example using tag_cloud.
def tag_cloud(tags, classes)
tags = tags.load if tags.respond_to?(:all)

return [] if tags.empty?

max_count = tags.sort_by(&:count).last.count.to_f
Expand Down
18 changes: 9 additions & 9 deletions spec/acts_as_taggable_on/taggable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
end

it "should have tag_counts_on" do
TaggableModel.tag_counts_on(:tags).load.should be_empty
TaggableModel.tag_counts_on(:tags).should be_empty

@taggable.tag_list = ["awesome", "epic"]
@taggable.save
Expand All @@ -135,7 +135,7 @@
end

it "should have tags_on" do
TaggableModel.tags_on(:tags).load.should be_empty
TaggableModel.tags_on(:tags).should be_empty

@taggable.tag_list = ["awesome", "epic"]
@taggable.save
Expand Down Expand Up @@ -231,24 +231,24 @@
bob = TaggableModel.create(:name => "Bob", :tag_list => "ruby")
frank = TaggableModel.create(:name => "Frank", :tag_list => "Ruby")

ActsAsTaggableOn::Tag.all.load.size.should == 1
ActsAsTaggableOn::Tag.all.size.should == 1
TaggableModel.tagged_with("ruby").to_a.should == TaggableModel.tagged_with("Ruby").to_a
end

it "should be able to get tag counts on model as a whole" do
bob = TaggableModel.create(:name => "Bob", :tag_list => "ruby, rails, css")
frank = TaggableModel.create(:name => "Frank", :tag_list => "ruby, rails")
charlie = TaggableModel.create(:name => "Charlie", :skill_list => "ruby")
TaggableModel.tag_counts.load.should_not be_empty
TaggableModel.skill_counts.load.should_not be_empty
TaggableModel.tag_counts.should_not be_empty
TaggableModel.skill_counts.should_not be_empty
end

it "should be able to get all tag counts on model as whole" do
bob = TaggableModel.create(:name => "Bob", :tag_list => "ruby, rails, css")
frank = TaggableModel.create(:name => "Frank", :tag_list => "ruby, rails")
charlie = TaggableModel.create(:name => "Charlie", :skill_list => "ruby")

TaggableModel.all_tag_counts.load.should_not be_empty
TaggableModel.all_tag_counts.should_not be_empty
TaggableModel.all_tag_counts(:order => 'tags.id').first.count.should == 3 # ruby
end

Expand All @@ -257,7 +257,7 @@
frank = TaggableModel.create(:name => "Frank", :tag_list => "ruby, rails")
charlie = TaggableModel.create(:name => "Charlie", :skill_list => "ruby")

TaggableModel.all_tags.load.should_not be_empty
TaggableModel.all_tags.should_not be_empty
TaggableModel.all_tags(:order => 'tags.id').first.name.should == "ruby"
end

Expand Down Expand Up @@ -536,7 +536,7 @@
end

it "should have tag_counts_on" do
NonStandardIdTaggableModel.tag_counts_on(:tags).load.should be_empty
NonStandardIdTaggableModel.tag_counts_on(:tags).should be_empty

@taggable.tag_list = ["awesome", "epic"]
@taggable.save
Expand All @@ -546,7 +546,7 @@
end

it "should have tags_on" do
NonStandardIdTaggableModel.tags_on(:tags).load.should be_empty
NonStandardIdTaggableModel.tags_on(:tags).should be_empty

@taggable.tag_list = ["awesome", "epic"]
@taggable.save
Expand Down

0 comments on commit 44d79b2

Please sign in to comment.