Sha256: 68d42bf588670cb4e19b70cf80c8b1a92313456a7795ec738693e7896c7b2771

Contents?: true

Size: 875 Bytes

Versions: 3

Compression:

Stored size: 875 Bytes

Contents

module Beef
  module Has
    module Assets
      def self.included(base)
        base.extend ClassMethods
      end
      
      module ClassMethods
        def has_assets
          has_many :assetings, :as => :assetable, :order => 'assetable.id', :dependent => :delete_all
          has_many :assets, :through => :assetings
          
          # Override the default asset_ids method
          define_method("asset_ids=") do |new_value|
            ids = (new_value || []).reject { |nid| nid.blank? }
            ids.collect!{ |id| id.to_i}
            logger.debug "ASSETS the same #{ids == self.asset_ids} | IDS #{ids} | #{self.asset_ids}"
            return if ids == self.asset_ids
            self.assets.clear
            ids.each_index do |idx|
              self.assets << Asset.find(ids[idx])
            end
          end
        end
      end
    end
  end  
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
beef-has_assets-0.1.1 lib/has_assets.rb
beef-has_assets-0.2.0 lib/has_assets.rb
beef-has_assets-0.2.1 lib/has_assets.rb