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/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index 1fff7ea1a8..36473d4e24 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' belongs_to :primary_taxon, class_name: 'Spree::Taxon' @@ -13,8 +12,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, :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, :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, :images_attributes validates_presence_of :supplier validates_presence_of :primary_taxon 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/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 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 e964e9a704..7e4c0f0b3e 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,60 +1,79 @@ / replace "fieldset[data-hook='new_product']" - -%fieldset{ id: "new_product" } - %legend{align: "center"}= t(:new_product) - - .row - .alpha.eleven.columns - = f.field_container :name do - = f.label :name, t(:name) - %span.required * +.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 + .six.columns.alpha + = render 'spree/admin/products/primary_taxon_form', f: f + .three.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 + .three.columns.omega + = 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 + .twelve.columns.alpha + = f.field_container :description do + = f.label :product_description, t(:product_description) %br/ - = f.text_field :name, :class => 'fullwidth title' - = f.error_message_on :name - .five.columns.omega - = 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 - - .row - .alpha.six.columns - = render 'spree/admin/products/primary_taxon_form', f: f - .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 - = 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 - - .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 - = render :partial => 'spree/admin/shared/new_resource_links' + = 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 + = 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" + %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' :javascript 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"} 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)} diff --git a/app/views/spree/admin/products/_primary_taxon_form.html.haml b/app/views/spree/admin/products/_primary_taxon_form.html.haml index a844f4f447..3cd7bdc931 100644 --- a/app/views/spree/admin/products/_primary_taxon_form.html.haml +++ b/app/views/spree/admin/products/_primary_taxon_form.html.haml @@ -1,5 +1,5 @@ = f.field_container :primary_taxon_id do - = f.label :primary_taxon_id + = f.label :primary_taxon_id, t(:product_category) %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 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" 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..aee2b6df56 --- /dev/null +++ b/spec/controllers/spree/admin/products_controller_spec.rb @@ -0,0 +1,55 @@ +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) + t = create(:taxon) + 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: "", + primary_taxon_id: t.id + }, + 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) + t = create(:taxon) + 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: "", + primary_taxon_id: t.id + }, + button: 'add_another' + } + response.should redirect_to "/admin/products/new" + end + end +end \ No newline at end of file diff --git a/spec/features/admin/products_spec.rb b/spec/features/admin/products_spec.rb index 3714a0e91f..3224254f62 100644 --- a/spec/features/admin/products_spec.rb +++ b/spec/features/admin/products_spec.rb @@ -15,26 +15,39 @@ 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 - within('#sidebar') { click_link 'Product Distributions' } + visit spree.product_distributions_admin_product_path(product) check @distributors[0].name select @enterprise_fees[0].name, :from => 'product_product_distributions_attributes_0_enterprise_fee_id' 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 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