Skip to content

Commit

Permalink
Merge 1d7f4fe into 8718828
Browse files Browse the repository at this point in the history
  • Loading branch information
YOSHIDA Cake committed Sep 11, 2015
2 parents 8718828 + 1d7f4fe commit eebcd26
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
2 changes: 2 additions & 0 deletions core/app/models/comable/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Product < ActiveRecord::Base

accepts_nested_attributes_for :images, allow_destroy: true

scope :published, -> (published_at = nil) { where('published_at <= ?', published_at || Time.now) }

validates :name, presence: true, length: { maximum: 255 }
validates :code, presence: true, length: { maximum: 255 }
validates :price, presence: true, numericality: { greater_than_or_equal_to: 0, allow_blank: true }
Expand Down
20 changes: 18 additions & 2 deletions core/spec/models/comable/product_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,38 @@
expect { described_class.where(id: [1, 2]) }.not_to raise_error
end

describe '.published' do
it 'includes published products' do
product = create(:product, published_at: Time.now)
expect(described_class.published).to include(product)
end

it 'excludes unpublished products' do
product = create(:product, published_at: nil)
expect(described_class.published).not_to include(product)
end
end

describe 'published?' do
subject { build(:product) }

it 'should be false when published_at is nil' do
subject.published_at = nil
expect(subject.published?).to be false
end

it 'should be false when published_at greater than now' do
subject.published_at = Time.now + 1
subject.published_at = 1.minute.since
expect(subject.published?).to be false
end

it 'should be true when published_at equal now' do
subject.published_at = Time.now
expect(subject.published?).to be true
end

it 'should be true when published_at less than now' do
subject.published_at = Time.now - 1
subject.published_at = 1.minute.ago
expect(subject.published?).to be true
end
end
Expand Down
7 changes: 3 additions & 4 deletions frontend/app/controllers/comable/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ def index
end

def show
@product = Comable::Product.find(params[:id])
fail ActiveRecord::RecordNotFound unless @product.published?
@product = Comable::Product.published.find(params[:id])
end

private
Expand All @@ -17,9 +16,9 @@ def load_category_and_products
@category = Comable::Category.where(id: params[:category_id]).first
if @category
subtree_of_category = Comable::Category.subtree_of(@category)
@products = Comable::Product.eager_load(:categories).merge(subtree_of_category)
@products = Comable::Product.published.eager_load(:categories).merge(subtree_of_category)
else
@products = Comable::Product.search(params[:q])
@products = Comable::Product.published.search(params[:q])
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion sample/db/samples/products.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@

products_attributes.each.with_index(next_id) do |attributes, index|
code = format('%05d', index)
Comable::Product.create!(attributes.merge(code: code))
Comable::Product.create!(attributes.merge(code: code, published_at: Date.today))
end

0 comments on commit eebcd26

Please sign in to comment.