From acbe49b8dea8e7bd1c8c3a91e98fd40930ed946b Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Sat, 24 May 2014 17:11:36 +1000 Subject: [PATCH] Revert primary taxons --- app/models/enterprise.rb | 1 - app/models/spree/classification_decorator.rb | 10 ---------- app/models/spree/product_decorator.rb | 13 +++++-------- .../add_primary_taxon_to_admin_product.rb | 4 ---- .../add_primary_taxon_to_admin_product_new.rb | 4 ---- .../_form/add_notes_field.html.haml.deface | 1 + .../admin/products/_primary_taxon_form.html.haml | 5 ----- ...0140522044009_add_primary_taxon_to_products.rb | 7 ------- db/schema.rb | 5 +---- spec/factories.rb | 7 ------- spec/features/admin/products_spec.rb | 6 ------ spec/models/enterprise_spec.rb | 4 ++-- spec/models/spree/classification_spec.rb | 15 --------------- spec/models/spree/product_spec.rb | 11 ++--------- 14 files changed, 11 insertions(+), 82 deletions(-) delete mode 100644 app/models/spree/classification_decorator.rb delete mode 100644 app/overrides/add_primary_taxon_to_admin_product.rb delete mode 100644 app/overrides/add_primary_taxon_to_admin_product_new.rb delete mode 100644 app/views/spree/admin/products/_primary_taxon_form.html.haml delete mode 100644 db/migrate/20140522044009_add_primary_taxon_to_products.rb delete mode 100644 spec/models/spree/classification_spec.rb diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 5e53316607..a72c6fdbd6 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -15,7 +15,6 @@ class Enterprise < ActiveRecord::Base has_and_belongs_to_many :payment_methods, join_table: 'distributors_payment_methods', class_name: 'Spree::PaymentMethod', foreign_key: 'distributor_id' has_and_belongs_to_many :shipping_methods, join_table: 'distributors_shipping_methods', class_name: 'Spree::ShippingMethod', foreign_key: 'distributor_id' - delegate :latitude, :longitude, :city, :state_name, :to => :address accepts_nested_attributes_for :address diff --git a/app/models/spree/classification_decorator.rb b/app/models/spree/classification_decorator.rb deleted file mode 100644 index 7e34e6890e..0000000000 --- a/app/models/spree/classification_decorator.rb +++ /dev/null @@ -1,10 +0,0 @@ -Spree::Classification.class_eval do - before_destroy :dont_destroy_if_primary_taxon - - def dont_destroy_if_primary_taxon - if product.primary_taxon == taxon - errors.add :base, "Taxon #{taxon.name} is the primary taxon of #{product.name} and cannot be deleted" - return false - end - end -end diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index 13491f94df..ba7cbc253d 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -6,7 +6,6 @@ Spree::Product.class_eval do belongs_to :supplier, :class_name => 'Enterprise' - belongs_to :primary_taxon, class_name: 'Spree::Taxon' has_many :product_distributions, :dependent => :destroy has_many :distributors, :through => :product_distributions @@ -14,10 +13,9 @@ Spree::Product.class_eval do accepts_nested_attributes_for :product_distributions, :allow_destroy => true delegate_belongs_to :master, :unit_value, :unit_description - attr_accessible :supplier_id, :primary_taxon_id, :distributor_ids, :product_distributions_attributes, :group_buy, :group_buy_unit_size, :variant_unit, :variant_unit_scale, :variant_unit_name, :unit_value, :unit_description, :notes + attr_accessible :supplier_id, :distributor_ids, :product_distributions_attributes, :group_buy, :group_buy_unit_size, :variant_unit, :variant_unit_scale, :variant_unit_name, :unit_value, :unit_description, :notes validates_presence_of :supplier - validates_presence_of :primary_taxon validates_presence_of :variant_unit, if: :has_variants? validates_presence_of :variant_unit_scale, @@ -27,7 +25,6 @@ Spree::Product.class_eval do after_initialize :set_available_on_to_now, :if => :new_record? after_save :update_units - before_save :add_primary_taxon_to_taxons # -- Joins @@ -121,6 +118,10 @@ Spree::Product.class_eval do order_cycle.variants_distributed_by(distributor).where(product_id: self) end + def primary_taxon + self.taxons.order.first + end + # Build a product distribution for each distributor def build_product_distributions_for_user user Enterprise.is_distributor.managed_by(user).each do |distributor| @@ -156,10 +157,6 @@ Spree::Product.class_eval do end end - def add_primary_taxon_to_taxons - taxons << primary_taxon unless taxons.find_by_id(primary_taxon) - end - def self.all_variant_unit_option_types Spree::OptionType.where('name LIKE ?', 'unit_%%') end diff --git a/app/overrides/add_primary_taxon_to_admin_product.rb b/app/overrides/add_primary_taxon_to_admin_product.rb deleted file mode 100644 index 9f7c5edde5..0000000000 --- a/app/overrides/add_primary_taxon_to_admin_product.rb +++ /dev/null @@ -1,4 +0,0 @@ -Deface::Override.new(:virtual_path => "spree/admin/products/_form", - :insert_top => "[data-hook='admin_product_form_right']", - :partial => "spree/admin/products/primary_taxon_form", - :name => "add_primary_taxon_to_admin_product") diff --git a/app/overrides/add_primary_taxon_to_admin_product_new.rb b/app/overrides/add_primary_taxon_to_admin_product_new.rb deleted file mode 100644 index e5c9d24e7f..0000000000 --- a/app/overrides/add_primary_taxon_to_admin_product_new.rb +++ /dev/null @@ -1,4 +0,0 @@ -Deface::Override.new(:virtual_path => "spree/admin/products/new", - :insert_before => "[data-hook='new_product_attrs']", - :partial => "spree/admin/products/primary_taxon_form", - :name => "add_primary_taxon_to_admin_product_new") diff --git a/app/overrides/spree/admin/products/_form/add_notes_field.html.haml.deface b/app/overrides/spree/admin/products/_form/add_notes_field.html.haml.deface index dcc7937d6b..dadebd01d4 100644 --- a/app/overrides/spree/admin/products/_form/add_notes_field.html.haml.deface +++ b/app/overrides/spree/admin/products/_form/add_notes_field.html.haml.deface @@ -4,3 +4,4 @@ = f.label :notes, t(:notes) = f.text_area :notes, { :class => 'fullwidth', rows: 5 } = f.error_message_on :notes + diff --git a/app/views/spree/admin/products/_primary_taxon_form.html.haml b/app/views/spree/admin/products/_primary_taxon_form.html.haml deleted file mode 100644 index a844f4f447..0000000000 --- a/app/views/spree/admin/products/_primary_taxon_form.html.haml +++ /dev/null @@ -1,5 +0,0 @@ -= f.field_container :primary_taxon_id do - = f.label :primary_taxon_id - %br - = f.collection_select(:primary_taxon_id, Spree::Taxon.all, :id, :name, {:include_blank => true}, {:class => "select2 fullwidth"}) - = f.error_message_on :primary_taxon_id diff --git a/db/migrate/20140522044009_add_primary_taxon_to_products.rb b/db/migrate/20140522044009_add_primary_taxon_to_products.rb deleted file mode 100644 index f6ff3cfe96..0000000000 --- a/db/migrate/20140522044009_add_primary_taxon_to_products.rb +++ /dev/null @@ -1,7 +0,0 @@ -class AddPrimaryTaxonToProducts < ActiveRecord::Migration - def change - add_column :spree_products, :primary_taxon_id, :integer - add_index :spree_products, :primary_taxon_id - add_foreign_key :spree_products, :spree_taxons, column: :primary_taxon_id - end -end diff --git a/db/schema.rb b/db/schema.rb index 6005fd7cb5..77df8baf75 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20140522044009) do +ActiveRecord::Schema.define(:version => 20140522015012) do create_table "adjustment_metadata", :force => true do |t| t.integer "adjustment_id" @@ -681,7 +681,6 @@ ActiveRecord::Schema.define(:version => 20140522044009) do t.float "variant_unit_scale" t.string "variant_unit_name" t.text "notes" - t.integer "primary_taxon_id" end add_index "spree_products", ["available_on"], :name => "index_products_on_available_on" @@ -689,7 +688,6 @@ ActiveRecord::Schema.define(:version => 20140522044009) do add_index "spree_products", ["name"], :name => "index_products_on_name" add_index "spree_products", ["permalink"], :name => "index_products_on_permalink" add_index "spree_products", ["permalink"], :name => "permalink_idx_unique", :unique => true - add_index "spree_products", ["primary_taxon_id"], :name => "index_spree_products_on_primary_taxon_id" create_table "spree_products_promotion_rules", :id => false, :force => true do |t| t.integer "product_id" @@ -1094,7 +1092,6 @@ ActiveRecord::Schema.define(:version => 20140522044009) do add_foreign_key "spree_products", "enterprises", name: "spree_products_supplier_id_fk", column: "supplier_id" add_foreign_key "spree_products", "spree_shipping_categories", name: "spree_products_shipping_category_id_fk", column: "shipping_category_id" add_foreign_key "spree_products", "spree_tax_categories", name: "spree_products_tax_category_id_fk", column: "tax_category_id" - add_foreign_key "spree_products", "spree_taxons", name: "spree_products_primary_taxon_id_fk", column: "primary_taxon_id" add_foreign_key "spree_products_promotion_rules", "spree_products", name: "spree_products_promotion_rules_product_id_fk", column: "product_id" add_foreign_key "spree_products_promotion_rules", "spree_promotion_rules", name: "spree_products_promotion_rules_promotion_rule_id_fk", column: "promotion_rule_id" diff --git a/spec/factories.rb b/spec/factories.rb index 394112125a..6da7824d47 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -2,9 +2,6 @@ require 'ffaker' require 'spree/core/testing_support/factories' FactoryGirl.define do - factory :classification, class: Spree::Classification do - end - factory :order_cycle, :parent => :simple_order_cycle do coordinator_fees { [create(:enterprise_fee, enterprise: coordinator)] } @@ -153,9 +150,6 @@ end FactoryGirl.modify do - factory :product do - primary_taxon { Spree::Taxon.first || FactoryGirl.create(:taxon) } - end factory :simple_product do # Fix product factory name sequence with Kernel.rand so it is not interpreted as a Spree::Product method # Pull request: https://github.com/spree/spree/pull/1964 @@ -163,7 +157,6 @@ FactoryGirl.modify do sequence(:name) { |n| "Product ##{n} - #{Kernel.rand(9999)}" } supplier { Enterprise.is_primary_producer.first || FactoryGirl.create(:supplier_enterprise) } - primary_taxon { Spree::Taxon.first || FactoryGirl.create(:taxon) } on_hand 3 variant_unit 'weight' diff --git a/spec/features/admin/products_spec.rb b/spec/features/admin/products_spec.rb index b9fec1ef9f..b9f9d7a206 100644 --- a/spec/features/admin/products_spec.rb +++ b/spec/features/admin/products_spec.rb @@ -6,7 +6,6 @@ feature %q{ } do include AuthenticationWorkflow include WebHelper - let!(:taxon) { create(:taxon) } background do @supplier = create(:supplier_enterprise, :name => 'New supplier') @@ -23,7 +22,6 @@ feature %q{ fill_in 'product_name', with: 'A new product !!!' fill_in 'product_price', with: '19.99' - select taxon.name, from: "product_primary_taxon_id" select 'New supplier', from: 'product_supplier_id' click_button 'Create' @@ -45,8 +43,6 @@ feature %q{ product.reload product.distributors.sort.should == [@distributors[0], @distributors[2]].sort - - product.product_distributions.map { |pd| pd.enterprise_fee }.sort.should == [@enterprise_fees[0], @enterprise_fees[2]].sort end @@ -59,7 +55,6 @@ feature %q{ fill_in 'product_name', :with => 'A new product !!!' fill_in 'product_price', :with => '19.99' - select taxon.name, from: "product_primary_taxon_id" select 'New supplier', :from => 'product_supplier_id' choose 'product_group_buy_1' fill_in 'Group buy unit size', :with => '10' @@ -104,7 +99,6 @@ feature %q{ page.should have_selector('#product_supplier_id') select 'Another Supplier', :from => 'product_supplier_id' - select taxon.name, from: "product_primary_taxon_id" # Should only have suppliers listed which the user can manage within "#product_supplier_id" do diff --git a/spec/models/enterprise_spec.rb b/spec/models/enterprise_spec.rb index fac9286d9b..f375b8890a 100644 --- a/spec/models/enterprise_spec.rb +++ b/spec/models/enterprise_spec.rb @@ -422,8 +422,8 @@ describe Enterprise do let(:supplier) { create(:supplier_enterprise) } let(:taxon1) { create(:taxon) } let(:taxon2) { create(:taxon) } - let(:product1) { create(:simple_product, primary_taxon: taxon1, taxons: [taxon1]) } - let(:product2) { create(:simple_product, primary_taxon: taxon1, taxons: [taxon1, taxon2]) } + let(:product1) { create(:simple_product, taxons: [taxon1]) } + let(:product2) { create(:simple_product, taxons: [taxon1, taxon2]) } it "gets all taxons of all distributed products" do Spree::Product.stub(:in_distributor).and_return [product1, product2] diff --git a/spec/models/spree/classification_spec.rb b/spec/models/spree/classification_spec.rb deleted file mode 100644 index f26f6da0c0..0000000000 --- a/spec/models/spree/classification_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'spec_helper' - -module Spree - describe Classification do - let(:product) { create(:simple_product) } - let(:taxon) { create(:taxon) } - let(:classification) { create(:classification, taxon: taxon, product: product) } - - it "won't destroy if classification is the primary taxon" do - product.primary_taxon = taxon - classification.destroy.should be_false - classification.errors.messages[:base].should == ["Taxon #{taxon.name} is the primary taxon of #{product.name} and cannot be deleted"] - end - end -end diff --git a/spec/models/spree/product_spec.rb b/spec/models/spree/product_spec.rb index 6a50190448..6d90b70761 100644 --- a/spec/models/spree/product_spec.rb +++ b/spec/models/spree/product_spec.rb @@ -5,7 +5,6 @@ module Spree describe "associations" do it { should belong_to(:supplier) } - it { should belong_to(:primary_taxon) } it { should have_many(:product_distributions) } end @@ -14,12 +13,6 @@ module Spree create(:product).should be_valid end - it "requires a primary taxon" do - product = create(:simple_product) - product.primary_taxon = nil - product.should_not be_valid - end - it "requires a supplier" do product = create(:simple_product) product.supplier = nil @@ -558,10 +551,10 @@ module Spree describe "Taxons" do let(:taxon1) { create(:taxon) } let(:taxon2) { create(:taxon) } - let(:product) { create(:simple_product) } + let(:product) { create(:simple_product, taxons: [taxon1, taxon2]) } it "returns the first taxon as the primary taxon" do - product.taxons.should == [product.primary_taxon] + product.primary_taxon.should == taxon1 end end end