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 demo #21

Open
wants to merge 3 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
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,8 @@ gem 'spring', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]

gem "devise"

gem "bootstrap-sass"

gem 'simple_form'
18 changes: 18 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ 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)
coffee-rails (4.0.1)
coffee-script (>= 2.2.0)
Expand All @@ -36,6 +39,12 @@ GEM
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 @@ -53,6 +62,7 @@ GEM
mime-types (1.25.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 +94,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 @@ -108,17 +121,22 @@ 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
coffee-rails (~> 4.0.0)
devise
jbuilder (~> 2.0)
jquery-rails
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/
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
*/
48 changes: 48 additions & 0 deletions app/controllers/admin/products_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
class Admin::ProductsController < ApplicationController

before_action :authenticate_user!
before_action :admin_required

def index
@product = Product.all
end

def new
@product = Product.new
end

def create
@product = Product.new(product_params)

if @product.save
redirect_to admin_products_path
else
render :new
end
end

def show
@product = Product.find(params[:id])
end

def edit
@product = Product.find(params[:id])
end

def update
@product = Product.find(params[:id])

if @product.update(product_params)
redirect_to admin_products_path
else
render :edit
end
end

private

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

end
6 changes: 6 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@ 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
if !current_user.admin?
redirect_to root_path
end
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
2 changes: 2 additions & 0 deletions app/models/product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Product < ActiveRecord::Base
end
9 changes: 9 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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 admin?
is_admin
end
end
6 changes: 6 additions & 0 deletions app/views/admin/products/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<%= simple_form_for [:admin, @product] do |f| %>
<%= f.input :title, label: 'Title' %>
<%= f.input :description, label: 'Description' %>
<%= f.input :quantity, label: 'Quantity' %>
<%= f.button :submit %>
<% end %>
23 changes: 23 additions & 0 deletions app/views/admin/products/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<h1>Index page</h1>
<table class="table table-bordered">
<thead>
<tr>
<th>#</th>
<th>Title</th>
<th>Description</th>
<th>Quantity</th>
<th>Modify</th>
</tr>
</thead>
<% @product.each do |p| %>
<tbody>
<tr>
<td><%= p.id %></td>
<td><%= p.title %> </td>
<td><%= p[:description] %></td>
<td><%= p.quantity %></td>
<td><%= link_to "Edit", edit_admin_product_path(p.id) %></td>
</tr>
</tbody>
<% end %>
</table>
6 changes: 6 additions & 0 deletions app/views/admin/products/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<%= simple_form_for [:admin, @product] do |f| %>
<%= f.input :title, label: 'Title' %>
<%= f.input :description, label: 'Description' %>
<%= f.input :quantity, label: 'Quantity' %>
<%= f.button :submit %>
<% end %>
5 changes: 5 additions & 0 deletions app/views/admin/products/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<%= simple_form_for [:admin, @product] do |f| %>
<%= f.input :title, label: 'Title' %>
<%= f.input :description, label: 'Description' %>
<%= f.input :quantity, label: 'Quantity' %>
<% end %>
27 changes: 17 additions & 10 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
<!DOCTYPE html>
<html>
<head>
<title>Artstore</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<head>
<title>Artstore</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>

<%= yield %>

</body>
</html>

<% if !current_user %>
<%= link_to("登入", new_user_session_path) %>
<% else %>
<%= link_to("登出", destroy_user_session_path, :method => :delete ) %>
<% end %>
<%= yield %>

</body>
</html>
25 changes: 25 additions & 0 deletions artstore.todo
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Readme:
☐ cmd + i
☐ cmd + d
☐ cmd + enter
☐ ...
身為商家的管理者,我要能夠在後台上架我的東西,並設定能夠販賣:
☐ 管理者必須要有一個後台(/admin/products/)
☐ 後台必須要可以新增產品
☐ 產品內容必須要有標題、文字、數量、圖片
身為商家的管理者,我要能夠在後台設定權限,權限分成管理者以及消費者:
☐ 身為管理者,才可以進入後台
☐ 身為管理者,必須要登入且是admin
☐ 管理者身份必須要被分為admin/user
身為消費者,我能在一開始頁面就看到商品清單:
☐ 一進入頁面就看到商品清單
Day01作業:
☐ 寫後台的User Story
☐ 實作圖片上傳 carrierwave
☐ 用Bootstrap做一個有"Navbar"的layout
☐ 使用simple_form 換掉form
☐ 實作products/show, products/index
快捷鍵:
☐ 在html.erb中,按 option + command + F 排版
☐ 在source.rb中,按 ctrl + command + K 排版
☐ ctrl + shift + K,刪除一行
Loading