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

Cart Quantities User Story 24/5 #152

Merged
merged 17 commits into from
Apr 8, 2019
Merged
Show file tree
Hide file tree
Changes from 37 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
11 changes: 11 additions & 0 deletions app/controllers/admin/merchants_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
class Admin::MerchantsController < Admin::BaseController
def show
end

def index
@merchants = User.all_merchants
end

def update
merchant = User.find(params[:merchant_id])
merchant.toggle :enabled
merchant.save
redirect_to admin_merchants_path
end
end
7 changes: 6 additions & 1 deletion app/controllers/carts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
class CartsController < ApplicationController
before_action :require_customer

def update
@cart.update_quantity(params[:item_id], params[:quantity].to_i)
redirect_to cart_path
end

def destroy
session[:cart] = {}
redirect_to cart_path
end

def show
@items = {}
@cart.contents.each do |item_id, quantity|
Expand Down
1 change: 1 addition & 0 deletions app/controllers/merchants_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class MerchantsController < ApplicationController
def index
@merchants = User.active_merchants
end
end
18 changes: 8 additions & 10 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ def edit

def update
@user = current_user
if update_params[:password] == ""
@user.update(update_params.except(:password, :password_confirmation))
elsif update_params[:password]
@user.update(update_params)
if user_info[:password] == ""
@user.update(user_info.except(:password, :password_confirmation))
elsif user_info[:password]
@user.update(user_info)
end
if @user.save
flash[:notice] = "You have updated your information."
Expand All @@ -32,7 +32,9 @@ def create
redisplay_new_form "Please fill in all fields"

elsif email_in_use
redisplay_new_form "E-Mail already in use", form_info.except("email")
@user = User.new(user_info.except(:email))
flash[:info] = "E-Mail already in use"
render :new

else
@user = User.create(user_info)
Expand All @@ -52,10 +54,6 @@ def new

private

def update_params
params.require(:user).permit(:name, :street_address, :city, :state, :zip_code, :email, :password, :password_confirmation)
end

def redisplay_new_form(message, pre_fill = form_info)
@user = User.new
flash[:info] = message
Expand All @@ -78,7 +76,7 @@ def user_info
params
.require(:user)
.permit(:name, :street_address, :city, :state, :zip_code, :email,
:password)
:password, :password_confirmation)
end

def form_info
Expand Down
8 changes: 8 additions & 0 deletions app/models/cart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,12 @@ def add_item(item_id)
def total_count
@contents.values.sum
end

def update_quantity(item_id, quantity)
if quantity == 0
@contents.delete(item_id)
else
@contents[item_id] = quantity
end
end
end
13 changes: 12 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ class User < ApplicationRecord
:email,
:role

validates_presence_of :password, require: true
# validates_presence_of :password, require: true
validates :password, presence: true, length: {minimum: 5, maximum: 120}, on: :create
validates :password, length: {minimum: 5, maximum: 120}, on: :update, allow_blank: true
validates_uniqueness_of :email

# validates_exclusion_of :enabled, in: [nil]
Expand All @@ -18,4 +20,13 @@ class User < ApplicationRecord
enum role: ['user', 'merchant', 'admin']

has_secure_password

def self.active_merchants
User.where(role: 1)
.where(enabled: true)
end

def self.all_merchants
User.where(role: 1)
end
end
14 changes: 14 additions & 0 deletions app/views/admin/merchants/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<h1>All Merchants in the system</h1>

<% @merchants.each do |merchant| %>
<section id="merchant-<%= merchant.id %>">
<p>Name: <%= link_to "#{merchant.name}", admin_merchant_path(merchant) %></p>
<p>Located in <%= merchant.city %>, <%= merchant.state %></p>
<p>Joined the store on <%= merchant.created_at %></p>
<% if merchant.enabled? %>
<%= link_to "Disable this Merchant", admin_merchant_change_status_path(merchant), method: :patch %>
<% elsif !merchant.enabled? %>
<%= link_to "Enable this Merchant", admin_merchant_change_status_path(merchant), method: :patch %>
<% end %>
</section>
<% end %>
14 changes: 11 additions & 3 deletions app/views/carts/_cart_item.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
<div id=<%="cart-item-#{item.id}"%>>
<div id=item-name><%=item.name%></div>
<div id=item-name>Item Name: <%=item.name%></div>
<div id=item-merchant><%=item.user.name%></div>
<div id=item-price><%=item.current_price%></div>
<div id=item-quantity><%=quantity%></div>
<div id=subtotal><%=quantity*item.current_price%></div>
<img src=<%=item.image_url%> height=20px width=20px/>
<div id=item-quantity>
<%= form_tag cart_path(item_id: item.id), method: :patch do %>
<%= select_tag "quantity", options_for_select((0..item.quantity).to_a, selected: quantity) %>

