Permalink
Browse files

Monkeypatch for dirty objects in acts_as_taggable_on

  • Loading branch information...
1 parent d6d7498 commit 16a34f599550e808179cb22bd849578bcd5473ba @VictorSB VictorSB committed Jun 17, 2011
@@ -0,0 +1,37 @@
+module ActsAsTaggableOn::Taggable
+ module Dirty
+ def self.included(base)
+ base.extend ActsAsTaggableOn::Taggable::Dirty::ClassMethods
+
+ base.initialize_acts_as_taggable_on_dirty
+ end
+
+ module ClassMethods
+ def initialize_acts_as_taggable_on_dirty
+ tag_types.map(&:to_s).each do |tags_type|
+ tag_type = tags_type.to_s.singularize
+ context_tags = tags_type.to_sym
+
+ class_eval %(
+ def #{tag_type}_list_changed?
+ changed_attributes.include?("#{tag_type}_list")
+ end
+
+ def #{tag_type}_list_was
+ changed_attributes.include?("#{tag_type}_list") ? changed_attributes["#{tag_type}_list"] : __send__("#{tag_type}_list")
+ end
+
+ def #{tag_type}_list_change
+ [changed_attributes['#{tag_type}_list'], __send__('#{tag_type}_list')] if changed_attributes.include?("#{tag_type}_list")
+ end
+
+ def #{tag_type}_list_changes
+ [changed_attributes['#{tag_type}_list'], __send__('#{tag_type}_list')] if changed_attributes.include?("#{tag_type}_list")
+ end
+ )
+
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,58 @@
+require "acts_as_taggable_on/acts_as_taggable_on/dirty"
+
+module ActsAsTaggableOn::Taggable
+ def acts_as_taggable_on(*tag_types)
+ tag_types = tag_types.to_a.flatten.compact.map(&:to_sym)
+
+ if taggable?
+ write_inheritable_attribute(:tag_types, (self.tag_types + tag_types).uniq)
+ else
+ write_inheritable_attribute(:tag_types, tag_types)
+ class_inheritable_reader(:tag_types)
+
+ class_eval do
+ has_many :taggings, :as => :taggable, :dependent => :destroy, :include => :tag, :class_name => "ActsAsTaggableOn::Tagging"
+ has_many :base_tags, :through => :taggings, :source => :tag, :class_name => "ActsAsTaggableOn::Tag"
+
+ def self.taggable?
+ true
+ end
+
+ include ActsAsTaggableOn::Taggable::Core
+ include ActsAsTaggableOn::Taggable::Collection
+ include ActsAsTaggableOn::Taggable::Cache
+ include ActsAsTaggableOn::Taggable::Ownership
+ include ActsAsTaggableOn::Taggable::Related
+ include ActsAsTaggableOn::Taggable::Dirty
+ end
+ end
+ end
+end
+
+
+module ActsAsTaggableOn::Taggable::Core::InstanceMethods
+ def set_tag_list_on(context, new_list)
+ add_custom_context(context)
+
+ variable_name = "@#{context.to_s.singularize}_list"
+ process_dirty_object(context, new_list)
+
+ instance_variable_set(variable_name, ActsAsTaggableOn::TagList.from(new_list))
+ end
+
+ def process_dirty_object(context,new_list)
+ value = new_list.is_a?(Array) ? new_list.join(', ') : new_list
+ attr = "#{context.to_s.singularize}_list"
+
+ if changed_attributes.include?(attr)
+ # The attribute already has an unsaved change.
+ old = changed_attributes[attr]
+ changed_attributes.delete(attr) if (old.to_s == value.to_s)
+ else
+ old = tag_list_on(context).to_s
+ changed_attributes[attr] = old if (old.to_s != value.to_s)
+ end
+ end
+end
+
+
@@ -26,6 +26,7 @@
require 'omniauth/oauth'
#Tags
require 'acts-as-taggable-on'
+require 'acts_as_taggable_on/social_stream'
# HTML forms
require 'formtastic'
@@ -10,7 +10,7 @@ module Object
attr_accessor :_activity_tie_id
attr_accessor :_activity_parent_id
- belongs_to :activity_object, :dependent => :destroy
+ belongs_to :activity_object, :dependent => :destroy, :autosave => true
has_many :activity_object_activities, :through => :activity_object
delegate :post_activity,

0 comments on commit 16a34f5

Please sign in to comment.