Skip to content

Commit

Permalink
Merge pull request #35 from mayawang/ev_add_product
Browse files Browse the repository at this point in the history
Ev add product
  • Loading branch information
ellevargas authored Oct 24, 2016
2 parents 8df5493 + 456c9e2 commit 38c0ced
Show file tree
Hide file tree
Showing 20 changed files with 121 additions and 47 deletions.
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
source 'https://rubygems.org'
ruby '2.3.1'

#gem for authenticated user actions
#gem 'devise', '~> 4.2'

#gem for aAuth github Authentication
#gem for oAuth github Authentication
gem 'omniauth'
gem 'omniauth-github'

Expand Down
12 changes: 12 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ GEM
coffee-script-source (1.10.0)
concurrent-ruby (1.0.2)
debug_inspector (0.0.2)
devise (4.2.0)
bcrypt (~> 3.0)
orm_adapter (~> 0.1)
railties (>= 4.1.0, < 5.1)
responders
warden (~> 1.2.3)
dotenv (2.1.1)
dotenv-rails (2.1.1)
dotenv (= 2.1.1)
Expand Down Expand Up @@ -119,6 +125,7 @@ GEM
omniauth-oauth2 (1.4.0)
oauth2 (~> 1.0)
omniauth (~> 1.2)
orm_adapter (0.5.0)
pry (0.10.4)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
Expand Down Expand Up @@ -153,6 +160,8 @@ GEM
rake (11.3.0)
rdoc (4.2.2)
json (~> 1.4)
responders (2.3.0)
railties (>= 4.2.0, < 5.1)
ruby-progressbar (1.8.1)
sass (3.4.22)
sass-rails (5.0.6)
Expand Down Expand Up @@ -188,6 +197,8 @@ GEM
thread_safe (~> 0.1)
uglifier (3.0.2)
execjs (>= 0.3.0, < 3)
warden (1.2.6)
rack (>= 1.0)
web-console (2.3.0)
activemodel (>= 4.0)
binding_of_caller (>= 0.7.2)
Expand All @@ -203,6 +214,7 @@ DEPENDENCIES
better_errors
byebug
coffee-rails (~> 4.1.0)
devise (~> 4.2)
dotenv-rails
factory_girl_rails (~> 4.0)
jbuilder (~> 2.0)
Expand Down
10 changes: 8 additions & 2 deletions app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ def show
@product = Product.find(params[:id].to_i)

if @product.user_id
@seller = User.find(@product.user_id)
@seller = @product.user
# @seller = User.find(@product.user_id)
else
@seller = User.new
end
Expand All @@ -22,10 +23,15 @@ def show_seller_products
end

def new

@product = Product.new
end

def create
@product = Product.new(name: params[:product][:name], description: params[:product][:description], price: params[:product][:price], inventory: params[:product][:inventory], category: params[:product][:category], user_id: session[:user_id])
@product.save

redirect_to show_seller_products_path(@product.id)
# does rails think I mean to redirect to the method instead of the path? why doesn't it need an id for this route?
end

def edit
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def create

#GITHUB AUTHENTICATION START
auth_hash = request.env['omniauth.auth']

#raise can go here too to get auth hash
return redirect_to root_path unless auth_hash['uid']

@user = User.find_by(uid: auth_hash[:uid], provider: 'github')
Expand All @@ -35,7 +35,7 @@ def create
@user = User.build_from_github(auth_hash)
flash[:notice] = "Unable to Save the User"
#using method below to save time
return redirect_to root_path unless @user.save
return redirect_to root_path unless @user.save!
# render :creation_failure unless @user.save
end

Expand All @@ -58,7 +58,7 @@ def update
end

def destroy
session[:user_id] = nil
session[:user_id] = nil
redirect_to root_path, notice: 'Successfully logged out.'
end
end
15 changes: 7 additions & 8 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class UsersController < ApplicationController

# before_action :find_user only [:show, :edit, :update, :destroy]

#AN IDEA FOR HOW TO LIMIT PRODUCT EDITING?
Expand All @@ -17,7 +17,7 @@ def index
end

def show
# @my_user = User.find{ params[:id].to_i }
# @my_user = User.find{ params[:id].to_i }
end

def new
Expand All @@ -26,23 +26,22 @@ def new

def create
# @my_user = User.new user_params
# if @my_user.save
# if @my_user.save
# redirect_to root_path, notice: 'Successfully created user.'
# else render :new
# end
end


def edit
@my_user = User.find{ params[:id].to_i }
@my_user = User.find{ params[:id].to_i }
@path = 'update_path'
end

