Skip to content

@bigfive /gist:1399762
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Ruby on Rails. Module to handle 'quantity' attributes by overriding the << and delete methods of the has_many association
class Package < ActiveRecord::Base
has_many :products, :through => :packagings, extend => QuantityAssociation
# ... do stuff
end
module QuantityAssociation
def include_duplicates
return_array = []
through_collection.each do |through|
associate = through.send(reflection_name)
return_array.concat Array.new(through.quantity || 1).fill( associate )
end
return_array
end
def <<(*records)
result = true
load_target if proxy_association.owner.new_record?
transaction do
flatten_deeper(records).each do |record|
existing = get_existing_through record
if existing.present?
existing.increment! :quantity
else
result &&= super(record)
end
end
end
result && self
end
def delete(*records)
transaction do
flatten_deeper(records).each do |record|
existing = get_existing_through record
if existing.present? and existing.quantity.present? and existing.quantity > 1
existing.decrement! :quantity
else
super(record)
end
end
end
end
private
def reflection_name
proxy_association.source_reflection.name
end
def through_source_key
proxy_association.reflection.source_reflection.foreign_key
end
def through_name
proxy_association.reflection.through_reflection.name
end
def through_collection
proxy_association.owner.send through_name
end
def get_existing_through record
through_collection.where("#{through_source_key}" => record.id).try(:first)
end
def flatten_deeper(array)
array.collect { |element| (element.respond_to?(:flatten) && !element.is_a?(Hash)) ? element.flatten : element }.flatten
end
end
#--------
#--------
# Means you can just do things like
package = Package.first
package.products << Product.first
# and if the product is already associated, the 'quantity' column will be
# incremented on the 'packagings' table
@bigfive
Owner

The 'get_existing_through' method could well be done simpler. Anyone know a better way?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.