Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify store items #306

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions app/models/store_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
# **`active`** | `boolean` | `default(TRUE)`
# **`created_at`** | `datetime` | `not null`
# **`id`** | `integer` | `not null, primary key`
# **`in_stock`** | `boolean` |
# **`name`** | `string(255)` |
# **`price`** | `decimal(8, 2)` |
# **`quantity`** | `integer` |
# **`updated_at`** | `datetime` | `not null`
#

Expand All @@ -21,9 +21,4 @@ class StoreItem < ActiveRecord::Base
scope :active, -> { where(active: true) }
scope :inactive, -> { where(active: false) }

def quantity_available
unless self.quantity.nil?
self.quantity - (self.store_purchases.where(charge: nil).pluck(:quantity_purchased).inject{|sum,x| sum + x } || 0 )
end
end
end
8 changes: 7 additions & 1 deletion app/views/store/items/_item.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
<tr>
<td><%= link_to item.name, item, :class => 'btn btn-info btn-xs' %></td>
<td><%= number_to_currency item.price %></td>
<td><%= item.quantity_available %></td>
<td>
<% if item.in_stock? %>
Available
<% else %>
Out of Stock
<% end %>
</td>
<td>
<% if can?(:create, Charge) %>
<%= link_to t('.add_to_cart', :default => t("helpers.links.add_to_cart")),
Expand Down
2 changes: 1 addition & 1 deletion app/views/store/items/_items.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<tr>
<th>Name</th>
<th>Price</th>
<th>Quantity Available</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
Expand Down
10 changes: 8 additions & 2 deletions app/views/store/items/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
<h1><%= @store_item.name %></h1>
<dt><strong><%= model_class.human_attribute_name(:price) %>:</strong></dt>
<dd><%= number_to_currency @store_item.price %></dd>
<dt><strong><%= model_class.human_attribute_name(:quantity) %>:</strong></dt>
<dd><%= @store_item.quantity %></dd>
<dt><strong>Status:</strong></dt>
<dd>
<% if @store_item.in_stock? %>
Available
<% else %>
Out of Stock
<% end %>
</dd>

</div>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class ChangeStoreItemQuantityToInStock < ActiveRecord::Migration
def change
remove_column :store_items, :quantity, :integer
add_column :store_items, :in_stock, :boolean
end
end
4 changes: 2 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20190322200326) do
ActiveRecord::Schema.define(version: 20190417031413) do

create_table "certification_types", force: :cascade do |t|
t.string "name", limit: 255
Expand Down Expand Up @@ -261,10 +261,10 @@
create_table "store_items", force: :cascade do |t|
t.string "name", limit: 255
t.decimal "price", precision: 8, scale: 2
t.integer "quantity", limit: 4
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "active", default: true
t.boolean "in_stock"
end

create_table "store_purchases", force: :cascade do |t|
Expand Down
2 changes: 1 addition & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
csv_text = File.read(Rails.root.join('lib', 'seeds', gdrive_doc + 'store.csv'))
csv = CSV.parse(csv_text, :headers => true)
csv.each do |row|
StoreItem.create(name: row['name'].strip, price: Integer(row['price'] || "0"), quantity: Integer(row['quantity'] || "0"))
StoreItem.create(name: row['name'].strip, price: Integer(row['price'] || "0"), in_stock: row['in_stock'] || true)
end

puts ' Tools'
Expand Down
7 changes: 2 additions & 5 deletions test/unit/store_item_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
# **`active`** | `boolean` | `default(TRUE)`
# **`created_at`** | `datetime` | `not null`
# **`id`** | `integer` | `not null, primary key`
# **`in_stock`** | `boolean` |
# **`name`** | `string(255)` |
# **`price`** | `decimal(8, 2)` |
# **`quantity`** | `integer` |
# **`updated_at`** | `datetime` | `not null`
#

Expand All @@ -28,7 +28,7 @@ class StoreItemTest < ActiveSupport::TestCase
context "With a proper context, " do
setup do
# Create store
@store_item = FactoryGirl.create(:store_item, :name => "Hammer", :price => 20, :quantity => 5)
@store_item = FactoryGirl.create(:store_item, :name => "Hammer", :price => 20, :in_stock => true)
# Create charge
@charge = FactoryGirl.create(:charge, :is_approved => true)
# Create store_purchase
Expand All @@ -38,8 +38,5 @@ class StoreItemTest < ActiveSupport::TestCase
teardown do
end

should "show that quantity_available method works correctly" do
assert_equal 4, @store_item.quantity_available
end
end
end
2 changes: 1 addition & 1 deletion test/unit/store_purchase_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class StorePurchaseTest < ActiveSupport::TestCase
context "With a proper context, " do
setup do
# Create store
@store_item = FactoryGirl.create(:store_item, :name => "Hammer", :price => 20, :quantity => 3)
@store_item = FactoryGirl.create(:store_item, :name => "Hammer", :price => 20, :in_stock => true)
# Create store_purchase
@store_purchase = FactoryGirl.create(:store_purchase, :price_at_purchase => 20, :quantity_purchased => 1, :store_item_id => @store_item.id)
end
Expand Down