Permalink
Browse files

Merge pull request #49 from Jellyfishboy/feature/sort-sku-stock-selec…

…tion

Modified Sku dropdown on storefront product pages to push out of stoc…
  • Loading branch information...
2 parents 4fe1fea + b0abe36 commit 07ebc7bd4f2f73ff9a8c442e79f1bbb784560c53 @Jellyfishboy committed on GitHub Jan 9, 2017
Showing with 30 additions and 9 deletions.
  1. +1 −1 app/controllers/products_controller.rb
  2. +7 −7 app/models/product.rb
  3. +1 −0 app/models/sku.rb
  4. +1 −1 config/database.yml
  5. +20 −0 spec/models/sku_spec.rb
@@ -17,7 +17,7 @@ def set_product
end
def set_skus
- @skus = @product.active_skus
+ @skus = @product.active_skus.sort_by{|s| s.in_stock? ? 0 : 1}
end
def set_variant_types
View
@@ -58,14 +58,14 @@ class Product < ActiveRecord::Base
validates :name, :sku, :part_number, presence: true
validates :meta_description, :description,
- :weighting, :category_id, :page_title, presence: true, :if => :published?
+ :weighting, :category_id, :page_title, presence: true, if: :published?
validates :part_number, :sku, :name, uniqueness: { scope: :active }
validates :page_title, length: { maximum: 70, message: :too_long }
- validates :meta_description, length: { maximum: 150, message: :too_long }, :if => :published?
- validates :name, length: { minimum: 10, message: :too_short }, :if => :published?
- validates :description, length: { minimum: 20, message: :too_short }, :if => :published?
- validates :short_description, length: { maximum: 300, message: :too_long }, :if => :published?
- validates :part_number, numericality: { only_integer: true }, :if => :published?
+ validates :meta_description, length: { maximum: 150, message: :too_long }, if: :published?
+ validates :name, length: { minimum: 10, message: :too_short }, if: :published?
+ validates :description, length: { minimum: 20, message: :too_short }, if: :published?
+ validates :short_description, length: { maximum: 300, message: :too_long }, if: :published?
+ validates :part_number, numericality: { only_integer: true }, if: :published?
accepts_nested_attributes_for :skus
accepts_nested_attributes_for :tags
@@ -111,6 +111,6 @@ def in_stock?
#
# @return [Object] sku record
def first_available_sku
- skus.active.order(price: :asc).first
+ skus.active.in_stock.order(price: :asc).first
end
end
View
@@ -55,6 +55,7 @@ class Sku < ActiveRecord::Base
accepts_nested_attributes_for :variants, :stock_adjustments
scope :complete, -> { where('stock IS NOT NULL') }
+ scope :in_stock, -> { where('stock > ?', 0) }
include ActiveScope
View
@@ -1,7 +1,7 @@
development:
adapter: postgresql
encoding: unicode
- database: trado_live
+ database: trado_dev
pool: 5
username: tomdallimore
password:
@@ -223,4 +223,24 @@
end
end
end
+
+ describe "Scoping only in stock Skus" do
+ let!(:sku_1) { create(:sku, stock: 0, duplicate: true) }
+ let!(:sku_2) { create(:sku) }
+ let!(:sku_3) { create(:sku) }
+
+ it "should only list Skus which are in stock" do
+ expect(Sku.in_stock).to match_array([sku_2, sku_3])
+ end
+ end
+
+ describe "Scoping only complete Skus" do
+ let!(:sku_1) { create(:sku) }
+ let!(:sku_2) { create(:sku) }
+ let!(:sku_3) { create(:sku, stock: nil, duplicate: true) }
+
+ it "should only list Skus which are in stock" do
+ expect(Sku.complete).to match_array([sku_1, sku_2])
+ end
+ end
end

0 comments on commit 07ebc7b

Please sign in to comment.