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

Week1_HW #9

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
8 changes: 7 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ gem 'sdoc', '~> 0.4.0', group: :doc

# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring', group: :development

gem "devise"
gem 'bootstrap-sass'
gem 'autoprefixer-rails'
gem 'simple_form'
gem 'carrierwave'
gem 'mini_magick'
gem 'will_paginate', '~> 3.0.5'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

Expand Down
34 changes: 34 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,31 @@ GEM
thread_safe (~> 0.1)
tzinfo (~> 1.1)
arel (5.0.1.20140414130214)
autoprefixer-rails (2.1.0.20140628)
execjs
bcrypt (3.1.7)
bcrypt-ruby (3.1.5)
bcrypt (>= 3.1.3)
bootstrap-sass (3.1.1.0)
sass (~> 3.2)
builder (3.2.2)
carrierwave (0.10.0)
activemodel (>= 3.2.0)
activesupport (>= 3.2.0)
json (>= 1.7)
mime-types (>= 1.16)
coffee-rails (4.0.1)
coffee-script (>= 2.2.0)
railties (>= 4.0.0, < 5.0)
coffee-script (2.2.0)
coffee-script-source
execjs
coffee-script-source (1.7.0)
devise (3.0.2)
bcrypt-ruby (~> 3.0)
orm_adapter (~> 0.1)
railties (>= 3.2.6, < 5)
warden (~> 1.2.3)
erubis (2.7.0)
execjs (2.2.0)
hike (1.2.3)
Expand All @@ -51,8 +68,11 @@ GEM
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.25.1)
mini_magick (3.7.0)
subexec (~> 0.2.1)
minitest (5.3.4)
multi_json (1.10.1)
orm_adapter (0.5.0)
polyglot (0.3.5)
rack (1.5.2)
rack-test (0.6.2)
Expand Down Expand Up @@ -84,6 +104,9 @@ GEM
sdoc (0.4.0)
json (~> 1.8)
rdoc (~> 4.0, < 5.0)
simple_form (3.0.2)
actionpack (~> 4.0)
activemodel (~> 4.0)
spring (1.1.3)
sprockets (2.11.0)
hike (~> 1.2)
Expand All @@ -95,6 +118,7 @@ GEM
activesupport (>= 3.0)
sprockets (~> 2.8)
sqlite3 (1.3.9)
subexec (0.2.3)
thor (0.19.1)
thread_safe (0.3.4)
tilt (1.4.1)
Expand All @@ -108,18 +132,28 @@ GEM
uglifier (2.5.0)
execjs (>= 0.3.0)
json (>= 1.8.0)
warden (1.2.3)
rack (>= 1.0)
will_paginate (3.0.7)

PLATFORMS
ruby

DEPENDENCIES
autoprefixer-rails
bootstrap-sass
carrierwave
coffee-rails (~> 4.0.0)
devise
jbuilder (~> 2.0)
jquery-rails
mini_magick
rails (= 4.1.0)
sass-rails (~> 4.0.3)
sdoc (~> 0.4.0)
simple_form
spring
sqlite3
turbolinks
uglifier (>= 1.3.0)
will_paginate (~> 3.0.5)
3 changes: 3 additions & 0 deletions app/assets/javascripts/admin/products.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# 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/
1 change: 1 addition & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require bootstrap
//= require_tree .
3 changes: 3 additions & 0 deletions app/assets/stylesheets/admin/products.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the admin::products controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
*
*= require_tree .
*= require_self
*= require "bootstrap"
*/
31 changes: 31 additions & 0 deletions app/controllers/admin/products_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Admin::ProductsController < ApplicationController
before_action :authenticate_user!, :except => [:index, :show]
before_action :admin_required, :except => [:index, :show]
def new
@product = Product.new
end
def show
@product = Product.find(params[:id])
end
def create
@product = Product.new(product_params)
if @product.save
redirect_to root_path
else
render :new
end
end
def index
@products = Product.paginate(:page => params[:page], :per_page => 4)
end
def destroy
@product = Product.find(params[:id])
@product.destroy
redirect_to root_path
end

private
def product_params
params.require(:product).permit(:title, :description, :quantity, :image, :price)
end
end
3 changes: 3 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
def admin_required
current_user.admin? if authenticate_user!
end
end
2 changes: 2 additions & 0 deletions app/helpers/admin/products_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module Admin::ProductsHelper
end
7 changes: 7 additions & 0 deletions app/models/product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Product < ActiveRecord::Base
mount_uploader :image, ImageUploader

validates :title, presence: true, uniqueness: true
validates :quantity, numericality: { only_integer: true, greater_than_or_equal_to: 0}
validates :price, numericality: { only_integer: true, greater_than_or_equal_to: 0}
end
11 changes: 11 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable

def admin?
is_admin
end
end
51 changes: 51 additions & 0 deletions app/uploaders/image_uploader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# encoding: utf-8

class ImageUploader < CarrierWave::Uploader::Base

# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
include CarrierWave::MiniMagick

# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog

# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end

# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url
# # For Rails 3.1+ asset pipeline compatibility:
# # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
#
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
# end

# Process files as they are uploaded:
# process :scale => [200, 300]
#
# def scale(width, height)
# # do something
# end