<%= submit_tag "Update Quantity"%>
<%end%>
<%= button_to "Remove Item", cart_path(item_id: item.id, quantity:0), method: :patch %>
</div>
<div id=subtotal><%=(quantity*item.current_price).round(2)%></div>
</div>
9 changes: 9 additions & 0 deletions app/views/carts/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,13 @@ Your cart is empty.
<%end%>
<div id=total><%=@total%><div>
<%= button_to "Empty Cart", cart_path, method: :delete%>

<div id=checkout>
<% if current_user %>
<%= button_to "Checkout" %>
<% else %>
You must register or log in to checkout
<%= link_to "Log In", login_path %> <%= link_to "Register", register_path %>
<%end%>
</div>
<%end%>
9 changes: 9 additions & 0 deletions app/views/merchants/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1>All Merchants:</h1>

<% @merchants.each do |merchant| %>
<section id="merchant-<%= merchant.id %>">
<p>Name: <%= merchant.name %></p>
<p>Located in <%= merchant.city %>, <%= merchant.state %></p>
<p>Joined the store on <%= merchant.created_at %></p>
</section>
<% end %>
45 changes: 45 additions & 0 deletions app/views/shared/_user_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<% if @user.errors.any? %>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>

<%= form_for @user do |f| %>
<p>
<%= f.label :name %>
<%= f.text_field :name %>
</p>
<p>
<%= f.label :street_address, "Street Address" %>
<%= f.text_field :street_address %>
</p>
<p>
<%= f.label :city %>
<%= f.text_field :city %>
</p>
<p>
<%= f.label :state %>
<%= f.text_field :state %>
</p>
<p>
<%= f.label :zip_code, "Zip Code" %>
<%= f.text_field :zip_code %>
</p>
<p>
<%= f.label :email, "E-Mail"%>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :password%>
<%= f.password_field :password %>
</p>
<p>
<%= f.label :password_confirmation, "Confirm Password" %>
<%= f.password_field :password_confirmation %>
</p>

<%= f.submit "#{form_type} User" %>

<% end %>
46 changes: 1 addition & 45 deletions app/views/users/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,45 +1 @@
<% if @user.errors.any? %>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>

<%= form_for @user do |f| %>
<p>
<%= f.label :name %>
<%= f.text_field :name %>
</p>
<p>
<%= f.label :street_address, "Street Address" %>
<%= f.text_field :street_address %>
</p>
<p>
<%= f.label :city %>
<%= f.text_field :city %>
</p>
<p>
<%= f.label :state %>
<%= f.text_field :state %>
</p>
<p>
<%= f.label :zip_code, "Zip Code" %>
<%= f.text_field :zip_code %>
</p>
<p>
<%= f.label :email, "Email"%>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :password%>
<%= f.password_field :password %>
</p>
<p>
<%= f.label :password_confirmation, "Confirm Password" %>
<%= f.password_field :password_confirmation %>
</p>

<%= f.submit %>

<% end %>
<%= render "shared/user_form", form_type:"Update" %>
38 changes: 1 addition & 37 deletions app/views/users/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,37 +1 @@

<%= form_for @user do |f| %>
<p>
<%= f.label :name %>
<%= f.text_field :name , value: params[:name]%>
</p>
<p>
<%= f.label :street_address, "Street Address" %>
<%= f.text_field :street_address, value: params[:street_address]%>
</p>
<p>
<%= f.label :city %>
<%= f.text_field :city, value: params[:city] %>
</p>
<p>
<%= f.label :state %>
<%= f.text_field :state, value: params[:state] %>
</p>
<p>
<%= f.label :zip_code, "Zip Code" %>
<%= f.text_field :zip_code, value: params[:zip_code] %>
</p>
<p>
<%= f.label :email, "E-Mail"%>
<%= f.text_field :email, value: params[:email] %>
</p>
<p>
<%= f.label :password%>
<%= f.password_field :password %>
</p>
<p>
<%= f.label :password_confirmation, "Confirm Password" %>
<%= f.password_field :password_confirmation %>
</p>

<%= f.submit "Register New User" %>
<%end%>
<%= render "shared/user_form", form_type:"Create" %>
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
get '/dashboard', to: "orders#index"
resources :users, only: [:index, :show]
get '/merchants/:id', to: "merchants#show", as: :merchant
get '/merchants', to: "merchants#index", as: :merchants
patch '/merchant/:merchant_id/', to: "merchants#update", as: :merchant_change_status
end

resources :users, only: [:create, :update]
Expand All @@ -17,6 +19,7 @@
get '/profile', to: "users#show"
get '/cart', to: 'carts#show'
delete '/cart', to: 'carts#destroy'
patch '/cart', to: 'carts#update'
resources :carts, only: [:create]
get '/merchants', to: "merchants#index"

Expand Down
Loading