Sha256: e4a1f06bc933fc5028858ccc89e06b3e7edd91fd7e9500422435be0f951d38d7

Contents?: true

Size: 744 Bytes

Versions: 4

Compression:

Stored size: 744 Bytes

Contents

module Spraypaint::Behaviour::Persistence  
  def self.included(base)
    unless base == parent
      base.has_many 'spraypaint_taggings', :as => 'target', :dependent => :destroy, :class_name => 'Spraypaint::Model::Tagging'
      base.has_many 'spraypaint_tags', :through => 'spraypaint_taggings', :source => 'tag', :class_name => '::Spraypaint::Model::Tag', :order => 'spraypaint_taggings.id'
    end
  end
  
  private
  
  def load_tag_names
    self.spraypaint_tags.collect(&:name)
  end
  
  def save_tag_names(names)
    transaction do
      spraypaint_taggings.destroy_all
      names.each do |tag_name|
        tag = Spraypaint::Model::Tag.find_or_create_by_name(tag_name)
        self.spraypaint_tags << tag
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
spraypaint-1.0.3 lib/spraypaint/behaviour/persistence.rb
spraypaint-1.0.2 lib/spraypaint/behaviour/persistence.rb
spraypaint-1.0.1 lib/spraypaint/behaviour/persistence.rb
spraypaint-1.0.0 lib/spraypaint/behaviour/persistence.rb