def update
@my_user = User.find{ params[:id].to_i }
@params = params
@my_user.first_name = params[:user][:first_name]
@my_user.last_name = params[:user][:last_name]
@my_user.fname = params[:user][:name]
@my_user.email = params[:user][:email]
@my_user.street_address = params[:user][:street_address]
@my_user.city = params[:user][:city]
Expand Down Expand Up @@ -72,4 +71,4 @@ def user_params
# params.require(:user).permit :email, :password, :password_confirmation
end

end
end
6 changes: 2 additions & 4 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ class User < ActiveRecord::Base
has_many :products
# has_secure_password


validates :email, :uid, :provider, presence: true

def self.build_from_github(auth_hash)
user = User.new
user.uid = auth_hash[:uid]
user.provider = 'github'
user.first_name = auth_hash['info']['first_name']
user.last_name = auth_hash['info']['last_name']
user.name = auth_hash['info']['name']
user.email = auth_hash['info']['email']

#raise to get info for auth hash
return user
end
end
11 changes: 10 additions & 1 deletion app/views/homepages/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
<% end %>


<h3><%= link_to "Browse All collections", index_products_path%></h3>
<h3><%= link_to "Browse All Collections", index_products_path%></h3>

<%= render partial: 'layouts/navigation_links' %>

<% if session[:user_id] != nil %>
<%= button_to "Create New Product", new_products_path, method: :get, :class => 'button' %>
<% end %>

<%# if current_user.present? %>
<%# button_to "Create New Product", new_products_path, method: :get, :class => 'button', uid: auth_hash[:uid] %>
<%# render :partial => "home/statusbar" %>
<%# end %>
31 changes: 31 additions & 0 deletions app/views/products/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<div class="basic">
<%= form_for @product, url: action_name do |f| %>

<div class="field">
<%= f.label "Product Name" %>
<%= f.text_field :name, "Product Name" => nil, :required => true %>
</div>

<div class="field">
<%= f.label "Description" %>
<%= f.text_field :description, "Description" => nil, :required => true %>
</div>

<div class="field">
<%= f.label "Price" %>
<%= f.number_field :price, "Price" => nil, :required => true %>
</div>

<div class="field">
<%= f.label "Inventory" %>
<%= f.number_field :inventory, "Inventory" => nil, :required => true %>
</div>

<div class="field">
<%= f.label "Category" %>
<%= f.select :category, Product.uniq.pluck(:category), "Category" => nil, :required => true %>
</div>

<%= f.submit "Create New Product", class: 'button' %>
<% end %>
</div>
3 changes: 1 addition & 2 deletions app/views/products/create.html.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
<h1>Products#create</h1>
<p>Find me in app/views/products/create.html.erb</p>
<%# params %>
8 changes: 6 additions & 2 deletions app/views/products/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
<h1>Products#new</h1>
<p>Find me in app/views/products/new.html.erb</p>
<div class="basic">
<% if session[:user_id] != nil %>
<h2>Post a New Product</h2>
<%= render partial: "form", locals: {action_name: "create"}%>
<% end %>
</div>
2 changes: 1 addition & 1 deletion app/views/products/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<p>Price: $<%= @product.price %></p>
<p>In-stock: <%= @product.inventory %></p>
<p>Category: <%= @product.category %></p>
<p>Seller: <%= link_to "#{@seller.first_name} #{@seller.last_name}", show_seller_products_path %></p>
<p>Seller: <%= link_to "#{@seller.name}", show_seller_products_path %></p>
<p>Average rating: <%= @product.average_rating %></p>

<p>Review:
Expand Down
16 changes: 10 additions & 6 deletions app/views/products/show_seller_products.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<h1>Collections by this Seller:</h1>
<% @products.each do |product| %>
<p><%= link_to image_tag("product.photo_url"), show_products_path(product.id), { :class => "product_img" } %></p>
<p><%= link_to product.name, show_products_path(product.id) %></p>
<p><%= product.price %></p>
<% end %>
<div class="basic">
<div class="columns">
<h2>Collections by this Seller:</h2>
<% @products.each do |product| %>
<p><%= link_to image_tag(product.photo_url), show_products_path(product.id), :class => "photo" %></p>
<p><%= link_to product.name, show_products_path(product.id) %></p>
<p><%= product.price %></p>
<% end %>
</div>
</div>
3 changes: 3 additions & 0 deletions config/initializers/devise.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# config.omniauth :github, "4fa0e3e1a1c088c7fba0", "0644062e493cedc8b257f39abdfd768c653b5983"
#
# config.omniauth :github, "4fa0e3e1a1c088c7fba0", "0644062e493cedc8b257f39abdfd768c653b5983", callback_url: "http://localhost:3000/auth/github/callback"
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

get 'products/index' => 'products#index', as: 'index_products'

