Sha256: 0559fa98e7d36103e2020fb7b4e790840ae3348a7da807c4e16170f0537fbfbc

Contents?: true

Size: 1.36 KB

Versions: 46

Compression:

Stored size: 1.36 KB

Contents

module Wheels
  module ActiveRecordExtensions
    def commentable_tags
      class_eval do
        after_save.reject! {|callback| callback.method.to_s == 'save_tags' }

        def save_commentable_tags
          tagging_contexts.each do |context|
            next unless tag_list_cache_set_on(context)

            tag_list = tag_list_cache_on(context).uniq

            #  Do it normal like, but get the taggings, too.


            # Find existing tags or create non-existing tags:
            tag_list = ActsAsTaggableOn::Tag.find_or_create_all_with_like_by_name(tag_list)

            current_tags = tags_on(context)
            old_tags     = current_tags - tag_list
            new_tags     = tag_list     - current_tags

            # Find taggings to remove:
            old_taggings = taggings.where(:tagger_type => nil, :tagger_id => nil,
                                          :context => context.to_s, :tag_id => old_tags).all

            if old_taggings.present?
              # Destroy old taggings:
              ActsAsTaggableOn::Tagging.destroy_all :id => old_taggings.map(&:id)
            end

            # Create new taggings:
            new_tags.each do |tag|
              taggings.create!(:tag_id => tag.id, :context => context.to_s, :taggable => self)
            end
          end
        end
        after_save :save_commentable_tags

      end
    end
  end
end

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
wheels-0.1.13 lib/wheels/active_record_extensions.rb
wheels-0.1.12 lib/wheels/active_record_extensions.rb
wheels-0.1.11 lib/wheels/active_record_extensions.rb
wheels-0.1.10 lib/wheels/active_record_extensions.rb
wheels-0.1.9 lib/wheels/active_record_extensions.rb
wheels-0.1.8 lib/wheels/active_record_extensions.rb
wheels-0.1.7 lib/wheels/active_record_extensions.rb
wheels-0.1.6 lib/wheels/active_record_extensions.rb
wheels-0.1.5 lib/wheels/active_record_extensions.rb
wheels-0.1.4 lib/wheels/active_record_extensions.rb
wheels-0.1.3 lib/wheels/active_record_extensions.rb
wheels-0.1.2 lib/wheels/active_record_extensions.rb
wheels-0.1.1 lib/wheels/active_record_extensions.rb
wheels-0.1.0 lib/wheels/active_record_extensions.rb
wheels-0.0.49 lib/wheels/active_record_extensions.rb
wheels-0.0.48 lib/wheels/active_record_extensions.rb
wheels-0.0.47 lib/wheels/active_record_extensions.rb
wheels-0.0.46 lib/wheels/active_record_extensions.rb
wheels-0.0.45 lib/wheels/active_record_extensions.rb
wheels-0.0.44 lib/wheels/active_record_extensions.rb