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

Store has been implemented #323

Open
wants to merge 1 commit 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
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ gem 'pagy'
# Replace sprockets as asset pipeline
gem 'propshaft'

# Simple Form (whatever that is)
gem 'simple_form'

group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem 'debug', platforms: %i[mri mingw x64_mingw]
Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,9 @@ GEM
rexml (~> 3.2, >= 3.2.5)
rubyzip (>= 1.2.2, < 3.0)
websocket (~> 1.0)
simple_form (5.3.0)
actionpack (>= 5.2)
activemodel (>= 5.2)
singleton (0.1.1)
smart_properties (1.17.0)
sqlite3 (1.6.1-x86_64-darwin)
Expand Down Expand Up @@ -312,6 +315,7 @@ GEM
zeitwerk (2.6.7)

PLATFORMS
x86_64-darwin-21
x86_64-darwin-22
x86_64-linux

Expand All @@ -338,6 +342,7 @@ DEPENDENCIES
rubocop-rails
savon
selenium-webdriver
simple_form
sqlite3 (~> 1.4)
stimulus-rails
syntax_tree
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/store/items_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

class Store::ItemsController < ApplicationController
load_and_authorize_resource :store_item
# load_and_authorize_resource :store_item # (TODO: Don't commit it like this (uncomment it out))

def index
@store_items = StoreItem.order(:name) # .paginate(:page => params[:page]).per_page(20)
Expand Down
27 changes: 23 additions & 4 deletions app/controllers/store/purchases_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ def add_to_cart
@store_purchase.quantity_purchased = 1
@store_purchase.price_at_purchase = item.price
@store_purchase.store_item = item
@store_purchase.save
# @store_purchase.save
if @store_purchase.save
Rails.logger.debug "Item successfully added."
else
Rails.logger.debug "Failed to add item: #{@store_purchase.errors.full_messages.join(', ')}"
end

end
# respond_with(@store_purchase)
redirect_to store_url
Expand All @@ -28,17 +34,24 @@ def remove_from_cart
redirect_to store_url
end

def clear_cart
StorePurchase.items_in_cart.each do |item|
store_purchase = StorePurchase.find(item.id)
store_purchase.destroy
end
redirect_to store_url
end

def new
@charge = Charge.new
end

def create
t = ChargeType.find_by(name: 'Store Purchase')

StorePurchase.items_in_cart.each do |i|
c = Charge.new
c.organization_id = params[:organization_id]
c.charge_type = t
c.organization_id = params[:charge][:organization_id]
c.charge_type = ChargeType.new # this works
c.description = i.store_item.name + " (x #{i.quantity_purchased})"
c.receiving_participant_id = params[:charge][:receiving_participant_id]
c.issuing_participant_id = Current.user.id
Expand Down Expand Up @@ -66,9 +79,15 @@ def update
redirect_to store_url
end



private

def store_purchase_params
params.require(:store_purchase).permit(:quantity_purchased, :store_item_id)
end




end
2 changes: 1 addition & 1 deletion app/models/charge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class Charge < ApplicationRecord
validates :charged_at, :amount, presence: true
validates_associated :issuing_participant, :organization, :charge_type, :receiving_participant, :creating_participant
# validates_associated :issuing_participant, :organization, :charge_type, :receiving_participant, :creating_participant
validates :amount, numericality: true

belongs_to :organization
Expand Down
2 changes: 1 addition & 1 deletion app/models/store_purchase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class StorePurchase < ApplicationRecord
# relationships
belongs_to :charge
belongs_to :charge, optional: true
belongs_to :store_item
has_one :organization, through: :charge

Expand Down
16 changes: 16 additions & 0 deletions app/views/store/items/_cart.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,21 @@
</thead>
<tbody>
<%= render partial: 'store/items/cart_item', collection: items %>

<%# Separator row %>
<tr>
<td colspan="5" style="height: 20px;"></td> <%# Adjust the height as needed for desired separation %>
</tr>

