From e110677b3bad10a38ae66196c5e8c7fee8ab5246 Mon Sep 17 00:00:00 2001 From: Rob H Date: Thu, 29 May 2014 12:11:06 +1000 Subject: [PATCH 01/15] WIP: Rearrange new product form --- .../new/replace_form.html.haml.deface | 115 +++++++++++------- 1 file changed, 69 insertions(+), 46 deletions(-) diff --git a/app/overrides/spree/admin/products/new/replace_form.html.haml.deface b/app/overrides/spree/admin/products/new/replace_form.html.haml.deface index c752744ee6..2d2fccc630 100644 --- a/app/overrides/spree/admin/products/new/replace_form.html.haml.deface +++ b/app/overrides/spree/admin/products/new/replace_form.html.haml.deface @@ -1,55 +1,78 @@ / replace "fieldset[data-hook='new_product']" - -%fieldset{ id: "new_product" } - %legend{align: "center"}= t(:new_product) - .row - = f.field_container :name do - = f.label :name, t(:name) - %span.required * - %br/ - = f.text_field :name, :class => 'fullwidth title' - = f.error_message_on :name - .row - .alpha.six.columns - = f.field_container :supplier do - = f.label :supplier - %span.required * - = f.collection_select(:supplier_id, Enterprise.is_primary_producer.managed_by(spree_current_user).by_name, :id, :name, {:include_blank => true}, {:class => "select2 fullwidth"}) - = f.error_message_on :supplier - .five.columns - = f.field_container :price do - = f.label :price, t(:price) - %span.required * - %br/ - = f.text_field :price, :class => 'fullwidth' - = f.error_message_on :price - .five.columns.omega +.twelve.columns.alpha + %fieldset.no-border-bottom{ id: "new_product" } + %legend{align: "center"}= t(:new_product) + .twelve.columns.alpha + .six.columns.alpha + = f.field_container :supplier do + = f.label :supplier_id, t(:supplier) + %span.required * + = f.collection_select(:supplier_id, Enterprise.is_primary_producer.managed_by(spree_current_user).by_name, :id, :name, {:include_blank => true}, {:class => "select2 fullwidth"}) + = f.error_message_on :supplier + .six.columns.omega + = f.field_container :name do + = f.label :name, t(:product_name) + %span.required * + %br/ + = f.text_field :name, :class => 'fullwidth title' + = f.error_message_on :name + .twelve.columns.alpha{ 'ng-controller' => 'unitsCtrl' } + .six.columns.alpha + = f.field_container :units do + = f.label :variant_unit_with_scale, :units + %select.select2.fullwidth{ id: 'product_variant_unit_with_scale', 'ng-model' => 'product.variant_unit_with_scale', 'ng-options' => 'unit[1] as unit[0] for unit in variant_unit_options' } + %option{'value' => '', 'ng-hide' => "hasUnit(product)"} + %input{ type: 'hidden', 'ng-value' => 'product.variant_unit', name: 'product[variant_unit]' } + %input{ type: 'hidden', 'ng-value' => 'product.variant_unit_scale', name: 'product[variant_unit_scale]' } + .three.columns + = f.field_container :unit_value do + = f.label :product_unit_value_with_description, :value, 'ng-disabled' => "!hasUnit(product)" + %input.fullwidth{ id: 'product_unit_value_with_description', 'ng-model' => 'product.master.unit_value_with_description', :type => 'text', placeholder: "eg. 2", 'ng-disabled' => "!hasUnit(product)" } + %input{ type: 'hidden', 'ng-value' => 'product.master.unit_value', name: 'product[unit_value]' } + %input{ type: 'hidden', 'ng-value' => 'product.master.unit_description', name: 'product[unit_description]' } + .three.columns.omega{ 'ng-show' => "product.variant_unit_with_scale == 'items'" } + = f.field_container :unit_name do + = f.label :product_variant_unit_name, :unit_name + %input.fullwidth{ id: 'product_variant_unit_name','ng-model' => 'product.variant_unit_name', :name => 'product[variant_unit_name]', :placeholder => 'eg. bunches', :type => 'text' } + .twelve.columns.alpha + .three.columns.alpha + = f.field_container :price do + = f.label :price, t(:price) + %span.required * + %br/ + = f.text_field :price, class: 'fullwidth' + = f.error_message_on :price + .three.columns + = f.field_container :on_hand do + = f.label :on_hand, t(:on_hand) + %span.required * + %br/ + = f.text_field :on_hand, class: 'fullwidth' + = f.error_message_on :on_hand + .six.columns.omega + = f.field_container :primary_taxon do + = f.label :product_category + %br/ + = text_field_tag :primary_taxon, nil, class: 'fullwidth' + .twelve.columns.alpha = f.field_container :on_hand do - = f.label :on_hand, t(:on_hand) - %span.required * + = f.label :product_description, t(:product_description) %br/ - = f.text_field :on_hand, :class => 'fullwidth' - = f.error_message_on :on_hand - .row{ 'ng-controller' => 'unitsCtrl' } - .six.columns.alpha - = f.label :variant_unit_with_scale, :units - %select.select2.fullwidth{ id: 'product_variant_unit_with_scale', 'ng-model' => 'product.variant_unit_with_scale', 'ng-options' => 'unit[1] as unit[0] for unit in variant_unit_options' } - %option{'value' => '', 'ng-hide' => "hasUnit(product)"} - %input{ type: 'hidden', 'ng-value' => 'product.variant_unit', name: 'product[variant_unit]' } - %input{ type: 'hidden', 'ng-value' => 'product.variant_unit_scale', name: 'product[variant_unit_scale]' } - .five.columns - = f.label :product_unit_value_with_description, :value - %input.fullwidth{ id: 'product_unit_value_with_description', 'ng-model' => 'product.master.unit_value_with_description', :type => 'text', :placeholder => 'eg. 2', 'ng-disabled' => "!hasUnit(product)" } - %input{ type: 'hidden', 'ng-value' => 'product.master.unit_value', name: 'product[unit_value]' } - %input{ type: 'hidden', 'ng-value' => 'product.master.unit_description', name: 'product[unit_description]' } - .five.columns.omega{ 'ng-show' => "product.variant_unit_with_scale == 'items'" } - = f.label :product_variant_unit_name, :unit_name - %input.fullwidth{ id: 'product_variant_unit_name','ng-model' => 'product.variant_unit_name', :name => 'product[variant_unit_name]', :placeholder => 'eg. bunches', :type => 'text' } - #product-from-prototype.clearfix{"data-hook" => "product-from-prototype"} - = render :file => 'spree/admin/prototypes/show' if @prototype + = f.text_area :description, class: 'fullwidth', rows: 3 + = f.error_message_on :description +.four.columns.omega{ style: "text-align: center" } + %fieldset.no-border-bottom{ id: "image" } + %legend{align: "center"}= t(:image) + .row + = image_tag "noimage/product.png", class: "four columns alpha" + .row + = button "Select An image" +.sixteen.columns.alpha = render :partial => 'spree/admin/shared/new_resource_links' + + :javascript angular.element(document.getElementById("new_product")).ready(function() { angular.bootstrap(document.getElementById("new_product"), ['admin.products']); From 9b10a2cb9448adff9703dd751d1d62049fd95b01 Mon Sep 17 00:00:00 2001 From: Rob H Date: Thu, 29 May 2014 12:13:55 +1000 Subject: [PATCH 02/15] Added 'Add Another' link to new product page. Altered redirects --- .../admin/products_controller_decorator.rb | 7 +++ .../new/replace_form.html.haml.deface | 9 +++- .../spree/admin/products_controller_spec.rb | 51 +++++++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 spec/controllers/spree/admin/products_controller_spec.rb diff --git a/app/controllers/spree/admin/products_controller_decorator.rb b/app/controllers/spree/admin/products_controller_decorator.rb index a6c2967b87..d185f4f85d 100644 --- a/app/controllers/spree/admin/products_controller_decorator.rb +++ b/app/controllers/spree/admin/products_controller_decorator.rb @@ -5,6 +5,13 @@ Spree::Admin::ProductsController.class_eval do respond_to :json, :only => :clone + respond_override create: { html: { success: lambda { + if params[:button] == "add_another" + redirect_to new_admin_product_path + else + redirect_to '/admin/products/bulk_edit' + end + } } } #respond_override :clone => { :json => {:success => lambda { redirect_to bulk_index_admin_products_url+"?q[id_eq]=#{@new.id}" } } } def product_distributions diff --git a/app/overrides/spree/admin/products/new/replace_form.html.haml.deface b/app/overrides/spree/admin/products/new/replace_form.html.haml.deface index 2d2fccc630..4421819fb6 100644 --- a/app/overrides/spree/admin/products/new/replace_form.html.haml.deface +++ b/app/overrides/spree/admin/products/new/replace_form.html.haml.deface @@ -68,7 +68,14 @@ .row = button "Select An image" .sixteen.columns.alpha - = render :partial => 'spree/admin/shared/new_resource_links' + .form-buttons.filter-actions.actions{ 'data-hook' => "buttons" } + = button t('actions.create'), 'icon-ok', :submit, value: "create" + %span.or + = t(:or) + = button "Create And Add Another", 'icon-repeat', :submit, value: 'add_another' + %span.or + = t(:or) + = link_to_with_icon 'icon-remove', t('actions.cancel'), collection_url, :class => 'button' diff --git a/spec/controllers/spree/admin/products_controller_spec.rb b/spec/controllers/spree/admin/products_controller_spec.rb new file mode 100644 index 0000000000..e55d5e5e0e --- /dev/null +++ b/spec/controllers/spree/admin/products_controller_spec.rb @@ -0,0 +1,51 @@ +require 'spec_helper' + +describe Spree::Admin::ProductsController do + context "Creating a new product" do + let(:user) do + user = create(:user) + user.spree_roles << Spree::Role.find_or_create_by_name!('admin') + user + end + + before do + controller.stub spree_current_user: user + end + + it "redirects to bulk_edit when the user hits 'create'" do + s = create(:supplier_enterprise) + spree_post :create, { + product: { + name: "Product1", + supplier_id: s.id, + price: 5.0, + on_hand: 5, + variant_unit: 'weight', + variant_unit_scale: 1000, + unit_value: 10, + unit_description: "" + }, + button: 'create' + } + response.should redirect_to "/admin/products/bulk_edit" + end + + it "redirects to new when the user hits 'add_another'" do + s = create(:supplier_enterprise) + spree_post :create, { + product: { + name: "Product1", + supplier_id: s.id, + price: 5.0, + on_hand: 5, + variant_unit: 'weight', + variant_unit_scale: 1000, + unit_value: 10, + unit_description: "" + }, + button: 'add_another' + } + response.should redirect_to "/admin/products/new" + end + end +end \ No newline at end of file From c9acb14809cec966c86db98c1a1f2c3bc2eb6507 Mon Sep 17 00:00:00 2001 From: Rob H Date: Fri, 30 May 2014 11:30:00 +1000 Subject: [PATCH 03/15] Adding primary taxon migration to schema --- db/schema.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/db/schema.rb b/db/schema.rb index 77df8baf75..6005fd7cb5 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 => 20140522015012) do +ActiveRecord::Schema.define(:version => 20140522044009) do create_table "adjustment_metadata", :force => true do |t| t.integer "adjustment_id" @@ -681,6 +681,7 @@ ActiveRecord::Schema.define(:version => 20140522015012) 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" @@ -688,6 +689,7 @@ ActiveRecord::Schema.define(:version => 20140522015012) 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" @@ -1092,6 +1094,7 @@ ActiveRecord::Schema.define(:version => 20140522015012) 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" From d9f786557270728bb47403401211496fbbdb538c Mon Sep 17 00:00:00 2001 From: Rob H Date: Fri, 30 May 2014 11:31:53 +1000 Subject: [PATCH 04/15] Can add an image from redesigned new product page --- app/models/spree/product_decorator.rb | 4 ++-- app/models/spree/variant_decorator.rb | 3 ++- .../spree/admin/products/new/replace_form.html.haml.deface | 5 ++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index 622bef3f43..4b93e0427e 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -4,7 +4,6 @@ Spree::Product.class_eval do # https://github.com/rails/rails/issues/7618 has_many :option_types, :through => :product_option_types, :dependent => :destroy - belongs_to :supplier, :class_name => 'Enterprise' has_many :product_distributions, :dependent => :destroy @@ -12,8 +11,9 @@ Spree::Product.class_eval do accepts_nested_attributes_for :product_distributions, :allow_destroy => true delegate_belongs_to :master, :unit_value, :unit_description + delegate :images_attributes=, to: :master - 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 + 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, :images_attributes validates_presence_of :supplier diff --git a/app/models/spree/variant_decorator.rb b/app/models/spree/variant_decorator.rb index 165c2d34f6..42f6efe0ac 100644 --- a/app/models/spree/variant_decorator.rb +++ b/app/models/spree/variant_decorator.rb @@ -2,7 +2,8 @@ Spree::Variant.class_eval do has_many :exchange_variants, dependent: :destroy has_many :exchanges, through: :exchange_variants - attr_accessible :unit_value, :unit_description + attr_accessible :unit_value, :unit_description, :images_attributes + accepts_nested_attributes_for :images validates_presence_of :unit_value, if: -> v { %w(weight volume).include? v.product.variant_unit }, diff --git a/app/overrides/spree/admin/products/new/replace_form.html.haml.deface b/app/overrides/spree/admin/products/new/replace_form.html.haml.deface index 4421819fb6..efe611e156 100644 --- a/app/overrides/spree/admin/products/new/replace_form.html.haml.deface +++ b/app/overrides/spree/admin/products/new/replace_form.html.haml.deface @@ -66,7 +66,8 @@ .row = image_tag "noimage/product.png", class: "four columns alpha" .row - = button "Select An image" + = f.fields_for 'images_attributes[]', f.object.images.build do |image_fields| + = image_fields.file_field :attachment .sixteen.columns.alpha .form-buttons.filter-actions.actions{ 'data-hook' => "buttons" } = button t('actions.create'), 'icon-ok', :submit, value: "create" @@ -78,8 +79,6 @@ = link_to_with_icon 'icon-remove', t('actions.cancel'), collection_url, :class => 'button' - - :javascript angular.element(document.getElementById("new_product")).ready(function() { angular.bootstrap(document.getElementById("new_product"), ['admin.products']); From 4aae6be9b70de63068580859fbf36ca68a36b120 Mon Sep 17 00:00:00 2001 From: Rob H Date: Fri, 30 May 2014 15:48:36 +1000 Subject: [PATCH 05/15] Remove unused overrides for primary taxons and product/new --- app/overrides/add_primary_taxon_to_admin_product.rb | 4 ---- app/overrides/add_primary_taxon_to_admin_product_new.rb | 4 ---- .../add_supplier_and_group_buy_to_admin_product_new.rb | 5 ----- 3 files changed, 13 deletions(-) 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/overrides/add_supplier_and_group_buy_to_admin_product_new.rb 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/add_supplier_and_group_buy_to_admin_product_new.rb b/app/overrides/add_supplier_and_group_buy_to_admin_product_new.rb deleted file mode 100644 index 4fd7730b0d..0000000000 --- a/app/overrides/add_supplier_and_group_buy_to_admin_product_new.rb +++ /dev/null @@ -1,5 +0,0 @@ -Deface::Override.new(:virtual_path => "spree/admin/products/new", - :insert_before => "[data-hook='new_product_attrs']", - :partial => "spree/admin/products/supplier_and_group_buy_for_new", - :name => "add_supplier_and_group_buy_to_admin_product_new", - :original => '59b7ed369769267bdedb596768fcfcc2cb94f122') \ No newline at end of file From 66a47f053cc43c0e05dc1d2bee7013ef0efe93c5 Mon Sep 17 00:00:00 2001 From: Rob H Date: Fri, 30 May 2014 16:21:04 +1000 Subject: [PATCH 06/15] Amend products controller spec to include requirement for primary_taxon_id --- spec/controllers/spree/admin/products_controller_spec.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/spec/controllers/spree/admin/products_controller_spec.rb b/spec/controllers/spree/admin/products_controller_spec.rb index e55d5e5e0e..aee2b6df56 100644 --- a/spec/controllers/spree/admin/products_controller_spec.rb +++ b/spec/controllers/spree/admin/products_controller_spec.rb @@ -14,6 +14,7 @@ describe Spree::Admin::ProductsController do it "redirects to bulk_edit when the user hits 'create'" do s = create(:supplier_enterprise) + t = create(:taxon) spree_post :create, { product: { name: "Product1", @@ -23,7 +24,8 @@ describe Spree::Admin::ProductsController do variant_unit: 'weight', variant_unit_scale: 1000, unit_value: 10, - unit_description: "" + unit_description: "", + primary_taxon_id: t.id }, button: 'create' } @@ -32,6 +34,7 @@ describe Spree::Admin::ProductsController do it "redirects to new when the user hits 'add_another'" do s = create(:supplier_enterprise) + t = create(:taxon) spree_post :create, { product: { name: "Product1", @@ -41,7 +44,8 @@ describe Spree::Admin::ProductsController do variant_unit: 'weight', variant_unit_scale: 1000, unit_value: 10, - unit_description: "" + unit_description: "", + primary_taxon_id: t.id }, button: 'add_another' } From 1d5cc5dcedc39efe0fe13fea67ca90b30f61e5aa Mon Sep 17 00:00:00 2001 From: Rob H Date: Fri, 30 May 2014 16:38:29 +1000 Subject: [PATCH 07/15] Adding specs for new product creation --- spec/features/admin/products_spec.rb | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/spec/features/admin/products_spec.rb b/spec/features/admin/products_spec.rb index fd7235200b..3224254f62 100644 --- a/spec/features/admin/products_spec.rb +++ b/spec/features/admin/products_spec.rb @@ -15,22 +15,35 @@ feature %q{ end describe "creating a product" do - scenario "assigning a supplier, distributors and units to the product" do + scenario "assigning a important attributes" do login_to_admin_section click_link 'Products' click_link 'New Product' - 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' + fill_in 'product_name', with: 'A new product !!!' + # select "Weight (kg)", from: 'product_variant_unit_with_scale' + # fill_in 'product_unit_value_with_description', with: 5 + select taxon.name, from: "product_primary_taxon_id" + fill_in 'product_price', with: '19.99' + fill_in 'product_on_hand', with: 5 + fill_in 'product_description', with: "A description..." click_button 'Create' flash_message.should == 'Product "A new product !!!" has been successfully created!' product = Spree::Product.find_by_name('A new product !!!') product.supplier.should == @supplier + #product.variant_unit.should == 'weight' + #product.variant_unit_scale.should == 1000 + #product.unit_value.should == 5 + #product.unit_value_description.should == "" + #product.unit_name.should == "" + product.primary_taxon_id.should == taxon.id + product.price.to_s.should == '19.99' + product.on_hand.should == 5 + product.description.should == "A description..." product.group_buy.should be_false # Distributors From bf4d83aecc3ceb50682e3a60c62d8924b7991c4e Mon Sep 17 00:00:00 2001 From: Rob H Date: Fri, 30 May 2014 18:39:41 +1000 Subject: [PATCH 08/15] Fix master options_text display on front end --- app/models/spree/product_decorator.rb | 2 +- app/views/shop/products/_master.html.haml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index 36473d4e24..ea986cd64e 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -155,7 +155,7 @@ Spree::Product.class_eval do end def update_units - if variant_unit_changed? + if variant_unit_changed? && !variant_unit_change.first.nil? option_types.delete self.class.all_variant_unit_option_types option_types << variant_unit_option_type if variant_unit.present? variants_including_master.each { |v| v.delete_unit_option_values } diff --git a/app/views/shop/products/_master.html.haml b/app/views/shop/products/_master.html.haml index bd8a8cd3de..e8972082dd 100644 --- a/app/views/shop/products/_master.html.haml +++ b/app/views/shop/products/_master.html.haml @@ -3,7 +3,7 @@   .small-4.columns - ({{ product.master.options_text }}) + {{ product.master.options_text }} -# WITHOUT GROUP BUY .small-5.columns{"bo-if" => "!product.group_buy"} From f97ef0838cf7677f77dd0d1ed9525a8949390017 Mon Sep 17 00:00:00 2001 From: Rob H Date: Fri, 30 May 2014 19:20:15 +1000 Subject: [PATCH 09/15] Rollback variant_unit_change check, auto-set primary taxons where they exist --- app/models/spree/product_decorator.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index ea986cd64e..72da0e52b1 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -28,6 +28,7 @@ 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 + before_validation :set_primary_taxon_to_first_taxon # -- Joins @@ -155,13 +156,17 @@ Spree::Product.class_eval do end def update_units - if variant_unit_changed? && !variant_unit_change.first.nil? + if variant_unit_changed? option_types.delete self.class.all_variant_unit_option_types option_types << variant_unit_option_type if variant_unit.present? variants_including_master.each { |v| v.delete_unit_option_values } end end + def set_primary_taxon_to_first_taxon + self.primary_taxon = taxons.first unless primary_taxon + end + def add_primary_taxon_to_taxons taxons << primary_taxon unless taxons.find_by_id(primary_taxon) end From bcfa1a82662b072252dc600770374c710cee1974 Mon Sep 17 00:00:00 2001 From: Rob H Date: Fri, 30 May 2014 20:06:40 +1000 Subject: [PATCH 10/15] Fix spec to check validation of missing primary_taxon correctly --- spec/models/spree/product_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/models/spree/product_spec.rb b/spec/models/spree/product_spec.rb index 8d04d690b0..08939532f6 100644 --- a/spec/models/spree/product_spec.rb +++ b/spec/models/spree/product_spec.rb @@ -16,6 +16,7 @@ module Spree it "requires a primary taxon" do product = create(:simple_product) + product.taxons = [] product.primary_taxon = nil product.should_not be_valid end From 8fa053a0a8bbdd25cf0c8834d2f359d514d69ee2 Mon Sep 17 00:00:00 2001 From: summerscope Date: Tue, 3 Jun 2014 10:28:18 +1000 Subject: [PATCH 11/15] Commenting out promo image from About tab in shopfront page. This needs an additional image style to be applied before it can be implemented correctly. --- app/views/shopping_shared/_about.html.haml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/views/shopping_shared/_about.html.haml b/app/views/shopping_shared/_about.html.haml index 0fbfc1cc11..e03eb05255 100644 --- a/app/views/shopping_shared/_about.html.haml +++ b/app/views/shopping_shared/_about.html.haml @@ -3,5 +3,6 @@ .small-12.large-9.columns %p= current_distributor.long_description.andand.html_safe .small-12.large-3.columns - %img.about.right{src: current_distributor.promo_image.url(:large)} + / Hide image until image styles are working correctly: + / %img.about.right{src: current_distributor.promo_image.url(:large)} From 60711301c15075c76b3f522280dc61a571c891aa Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Tue, 3 Jun 2014 11:16:42 +1000 Subject: [PATCH 12/15] Initialise primary taxon in migration instead of in model callback --- app/models/spree/product_decorator.rb | 5 ----- ...522044009_add_primary_taxon_to_products.rb | 21 +++++++++++-------- db/schema.rb | 2 +- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index 72da0e52b1..36473d4e24 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -28,7 +28,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 - before_validation :set_primary_taxon_to_first_taxon # -- Joins @@ -163,10 +162,6 @@ Spree::Product.class_eval do end end - def set_primary_taxon_to_first_taxon - self.primary_taxon = taxons.first unless primary_taxon - end - def add_primary_taxon_to_taxons taxons << primary_taxon unless taxons.find_by_id(primary_taxon) end diff --git a/db/migrate/20140522044009_add_primary_taxon_to_products.rb b/db/migrate/20140522044009_add_primary_taxon_to_products.rb index fb7b9b3160..28c27f820f 100644 --- a/db/migrate/20140522044009_add_primary_taxon_to_products.rb +++ b/db/migrate/20140522044009_add_primary_taxon_to_products.rb @@ -1,15 +1,18 @@ class AddPrimaryTaxonToProducts < ActiveRecord::Migration - def change + def up add_column :spree_products, :primary_taxon_id, :integer -<<<<<<< HEAD -<<<<<<< HEAD + add_index :spree_products, :primary_taxon_id add_foreign_key :spree_products, :spree_taxons, column: :primary_taxon_id -======= ->>>>>>> fd1e7eb... Adding primary taxon field to product -======= - add_index :spree_products, :primary_taxon_id - add_foreign_key :spree_products, :spree_taxons, column: :primary_taxon_id ->>>>>>> 110a6f2... Adding primary taxon to admin forms + + Spree::Product.all.each do |p| + p.update_column :primary_taxon_id, (p.taxons.first || Spree::Taxon.first) + end + + change_column :spree_products, :primary_taxon_id, :integer, null: false + end + + def down + remove_column :spree_products, :primary_taxon_id end end diff --git a/db/schema.rb b/db/schema.rb index 6005fd7cb5..999ff55705 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -681,7 +681,7 @@ ActiveRecord::Schema.define(:version => 20140522044009) do t.float "variant_unit_scale" t.string "variant_unit_name" t.text "notes" - t.integer "primary_taxon_id" + t.integer "primary_taxon_id", :null => false end add_index "spree_products", ["available_on"], :name => "index_products_on_available_on" From 4e45a682fd6e93a54b273fda611091c6a286854d Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Tue, 3 Jun 2014 11:29:07 +1000 Subject: [PATCH 13/15] Do not show deleted products in order cycle admin --- .../admin/enterprises/_supplied_product.rabl | 10 ++++++++++ app/views/admin/enterprises/index.rabl | 11 +++-------- spec/views/admin/enterprises/index.rabl_spec.rb | 15 +++++++++++++++ 3 files changed, 28 insertions(+), 8 deletions(-) create mode 100644 app/views/admin/enterprises/_supplied_product.rabl create mode 100644 spec/views/admin/enterprises/index.rabl_spec.rb diff --git a/app/views/admin/enterprises/_supplied_product.rabl b/app/views/admin/enterprises/_supplied_product.rabl new file mode 100644 index 0000000000..268ff302d5 --- /dev/null +++ b/app/views/admin/enterprises/_supplied_product.rabl @@ -0,0 +1,10 @@ +object @product + +attributes :name +node(:supplier_name) { |p| p.supplier.andand.name } +node(:image_url) { |p| p.images.present? ? p.images.first.attachment.url(:mini) : nil } +node(:master_id) { |p| p.master.id } +child variants: :variants do |variant| + attributes :id + node(:label) { |v| v.options_text } +end diff --git a/app/views/admin/enterprises/index.rabl b/app/views/admin/enterprises/index.rabl index 9e1ee893d1..1342d6eb1a 100644 --- a/app/views/admin/enterprises/index.rabl +++ b/app/views/admin/enterprises/index.rabl @@ -2,13 +2,8 @@ collection @collection attributes :id, :name -child supplied_products: :supplied_products do |product| - attributes :name - node(:supplier_name) { |p| p.supplier.andand.name } - node(:image_url) { |p| p.images.present? ? p.images.first.attachment.url(:mini) : nil } - node(:master_id) { |p| p.master.id } - child variants: :variants do |variant| - attributes :id - node(:label) { |v| v.options_text } +node(:supplied_products) do |enterprise| + enterprise.supplied_products.not_deleted.map do |product| + partial 'admin/enterprises/supplied_product', object: product end end diff --git a/spec/views/admin/enterprises/index.rabl_spec.rb b/spec/views/admin/enterprises/index.rabl_spec.rb new file mode 100644 index 0000000000..e92388da61 --- /dev/null +++ b/spec/views/admin/enterprises/index.rabl_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +describe "admin/enterprises/index.rabl" do + let(:enterprise) { create(:distributor_enterprise) } + let!(:product) { create(:simple_product, supplier: enterprise) } + let!(:deleted_product) { create(:simple_product, supplier: enterprise, deleted_at: Time.now) } + let(:render) { Rabl.render([enterprise], 'admin/enterprises/index', view_path: 'app/views', scope: RablHelper::FakeContext.instance) } + + describe "supplied products" do + it "does not render deleted products" do + render.should have_json_size(1).at_path '0/supplied_products' + render.should be_json_eql(product.master.id).at_path '0/supplied_products/0/master_id' + end + end +end From d4f65a63bb96dac3b6cfcf2bf57b6c81af13d504 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Tue, 3 Jun 2014 11:31:30 +1000 Subject: [PATCH 14/15] Remove spec for removed image --- spec/features/consumer/shopping/shopping_spec.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/features/consumer/shopping/shopping_spec.rb b/spec/features/consumer/shopping/shopping_spec.rb index b1b905fe82..ecc988f7ea 100644 --- a/spec/features/consumer/shopping/shopping_spec.rb +++ b/spec/features/consumer/shopping/shopping_spec.rb @@ -24,7 +24,6 @@ feature "As a consumer I want to shop with a distributor", js: true do page.should have_text distributor.name find("#tab_about a").click first("distributor img")['src'].should == distributor.logo.url(:thumb) - first("#about img")['src'].should == distributor.promo_image.url(:large) end it "shows the producers for a distributor" do From d52c1fa5daeb6cbc8310e94b4e4c078ed07f8a7b Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Tue, 3 Jun 2014 12:18:27 +1000 Subject: [PATCH 15/15] Revert "Do not show deleted products in order cycle admin" This reverts commit 4e45a682fd6e93a54b273fda611091c6a286854d. --- .../admin/enterprises/_supplied_product.rabl | 10 ---------- app/views/admin/enterprises/index.rabl | 11 ++++++++--- spec/views/admin/enterprises/index.rabl_spec.rb | 15 --------------- 3 files changed, 8 insertions(+), 28 deletions(-) delete mode 100644 app/views/admin/enterprises/_supplied_product.rabl delete mode 100644 spec/views/admin/enterprises/index.rabl_spec.rb diff --git a/app/views/admin/enterprises/_supplied_product.rabl b/app/views/admin/enterprises/_supplied_product.rabl deleted file mode 100644 index 268ff302d5..0000000000 --- a/app/views/admin/enterprises/_supplied_product.rabl +++ /dev/null @@ -1,10 +0,0 @@ -object @product - -attributes :name -node(:supplier_name) { |p| p.supplier.andand.name } -node(:image_url) { |p| p.images.present? ? p.images.first.attachment.url(:mini) : nil } -node(:master_id) { |p| p.master.id } -child variants: :variants do |variant| - attributes :id - node(:label) { |v| v.options_text } -end diff --git a/app/views/admin/enterprises/index.rabl b/app/views/admin/enterprises/index.rabl index 1342d6eb1a..9e1ee893d1 100644 --- a/app/views/admin/enterprises/index.rabl +++ b/app/views/admin/enterprises/index.rabl @@ -2,8 +2,13 @@ collection @collection attributes :id, :name -node(:supplied_products) do |enterprise| - enterprise.supplied_products.not_deleted.map do |product| - partial 'admin/enterprises/supplied_product', object: product +child supplied_products: :supplied_products do |product| + attributes :name + node(:supplier_name) { |p| p.supplier.andand.name } + node(:image_url) { |p| p.images.present? ? p.images.first.attachment.url(:mini) : nil } + node(:master_id) { |p| p.master.id } + child variants: :variants do |variant| + attributes :id + node(:label) { |v| v.options_text } end end diff --git a/spec/views/admin/enterprises/index.rabl_spec.rb b/spec/views/admin/enterprises/index.rabl_spec.rb deleted file mode 100644 index e92388da61..0000000000 --- a/spec/views/admin/enterprises/index.rabl_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'spec_helper' - -describe "admin/enterprises/index.rabl" do - let(:enterprise) { create(:distributor_enterprise) } - let!(:product) { create(:simple_product, supplier: enterprise) } - let!(:deleted_product) { create(:simple_product, supplier: enterprise, deleted_at: Time.now) } - let(:render) { Rabl.render([enterprise], 'admin/enterprises/index', view_path: 'app/views', scope: RablHelper::FakeContext.instance) } - - describe "supplied products" do - it "does not render deleted products" do - render.should have_json_size(1).at_path '0/supplied_products' - render.should be_json_eql(product.master.id).at_path '0/supplied_products/0/master_id' - end - end -end