Sha256: 9205bc1fe1c4f8a95167f7cbafcde7a5c67dbf7ddcdfcbd500c5721f2b0865f2

Contents?: true

Size: 1.26 KB

Versions: 3

Compression:

Stored size: 1.26 KB

Contents

module HasMachineTags
  # Methods for console/irb use.
  module Console
    module InstanceMethods
      # Removes first list, adds second list and saves changes.
      def tag_add_and_remove(remove_list, add_list)
        self.class.transaction do
          tag_add_and_save(add_list)
          tag_remove_and_save(remove_list)
        end
      end

      # Adds given list and saves.
      def tag_add_and_save(add_list)
        self.tag_list = self.tag_list + current_tag_list(add_list)
        self.save
        self.tag_list
      end
      
      # Removes given list and saves.
      def tag_remove_and_save(remove_list)
        self.tag_list = self.tag_list - current_tag_list(remove_list)
        self.save
        self.tag_list
      end
      
      # Resets tag_list to given tag_list and saves.
      def tag_and_save(tag_list)
        self.tag_list = tag_list
        self.save
        self.tag_list
      end
    end
    
    module ClassMethods
      # Updates items tagged with an old tag to use the given new tag in place of the old tag.
      def find_and_change_tag(old_tag, new_tag)
        results = tagged_with(old_tag)
        results.each {|e| e.tag_add_and_remove(old_tag, new_tag)}
        puts "Changed tag for #{results.length} records"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
cldwalker-has_machine_tags-0.1.4 lib/has_machine_tags/console.rb
cldwalker-has_machine_tags-0.1.5 lib/has_machine_tags/console.rb
has_machine_tags-0.1.6 lib/has_machine_tags/console.rb