get 'products/:id/seller_products' => 'products#show_seller_products', as: 'show_seller_products'
get 'products/:id/show_seller_products' => 'products#show_seller_products', as: 'show_seller_products'

get 'products/show/:id' => 'products#show', as: 'show_products'

Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20161024205357_remove_last_name_from_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RemoveLastNameFromUsers < ActiveRecord::Migration
def change
remove_column :users, :last_name
end
end
5 changes: 5 additions & 0 deletions db/migrate/20161024205557_rename_first_name_column_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RenameFirstNameColumnUsers < ActiveRecord::Migration
def change
rename_column :users, :first_name, :name
end
end
5 changes: 2 additions & 3 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: 20161024090318) do
ActiveRecord::Schema.define(version: 20161024205557) do

create_table "order_items", force: :cascade do |t|
t.decimal "unit_price"
Expand Down Expand Up @@ -74,8 +74,7 @@
add_index "reviews", ["product_id"], name: "index_reviews_on_product_id"

create_table "users", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.string "name"
t.string "email"
t.string "street_address"
t.string "city"
Expand Down
2 changes: 1 addition & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
end

CSV.foreach ('seed_csvs/users.csv') do |csv_obj|
User.create(first_name: csv_obj[1], last_name: csv_obj[2], email: csv_obj[3], street_address: csv_obj[4], city: csv_obj[5], state: csv_obj[6], zip: csv_obj[7], phone: csv_obj[8], merchant: csv_obj[9], provider: csv_obj[10])
User.create(name: csv_obj[1], email: csv_obj[2], street_address: csv_obj[3], city: csv_obj[4], state: csv_obj[5], zip: csv_obj[6], phone: csv_obj[7], merchant: csv_obj[8], provider: csv_obj[9])
end
16 changes: 8 additions & 8 deletions seed_csvs/users.csv
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
1,"Rusty","Rail","rusty@vampires.io","123 Madison Ave","Seattle","WA",98102,2062222222,true,"github"
2,"Sally Beth","Walcott","sally@vampires.io","1700 Undead Lane","Ruidoso","NM",88345,5055555555,true,"github"
3,"Lestat","Jones","lestat@aol.com","541 Old Hidden Country Route 5 North","Flushing","MI",48433,8108888888,true,"github"
4,"Madeleine","Kinoko","maddy@mushroom.com","700 Ocarina Way Apt 2","Phoenix","AZ",85345,6022222222,true,"github"
5,"Juanita","Torres","juanita@aol.com","814 East Archer Drive","Ruidoso","NM",88355,5054444444,true,"github"
6,"Marcy","Ogulo","marcy@vampires.io","10109 Eldritch Circle","Phoenix","AZ",85003,6233333333,true,"github"
7,"Laila","Satrapi","laila@gmail.com","222 Lugosi Lane #C","Seattle","WA",98104,2066666666,true,"github"
8,"Ambrose","Ardenmoor","ambrose@vampires.io","3 Unseen Court","Casper","WY",82002,8777777777,true,"github"
1,"Rusty Rail","rusty@vampires.io","123 Madison Ave","Seattle","WA",98102,2062222222,true,"github"
2,"Sally Beth Walcott","sally@vampires.io","1700 Undead Lane","Ruidoso","NM",88345,5055555555,true,"github"
3,"Lestat Jones","lestat@aol.com","541 Old Hidden Country Route 5 North","Flushing","MI",48433,8108888888,true,"github"
4,"Madeleine Kinoko","maddy@mushroom.com","700 Ocarina Way Apt 2","Phoenix","AZ",85345,6022222222,true,"github"
5,"Juanita Torres","juanita@aol.com","814 East Archer Drive","Ruidoso","NM",88355,5054444444,true,"github"
6,"Marcy Ogulo","marcy@vampires.io","10109 Eldritch Circle","Phoenix","AZ",85003,6233333333,true,"github"
7,"Laila Satrapi","laila@gmail.com","222 Lugosi Lane #C","Seattle","WA",98104,2066666666,true,"github"
8,"Ambrose Ardenmoor","ambrose@vampires.io","3 Unseen Court","Casper","WY",82002,8777777777,true,"github"
6 changes: 2 additions & 4 deletions test/fixtures/users.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

rbg:
first_name: Ruth
last_name: Ginsburg
name: Ruth Ginsburg
email: notorious@thesupremes.io
street_address: 1 First Street NE
city: Washington
Expand All @@ -14,8 +13,7 @@ rbg:
status: true

ss:
first_name: Sonia
last_name: Sotomeyer
name: Sonia Sotomayor
email: not-red-sonia@thesupremes.io
street_address: 1 First Street NE
city: Washington
Expand Down

0 comments on commit 38c0ced

Please sign in to comment.