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

Homework #11

Open
wants to merge 13 commits 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
# Ignore all logfiles and tempfiles.
/log/*.log
/tmp
/public/uploads/*
12 changes: 12 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ 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 'carrierwave'
gem "mini_magick"


gem 'simple_form'


# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

Expand Down
28 changes: 28 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,28 @@ GEM
thread_safe (~> 0.1)
tzinfo (~> 1.1)
arel (5.0.1.20140414130214)
bcrypt (3.1.7)
bootstrap-sass (3.2.0.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.2.4)
bcrypt (~> 3.0)
orm_adapter (~> 0.1)
railties (>= 3.2.6, < 5)
thread_safe (~> 0.1)
warden (~> 1.2.3)
erubis (2.7.0)
execjs (2.2.0)
hike (1.2.3)
Expand All @@ -51,8 +65,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 +101,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 +115,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,17 +129,24 @@ GEM
uglifier (2.5.0)
execjs (>= 0.3.0)
json (>= 1.8.0)
warden (1.2.3)
rack (>= 1.0)

PLATFORMS
ruby

DEPENDENCIES
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
Expand Down
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/
2 changes: 2 additions & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@
//= require jquery_ujs
//= require turbolinks
//= require_tree .
//= require bootstrap

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/
1 change: 1 addition & 0 deletions app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
*
*= require_tree .
*= require_self
*= require bootstrap
*/
36 changes: 36 additions & 0 deletions app/controllers/admin/products_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Admin::ProductsController < ApplicationController

before_action :authenticate_user!
before_action :admin_required


def index
@products = Product.all
end

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 admin_products_path
else
render :new
end
end

private

def product_params
params.require(:product).permit(:title,:description,:quantity, :image)
end


end
5 changes: 5 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ 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.is_admin?
end

end
13 changes: 13 additions & 0 deletions app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class ProductsController < ApplicationController


def index
@products = Product.all
end

def show
@product = Product.find(params[:id])
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
5 changes: 5 additions & 0 deletions app/models/product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Product < ActiveRecord::Base
mount_uploader :image , ImageUploader


end
12 changes: 12 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable

def is_admin?
is_admin
end


end
67 changes: 67 additions & 0 deletions app/uploaders/image_uploader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# 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_fit => [150, 150]
# end

version :md do
process resize_to_fit: [700, 500]
end

version :sm, from_version: :md do
process resize_to_fit: [300, 300]
end

version :square, from_version: :sm do
process resize_to_fill: [220, 220]
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
18 changes: 18 additions & 0 deletions app/views/admin/products/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<%= link_to "新增",new_admin_product_path, class: "btn btn-warning"%>
<br><br>
<%@products.each do |product| %>
<div class="col-md-4">
<div class="thumbnail">
<%= image_tag product.image.url(:sm)%>
<div class="caption">
<h3><%= link_to product.title,admin_product_path(product) %></h3>
<p><%= product.description %></p>
<p>數量: <%= product.quantity %> </p>
</div>
</div>
</div>

<% end %>



27 changes: 27 additions & 0 deletions app/views/admin/products/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@



<%= form_for [:admin, @product] do |form| %>
<div class="group">
<%= form.label("標題")%>
<%= form.text_field :title%>
</div>

<div class="group">
<%= form.label("敘述")%>
<%= form.text_field :description%>
</div>


<div class="group">
<%= form.label("數量")%>
<%= form.text_field :quantity%>
</div>

<div class="group">
<%= form.label("上傳")%>
<%= form.file_field :image%>
</div>

<%= form.submit "submit", :disable_with=> 'Submiting....'%>
<%end%>
16 changes: 16 additions & 0 deletions app/views/admin/products/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<%= link_to "回清單",admin_products_path, class: "btn btn-primary"%>
<br><br>
<div class="row">
<div class="col-md-5">
<%= image_tag @product.image.url(:sm)%>
</div>
<div class="col-md-7">
這是[<%= @product.title %>]<br>
<%= @product.description %><br>
數量:<%= @product.quantity %><br>
</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: @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: @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: @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, autocomplete: "off" %></div>

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

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

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