Commit 13bbc9a4 by Vy Quoc Vu

admin

parent 3e2fe2bf
......@@ -56,8 +56,3 @@ group :test do
gem 'mini_backtrace', '0.1.3'
gem 'guard-minitest', '2.3.1'
end
\ No newline at end of file
group :production do
gem 'pg', '0.17.1'
gem 'rails_12factor', '0.0.2'
end
\ No newline at end of file
......@@ -36,6 +36,7 @@ GEM
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
ansi (1.5.0)
arel (6.0.2)
bcrypt (3.1.10)
binding_of_caller (0.7.2)
......@@ -47,6 +48,7 @@ GEM
builder (3.2.2)
byebug (5.0.0)
columnize (= 0.9.0)
coderay (1.1.0)
coffee-rails (4.1.0)
coffee-script (>= 2.2.0)
railties (>= 4.0.0, < 5.0)
......@@ -68,8 +70,22 @@ GEM
execjs (2.5.2)
faker (1.4.2)
i18n (~> 0.5)
ffi (1.9.10)
formatador (0.2.5)
globalid (0.3.5)
activesupport (>= 4.1.0)
guard (2.13.0)
formatador (>= 0.2.4)
listen (>= 2.7, <= 4.0)
lumberjack (~> 1.0)
nenv (~> 0.1)
notiffany (~> 0.0)
pry (>= 0.9.12)
shellany (~> 0.0)
thor (>= 0.18.1)
guard-minitest (2.3.1)
guard (~> 2.0)
minitest (>= 3.0)
i18n (0.7.0)
jbuilder (2.3.1)
activesupport (>= 3.0.0, < 5)
......@@ -81,19 +97,40 @@ GEM
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
json (1.8.3)
listen (3.0.3)
rb-fsevent (>= 0.9.3)
rb-inotify (>= 0.9)
loofah (2.0.2)
nokogiri (>= 1.5.9)
lumberjack (1.0.9)
mail (2.6.3)
mime-types (>= 1.16, < 3)
method_source (0.8.2)
mime-types (2.6.1)
mini_backtrace (0.1.3)
minitest (> 1.2.0)
rails (>= 2.3.3)
mini_portile (0.6.2)
minitest (5.7.0)
minitest-reporters (1.0.5)
ansi
builder
minitest (>= 5.0)
ruby-progressbar
multi_json (1.11.2)
multi_xml (0.5.5)
mysql2 (0.3.18)
nenv (0.2.0)
nokogiri (1.6.6.2)
mini_portile (~> 0.6.0)
notiffany (0.0.7)
nenv (~> 0.1)
shellany (~> 0.0)
orm_adapter (0.5.0)
pry (0.10.1)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
rack (1.6.4)
rack-test (0.6.3)
rack (>= 1.0)
......@@ -122,10 +159,14 @@ GEM
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rake (10.4.2)
rb-fsevent (0.9.5)
rb-inotify (0.9.5)
ffi (>= 0.5.0)
rdoc (4.2.0)
json (~> 1.4)
responders (2.1.0)
railties (>= 4.2.0, < 5)
ruby-progressbar (1.7.5)
sass (3.4.16)
sass-rails (5.0.3)
railties (>= 4.0.0, < 5.0)
......@@ -136,6 +177,8 @@ GEM
sdoc (0.4.1)
json (~> 1.7, >= 1.7.7)
rdoc (~> 4.0)
shellany (0.0.1)
slop (3.6.0)
spring (1.3.6)
sprockets (3.2.0)
rack (~> 1.0)
......@@ -177,8 +220,11 @@ DEPENDENCIES
coffee-rails (~> 4.1.0)
devise
faker (= 1.4.2)
guard-minitest (= 2.3.1)
jbuilder (~> 2.0)
jquery-rails
mini_backtrace (= 0.1.3)
minitest-reporters (= 1.0.5)
mysql2
rails (= 4.2.3)
sass-rails (~> 5.0)
......
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
// Place all the styles related to the admin/admins controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
// Place all the styles related to the admin/users controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
class Admin::AdminsController < ApplicationController
end
class Admin::UsersController < ApplicationController
end
......@@ -3,6 +3,11 @@ class CartProductsController < ApplicationController
include CategoriesHelper
def add
if params[:id].nil? || params[:quantity].nil?
flash[:danger] = "Product not found!"
redirect_to :action => :index
end
if params[:quantity].to_i > 0
id = params[:id]
if session[:cart] then
cart_product = session[:cart]
......@@ -12,13 +17,28 @@ class CartProductsController < ApplicationController
end
if cart_product[id] then
cart_product[id] = cart_product[id] +1
cart_product[id] = cart_product[id] + params[:quantity].to_i
else
cart_product[id] = 1
cart_product[id] = params[:quantity].to_i
end
flash[:success] = "success"
redirect_to :back
end
end
def remove
session[:cart].delete(params[:id].to_s)
redirect_to :action => :index
end
def update
if params[:new_quantity].nil? || params[:id].nil?
flash[:danger] = "Product not found!"
else
session[:cart][params[:id].to_s] = params[:new_quantity].to_i
end
redirect_to :action => :index
end
def clear
session[:cart] = nil
redirect_to :action => :index
......@@ -31,5 +51,4 @@ class CartProductsController < ApplicationController
@cart_product = {}
end
end
end
\ No newline at end of file
......@@ -16,7 +16,7 @@ class CartsController < ApplicationController
new_cart.user_id = current_user.id
end
new_cart.save
byebug
if !new_cart.id.nil?
session[:cart].each do |id, quantity|
cart_product = CartProduct.new
cart_product.cart_id = new_cart.id
......@@ -25,13 +25,18 @@ class CartsController < ApplicationController
cart_product.quantity = quantity
cart_product.save
end
Emailer.send_email_to(cart_params[:mail].to_s,session[:cart]).deliver
flash[:success] = "Success!"
session[:cart] = nil
end
end
redirect_to :action => "show"
end
def info
if user_signed_in?
@current_user
end
end
def cart_params
......
module Admin::AdminsHelper
end
module Admin::UsersHelper
end
class ApplicationMailer < ActionMailer::Base
default from: "from@example.com"
layout 'mailer'
end
class Emailer < ApplicationMailer
include ApplicationHelper
include CartsHelper
include CategoriesHelper
def send_email_to(email,cart)
mail to: email, subject: "Venshop!!"
@cart_product = cart
end
end
class ModelMailer < ApplicationMailer
# Subject can be set in your I18n file at config/locales/en.yml
# with the following lookup:
#
# en.model_mailer.new_record_notification.subject
#
def new_record_notification(record)
@record = record
mail to: "recipient@MYDOMAIN.com", subject: "Success! You did it."
end
end
class Admin < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
<%= will_paginate %>
<div class="row">
<% @products.each do |product| %>
<div class="col-md-4" style="padding-top : 20px">
<div>
<%= image_tag(product.image, size: "180x230")%>
<%= simple_format(truncate(product.name, length:21)) %>
<%= (product.price/100.to_f).to_s + "$" %>
</div>
<div>
</div>
</div>
<% end %>
</div>
<%= will_paginate %>
\ No newline at end of file
<h1>Your Cart</h1>
<form class="navbar-left" >
<%= link_to "Empty Cart" ,'/cart_product/clear' %>
</form>
<form class=" navbar-right" >
<%= link_to "Back " ,root_path %>
</form>
<div class="col-md-12">
<form class="navbar-left" style="padding-left: 0px" >
<%= link_to "Empty Cart" ,'/cart_product/clear' %>
</form>
<form class=" navbar-right" >
<%= link_to "Back " ,root_path %>
</form>
</div>
<div>
<ul>
<% total = 0 %>
......@@ -12,22 +15,35 @@
<% @cart_product.each do |id, quantity| %>
<% product = Product.find_by_id(id) %>
<% if !product.nil? %>
<% total = total + product.price * quantity %>
<% total = total + product.price * quantity.to_i %>
<div class="col-md-4">
<%= image_tag(product.image, size: "170x210")%>
<h5><%= truncate(product.name, length: 22) %></h5>
<%= number_to_currency(product.price/100.to_f) %>
</br>
<form action="/cart_product/update" style="padding-left : 0px ; resize: vertical;" >
<input type="number" name="new_quantity" min="1" max="100" value= <%= quantity %> >
</br>
<input type="submit" value="Update" style="font-size: 1em;" />
<input type="hidden" name="id" value="<%= product.id %>"/>
</form>
<form action="/cart_product/remove" style="padding-left : 0px" >
<input type="submit" value="Remove" style="font-size: 1em;" />
<input type="hidden" name="id" value="<%= product.id %>"/>
</form>
</div>
<% end %>
<% end -%>
<% else %>
<h3>Empty</h3>
<h2>Empty</h2>
<% end -%>
</ul>
</div>
<div class="col-md-12">
<nav>
<%=(total/100.to_f).to_s + "$" %>
</nav>
</div>
\ No newline at end of file
<div class="footer col-md-12">
</br>
</br>
<h2> Total price: <%=(total/100.to_f).to_s + "$" %></h2>
</br>
<%= link_to "Checkout", "/cart", class: "btn btn-lg btn-danger"%>
</footer>
\ No newline at end of file
<% if !session[:cart].nil? %>
<% if !session[:cart].nil? && !session[:cart].empty? %>
<h1>Cart info</h1>
<ul>
<% @total = 0 %>
......@@ -18,11 +18,18 @@
</ul>
<div class="row">
<div class="col-md-10 ">
<% if user_signed_in? %>
<% mail = @current_user.email %>
<% name = "" %>
<% address = "" %>
<% else %>
<% mail = "" %>
<% name = "" %>
<% address = "" %>
<% end %>
<%= form_for(:session, url: create_cart_path) do |f| %>
<%= f.label :mail %>
<%= f.email_field :mail, class: 'form-control' %>
<%= f.email_field :mail, value: mail, class: 'form-control'%>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :address %>
......
......@@ -7,8 +7,8 @@
<%= cart.mail.to_s + " | " %>
<%= cart.name.to_s + " | " %>
<%= cart.total_price.to_s + " | " %>
<%= cart.status.to_s + " | " %>
<%= cart.address %>
<%= cart.address+ " | " %>
<%= cart.status.to_s %>
</h4>
<% end -%>
<% else -%>
......
<h2>Sign up</h2>
<h2>Sign up in devise</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
......
<h1>Your Cart</h1>
<ul>
<% total = 0 %>
<%byebug%>
<% if !@cart_product.nil? %>
<% @cart_product.each do |id, quantity| %>
<% product = Product.find_by_id(id) %>
<% if !product.nil? %>
<% total = total + product.price * quantity %>
<li>
<%= link_to truncate(product.name, length:15), "/products/#{product.id}" %>
<%= quantity %>
</li>
<% end %>
<% end -%>
<% else %>
<h3>Empty</h3>
<% end -%>
<br>
<br>
<h2> <%= number_to_currency(total/100.to_f, :unit => '$')%> </h2>
<h2> Thank !</h2>
</ul>
......@@ -21,6 +21,9 @@
</table>
</div>
<div class="col-md-9" style=" padding-top: 50px;">
<% flash.each do |message_type, message| %>
<div class="alert alert-<%= message_type %>"><%= message %></div>
<% end %>
<%= yield %>
</div>
</div>
......
<html>
<body>
<%= yield %>
</body>
</html>
<h1>ModelMailer#new_record_notification</h1>
<p>
<%= @greeting %>, find me in app/views/model_mailer/new_record_notification.html.erb
</p>
ModelMailer#new_record_notification
<%= @greeting %>, find me in app/views/model_mailer/new_record_notification.text.erb
<%= will_paginate %>
<div class="row">
<% @products.each do |product| %>
<div class="col-md-4">
<div class="center jumbotron" style="padding-left: 35px;">
<div class="col-md-4" style="padding-top : 20px">
<div>
<%= image_tag(product.image, size: "180x230")%>
<h6><%= truncate(product.name, length:24) %></h6>
<%= simple_format(truncate(product.name, length:21)) %>
<%= (product.price/100.to_f).to_s + "$" %>
</div>
<div>
<form action="/cart_products" style="padding-left : 0px" >
<input type="number" name="quantity" value= "1" min="1" max="100" value="1">
</br>
<h6><%= link_to "Buy", "/cart_products/#{product.id}", class: "btn btn-lg btn-danger" %>
<%= link_to "Info", "/products/#{product.id}", class: "btn btn-lg btn-info" %> </h6>
<input type="submit" value="Add" class = "btn btn-lg btn-success" />
<%= link_to "Detail", "/products/#{product.id}", class: "btn btn-lg btn-info" %>
<input type="hidden" name="id" value="<%= product.id %>"/>
</form>
</div>
</div>
<% end %>
</div>
<%= will_paginate %>
\ No newline at end of file
<div>
<div class="center jumbotron" style="padding-left: 35px;">
<%= image_tag(@product.image)%>
<h3><%= @product.name %></h3>
<h3><%= simple_format(@product.name) %></h3>
<h3><%= (@product.price/100.to_f).to_s + "$" %></h3>
</br>
<%= simple_format(@product.description) %>
</br>
<%= link_to "Add to cart", "/cart_products/#{@product.id}", class: "btn btn-lg btn-danger"%>
<form action="/cart_products" style="padding-left: 0px" >
<input type="number" name="quantity" value= "1" min="1" max="100" value="1">
</br>
<input type="submit" value="Add" class = "btn btn-lg btn-success" />
<input type="hidden" name="id" value="<%= @product.id %>"/>
</form>
</div>
</div>
\ No newline at end of file
<h2>Sign up</h2>
<h2>User Sign up</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
......
Rails.application.routes.draw do
#devise_for :admins
devise_for :users, controllers: { sessions: "users/sessions" }
devise_for :admins, controllers: { sessions: "admins/sessions" }
get 'index' => 'products#index'
get 'login' => 'sessions#new'
get 'category/:id' => 'category#show'
get 'products/:id' => 'products#show'
get 'cart_products/:id' => 'cart_products#add'
get 'cart_products' => 'cart_products#add'
get 'cart_product/remove' => 'cart_products#remove'
get 'cart_product/update' => 'cart_products#update'
get 'cart_product/clear' => 'cart_products#clear'
get 'cart' => 'carts#info'
get 'carts' => 'cart_products#index'
......
class CreateProducts < ActiveRecord::Migration
def change
create_table :products do |t|
t.string :name, null: false
t.string :name, limit: 3000, null: false
t.string :image,limit: 1000, null: false
t.integer :category_id
t.integer :price, null: false
......
class DeviseCreateAdmins < ActiveRecord::Migration
def change
create_table(:admins) do |t|
## Database authenticatable
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at
## Rememberable
t.datetime :remember_created_at
## Trackable
t.integer :sign_in_count, default: 0, null: false
t.datetime :current_sign_in_at
t.datetime :last_sign_in_at
t.string :current_sign_in_ip
t.string :last_sign_in_ip
## Confirmable
# t.string :confirmation_token
# t.datetime :confirmed_at
# t.datetime :confirmation_sent_at
# t.string :unconfirmed_email # Only if using reconfirmable
## Lockable
# t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
# t.string :unlock_token # Only if unlock strategy is :email or :both
# t.datetime :locked_at
t.timestamps null: false
end
add_index :admins, :email, unique: true
add_index :admins, :reset_password_token, unique: true
# add_index :admins, :confirmation_token, unique: true
# add_index :admins, :unlock_token, unique: true
end
end
......@@ -11,7 +11,25 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20150724084151) do
ActiveRecord::Schema.define(version: 20150728075925) do
create_table "admins", force: :cascade do |t|
t.string "email", limit: 255, default: "", null: false
t.string "encrypted_password", limit: 255, default: "", null: false
t.string "reset_password_token", limit: 255
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", limit: 4, default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip", limit: 255
t.string "last_sign_in_ip", limit: 255
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "admins", ["email"], name: "index_admins_on_email", unique: true, using: :btree
add_index "admins", ["reset_password_token"], name: "index_admins_on_reset_password_token", unique: true, using: :btree
create_table "cart_products", force: :cascade do |t|
t.integer "cart_id", limit: 4, null: false
......@@ -42,7 +60,7 @@ ActiveRecord::Schema.define(version: 20150724084151) do
end
create_table "products", force: :cascade do |t|
t.string "name", limit: 255, null: false
t.string "name", limit: 3000, null: false
t.string "image", limit: 1000, null: false
t.integer "category_id", limit: 4
t.integer "price", limit: 4, null: false
......
require 'test_helper'
class Admin::AdminsControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end
require 'test_helper'
class Admin::UsersControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
# This model initially had no columns defined. If you add columns to the
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value
require 'test_helper'
class EmailerTest < ActionMailer::TestCase
# test "the truth" do
# assert true
# end
end
require 'test_helper'
class ModelMailerTest < ActionMailer::TestCase
test "new_record_notification" do
mail = ModelMailer.new_record_notification
assert_equal "New record notification", mail.subject
assert_equal ["to@example.org"], mail.to
assert_equal ["from@example.com"], mail.from
assert_match "Hi", mail.body.encoded
end
end
# Preview all emails at http://localhost:3000/rails/mailers/emailer
class EmailerPreview < ActionMailer::Preview
end
# Preview all emails at http://localhost:3000/rails/mailers/model_mailer
class ModelMailerPreview < ActionMailer::Preview
# Preview this email at http://localhost:3000/rails/mailers/model_mailer/new_record_notification
def new_record_notification
ModelMailer.new_record_notification
end
end
require 'test_helper'
class AdminTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment