Product distributions now editable

This commit is contained in:
Rohan Mitchell
2012-06-28 15:38:26 +10:00
parent 474c049f0d
commit c09d4c98c8
2 changed files with 15 additions and 6 deletions

View File

@@ -6,10 +6,20 @@ Spree::Product.class_eval do
accepts_nested_attributes_for :product_distributions, :allow_destroy => true
attr_accessible :supplier_id, :distributor_ids
attr_accessible :supplier_id, :distributor_ids, :product_distributions_attributes
validates_presence_of :supplier
scope :in_supplier, lambda { |supplier| where(:supplier_id => supplier) }
scope :in_distributor, lambda { |distributor| joins(:product_distributions).where('product_distributions.distributor_id = ?', (distributor.respond_to?(:id) ? distributor.id : distributor.to_i)) }
# Build a product distribution for each distributor
def build_product_distributions
Spree::Distributor.all.each do |distributor|
unless self.product_distributions.find_by_distributor_id distributor.id
self.product_distributions.build(:distributor => distributor)
end
end
end
end

View File

@@ -1,15 +1,14 @@
%h2 Distributors
= f.field_container :product_distributions do
- Spree::Distributor.all.each do |d|
- f.object.product_distributions.build(:distributor => d) unless f.object.product_distributions.find_by_distributor_id d.id
- f.object.build_product_distributions
%table
= f.fields_for :product_distributions do |pd_form|
%tr
%td
= hidden_field_tag "#{pd_form.object_name}[_destroy]", 1
= check_box_tag "#{pd_form.object_name}[_destroy]", 0
= hidden_field_tag "#{pd_form.object_name}[_destroy]", 1, :id => nil
= check_box_tag "#{pd_form.object_name}[_destroy]", 0, !pd_form.object.new_record?
%td
= pd_form.object.distributor.name
= label_tag "#{pd_form.object_name}[_destroy]", pd_form.object.distributor.name
= pd_form.hidden_field :distributor_id
%td
= pd_form.collection_select :shipping_method_id, Spree::ShippingMethod.all, :id, :name