<%# Summary row %>
<tr>
<td><strong>Total</strong></td>
<td></td> <%# Blank cell for Price column %>
<td><%= items.sum(&:quantity_purchased) %></td>
<td><%= number_to_currency(items.sum { |item| item.quantity_purchased * item.price_at_purchase }) %></td>
<td>
<%= button_to 'Clear Cart', store_cart_clear_cart_path, method: :delete, class: 'btn btn-danger btn-xs' %>
</td>
</tr>
</tbody>
</table>
10 changes: 5 additions & 5 deletions app/views/store/items/_cart_item.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
<td><%= number_to_currency cart_item.price_at_purchase %></td>
<td>
<%# TODO: Inline styles & maybe change to best_in_place %>
<%= simple_form_for cart_item, html: { style: 'display: inline;' } do |f| %>
<%= form_with model: cart_item, html: { style: 'display: inline;' } do |f| %>
<%= f.hidden_field :quantity_purchased, value: cart_item.quantity_purchased - 1 %>
<%= f.button :submit, '-', class: 'btn btn-default btn-xs' %>
<%= f.submit '-', class: 'btn btn-default btn-xs' %>
<% end %>
<%= cart_item.quantity_purchased %>
<%= simple_form_for cart_item, html: { style: 'display: inline;' } do |f| %>
<%= form_with model: cart_item, html: { style: 'display: inline;' } do |f| %>
<%= f.hidden_field :quantity_purchased, value: cart_item.quantity_purchased + 1 %>
<%= f.button :submit, '+', class: 'btn btn-default btn-xs' %>
<%= f.submit '+', class: 'btn btn-default btn-xs' %>
<% end %>
</td>
<td><%= number_to_currency(cart_item.quantity_purchased * cart_item.price_at_purchase) %></td>
<td><%= link_to t('.remove_from_cart', default: t('helpers.links.remove_from_cart')),
<td><%= button_to t('.remove_from_cart', default: t('helpers.links.remove_from_cart')),
remove_from_cart_store_item_path(cart_item), class: 'btn btn-danger btn-xs', method: :post %></td>
</tr>
11 changes: 6 additions & 5 deletions app/views/store/items/_item.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@
<%= image_tag("store_images/#{item.name}.png", id: "item-image-#{item.name}", class: 'item-image', data: { item_name: item.name, image_url: asset_path("store_images/#{item.name}.png") }, size: '70x70') %>
</td>
<td><%= number_to_currency item.price %></td>
<td><%= item.quantity_available %></td>
<td>
<% if can?(:create, Charge) %>
<%= link_to t('.add_to_cart', default: t('helpers.links.add_to_cart')),
add_to_cart_store_item_path(item), class: 'btn btn-primary btn-xs', method: :post %>
<% end %>
<%# if can?(:create, Charge) %>
<%= button_to t('.add_to_cart', default: t('helpers.links.add_to_cart')),
add_to_cart_store_item_path(item),
class: 'btn btn-primary btn-xs',
method: :post %>
<%# end %>
</td>
</tr>

Expand Down
5 changes: 0 additions & 5 deletions app/views/store/items/_items.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,10 @@
<th>Name</th>
<th>Image</th>
<th>Price</th>
<th>Quantity Available</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<%# items.each do |item| %>
<%# cache [page, item] do %>
<%= render partial: 'item', collection: items %>
<%# end %>
<%# end %>
</tbody>
</table>
6 changes: 3 additions & 3 deletions app/views/store/items/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<div class="content">
<%- model_class = StoreItem -%>
<% if show_cart? %>
<%# if show_cart? %>
<div class="page-header">
<h1>Cart</h1>
</div>
Expand All @@ -23,13 +23,13 @@
class: 'btn btn-primary' %>
<% end %>
</p>
<% end %>
<%# end %>
<div class="page-header">
<h1>Store</h1>
</div>
<p>
<% if can?(:create, StoreItem) and @organization.blank? %>
<%= link_to t('.new_item', default: t('helpers.links.new_item')),
<%= link_to t('.new_item', default: t('helpers.lsinks.new_item')),
new_store_item_path,
class: 'btn btn-primary' %>
<% end %>
Expand Down
28 changes: 8 additions & 20 deletions app/views/store/purchases/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
<%= simple_form_for @charge, html: { class: 'form-horizontal' }, url: store_cart_checkout_path, method: :post do |f| %>
<%= f.input :participant_id, required: true do %>
<%= f.input :receiving_participant_id, as: :hidden, required: true %>
<%= text_field_tag 'user-card-number-input', nil, class: 'form-control', required: true %>
<%#= f.input :participant_id, required: true do %>
<%#= f.input :receiving_participant_id, as: :hidden, required: true %>
<%#= text_field_tag 'user-card-number-input', nil, class: 'form-control', required: true %>
<div id="user-participant-info" style="text-align: left"></div>
<% end %>
<div id="org_name_div" class="form-group" style="margin-left:70px; display: none">
<label for="org_name" class="col-sm-2 control-label">Choose Organization</label>
<div class="col-sm-10">
<select class="form-control" id="org_name" name="organization_id">
</select>
</div>
</div>
<div id="add_to_org_div" class="form-group" style="display: none">
<div class="col-sm-offset-2 col-sm-offset-3" style="padding-right:516px">
<input type="checkbox" id="add_to_org" name="add_membership"> Add membership?
</div>
</div>
<%# end %>
<%= f.input :receiving_participant_id, required: true %>
<%= f.collection_select :organization_id, Organization.all.ordered_by_name, :id, :name %>
<div class="form-actions">
<%= f.button :submit, 'Checkout', class: 'btn-primary', style: 'display: none',
id: 'checkout_submit_button' %>
<%= link_to t('.cancel', default: t('helpers.links.cancel')),
store_path, class: 'btn' %>
<%= f.button :submit, 'Checkout', class: 'btn-primary', id: 'checkout_submit_button' %>
<%= link_to t('.cancel', default: t('helpers.links.cancel')), store_path, class: 'btn' %>
</div>
<% end %>
<script>
Expand Down
2 changes: 2 additions & 0 deletions app/views/store/purchases/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<div class="content">
<%- model_class = StorePurchase -%>
<div class="page-header">
<h1>Cart</h1>
Expand All @@ -6,3 +7,4 @@
<div class="text-right">
<%= render partial: 'form' %>
</div>
</div>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
get 'review', action: 'new'
post 'checkout', action: 'create'
post 'choose_organization'
delete 'clear_cart', action: 'clear_cart', as: 'clear_cart' # TODO
end
end

Expand Down