# Create different versions of your uploaded files:
version :thumb do
process :resize_to_fill => [200, 200]
end

# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
# def extension_white_list
# %w(jpg jpeg gif png)
# end

# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
# "something.jpg" if original_filename
# end

end
27 changes: 27 additions & 0 deletions app/views/admin/products/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<h2>Product Listing</h2>




<div class="row">
<% @products.each do |product|%>
<div class="col-xs-6 col-md-3">
<%= link_to(admin_product_path(product),:class => "thumbnail") do%>
<%= image_tag product.image.thumb.url %>
<div class="text-center"><h3><%= product.title %></h3></div>
<%end%>
<%= button_to "Delete", admin_product_path(product), :method => "delete", :class => "btn btn-mini"%>
</div>
<%end%>
</div>



<%if current_user %>
<div>
<%if current_user.is_admin? %>
<%= link_to "New Product", new_admin_product_path, :class => "btn btn-primary"%>
<%end%>
</div>
<%end%>
<%= will_paginate @products%>
22 changes: 22 additions & 0 deletions app/views/admin/products/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

<h2>Creat Product</h2>

<% if @product.errors.any? %>
<div>
<h1><%= pluralize(@product.errors.count, "error")%> prohibited this post from being saved:</h1>
<ul>
<% @product.errors.full_messages.each do |msg|%>
<li><%= msg%></li>
<%end%>
</ul>
</div>
<%end%>
<%= simple_form_for [:admin, @product] do |f|%>
<%= f.input :title, input_html: { maxlength: 20 }%>
<%= f.input :description%>
<%= f.input :quantity%>
<%= f.input :image%>
<%= f.input :price%>
<%= f.button :submit%>
<%end%>

18 changes: 18 additions & 0 deletions app/views/admin/products/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<div >
<div class="row">
<div class="col-md-4">
<%= image_tag @product.image.thumb.url, :class => "img-thumbnail" %>
</div>
<div class="col-md-4">
<h1><%= @product.title%></h1>
<p><%= @product.description%></p>
<p>數量:<%= @product.quantity %></p>
<p><h2 class="text-danger">$ <%= @product.price %></h2></p>
</div>
</div>
<div class="row">
<div class="col-md-10">
<p class="text-right"> <%= link_to "加入購物車", '#',:class => "btn btn-primary"%> </p>
</div>
</div>
</div>
12 changes: 12 additions & 0 deletions app/views/devise/confirmations/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<h2>Resend confirmation instructions</h2>

<%= form_for(resource, :as => resource_name, :url => confirmation_path(resource_name), :html => { :method => :post }) do |f| %>
<%= devise_error_messages! %>

<div><%= f.label :email %><br />
<%= f.email_field :email, :autofocus => true %></div>

<div><%= f.submit "Resend confirmation instructions" %></div>
<% end %>

<%= render "devise/shared/links" %>
5 changes: 5 additions & 0 deletions app/views/devise/mailer/confirmation_instructions.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<p>Welcome <%= @email %>!</p>

<p>You can confirm your account email through the link below:</p>

<p><%= link_to 'Confirm my account', confirmation_url(@resource, :confirmation_token => @resource.confirmation_token) %></p>
8 changes: 8 additions & 0 deletions app/views/devise/mailer/reset_password_instructions.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<p>Hello <%= @resource.email %>!</p>

<p>Someone has requested a link to change your password. You can do this through the link below.</p>

<p><%= link_to 'Change my password', edit_password_url(@resource, :reset_password_token => @resource.reset_password_token) %></p>

<p>If you didn't request this, please ignore this email.</p>
<p>Your password won't change until you access the link above and create a new one.</p>
7 changes: 7 additions & 0 deletions app/views/devise/mailer/unlock_instructions.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<p>Hello <%= @resource.email %>!</p>

<p>Your account has been locked due to an excessive number of unsuccessful sign in attempts.</p>

<p>Click the link below to unlock your account:</p>

<p><%= link_to 'Unlock my account', unlock_url(@resource, :unlock_token => @resource.unlock_token) %></p>
16 changes: 16 additions & 0 deletions app/views/devise/passwords/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<h2>Change your password</h2>

<%= form_for(resource, :as => resource_name, :url => password_path(resource_name), :html => { :method => :put }) do |f| %>
<%= devise_error_messages! %>
<%= f.hidden_field :reset_password_token %>

<div><%= f.label :password, "New password" %><br />
<%= f.password_field :password, :autofocus => true %></div>

<div><%= f.label :password_confirmation, "Confirm new password" %><br />
<%= f.password_field :password_confirmation %></div>

<div><%= f.submit "Change my password" %></div>
<% end %>

<%= render "devise/shared/links" %>
12 changes: 12 additions & 0 deletions app/views/devise/passwords/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<h2>Forgot your password?</h2>

<%= form_for(resource, :as => resource_name, :url => password_path(resource_name), :html => { :method => :post }) do |f| %>
<%= devise_error_messages! %>

<div><%= f.label :email %><br />
<%= f.email_field :email, :autofocus => true %></div>

<div><%= f.submit "Send me reset password instructions" %></div>
<% end %>

<%= render "devise/shared/links" %>
Loading