Skip to content

Commit

Permalink
Merge pull request #49 from Jellyfishboy/feature/sort-sku-stock-selec…
Browse files Browse the repository at this point in the history
…tion

Modified Sku dropdown on storefront product pages to push out of stoc…
  • Loading branch information
Jellyfishboy committed Jan 9, 2017
2 parents 4fe1fea + b0abe36 commit 07ebc7b
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/controllers/products_controller.rb
Expand Up @@ -17,7 +17,7 @@ def set_product
end end


def set_skus def set_skus
@skus = @product.active_skus @skus = @product.active_skus.sort_by{|s| s.in_stock? ? 0 : 1}
end end


def set_variant_types def set_variant_types
Expand Down
14 changes: 7 additions & 7 deletions app/models/product.rb
Expand Up @@ -58,14 +58,14 @@ class Product < ActiveRecord::Base


validates :name, :sku, :part_number, presence: true validates :name, :sku, :part_number, presence: true
validates :meta_description, :description, 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 :part_number, :sku, :name, uniqueness: { scope: :active }
validates :page_title, length: { maximum: 70, message: :too_long } validates :page_title, length: { maximum: 70, message: :too_long }
validates :meta_description, length: { maximum: 150, message: :too_long }, :if => :published? validates :meta_description, length: { maximum: 150, message: :too_long }, if: :published?
validates :name, length: { minimum: 10, message: :too_short }, :if => :published? validates :name, length: { minimum: 10, message: :too_short }, if: :published?
validates :description, length: { minimum: 20, 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 :short_description, length: { maximum: 300, message: :too_long }, if: :published?
validates :part_number, numericality: { only_integer: true }, :if => :published? validates :part_number, numericality: { only_integer: true }, if: :published?


accepts_nested_attributes_for :skus accepts_nested_attributes_for :skus
accepts_nested_attributes_for :tags accepts_nested_attributes_for :tags
Expand Down Expand Up @@ -111,6 +111,6 @@ def in_stock?
# #
# @return [Object] sku record # @return [Object] sku record
def first_available_sku def first_available_sku
skus.active.order(price: :asc).first skus.active.in_stock.order(price: :asc).first
end end
end end
1 change: 1 addition & 0 deletions app/models/sku.rb
Expand Up @@ -55,6 +55,7 @@ class Sku < ActiveRecord::Base
accepts_nested_attributes_for :variants, :stock_adjustments accepts_nested_attributes_for :variants, :stock_adjustments


scope :complete, -> { where('stock IS NOT NULL') } scope :complete, -> { where('stock IS NOT NULL') }
scope :in_stock, -> { where('stock > ?', 0) }


include ActiveScope include ActiveScope


Expand Down
2 changes: 1 addition & 1 deletion config/database.yml
@@ -1,7 +1,7 @@
development: development:
adapter: postgresql adapter: postgresql
encoding: unicode encoding: unicode
database: trado_live database: trado_dev
pool: 5 pool: 5
username: tomdallimore username: tomdallimore
password: password:
Expand Down
20 changes: 20 additions & 0 deletions spec/models/sku_spec.rb
Expand Up @@ -223,4 +223,24 @@
end end
end 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 end

0 comments on commit 07ebc7b

Please sign in to comment.