From 9943f341437cea3965848b2af5af0066eed46579 Mon Sep 17 00:00:00 2001 From: FunnyQ Date: Tue, 10 Jun 2014 16:32:57 +0800 Subject: [PATCH 01/71] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20Todo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- todo | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 todo diff --git a/todo b/todo new file mode 100644 index 00000000..49cd0546 --- /dev/null +++ b/todo @@ -0,0 +1,13 @@ + ☐ 身為商家的管理者,我要能夠在後台上架我的東西,並設定能夠販賣。 + ☐ 管理者需要有一個後台 + ☐ 在後台必須可以新增商品 + ☐ 商品必須有各種必要的屬性欄位 + ☐ 商品名稱 + ☐ 商品描述 + ☐ 商品圖片 + ☐ 商品庫存數量 + + ☐ 身為商家的管理者,我要能在後台設定權限,權限分為管理者和消費者。 + ☐ 後台必須要有登入的功能 + ☐ 使用者身份分為「管理者」和「消費者」兩種 + ☐ 只有「管理者」身份的使用者可以進入後台頁面 From 340072d80da5d156880021c583481f6c08730915 Mon Sep 17 00:00:00 2001 From: FunnyQ Date: Tue, 10 Jun 2014 17:35:49 +0800 Subject: [PATCH 02/71] =?UTF-8?q?=E5=BB=BA=E7=AB=8Badmin=20=E4=B8=8B?= =?UTF-8?q?=E7=9A=84=20products=20controller,=20=E4=B8=A6=E8=A8=AD?= =?UTF-8?q?=E5=AE=9A=20routes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/assets/javascripts/admin/products.js.coffee | 3 +++ app/assets/stylesheets/admin/products.css.scss | 3 +++ app/controllers/admin/products_controller.rb | 2 ++ app/helpers/admin/products_helper.rb | 2 ++ config/routes.rb | 4 ++++ test/controllers/admin/products_controller_test.rb | 7 +++++++ test/helpers/admin/products_helper_test.rb | 4 ++++ 7 files changed, 25 insertions(+) create mode 100644 app/assets/javascripts/admin/products.js.coffee create mode 100644 app/assets/stylesheets/admin/products.css.scss create mode 100644 app/controllers/admin/products_controller.rb create mode 100644 app/helpers/admin/products_helper.rb create mode 100644 test/controllers/admin/products_controller_test.rb create mode 100644 test/helpers/admin/products_helper_test.rb diff --git a/app/assets/javascripts/admin/products.js.coffee b/app/assets/javascripts/admin/products.js.coffee new file mode 100644 index 00000000..24f83d18 --- /dev/null +++ b/app/assets/javascripts/admin/products.js.coffee @@ -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/ diff --git a/app/assets/stylesheets/admin/products.css.scss b/app/assets/stylesheets/admin/products.css.scss new file mode 100644 index 00000000..da8969d1 --- /dev/null +++ b/app/assets/stylesheets/admin/products.css.scss @@ -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/ diff --git a/app/controllers/admin/products_controller.rb b/app/controllers/admin/products_controller.rb new file mode 100644 index 00000000..b7b829ce --- /dev/null +++ b/app/controllers/admin/products_controller.rb @@ -0,0 +1,2 @@ +class Admin::ProductsController < ApplicationController +end diff --git a/app/helpers/admin/products_helper.rb b/app/helpers/admin/products_helper.rb new file mode 100644 index 00000000..977a242f --- /dev/null +++ b/app/helpers/admin/products_helper.rb @@ -0,0 +1,2 @@ +module Admin::ProductsHelper +end diff --git a/config/routes.rb b/config/routes.rb index 3f66539d..9306cd6d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,8 @@ Rails.application.routes.draw do + + namespace :admin do + resources :products + end # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". diff --git a/test/controllers/admin/products_controller_test.rb b/test/controllers/admin/products_controller_test.rb new file mode 100644 index 00000000..508fe6b7 --- /dev/null +++ b/test/controllers/admin/products_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class Admin::ProductsControllerTest < ActionController::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/helpers/admin/products_helper_test.rb b/test/helpers/admin/products_helper_test.rb new file mode 100644 index 00000000..0b696b1f --- /dev/null +++ b/test/helpers/admin/products_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class Admin::ProductsHelperTest < ActionView::TestCase +end From 92428a9e9b26f67fe35842f917f11c0bf0b5f7ae Mon Sep 17 00:00:00 2001 From: FunnyQ Date: Tue, 10 Jun 2014 18:08:15 +0800 Subject: [PATCH 03/71] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=BE=8C=E5=8F=B0?= =?UTF-8?q?=E7=9A=84=20new=20=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/admin/products_controller.rb | 21 +++++++++++++++++ app/models/product.rb | 2 ++ app/views/admin/products/new.html.erb | 16 +++++++++++++ db/migrate/20140610094630_create_products.rb | 11 +++++++++ db/schema.rb | 24 ++++++++++++++++++++ test/fixtures/products.yml | 11 +++++++++ test/models/product_test.rb | 7 ++++++ 7 files changed, 92 insertions(+) create mode 100644 app/models/product.rb create mode 100644 app/views/admin/products/new.html.erb create mode 100644 db/migrate/20140610094630_create_products.rb create mode 100644 db/schema.rb create mode 100644 test/fixtures/products.yml create mode 100644 test/models/product_test.rb diff --git a/app/controllers/admin/products_controller.rb b/app/controllers/admin/products_controller.rb index b7b829ce..c25e5789 100644 --- a/app/controllers/admin/products_controller.rb +++ b/app/controllers/admin/products_controller.rb @@ -1,2 +1,23 @@ class Admin::ProductsController < ApplicationController + + 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 + + private + + def product_params + params.require(:product).permit(:title, :description,:quantity) + end + end diff --git a/app/models/product.rb b/app/models/product.rb new file mode 100644 index 00000000..077a8197 --- /dev/null +++ b/app/models/product.rb @@ -0,0 +1,2 @@ +class Product < ActiveRecord::Base +end diff --git a/app/views/admin/products/new.html.erb b/app/views/admin/products/new.html.erb new file mode 100644 index 00000000..fed9a340 --- /dev/null +++ b/app/views/admin/products/new.html.erb @@ -0,0 +1,16 @@ +<%= form_for [:admin, @product] do |f| %> +
+ <%= f.label("標題") %> + <%= f.text_field :title %> +
+
+ <%= f.label("敘述") %> + <%= f.text_area :description %> +
+
+ <%= f.label("數量") %> + <%= f.text_field :quantity %> +
+ + <%= f.submit "Submit", :disable_with => 'Submiting...' %> +<% end %> \ No newline at end of file diff --git a/db/migrate/20140610094630_create_products.rb b/db/migrate/20140610094630_create_products.rb new file mode 100644 index 00000000..805c9766 --- /dev/null +++ b/db/migrate/20140610094630_create_products.rb @@ -0,0 +1,11 @@ +class CreateProducts < ActiveRecord::Migration + def change + create_table :products do |t| + t.string :title + t.text :description + t.integer :quantity + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 00000000..760e9c18 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,24 @@ +# encoding: UTF-8 +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# Note that this schema.rb definition is the authoritative source for your +# database schema. If you need to create the application database on another +# system, you should be using db:schema:load, not running all the migrations +# from scratch. The latter is a flawed and unsustainable approach (the more migrations +# you'll amass, the slower it'll run and the greater likelihood for issues). +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 20140610094630) do + + create_table "products", force: true do |t| + t.string "title" + t.text "description" + t.integer "quantity" + t.datetime "created_at" + t.datetime "updated_at" + end + +end diff --git a/test/fixtures/products.yml b/test/fixtures/products.yml new file mode 100644 index 00000000..c3b9bc15 --- /dev/null +++ b/test/fixtures/products.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + title: MyString + description: MyText + quantity: 1 + +two: + title: MyString + description: MyText + quantity: 1 diff --git a/test/models/product_test.rb b/test/models/product_test.rb new file mode 100644 index 00000000..211cdd0b --- /dev/null +++ b/test/models/product_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class ProductTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end From f91c02430efec6d87a5d850c5fe15ff03e81e28b Mon Sep 17 00:00:00 2001 From: FunnyQ Date: Tue, 10 Jun 2014 18:22:07 +0800 Subject: [PATCH 04/71] =?UTF-8?q?=E5=AE=8C=E6=88=90admin/products=20?= =?UTF-8?q?=E7=9A=84=20index?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/admin/products_controller.rb | 4 ++++ app/views/admin/products/index.html.erb | 7 +++++++ 2 files changed, 11 insertions(+) create mode 100644 app/views/admin/products/index.html.erb diff --git a/app/controllers/admin/products_controller.rb b/app/controllers/admin/products_controller.rb index c25e5789..3f35624b 100644 --- a/app/controllers/admin/products_controller.rb +++ b/app/controllers/admin/products_controller.rb @@ -14,6 +14,10 @@ def create end end + def index + @products = Product.all + end + private def product_params diff --git a/app/views/admin/products/index.html.erb b/app/views/admin/products/index.html.erb new file mode 100644 index 00000000..31faac96 --- /dev/null +++ b/app/views/admin/products/index.html.erb @@ -0,0 +1,7 @@ +
    + <% @products.each do |product| %> +
  • + <%= link_to(product.title, admin_products_path(product)) %> +
  • + <% end %> +
\ No newline at end of file From 3504bb783c96cddd5834f1b6c7ce85b9ec9586d0 Mon Sep 17 00:00:00 2001 From: FunnyQ Date: Tue, 10 Jun 2014 18:29:00 +0800 Subject: [PATCH 05/71] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20todo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- todo | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/todo b/todo index 49cd0546..8aac200e 100644 --- a/todo +++ b/todo @@ -1,11 +1,11 @@ ☐ 身為商家的管理者,我要能夠在後台上架我的東西,並設定能夠販賣。 - ☐ 管理者需要有一個後台 - ☐ 在後台必須可以新增商品 + ✔ 管理者需要有一個後台 @done (14-06-10 18:27) + ✔ 在後台必須可以新增商品 @done (14-06-10 18:27) ☐ 商品必須有各種必要的屬性欄位 - ☐ 商品名稱 - ☐ 商品描述 + ✔ 商品名稱 @done (14-06-10 18:27) + ✔ 商品描述 @done (14-06-10 18:27) ☐ 商品圖片 - ☐ 商品庫存數量 + ✔ 商品庫存數量 @done (14-06-10 18:27) ☐ 身為商家的管理者,我要能在後台設定權限,權限分為管理者和消費者。 ☐ 後台必須要有登入的功能 From fd13fa3524c6cf8e1e16bf78c8a4466d3180ad91 Mon Sep 17 00:00:00 2001 From: FunnyQ Date: Tue, 10 Jun 2014 20:16:02 +0800 Subject: [PATCH 06/71] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E8=80=85=E7=99=BB=E5=85=A5=E5=8A=9F=E8=83=BD=E8=88=87=E6=AC=8A?= =?UTF-8?q?=E9=99=90=E5=8D=80=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile | 2 + Gemfile.lock | 12 + app/controllers/admin/products_controller.rb | 7 + app/controllers/application_controller.rb | 7 + app/models/user.rb | 12 + app/views/layouts/application.html.erb | 6 + config/initializers/devise.rb | 252 ++++++++++++++++++ config/locales/devise.en.yml | 59 ++++ config/routes.rb | 2 + .../20140610112037_devise_create_users.rb | 46 ++++ .../20140610113852_add_is_admin_to_user.rb | 5 + db/schema.rb | 21 +- test/fixtures/users.yml | 11 + test/models/user_test.rb | 7 + todo | 8 +- 15 files changed, 452 insertions(+), 5 deletions(-) create mode 100644 app/models/user.rb create mode 100644 config/initializers/devise.rb create mode 100644 config/locales/devise.en.yml create mode 100644 db/migrate/20140610112037_devise_create_users.rb create mode 100644 db/migrate/20140610113852_add_is_admin_to_user.rb create mode 100644 test/fixtures/users.yml create mode 100644 test/models/user_test.rb diff --git a/Gemfile b/Gemfile index fba3b4e0..7e68779d 100644 --- a/Gemfile +++ b/Gemfile @@ -38,3 +38,5 @@ gem 'spring', group: :development # Use debugger # gem 'debugger', group: [:development, :test] +# Devise 提供使用者登入功能 +gem 'devise' \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 53538d9b..575bfb92 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -28,6 +28,9 @@ GEM thread_safe (~> 0.1) tzinfo (~> 1.1) arel (5.0.1.20140414130214) + bcrypt (3.1.7) + bcrypt-ruby (3.1.5) + bcrypt (>= 3.1.3) builder (3.2.2) coffee-rails (4.0.1) coffee-script (>= 2.2.0) @@ -36,6 +39,11 @@ GEM 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) @@ -53,6 +61,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) @@ -108,12 +117,15 @@ GEM uglifier (2.5.0) execjs (>= 0.3.0) json (>= 1.8.0) + warden (1.2.3) + rack (>= 1.0) PLATFORMS ruby DEPENDENCIES coffee-rails (~> 4.0.0) + devise jbuilder (~> 2.0) jquery-rails rails (= 4.1.0) diff --git a/app/controllers/admin/products_controller.rb b/app/controllers/admin/products_controller.rb index 3f35624b..80cb13e2 100644 --- a/app/controllers/admin/products_controller.rb +++ b/app/controllers/admin/products_controller.rb @@ -1,5 +1,12 @@ class Admin::ProductsController < ApplicationController + # 使用者必須先登入 + before_action :authenticate_user! + + # 使用者的身份必須是 admin + # admin_required 在 applicationController 裡定義 + before_action :admin_required + def new @product = Product.new end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d83690e1..95c15b73 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -2,4 +2,11 @@ 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 + # 目前的使用者身份是否為 admin? + # admin? 在 user.rb 定義 + current_user.admin? + end + end diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 00000000..9375287a --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,12 @@ +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 diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index da9769de..ab2e355f 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -8,6 +8,12 @@ + <% if !current_user %> + <%= link_to("登入", new_user_session_path) %> + <% else %> + <%= link_to("登出", destroy_user_session_path, :method => :delete) %> + <% end %> + <%= yield %> diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb new file mode 100644 index 00000000..ef3a3c7c --- /dev/null +++ b/config/initializers/devise.rb @@ -0,0 +1,252 @@ +# Use this hook to configure devise mailer, warden hooks and so forth. +# Many of these configuration options can be set straight in your model. +Devise.setup do |config| + # ==> Mailer Configuration + # Configure the e-mail address which will be shown in Devise::Mailer, + # note that it will be overwritten if you use your own mailer class with default "from" parameter. + config.mailer_sender = "please-change-me-at-config-initializers-devise@example.com" + + # Configure the class responsible to send e-mails. + # config.mailer = "Devise::Mailer" + + # ==> ORM configuration + # Load and configure the ORM. Supports :active_record (default) and + # :mongoid (bson_ext recommended) by default. Other ORMs may be + # available as additional gems. + require 'devise/orm/active_record' + + # ==> Configuration for any authentication mechanism + # Configure which keys are used when authenticating a user. The default is + # just :email. You can configure it to use [:username, :subdomain], so for + # authenticating a user, both parameters are required. Remember that those + # parameters are used only when authenticating and not when retrieving from + # session. If you need permissions, you should implement that in a before filter. + # You can also supply a hash where the value is a boolean determining whether + # or not authentication should be aborted when the value is not present. + # config.authentication_keys = [ :email ] + + # Configure parameters from the request object used for authentication. Each entry + # given should be a request method and it will automatically be passed to the + # find_for_authentication method and considered in your model lookup. For instance, + # if you set :request_keys to [:subdomain], :subdomain will be used on authentication. + # The same considerations mentioned for authentication_keys also apply to request_keys. + # config.request_keys = [] + + # Configure which authentication keys should be case-insensitive. + # These keys will be downcased upon creating or modifying a user and when used + # to authenticate or find a user. Default is :email. + config.case_insensitive_keys = [ :email ] + + # Configure which authentication keys should have whitespace stripped. + # These keys will have whitespace before and after removed upon creating or + # modifying a user and when used to authenticate or find a user. Default is :email. + config.strip_whitespace_keys = [ :email ] + + # Tell if authentication through request.params is enabled. True by default. + # It can be set to an array that will enable params authentication only for the + # given strategies, for example, `config.params_authenticatable = [:database]` will + # enable it only for database (email + password) authentication. + # config.params_authenticatable = true + + # Tell if authentication through HTTP Auth is enabled. False by default. + # It can be set to an array that will enable http authentication only for the + # given strategies, for example, `config.http_authenticatable = [:token]` will + # enable it only for token authentication. The supported strategies are: + # :database = Support basic authentication with authentication key + password + # :token = Support basic authentication with token authentication key + # :token_options = Support token authentication with options as defined in + # http://api.rubyonrails.org/classes/ActionController/HttpAuthentication/Token.html + # config.http_authenticatable = false + + # If http headers should be returned for AJAX requests. True by default. + # config.http_authenticatable_on_xhr = true + + # The realm used in Http Basic Authentication. "Application" by default. + # config.http_authentication_realm = "Application" + + # It will change confirmation, password recovery and other workflows + # to behave the same regardless if the e-mail provided was right or wrong. + # Does not affect registerable. + # config.paranoid = true + + # By default Devise will store the user in session. You can skip storage for + # :http_auth and :token_auth by adding those symbols to the array below. + # Notice that if you are skipping storage for all authentication paths, you + # may want to disable generating routes to Devise's sessions controller by + # passing :skip => :sessions to `devise_for` in your config/routes.rb + config.skip_session_storage = [:http_auth] + + # By default, Devise cleans up the CSRF token on authentication to + # avoid CSRF token fixation attacks. This means that, when using AJAX + # requests for sign in and sign up, you need to get a new CSRF token + # from the server. You can disable this option at your own risk. + # config.clean_up_csrf_token_on_authentication = true + + # ==> Configuration for :database_authenticatable + # For bcrypt, this is the cost for hashing the password and defaults to 10. If + # using other encryptors, it sets how many times you want the password re-encrypted. + # + # Limiting the stretches to just one in testing will increase the performance of + # your test suite dramatically. However, it is STRONGLY RECOMMENDED to not use + # a value less than 10 in other environments. + config.stretches = Rails.env.test? ? 1 : 10 + + # Setup a pepper to generate the encrypted password. + # config.pepper = "37dc20c6b7be0969ab82faba55fc0b386a1f916703955e86242ddd87ffd3ed40cb63b1d4c5c60902053c5ecbd3b2bc3d0fc2c6857b98c11f27c99fa4e3adb82f" + + # ==> Configuration for :confirmable + # A period that the user is allowed to access the website even without + # confirming his account. For instance, if set to 2.days, the user will be + # able to access the website for two days without confirming his account, + # access will be blocked just in the third day. Default is 0.days, meaning + # the user cannot access the website without confirming his account. + # config.allow_unconfirmed_access_for = 2.days + + # A period that the user is allowed to confirm their account before their + # token becomes invalid. For example, if set to 3.days, the user can confirm + # their account within 3 days after the mail was sent, but on the fourth day + # their account can't be confirmed with the token any more. + # Default is nil, meaning there is no restriction on how long a user can take + # before confirming their account. + # config.confirm_within = 3.days + + # If true, requires any email changes to be confirmed (exactly the same way as + # initial account confirmation) to be applied. Requires additional unconfirmed_email + # db field (see migrations). Until confirmed new email is stored in + # unconfirmed email column, and copied to email column on successful confirmation. + config.reconfirmable = true + + # Defines which key will be used when confirming an account + # config.confirmation_keys = [ :email ] + + # ==> Configuration for :rememberable + # The time the user will be remembered without asking for credentials again. + # config.remember_for = 2.weeks + + # If true, extends the user's remember period when remembered via cookie. + # config.extend_remember_period = false + + # Options to be passed to the created cookie. For instance, you can set + # :secure => true in order to force SSL only cookies. + # config.rememberable_options = {} + + # ==> Configuration for :validatable + # Range for password length. Default is 8..128. + config.password_length = 8..128 + + # Email regex used to validate email formats. It simply asserts that + # one (and only one) @ exists in the given string. This is mainly + # to give user feedback and not to assert the e-mail validity. + # config.email_regexp = /\A[^@]+@[^@]+\z/ + + # ==> Configuration for :timeoutable + # The time you want to timeout the user session without activity. After this + # time the user will be asked for credentials again. Default is 30 minutes. + # config.timeout_in = 30.minutes + + # If true, expires auth token on session timeout. + # config.expire_auth_token_on_timeout = false + + # ==> Configuration for :lockable + # Defines which strategy will be used to lock an account. + # :failed_attempts = Locks an account after a number of failed attempts to sign in. + # :none = No lock strategy. You should handle locking by yourself. + # config.lock_strategy = :failed_attempts + + # Defines which key will be used when locking and unlocking an account + # config.unlock_keys = [ :email ] + + # Defines which strategy will be used to unlock an account. + # :email = Sends an unlock link to the user email + # :time = Re-enables login after a certain amount of time (see :unlock_in below) + # :both = Enables both strategies + # :none = No unlock strategy. You should handle unlocking by yourself. + # config.unlock_strategy = :both + + # Number of authentication tries before locking an account if lock_strategy + # is failed attempts. + # config.maximum_attempts = 20 + + # Time interval to unlock the account if :time is enabled as unlock_strategy. + # config.unlock_in = 1.hour + + # ==> Configuration for :recoverable + # + # Defines which key will be used when recovering the password for an account + # config.reset_password_keys = [ :email ] + + # Time interval you can reset your password with a reset password key. + # Don't put a too small interval or your users won't have the time to + # change their passwords. + config.reset_password_within = 6.hours + + # ==> Configuration for :encryptable + # Allow you to use another encryption algorithm besides bcrypt (default). You can use + # :sha1, :sha512 or encryptors from others authentication tools as :clearance_sha1, + # :authlogic_sha512 (then you should set stretches above to 20 for default behavior) + # and :restful_authentication_sha1 (then you should set stretches to 10, and copy + # REST_AUTH_SITE_KEY to pepper). + # + # Require the `devise-encryptable` gem when using anything other than bcrypt + # config.encryptor = :sha512 + + # ==> Configuration for :token_authenticatable + # Defines name of the authentication token params key + # config.token_authentication_key = :auth_token + + # ==> Scopes configuration + # Turn scoped views on. Before rendering "sessions/new", it will first check for + # "users/sessions/new". It's turned off by default because it's slower if you + # are using only default views. + # config.scoped_views = false + + # Configure the default scope given to Warden. By default it's the first + # devise role declared in your routes (usually :user). + # config.default_scope = :user + + # Set this configuration to false if you want /users/sign_out to sign out + # only the current scope. By default, Devise signs out all scopes. + # config.sign_out_all_scopes = true + + # ==> Navigation configuration + # Lists the formats that should be treated as navigational. Formats like + # :html, should redirect to the sign in page when the user does not have + # access, but formats like :xml or :json, should return 401. + # + # If you have any extra navigational formats, like :iphone or :mobile, you + # should add them to the navigational formats lists. + # + # The "*/*" below is required to match Internet Explorer requests. + # config.navigational_formats = ["*/*", :html] + + # The default HTTP method used to sign out a resource. Default is :delete. + config.sign_out_via = :delete + + # ==> OmniAuth + # Add a new OmniAuth provider. Check the wiki for more information on setting + # up on your models and hooks. + # config.omniauth :github, 'APP_ID', 'APP_SECRET', :scope => 'user,public_repo' + + # ==> Warden configuration + # If you want to use other strategies, that are not supported by Devise, or + # change the failure app, you can configure them inside the config.warden block. + # + # config.warden do |manager| + # manager.intercept_401 = false + # manager.default_strategies(:scope => :user).unshift :some_external_strategy + # end + + # ==> Mountable engine configurations + # When using Devise inside an engine, let's call it `MyEngine`, and this engine + # is mountable, there are some extra configurations to be taken into account. + # The following options are available, assuming the engine is mounted as: + # + # mount MyEngine, at: "/my_engine" + # + # The router that invoked `devise_for`, in the example above, would be: + # config.router_name = :my_engine + # + # When using omniauth, Devise cannot automatically set Omniauth path, + # so you need to do it manually. For the users scope, it would be: + # config.omniauth_path_prefix = "/my_engine/users/auth" +end diff --git a/config/locales/devise.en.yml b/config/locales/devise.en.yml new file mode 100644 index 00000000..d01f375c --- /dev/null +++ b/config/locales/devise.en.yml @@ -0,0 +1,59 @@ +# Additional translations at https://github.com/plataformatec/devise/wiki/I18n + +en: + devise: + confirmations: + confirmed: "Your account was successfully confirmed. You are now signed in." + send_instructions: "You will receive an email with instructions about how to confirm your account in a few minutes." + send_paranoid_instructions: "If your email address exists in our database, you will receive an email with instructions about how to confirm your account in a few minutes." + failure: + already_authenticated: "You are already signed in." + inactive: "Your account was not activated yet." + invalid: "Invalid email or password." + invalid_token: "Invalid authentication token." + locked: "Your account is locked." + not_found_in_database: "Invalid email or password." + timeout: "Your session expired, please sign in again to continue." + unauthenticated: "You need to sign in or sign up before continuing." + unconfirmed: "You have to confirm your account before continuing." + mailer: + confirmation_instructions: + subject: "Confirmation instructions" + reset_password_instructions: + subject: "Reset password instructions" + unlock_instructions: + subject: "Unlock Instructions" + omniauth_callbacks: + failure: "Could not authenticate you from %{kind} because \"%{reason}\"." + success: "Successfully authenticated from %{kind} account." + passwords: + no_token: "You can't access this page without coming from a password reset email. If you do come from a password reset email, please make sure you used the full URL provided." + send_instructions: "You will receive an email with instructions about how to reset your password in a few minutes." + send_paranoid_instructions: "If your email address exists in our database, you will receive a password recovery link at your email address in a few minutes." + updated: "Your password was changed successfully. You are now signed in." + updated_not_active: "Your password was changed successfully." + registrations: + destroyed: "Bye! Your account was successfully cancelled. We hope to see you again soon." + signed_up: "Welcome! You have signed up successfully." + signed_up_but_inactive: "You have signed up successfully. However, we could not sign you in because your account is not yet activated." + signed_up_but_locked: "You have signed up successfully. However, we could not sign you in because your account is locked." + signed_up_but_unconfirmed: "A message with a confirmation link has been sent to your email address. Please open the link to activate your account." + update_needs_confirmation: "You updated your account successfully, but we need to verify your new email address. Please check your email and click on the confirm link to finalize confirming your new email address." + updated: "You updated your account successfully." + sessions: + signed_in: "Signed in successfully." + signed_out: "Signed out successfully." + unlocks: + send_instructions: "You will receive an email with instructions about how to unlock your account in a few minutes." + send_paranoid_instructions: "If your account exists, you will receive an email with instructions about how to unlock it in a few minutes." + unlocked: "Your account has been unlocked successfully. Please sign in to continue." + errors: + messages: + already_confirmed: "was already confirmed, please try signing in" + confirmation_period_expired: "needs to be confirmed within %{period}, please request a new one" + expired: "has expired, please request a new one" + not_found: "not found" + not_locked: "was not locked" + not_saved: + one: "1 error prohibited this %{resource} from being saved:" + other: "%{count} errors prohibited this %{resource} from being saved:" diff --git a/config/routes.rb b/config/routes.rb index 9306cd6d..57f4bce2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,7 @@ Rails.application.routes.draw do + devise_for :users + namespace :admin do resources :products end diff --git a/db/migrate/20140610112037_devise_create_users.rb b/db/migrate/20140610112037_devise_create_users.rb new file mode 100644 index 00000000..2099d998 --- /dev/null +++ b/db/migrate/20140610112037_devise_create_users.rb @@ -0,0 +1,46 @@ +class DeviseCreateUsers < ActiveRecord::Migration + def change + create_table(:users) 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 + 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 # Only if lock strategy is :failed_attempts + # t.string :unlock_token # Only if unlock strategy is :email or :both + # t.datetime :locked_at + + ## Token authenticatable + # t.string :authentication_token + + + t.timestamps + end + + add_index :users, :email, :unique => true + add_index :users, :reset_password_token, :unique => true + # add_index :users, :confirmation_token, :unique => true + # add_index :users, :unlock_token, :unique => true + # add_index :users, :authentication_token, :unique => true + end +end diff --git a/db/migrate/20140610113852_add_is_admin_to_user.rb b/db/migrate/20140610113852_add_is_admin_to_user.rb new file mode 100644 index 00000000..1ef84cff --- /dev/null +++ b/db/migrate/20140610113852_add_is_admin_to_user.rb @@ -0,0 +1,5 @@ +class AddIsAdminToUser < ActiveRecord::Migration + def change + add_column :users, :is_admin, :boolean, :default => false + end +end diff --git a/db/schema.rb b/db/schema.rb index 760e9c18..265247a0 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20140610094630) do +ActiveRecord::Schema.define(version: 20140610113852) do create_table "products", force: true do |t| t.string "title" @@ -21,4 +21,23 @@ t.datetime "updated_at" end + create_table "users", force: true do |t| + t.string "email", default: "", null: false + t.string "encrypted_password", default: "", null: false + t.string "reset_password_token" + t.datetime "reset_password_sent_at" + t.datetime "remember_created_at" + t.integer "sign_in_count", default: 0 + t.datetime "current_sign_in_at" + t.datetime "last_sign_in_at" + t.string "current_sign_in_ip" + t.string "last_sign_in_ip" + t.datetime "created_at" + t.datetime "updated_at" + t.boolean "is_admin", default: false + end + + add_index "users", ["email"], name: "index_users_on_email", unique: true + add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true + end diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml new file mode 100644 index 00000000..937a0c00 --- /dev/null +++ b/test/fixtures/users.yml @@ -0,0 +1,11 @@ +# 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 diff --git a/test/models/user_test.rb b/test/models/user_test.rb new file mode 100644 index 00000000..82f61e01 --- /dev/null +++ b/test/models/user_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class UserTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/todo b/todo index 8aac200e..581cc38f 100644 --- a/todo +++ b/todo @@ -7,7 +7,7 @@ ☐ 商品圖片 ✔ 商品庫存數量 @done (14-06-10 18:27) - ☐ 身為商家的管理者,我要能在後台設定權限,權限分為管理者和消費者。 - ☐ 後台必須要有登入的功能 - ☐ 使用者身份分為「管理者」和「消費者」兩種 - ☐ 只有「管理者」身份的使用者可以進入後台頁面 + ✔ 身為商家的管理者,我要能在後台設定權限,權限分為管理者和消費者。 @done (14-06-10 20:14) + ✔ 後台必須要有登入的功能 @done (14-06-10 20:14) + ✔ 使用者身份分為「管理者」和「消費者」兩種 @done (14-06-10 20:14) + ✔ 只有「管理者」身份的使用者可以進入後台頁面 @done (14-06-10 20:14) From 77d426190ded80379a0cb51ae3e2f3e47147c16c Mon Sep 17 00:00:00 2001 From: FunnyQ Date: Tue, 10 Jun 2014 21:23:09 +0800 Subject: [PATCH 07/71] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20simple=20form=20gem,?= =?UTF-8?q?=20=E6=8A=8A=20admin/products/new=20=E7=9A=84=20form=5Ffor=20?= =?UTF-8?q?=E6=94=B9=E6=88=90=20simple=5Fform=5Ffor.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile | 6 +- Gemfile.lock | 7 + app/assets/stylesheets/application.css | 1 + app/views/admin/products/new.html.erb | 12 +- config/initializers/simple_form.rb | 145 +++++++++++++++++++ config/initializers/simple_form_bootstrap.rb | 45 ++++++ config/locales/simple_form.en.yml | 26 ++++ lib/templates/erb/scaffold/_form.html.erb | 13 ++ 8 files changed, 253 insertions(+), 2 deletions(-) create mode 100644 config/initializers/simple_form.rb create mode 100644 config/initializers/simple_form_bootstrap.rb create mode 100644 config/locales/simple_form.en.yml create mode 100644 lib/templates/erb/scaffold/_form.html.erb diff --git a/Gemfile b/Gemfile index 7e68779d..1c371acb 100644 --- a/Gemfile +++ b/Gemfile @@ -39,4 +39,8 @@ gem 'spring', group: :development # gem 'debugger', group: [:development, :test] # Devise 提供使用者登入功能 -gem 'devise' \ No newline at end of file +gem 'devise' +# 加入 bootstrap css framework +gem 'bootstrap-sass' +# Rails forms made easy +gem 'simple_form' \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 575bfb92..455fdf07 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -31,6 +31,8 @@ GEM bcrypt (3.1.7) bcrypt-ruby (3.1.5) bcrypt (>= 3.1.3) + bootstrap-sass (3.1.1.1) + sass (~> 3.2) builder (3.2.2) coffee-rails (4.0.1) coffee-script (>= 2.2.0) @@ -93,6 +95,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) @@ -124,6 +129,7 @@ PLATFORMS ruby DEPENDENCIES + bootstrap-sass coffee-rails (~> 4.0.0) devise jbuilder (~> 2.0) @@ -131,6 +137,7 @@ DEPENDENCIES rails (= 4.1.0) sass-rails (~> 4.0.3) sdoc (~> 0.4.0) + simple_form spring sqlite3 turbolinks diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index a443db34..bcdb41c0 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -12,4 +12,5 @@ * *= require_tree . *= require_self + *= require bootstrap */ diff --git a/app/views/admin/products/new.html.erb b/app/views/admin/products/new.html.erb index fed9a340..dcec482a 100644 --- a/app/views/admin/products/new.html.erb +++ b/app/views/admin/products/new.html.erb @@ -1,3 +1,12 @@ + +<%= simple_form_for [:admin, @product] do |f| %> + <%= f.input :title, label: '標題' %> + <%= f.input :description, label: '產品敘述' %> + <%= f.input :quantity, label: '數量' %> +<% end %> + + + \ No newline at end of file diff --git a/config/initializers/simple_form.rb b/config/initializers/simple_form.rb new file mode 100644 index 00000000..6986de98 --- /dev/null +++ b/config/initializers/simple_form.rb @@ -0,0 +1,145 @@ +# Use this setup block to configure all options available in SimpleForm. +SimpleForm.setup do |config| + # Wrappers are used by the form builder to generate a + # complete input. You can remove any component from the + # wrapper, change the order or even add your own to the + # stack. The options given below are used to wrap the + # whole input. + config.wrappers :default, class: :input, + hint_class: :field_with_hint, error_class: :field_with_errors do |b| + ## Extensions enabled by default + # Any of these extensions can be disabled for a + # given input by passing: `f.input EXTENSION_NAME => false`. + # You can make any of these extensions optional by + # renaming `b.use` to `b.optional`. + + # Determines whether to use HTML5 (:email, :url, ...) + # and required attributes + b.use :html5 + + # Calculates placeholders automatically from I18n + # You can also pass a string as f.input placeholder: "Placeholder" + b.use :placeholder + + ## Optional extensions + # They are disabled unless you pass `f.input EXTENSION_NAME => :lookup` + # to the input. If so, they will retrieve the values from the model + # if any exists. If you want to enable the lookup for any of those + # extensions by default, you can change `b.optional` to `b.use`. + + # Calculates maxlength from length validations for string inputs + b.optional :maxlength + + # Calculates pattern from format validations for string inputs + b.optional :pattern + + # Calculates min and max from length validations for numeric inputs + b.optional :min_max + + # Calculates readonly automatically from readonly attributes + b.optional :readonly + + ## Inputs + b.use :label_input + b.use :hint, wrap_with: { tag: :span, class: :hint } + b.use :error, wrap_with: { tag: :span, class: :error } + end + + # The default wrapper to be used by the FormBuilder. + config.default_wrapper = :default + + # Define the way to render check boxes / radio buttons with labels. + # Defaults to :nested for bootstrap config. + # inline: input + label + # nested: label > input + config.boolean_style = :nested + + # Default class for buttons + config.button_class = 'btn' + + # Method used to tidy up errors. Specify any Rails Array method. + # :first lists the first message for each field. + # Use :to_sentence to list all errors for each field. + # config.error_method = :first + + # Default tag used for error notification helper. + config.error_notification_tag = :div + + # CSS class to add for error notification helper. + config.error_notification_class = 'alert alert-error' + + # ID to add for error notification helper. + # config.error_notification_id = nil + + # Series of attempts to detect a default label method for collection. + # config.collection_label_methods = [ :to_label, :name, :title, :to_s ] + + # Series of attempts to detect a default value method for collection. + # config.collection_value_methods = [ :id, :to_s ] + + # You can wrap a collection of radio/check boxes in a pre-defined tag, defaulting to none. + # config.collection_wrapper_tag = nil + + # You can define the class to use on all collection wrappers. Defaulting to none. + # config.collection_wrapper_class = nil + + # You can wrap each item in a collection of radio/check boxes with a tag, + # defaulting to :span. Please note that when using :boolean_style = :nested, + # SimpleForm will force this option to be a label. + # config.item_wrapper_tag = :span + + # You can define a class to use in all item wrappers. Defaulting to none. + # config.item_wrapper_class = nil + + # How the label text should be generated altogether with the required text. + # config.label_text = lambda { |label, required| "#{required} #{label}" } + + # You can define the class to use on all labels. Default is nil. + config.label_class = 'control-label' + + # You can define the class to use on all forms. Default is simple_form. + # config.form_class = :simple_form + + # You can define which elements should obtain additional classes + # config.generate_additional_classes_for = [:wrapper, :label, :input] + + # Whether attributes are required by default (or not). Default is true. + # config.required_by_default = true + + # Tell browsers whether to use the native HTML5 validations (novalidate form option). + # These validations are enabled in SimpleForm's internal config but disabled by default + # in this configuration, which is recommended due to some quirks from different browsers. + # To stop SimpleForm from generating the novalidate option, enabling the HTML5 validations, + # change this configuration to true. + config.browser_validations = false + + # Collection of methods to detect if a file type was given. + # config.file_methods = [ :mounted_as, :file?, :public_filename ] + + # Custom mappings for input types. This should be a hash containing a regexp + # to match as key, and the input type that will be used when the field name + # matches the regexp as value. + # config.input_mappings = { /count/ => :integer } + + # Custom wrappers for input types. This should be a hash containing an input + # type as key and the wrapper that will be used for all inputs with specified type. + # config.wrapper_mappings = { string: :prepend } + + # Default priority for time_zone inputs. + # config.time_zone_priority = nil + + # Default priority for country inputs. + # config.country_priority = nil + + # When false, do not use translations for labels. + # config.translate_labels = true + + # Automatically discover new inputs in Rails' autoload path. + # config.inputs_discovery = true + + # Cache SimpleForm inputs discovery + # config.cache_discovery = !Rails.env.development? + + # Default class for inputs + # config.input_class = nil +end diff --git a/config/initializers/simple_form_bootstrap.rb b/config/initializers/simple_form_bootstrap.rb new file mode 100644 index 00000000..ad4ca74c --- /dev/null +++ b/config/initializers/simple_form_bootstrap.rb @@ -0,0 +1,45 @@ +# Use this setup block to configure all options available in SimpleForm. +SimpleForm.setup do |config| + config.wrappers :bootstrap, tag: 'div', class: 'control-group', error_class: 'error' do |b| + b.use :html5 + b.use :placeholder + b.use :label + b.wrapper tag: 'div', class: 'controls' do |ba| + ba.use :input + ba.use :error, wrap_with: { tag: 'span', class: 'help-inline' } + ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' } + end + end + + config.wrappers :prepend, tag: 'div', class: "control-group", error_class: 'error' do |b| + b.use :html5 + b.use :placeholder + b.use :label + b.wrapper tag: 'div', class: 'controls' do |input| + input.wrapper tag: 'div', class: 'input-prepend' do |prepend| + prepend.use :input + end + input.use :hint, wrap_with: { tag: 'span', class: 'help-block' } + input.use :error, wrap_with: { tag: 'span', class: 'help-inline' } + end + end + + config.wrappers :append, tag: 'div', class: "control-group", error_class: 'error' do |b| + b.use :html5 + b.use :placeholder + b.use :label + b.wrapper tag: 'div', class: 'controls' do |input| + input.wrapper tag: 'div', class: 'input-append' do |append| + append.use :input + end + input.use :hint, wrap_with: { tag: 'span', class: 'help-block' } + input.use :error, wrap_with: { tag: 'span', class: 'help-inline' } + end + end + + # Wrappers for forms and inputs using the Twitter Bootstrap toolkit. + # Check the Bootstrap docs (http://twitter.github.com/bootstrap) + # to learn about the different styles for forms and inputs, + # buttons and other elements. + config.default_wrapper = :bootstrap +end diff --git a/config/locales/simple_form.en.yml b/config/locales/simple_form.en.yml new file mode 100644 index 00000000..0df11fe0 --- /dev/null +++ b/config/locales/simple_form.en.yml @@ -0,0 +1,26 @@ +en: + simple_form: + "yes": 'Yes' + "no": 'No' + required: + text: 'required' + mark: '*' + # You can uncomment the line below if you need to overwrite the whole required html. + # When using html, text and mark won't be used. + # html: '*' + error_notification: + default_message: "Please review the problems below:" + # Labels and hints examples + # labels: + # defaults: + # password: 'Password' + # user: + # new: + # email: 'E-mail to sign in.' + # edit: + # email: 'E-mail.' + # hints: + # defaults: + # username: 'User name to sign in.' + # password: 'No special characters, please.' + diff --git a/lib/templates/erb/scaffold/_form.html.erb b/lib/templates/erb/scaffold/_form.html.erb new file mode 100644 index 00000000..201a069e --- /dev/null +++ b/lib/templates/erb/scaffold/_form.html.erb @@ -0,0 +1,13 @@ +<%%= simple_form_for(@<%= singular_table_name %>) do |f| %> + <%%= f.error_notification %> + +
+ <%- attributes.each do |attribute| -%> + <%%= f.<%= attribute.reference? ? :association : :input %> :<%= attribute.name %> %> + <%- end -%> +
+ +
+ <%%= f.button :submit %> +
+<%% end %> From 41e2e038aa4c4f16d3281c955c36e51c980a7c36 Mon Sep 17 00:00:00 2001 From: FunnyQ Date: Tue, 10 Jun 2014 21:39:32 +0800 Subject: [PATCH 08/71] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20simple=5Fform=5Fboot?= =?UTF-8?q?strap.rb=20=E7=82=BA=20bootstrap=203=20=E9=81=A9=E7=94=A8?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/initializers/simple_form_bootstrap.rb | 84 +++++++++++++++----- 1 file changed, 65 insertions(+), 19 deletions(-) diff --git a/config/initializers/simple_form_bootstrap.rb b/config/initializers/simple_form_bootstrap.rb index ad4ca74c..cb31bf7f 100644 --- a/config/initializers/simple_form_bootstrap.rb +++ b/config/initializers/simple_form_bootstrap.rb @@ -1,45 +1,91 @@ +inputs = %w[ + CollectionSelectInput + DateTimeInput + FileInput + GroupedCollectionSelectInput + NumericInput + PasswordInput + RangeInput + StringInput + TextInput +] + +inputs.each do |input_type| + superclass = "SimpleForm::Inputs::#{input_type}".constantize + + new_class = Class.new(superclass) do + def input_html_classes + super.push('form-control') + end + end + + Object.const_set(input_type, new_class) +end + # Use this setup block to configure all options available in SimpleForm. SimpleForm.setup do |config| - config.wrappers :bootstrap, tag: 'div', class: 'control-group', error_class: 'error' do |b| + config.boolean_style = :nested + + config.wrappers :bootstrap3, tag: 'div', class: 'form-group', error_class: 'has-error', + defaults: { input_html: { class: 'default_class' } } do |b| + b.use :html5 + b.use :min_max + b.use :maxlength b.use :placeholder - b.use :label - b.wrapper tag: 'div', class: 'controls' do |ba| - ba.use :input - ba.use :error, wrap_with: { tag: 'span', class: 'help-inline' } - ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' } - end + + b.optional :pattern + b.optional :readonly + + b.use :label_input + b.use :hint, wrap_with: { tag: 'span', class: 'help-block' } + b.use :error, wrap_with: { tag: 'span', class: 'help-block has-error' } end - config.wrappers :prepend, tag: 'div', class: "control-group", error_class: 'error' do |b| + config.wrappers :prepend, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| b.use :html5 b.use :placeholder - b.use :label b.wrapper tag: 'div', class: 'controls' do |input| - input.wrapper tag: 'div', class: 'input-prepend' do |prepend| + input.wrapper tag: 'div', class: 'input-group' do |prepend| + prepend.use :label , class: 'input-group-addon' ###Please note setting class here fro the label does not currently work (let me know if you know a workaround as this is the final hurdle) prepend.use :input end input.use :hint, wrap_with: { tag: 'span', class: 'help-block' } - input.use :error, wrap_with: { tag: 'span', class: 'help-inline' } + input.use :error, wrap_with: { tag: 'span', class: 'help-block has-error' } end end - config.wrappers :append, tag: 'div', class: "control-group", error_class: 'error' do |b| + config.wrappers :append, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| b.use :html5 b.use :placeholder - b.use :label b.wrapper tag: 'div', class: 'controls' do |input| - input.wrapper tag: 'div', class: 'input-append' do |append| - append.use :input + input.wrapper tag: 'div', class: 'input-group' do |prepend| + prepend.use :input + prepend.use :label , class: 'input-group-addon' ###Please note setting class here fro the label does not currently work (let me know if you know a workaround as this is the final hurdle) end input.use :hint, wrap_with: { tag: 'span', class: 'help-block' } - input.use :error, wrap_with: { tag: 'span', class: 'help-inline' } + input.use :error, wrap_with: { tag: 'span', class: 'help-block has-error' } end end + config.wrappers :checkbox, tag: :div, class: "checkbox", error_class: "has-error" do |b| + + # Form extensions + b.use :html5 + + # Form components + b.wrapper tag: :label do |ba| + ba.use :input + ba.use :label_text + end + + b.use :hint, wrap_with: { tag: :p, class: "help-block" } + b.use :error, wrap_with: { tag: :span, class: "help-block text-danger" } + end + # Wrappers for forms and inputs using the Twitter Bootstrap toolkit. - # Check the Bootstrap docs (http://twitter.github.com/bootstrap) + # Check the Bootstrap docs (http://getbootstrap.com/) # to learn about the different styles for forms and inputs, # buttons and other elements. - config.default_wrapper = :bootstrap -end + config.default_wrapper = :bootstrap3 +end \ No newline at end of file From 111f34256b59a819e2de71a660f4b3d1b967a80f Mon Sep 17 00:00:00 2001 From: FunnyQ Date: Tue, 10 Jun 2014 21:57:02 +0800 Subject: [PATCH 09/71] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20todo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- todo | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/todo b/todo index 581cc38f..c6b182b5 100644 --- a/todo +++ b/todo @@ -11,3 +11,10 @@ ✔ 後台必須要有登入的功能 @done (14-06-10 20:14) ✔ 使用者身份分為「管理者」和「消費者」兩種 @done (14-06-10 20:14) ✔ 只有「管理者」身份的使用者可以進入後台頁面 @done (14-06-10 20:14) + + ☐ Week1 Homework + ☐ 寫後台的 User Story + ☐ 實作圖片上傳 + ☐ 用 Bootstrap 做一個「有 navbar」的 layout + ✔ 使用 simple_form 換掉 form_for @done (14-06-10 21:44) + ☐ products/show, products, index 實作 \ No newline at end of file From ad6a554e86e99cd0d52a2b2e5d2e1408861d37ab Mon Sep 17 00:00:00 2001 From: FunnyQ Date: Fri, 13 Jun 2014 13:13:15 +0800 Subject: [PATCH 10/71] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20Todo=E3=80=81mount?= =?UTF-8?q?=20bootstrap=20javascript=E3=80=81=E6=8A=8A=20application.css?= =?UTF-8?q?=20=E6=94=B9=E6=88=90=20scss?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/assets/javascripts/application.js | 1 + .../{application.css => application.css.scss} | 2 +- todo | 27 ++++++++++++++----- 3 files changed, 23 insertions(+), 7 deletions(-) rename app/assets/stylesheets/{application.css => application.css.scss} (99%) diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index d6925fa4..77431764 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -14,3 +14,4 @@ //= require jquery_ujs //= require turbolinks //= require_tree . +//= require bootstrap diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css.scss similarity index 99% rename from app/assets/stylesheets/application.css rename to app/assets/stylesheets/application.css.scss index bcdb41c0..44ca45cc 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css.scss @@ -13,4 +13,4 @@ *= require_tree . *= require_self *= require bootstrap - */ + */ \ No newline at end of file diff --git a/todo b/todo index c6b182b5..2bc4fac9 100644 --- a/todo +++ b/todo @@ -12,9 +12,24 @@ ✔ 使用者身份分為「管理者」和「消費者」兩種 @done (14-06-10 20:14) ✔ 只有「管理者」身份的使用者可以進入後台頁面 @done (14-06-10 20:14) - ☐ Week1 Homework - ☐ 寫後台的 User Story - ☐ 實作圖片上傳 - ☐ 用 Bootstrap 做一個「有 navbar」的 layout - ✔ 使用 simple_form 換掉 form_for @done (14-06-10 21:44) - ☐ products/show, products, index 實作 \ No newline at end of file +[ 後台 ] + ☐ 管理者登入後必須要有選單,以選擇想要進行的作業 + ☐ 管理商品 + ☐ 管理訂單 + ☐ 訂單必須有「未結賬」、「已結賬」、「出貨中」、「已出貨(消費者收到商品)」、「退貨申請」、「已退貨(已收到退貨商品)」等狀態。 + ☐ 消費者結賬後必須收到 mail 確認 + ☐ 商品出貨後消費者也必須收到 mail 確認 + ☐ 商品出貨後,該商品的庫存數量必須隨出貨量變更 + ☐ 管理者應該要可以管理訂單狀態 + +[ 前台 ] + ☐ 消費者應該要能看到所有商品 + + +[ 作業 ] + ☐ Week1 Homework + ☐ 寫後台的 User Story + ☐ 實作圖片上傳 + ☐ 用 Bootstrap 做一個「有 navbar」的 layout + ✔ 使用 simple_form 換掉 form_for @done (14-06-10 21:44) + ☐ products/show, products, index 實作 \ No newline at end of file From 105d5622be062e5de6f5fc985178ef921c6fd2b8 Mon Sep 17 00:00:00 2001 From: FunnyQ Date: Fri, 13 Jun 2014 15:05:38 +0800 Subject: [PATCH 11/71] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20TODO=20=E5=8A=A0?= =?UTF-8?q?=E4=B8=8A=E5=BE=8C=E5=8F=B0=20User=20story.=20=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=20products=20controller=20=E5=8A=A0=E5=85=A5=20index?= =?UTF-8?q?=20action?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/assets/javascripts/products.js.coffee | 3 +++ app/assets/stylesheets/products.css.scss | 3 +++ app/controllers/products_controller.rb | 7 +++++++ app/helpers/products_helper.rb | 2 ++ app/views/admin/products/new.html.erb | 1 + app/views/products/index.html.erb | 7 +++++++ config/routes.rb | 5 +++++ test/controllers/products_controller_test.rb | 7 +++++++ test/helpers/products_helper_test.rb | 4 ++++ todo | 14 +++++++++++++- 10 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 app/assets/javascripts/products.js.coffee create mode 100644 app/assets/stylesheets/products.css.scss create mode 100644 app/controllers/products_controller.rb create mode 100644 app/helpers/products_helper.rb create mode 100644 app/views/products/index.html.erb create mode 100644 test/controllers/products_controller_test.rb create mode 100644 test/helpers/products_helper_test.rb diff --git a/app/assets/javascripts/products.js.coffee b/app/assets/javascripts/products.js.coffee new file mode 100644 index 00000000..24f83d18 --- /dev/null +++ b/app/assets/javascripts/products.js.coffee @@ -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/ diff --git a/app/assets/stylesheets/products.css.scss b/app/assets/stylesheets/products.css.scss new file mode 100644 index 00000000..89e2e8db --- /dev/null +++ b/app/assets/stylesheets/products.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the products controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb new file mode 100644 index 00000000..c54e6456 --- /dev/null +++ b/app/controllers/products_controller.rb @@ -0,0 +1,7 @@ +class ProductsController < ApplicationController + + def index + @products = Product.all + end + +end diff --git a/app/helpers/products_helper.rb b/app/helpers/products_helper.rb new file mode 100644 index 00000000..ab5c42b3 --- /dev/null +++ b/app/helpers/products_helper.rb @@ -0,0 +1,2 @@ +module ProductsHelper +end diff --git a/app/views/admin/products/new.html.erb b/app/views/admin/products/new.html.erb index dcec482a..bf8d7eeb 100644 --- a/app/views/admin/products/new.html.erb +++ b/app/views/admin/products/new.html.erb @@ -3,6 +3,7 @@ <%= f.input :title, label: '標題' %> <%= f.input :description, label: '產品敘述' %> <%= f.input :quantity, label: '數量' %> + <%= f.submit "Submit", :disable_with => 'Submiting...' %> <% end %> diff --git a/app/views/products/index.html.erb b/app/views/products/index.html.erb new file mode 100644 index 00000000..8b4a4b17 --- /dev/null +++ b/app/views/products/index.html.erb @@ -0,0 +1,7 @@ +
    + <% @products.each do |product| %> +
  • + <%= link_to(product.title, products_path(product)) %> +
  • + <% end %> +
\ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 57f4bce2..58a05ae7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,6 +5,11 @@ namespace :admin do resources :products end + + resources :products + + # 暫時先把 root 放到這邊 + root :to => "products#index" # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". diff --git a/test/controllers/products_controller_test.rb b/test/controllers/products_controller_test.rb new file mode 100644 index 00000000..c881fa68 --- /dev/null +++ b/test/controllers/products_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class ProductsControllerTest < ActionController::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/helpers/products_helper_test.rb b/test/helpers/products_helper_test.rb new file mode 100644 index 00000000..0f5beab7 --- /dev/null +++ b/test/helpers/products_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class ProductsHelperTest < ActionView::TestCase +end diff --git a/todo b/todo index 2bc4fac9..03db352e 100644 --- a/todo +++ b/todo @@ -5,6 +5,7 @@ ✔ 商品名稱 @done (14-06-10 18:27) ✔ 商品描述 @done (14-06-10 18:27) ☐ 商品圖片 + ☐ 商品價格 ✔ 商品庫存數量 @done (14-06-10 18:27) ✔ 身為商家的管理者,我要能在後台設定權限,權限分為管理者和消費者。 @done (14-06-10 20:14) @@ -15,7 +16,14 @@ [ 後台 ] ☐ 管理者登入後必須要有選單,以選擇想要進行的作業 ☐ 管理商品 + ✔ 顯示列表 @done (14-06-13 13:24) + ✔ 新增 @done (14-06-13 13:23) + ☐ 修改 + ☐ 刪除 ☐ 管理訂單 + ☐ 顯示列表 + ☐ 變更狀態 + ☐ 刪除訂單 ☐ 訂單必須有「未結賬」、「已結賬」、「出貨中」、「已出貨(消費者收到商品)」、「退貨申請」、「已退貨(已收到退貨商品)」等狀態。 ☐ 消費者結賬後必須收到 mail 確認 ☐ 商品出貨後消費者也必須收到 mail 確認 @@ -24,11 +32,15 @@ [ 前台 ] ☐ 消費者應該要能看到所有商品 + ☐ 消費者要能將商品加到購物車 + ☐ 消費者要能確認購買、填寫資料並產生訂單 + ☐ 消費者要能進行結賬的動作 + ☐ 結賬後要能收到 mail 確認 [ 作業 ] ☐ Week1 Homework - ☐ 寫後台的 User Story + ✔ 寫後台的 User Story @done (14-06-13 13:45) ☐ 實作圖片上傳 ☐ 用 Bootstrap 做一個「有 navbar」的 layout ✔ 使用 simple_form 換掉 form_for @done (14-06-10 21:44) From ff1c3e1e78123daa41caefdf957726920ca8199f Mon Sep 17 00:00:00 2001 From: FunnyQ Date: Fri, 13 Jun 2014 15:56:26 +0800 Subject: [PATCH 12/71] =?UTF-8?q?=E5=AE=8C=E6=88=90=20products=20index,=20?= =?UTF-8?q?show.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/products_controller.rb | 4 ++++ app/views/admin/products/new.html.erb | 1 + app/views/products/index.html.erb | 2 +- app/views/products/show.html.erb | 13 +++++++++++++ todo | 2 +- 5 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 app/views/products/show.html.erb diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index c54e6456..fd7028d8 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -4,4 +4,8 @@ def index @products = Product.all end + def show + @product = Product.find(params[:id]) + end + end diff --git a/app/views/admin/products/new.html.erb b/app/views/admin/products/new.html.erb index bf8d7eeb..e86e37a7 100644 --- a/app/views/admin/products/new.html.erb +++ b/app/views/admin/products/new.html.erb @@ -3,6 +3,7 @@ <%= f.input :title, label: '標題' %> <%= f.input :description, label: '產品敘述' %> <%= f.input :quantity, label: '數量' %> + <%= f.submit "Submit", :disable_with => 'Submiting...' %> <% end %> diff --git a/app/views/products/index.html.erb b/app/views/products/index.html.erb index 8b4a4b17..f08e2452 100644 --- a/app/views/products/index.html.erb +++ b/app/views/products/index.html.erb @@ -1,7 +1,7 @@
    <% @products.each do |product| %>
  • - <%= link_to(product.title, products_path(product)) %> + <%= link_to(product.title, product_path(product)) %>
  • <% end %>
\ No newline at end of file diff --git a/app/views/products/show.html.erb b/app/views/products/show.html.erb new file mode 100644 index 00000000..6745e864 --- /dev/null +++ b/app/views/products/show.html.erb @@ -0,0 +1,13 @@ +

+ <%= @product.title %> +

+ +

產品描述

+

+ <%= @product.description %> +

+ +

剩餘數量

+

+ 尚有 <%= @product.quantity %> 件商品 +

\ No newline at end of file diff --git a/todo b/todo index 03db352e..dce2ceeb 100644 --- a/todo +++ b/todo @@ -44,4 +44,4 @@ ☐ 實作圖片上傳 ☐ 用 Bootstrap 做一個「有 navbar」的 layout ✔ 使用 simple_form 換掉 form_for @done (14-06-10 21:44) - ☐ products/show, products, index 實作 \ No newline at end of file + ✔ products/show, products, index 實作 @done (14-06-13 15:34) \ No newline at end of file From 34b41e05b5c92216fdddcefd0295b7a8439a2ff8 Mon Sep 17 00:00:00 2001 From: FunnyQ Date: Fri, 13 Jun 2014 17:57:31 +0800 Subject: [PATCH 13/71] =?UTF-8?q?=E5=AE=8C=E6=88=90=20navbar=20=E8=88=87?= =?UTF-8?q?=20dropdown=20=E9=81=B8=E5=96=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/assets/stylesheets/application.css.scss | 8 ++- app/views/common/_footer.html.erb | 0 app/views/common/_navbar.html.erb | 80 +++++++++++++++++++++ app/views/layouts/application.html.erb | 15 ++-- todo | 4 +- 5 files changed, 97 insertions(+), 10 deletions(-) create mode 100644 app/views/common/_footer.html.erb create mode 100644 app/views/common/_navbar.html.erb diff --git a/app/assets/stylesheets/application.css.scss b/app/assets/stylesheets/application.css.scss index 44ca45cc..ff72e2a3 100644 --- a/app/assets/stylesheets/application.css.scss +++ b/app/assets/stylesheets/application.css.scss @@ -10,7 +10,11 @@ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new * file per style scope. * + *= require bootstrap *= require_tree . *= require_self - *= require bootstrap - */ \ No newline at end of file + */ + +body { + margin-top: 60px; +} \ No newline at end of file diff --git a/app/views/common/_footer.html.erb b/app/views/common/_footer.html.erb new file mode 100644 index 00000000..e69de29b diff --git a/app/views/common/_navbar.html.erb b/app/views/common/_navbar.html.erb new file mode 100644 index 00000000..dcdf7d06 --- /dev/null +++ b/app/views/common/_navbar.html.erb @@ -0,0 +1,80 @@ + \ No newline at end of file diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index ab2e355f..83153dcd 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -8,13 +8,16 @@ - <% if !current_user %> - <%= link_to("登入", new_user_session_path) %> - <% else %> - <%= link_to("登出", destroy_user_session_path, :method => :delete) %> - <% end %> + +<%= render :partial => "common/navbar" %> -<%= yield %> + +
+ <%= yield %> +
+ + +<%= render :partial => "common/footer" %> diff --git a/todo b/todo index dce2ceeb..ea7d7490 100644 --- a/todo +++ b/todo @@ -31,7 +31,7 @@ ☐ 管理者應該要可以管理訂單狀態 [ 前台 ] - ☐ 消費者應該要能看到所有商品 + ✔ 消費者應該要能看到所有商品 @done (14-06-13 16:05) ☐ 消費者要能將商品加到購物車 ☐ 消費者要能確認購買、填寫資料並產生訂單 ☐ 消費者要能進行結賬的動作 @@ -42,6 +42,6 @@ ☐ Week1 Homework ✔ 寫後台的 User Story @done (14-06-13 13:45) ☐ 實作圖片上傳 - ☐ 用 Bootstrap 做一個「有 navbar」的 layout + ✔ 用 Bootstrap 做一個「有 navbar」的 layout @done (14-06-13 17:54) ✔ 使用 simple_form 換掉 form_for @done (14-06-10 21:44) ✔ products/show, products, index 實作 @done (14-06-13 15:34) \ No newline at end of file From 8419cd751c499503c1cf0636ba65dd9e8232219f Mon Sep 17 00:00:00 2001 From: FunnyQ Date: Mon, 16 Jun 2014 00:42:18 +0800 Subject: [PATCH 14/71] trying carrierwave 4th time --- Gemfile | 7 ++++++- Gemfile.lock | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 1c371acb..92454149 100644 --- a/Gemfile +++ b/Gemfile @@ -43,4 +43,9 @@ gem 'devise' # 加入 bootstrap css framework gem 'bootstrap-sass' # Rails forms made easy -gem 'simple_form' \ No newline at end of file +gem 'simple_form' + +# 圖片上傳 +gem 'carrierwave' +# 圖片壓縮 resize +gem 'mini_magick' \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 455fdf07..58a6678d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -34,6 +34,11 @@ GEM bootstrap-sass (3.1.1.1) 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) @@ -61,6 +66,8 @@ 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) @@ -109,6 +116,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) @@ -130,10 +138,12 @@ PLATFORMS 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) From f31356c2f12f8e78209204e5f9449a22cd4d811d Mon Sep 17 00:00:00 2001 From: FunnyQ Date: Mon, 16 Jun 2014 02:11:50 +0800 Subject: [PATCH 15/71] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=9C=96=E7=89=87?= =?UTF-8?q?=E4=B8=8A=E5=82=B3=E5=8A=9F=E8=83=BD...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 + app/controllers/admin/products_controller.rb | 5 +- app/controllers/products_controller.rb | 32 ++++++++++ app/models/photo.rb | 7 +++ app/models/product.rb | 10 ++++ app/uploaders/image_uploader.rb | 61 ++++++++++++++++++++ app/views/admin/products/new.html.erb | 9 ++- db/migrate/20140615164554_create_photos.rb | 10 ++++ db/schema.rb | 9 ++- test/fixtures/photos.yml | 9 +++ test/models/photo_test.rb | 7 +++ todo | 6 +- 12 files changed, 162 insertions(+), 6 deletions(-) create mode 100644 app/models/photo.rb create mode 100644 app/uploaders/image_uploader.rb create mode 100644 db/migrate/20140615164554_create_photos.rb create mode 100644 test/fixtures/photos.yml create mode 100644 test/models/photo_test.rb diff --git a/.gitignore b/.gitignore index 6a502e99..8e356b6b 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,6 @@ # Ignore all logfiles and tempfiles. /log/*.log /tmp + +# ignore upload files +/public/uploads \ No newline at end of file diff --git a/app/controllers/admin/products_controller.rb b/app/controllers/admin/products_controller.rb index 80cb13e2..6c013de4 100644 --- a/app/controllers/admin/products_controller.rb +++ b/app/controllers/admin/products_controller.rb @@ -9,6 +9,8 @@ class Admin::ProductsController < ApplicationController def new @product = Product.new + # ??? + @photo = @product.photos.new end def create @@ -28,7 +30,8 @@ def index private def product_params - params.require(:product).permit(:title, :description,:quantity) + # :nested_attributes => [:關聯 Model 的欄位] + params.require(:product).permit(:title, :description, :quantity, :photos_attributes => [:image]) end end diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index fd7028d8..ed637204 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -8,4 +8,36 @@ def show @product = Product.find(params[:id]) end + # def render_product_photo(photo, size = "thumb") + + # if photo.present? + # image_url = photo.image.send(size).url + # else + + # case size + # when :medium + # volume = "300x300" + # else + # volume = "200x200" + # end + + # image_url = "http://placehold.it/#{volume}&text=No Pic" + # end + + # image_tag(image_url, :class => "thumbnail") + + # end + + # def render_product_name(product) + # product.title + # end + + # def render_product_desc(product) + # simple_format(product.description) + # end + + # def render_product_quantity(product) + # product.quantity + # end + end diff --git a/app/models/photo.rb b/app/models/photo.rb new file mode 100644 index 00000000..0f734e9c --- /dev/null +++ b/app/models/photo.rb @@ -0,0 +1,7 @@ +class Photo < ActiveRecord::Base + + belongs_to :product + + mount_uploader :image, ImageUploader + +end diff --git a/app/models/product.rb b/app/models/product.rb index 077a8197..1a2b4bcf 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -1,2 +1,12 @@ class Product < ActiveRecord::Base + + has_many :photos + + # accepts_nested_attributes_for :MODEL + # 可以直接更新關聯 MODEL 的資料 + accepts_nested_attributes_for :photos + + # 驗證表單是否填妥 + validates_presence_of :title, :quantity + end diff --git a/app/uploaders/image_uploader.rb b/app/uploaders/image_uploader.rb new file mode 100644 index 00000000..7d0d4a74 --- /dev/null +++ b/app/uploaders/image_uploader.rb @@ -0,0 +1,61 @@ +# 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: + + # 將最長邊縮至 1024px + process :resize_to_fit => [1024, 1024] + + # 製作 250 x 250 的 1:1 方形縮圖 + version :thumb do + process :resize_to_fill => [250, 250] + end + + # 製作最長邊 500 的中等尺寸 + version :midum do + process :resize_to_fit => [500, 500] + 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 diff --git a/app/views/admin/products/new.html.erb b/app/views/admin/products/new.html.erb index e86e37a7..7f330859 100644 --- a/app/views/admin/products/new.html.erb +++ b/app/views/admin/products/new.html.erb @@ -3,7 +3,14 @@ <%= f.input :title, label: '標題' %> <%= f.input :description, label: '產品敘述' %> <%= f.input :quantity, label: '數量' %> - + + <%= f.simple_fields_for :photos do |c| %> + <%= c.input :image , :as => :file %> + <% end %> <%= f.submit "Submit", :disable_with => 'Submiting...' %> <% end %> diff --git a/db/migrate/20140615164554_create_photos.rb b/db/migrate/20140615164554_create_photos.rb new file mode 100644 index 00000000..de592bf4 --- /dev/null +++ b/db/migrate/20140615164554_create_photos.rb @@ -0,0 +1,10 @@ +class CreatePhotos < ActiveRecord::Migration + def change + create_table :photos do |t| + t.integer :product_id + t.string :image + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 265247a0..c95cf271 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,14 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20140610113852) do +ActiveRecord::Schema.define(version: 20140615164554) do + + create_table "photos", force: true do |t| + t.integer "product_id" + t.string "image" + t.datetime "created_at" + t.datetime "updated_at" + end create_table "products", force: true do |t| t.string "title" diff --git a/test/fixtures/photos.yml b/test/fixtures/photos.yml new file mode 100644 index 00000000..5cc49b72 --- /dev/null +++ b/test/fixtures/photos.yml @@ -0,0 +1,9 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + product_id: 1 + image: MyString + +two: + product_id: 1 + image: MyString diff --git a/test/models/photo_test.rb b/test/models/photo_test.rb new file mode 100644 index 00000000..e2ec03a1 --- /dev/null +++ b/test/models/photo_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class PhotoTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/todo b/todo index ea7d7490..63de6624 100644 --- a/todo +++ b/todo @@ -4,7 +4,7 @@ ☐ 商品必須有各種必要的屬性欄位 ✔ 商品名稱 @done (14-06-10 18:27) ✔ 商品描述 @done (14-06-10 18:27) - ☐ 商品圖片 + ✔ 商品圖片 @done (14-06-16 02:06) ☐ 商品價格 ✔ 商品庫存數量 @done (14-06-10 18:27) @@ -39,9 +39,9 @@ [ 作業 ] - ☐ Week1 Homework + ✔ Week1 Homework @done (14-06-16 02:06) ✔ 寫後台的 User Story @done (14-06-13 13:45) - ☐ 實作圖片上傳 + ✔ 實作圖片上傳 @done (14-06-16 02:06) ✔ 用 Bootstrap 做一個「有 navbar」的 layout @done (14-06-13 17:54) ✔ 使用 simple_form 換掉 form_for @done (14-06-10 21:44) ✔ products/show, products, index 實作 @done (14-06-13 15:34) \ No newline at end of file From ad4514c80f52867cb004c5cd6fecc8b598e71802 Mon Sep 17 00:00:00 2001 From: FunnyQ Date: Mon, 16 Jun 2014 03:07:46 +0800 Subject: [PATCH 16/71] =?UTF-8?q?product#show=20=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=E9=A1=AF=E7=A4=BA=E5=9C=96=E7=89=87=EF=BC=8C=E4=BD=86=20produc?= =?UTF-8?q?t=20=E6=B2=92=E5=9C=96=E7=89=87=E6=99=82=E6=9C=83=20error.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/products_controller.rb | 32 -------------------------- app/helpers/products_helper.rb | 2 +- app/uploaders/image_uploader.rb | 2 +- app/views/products/show.html.erb | 3 ++- 4 files changed, 4 insertions(+), 35 deletions(-) diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index ed637204..fd7028d8 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -8,36 +8,4 @@ def show @product = Product.find(params[:id]) end - # def render_product_photo(photo, size = "thumb") - - # if photo.present? - # image_url = photo.image.send(size).url - # else - - # case size - # when :medium - # volume = "300x300" - # else - # volume = "200x200" - # end - - # image_url = "http://placehold.it/#{volume}&text=No Pic" - # end - - # image_tag(image_url, :class => "thumbnail") - - # end - - # def render_product_name(product) - # product.title - # end - - # def render_product_desc(product) - # simple_format(product.description) - # end - - # def render_product_quantity(product) - # product.quantity - # end - end diff --git a/app/helpers/products_helper.rb b/app/helpers/products_helper.rb index ab5c42b3..50ad52c9 100644 --- a/app/helpers/products_helper.rb +++ b/app/helpers/products_helper.rb @@ -1,2 +1,2 @@ module ProductsHelper -end +end \ No newline at end of file diff --git a/app/uploaders/image_uploader.rb b/app/uploaders/image_uploader.rb index 7d0d4a74..e9c15cb6 100644 --- a/app/uploaders/image_uploader.rb +++ b/app/uploaders/image_uploader.rb @@ -42,7 +42,7 @@ def store_dir end # 製作最長邊 500 的中等尺寸 - version :midum do + version :mid do process :resize_to_fit => [500, 500] end diff --git a/app/views/products/show.html.erb b/app/views/products/show.html.erb index 6745e864..f3e48f70 100644 --- a/app/views/products/show.html.erb +++ b/app/views/products/show.html.erb @@ -10,4 +10,5 @@

剩餘數量

尚有 <%= @product.quantity %> 件商品 -

\ No newline at end of file +

+<%= image_tag @product.photos.first.image_url %> \ No newline at end of file From 6dc8103aa5ce583092ba530c3151879d237b0921 Mon Sep 17 00:00:00 2001 From: FunnyQ Date: Mon, 16 Jun 2014 03:57:57 +0800 Subject: [PATCH 17/71] =?UTF-8?q?index=20=E5=9C=96=E7=89=87=E9=A1=AF?= =?UTF-8?q?=E7=A4=BA=E4=B8=8D=E5=87=BA=E4=BE=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/views/admin/products/index.html.erb | 2 ++ app/views/products/index.html.erb | 2 ++ app/views/products/show.html.erb | 2 +- 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/app/views/admin/products/index.html.erb b/app/views/admin/products/index.html.erb index 31faac96..95ca44b6 100644 --- a/app/views/admin/products/index.html.erb +++ b/app/views/admin/products/index.html.erb @@ -3,5 +3,7 @@
  • <%= link_to(product.title, admin_products_path(product)) %>
  • + + <%= image_tag product.photos.first.image_url(:thumb) %> <% end %> \ No newline at end of file diff --git a/app/views/products/index.html.erb b/app/views/products/index.html.erb index f08e2452..d1476d2c 100644 --- a/app/views/products/index.html.erb +++ b/app/views/products/index.html.erb @@ -3,5 +3,7 @@
  • <%= link_to(product.title, product_path(product)) %>
  • + + <%= image_tag product.photos.first.image_url(:thumb) %> <% end %> \ No newline at end of file diff --git a/app/views/products/show.html.erb b/app/views/products/show.html.erb index f3e48f70..f9114ef5 100644 --- a/app/views/products/show.html.erb +++ b/app/views/products/show.html.erb @@ -11,4 +11,4 @@

    尚有 <%= @product.quantity %> 件商品

    -<%= image_tag @product.photos.first.image_url %> \ No newline at end of file +<%= image_tag @product.photos.first.image_url(:thumb) %> \ No newline at end of file From 1c74a4dd3a9caf40d8d2138704ba887dfe1c2a7e Mon Sep 17 00:00:00 2001 From: FunnyQ Date: Mon, 16 Jun 2014 16:22:25 +0800 Subject: [PATCH 18/71] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20price=20=E6=AC=84?= =?UTF-8?q?=E4=BD=8D=EF=BC=8C=E8=AE=8A=E6=9B=B4=E6=95=B8=E9=87=8F=E7=9A=84?= =?UTF-8?q?=E9=A0=90=E8=A8=AD=E5=80=BC=E7=82=BA0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/migrate/20140616080727_add_price_to_product.rb | 5 +++++ .../20140616081302_change_price_and_quantity_default.rb | 5 +++++ db/schema.rb | 5 +++-- 3 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20140616080727_add_price_to_product.rb create mode 100644 db/migrate/20140616081302_change_price_and_quantity_default.rb diff --git a/db/migrate/20140616080727_add_price_to_product.rb b/db/migrate/20140616080727_add_price_to_product.rb new file mode 100644 index 00000000..f662c64f --- /dev/null +++ b/db/migrate/20140616080727_add_price_to_product.rb @@ -0,0 +1,5 @@ +class AddPriceToProduct < ActiveRecord::Migration + def change + add_column :products, :price, :float, :default => 0 + end +end diff --git a/db/migrate/20140616081302_change_price_and_quantity_default.rb b/db/migrate/20140616081302_change_price_and_quantity_default.rb new file mode 100644 index 00000000..b2f99e32 --- /dev/null +++ b/db/migrate/20140616081302_change_price_and_quantity_default.rb @@ -0,0 +1,5 @@ +class ChangePriceAndQuantityDefault < ActiveRecord::Migration + def change + change_column :products, :quantity, :integer, :default => 0 + end +end diff --git a/db/schema.rb b/db/schema.rb index c95cf271..b8dc388d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20140615164554) do +ActiveRecord::Schema.define(version: 20140616081302) do create_table "photos", force: true do |t| t.integer "product_id" @@ -23,9 +23,10 @@ create_table "products", force: true do |t| t.string "title" t.text "description" - t.integer "quantity" + t.integer "quantity", default: 0 t.datetime "created_at" t.datetime "updated_at" + t.float "price", default: 0.0 end create_table "users", force: true do |t| From 0db371a57615740bac50716bde916ad6da42f708 Mon Sep 17 00:00:00 2001 From: FunnyQ Date: Mon, 16 Jun 2014 16:59:56 +0800 Subject: [PATCH 19/71] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20edit=20=E8=88=87?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=20view?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/admin/products_controller.rb | 14 +++++++++++++- app/views/admin/products/edit.html.erb | 10 ++++++++++ app/views/admin/products/index.html.erb | 3 +-- app/views/admin/products/new.html.erb | 1 + todo | 6 +++--- 5 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 app/views/admin/products/edit.html.erb diff --git a/app/controllers/admin/products_controller.rb b/app/controllers/admin/products_controller.rb index 6c013de4..b5b8240b 100644 --- a/app/controllers/admin/products_controller.rb +++ b/app/controllers/admin/products_controller.rb @@ -23,6 +23,18 @@ def create end end + def edit + @product = Product.find(params[:id]) + end + + def update + if @product.update(product_params) + redirect_to admin_products_path + else + render :edit + end + end + def index @products = Product.all end @@ -31,7 +43,7 @@ def index def product_params # :nested_attributes => [:關聯 Model 的欄位] - params.require(:product).permit(:title, :description, :quantity, :photos_attributes => [:image]) + params.require(:product).permit(:title, :description, :quantity, :price, :photos_attributes => [:image]) end end diff --git a/app/views/admin/products/edit.html.erb b/app/views/admin/products/edit.html.erb new file mode 100644 index 00000000..9375e090 --- /dev/null +++ b/app/views/admin/products/edit.html.erb @@ -0,0 +1,10 @@ +<%= simple_form_for [:admin, @product] do |f| %> + <%= f.input :title, label: '標題' %> + <%= f.input :description, label: '產品敘述' %> + <%= f.input :quantity, label: '數量' %> + <%= f.simple_fields_for :photos do |c| %> + <%= c.input :image , :as => :file %> + <% end %> + <%= f.input :price, label: '價格' %> + <%= f.submit "Submit", :disable_with => 'Submiting...' %> +<% end %> \ No newline at end of file diff --git a/app/views/admin/products/index.html.erb b/app/views/admin/products/index.html.erb index 95ca44b6..5677dd44 100644 --- a/app/views/admin/products/index.html.erb +++ b/app/views/admin/products/index.html.erb @@ -1,9 +1,8 @@
      <% @products.each do |product| %>
    • - <%= link_to(product.title, admin_products_path(product)) %> + <%= link_to(product.title, admin_products_path(product)) %> | <%= link_to "EDIT", edit_admin_product_path(product) %>
    • - <%= image_tag product.photos.first.image_url(:thumb) %> <% end %>
    \ No newline at end of file diff --git a/app/views/admin/products/new.html.erb b/app/views/admin/products/new.html.erb index 7f330859..a17e0322 100644 --- a/app/views/admin/products/new.html.erb +++ b/app/views/admin/products/new.html.erb @@ -11,6 +11,7 @@ <%= f.simple_fields_for :photos do |c| %> <%= c.input :image , :as => :file %> <% end %> + <%= f.input :price, label: '價格' %> <%= f.submit "Submit", :disable_with => 'Submiting...' %> <% end %> diff --git a/todo b/todo index 63de6624..de3ccd65 100644 --- a/todo +++ b/todo @@ -1,11 +1,11 @@ - ☐ 身為商家的管理者,我要能夠在後台上架我的東西,並設定能夠販賣。 + ✔ 身為商家的管理者,我要能夠在後台上架我的東西,並設定能夠販賣。 @done (14-06-16 16:54) ✔ 管理者需要有一個後台 @done (14-06-10 18:27) ✔ 在後台必須可以新增商品 @done (14-06-10 18:27) - ☐ 商品必須有各種必要的屬性欄位 + ✔ 商品必須有各種必要的屬性欄位 @done (14-06-16 16:54) ✔ 商品名稱 @done (14-06-10 18:27) ✔ 商品描述 @done (14-06-10 18:27) ✔ 商品圖片 @done (14-06-16 02:06) - ☐ 商品價格 + ✔ 商品價格 @done (14-06-16 16:54) ✔ 商品庫存數量 @done (14-06-10 18:27) ✔ 身為商家的管理者,我要能在後台設定權限,權限分為管理者和消費者。 @done (14-06-10 20:14) From 78bc6ee50a3c8933fee9718c3dfb11657a9d407f Mon Sep 17 00:00:00 2001 From: FunnyQ Date: Mon, 16 Jun 2014 17:12:26 +0800 Subject: [PATCH 20/71] =?UTF-8?q?=E5=BE=8C=E5=8F=B0=E5=A2=9E=E5=8A=A0=20DE?= =?UTF-8?q?LET=20=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/admin/products_controller.rb | 6 ++++++ app/views/admin/products/index.html.erb | 2 +- app/views/products/index.html.erb | 1 - todo | 6 +++--- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/app/controllers/admin/products_controller.rb b/app/controllers/admin/products_controller.rb index b5b8240b..5a7acdbb 100644 --- a/app/controllers/admin/products_controller.rb +++ b/app/controllers/admin/products_controller.rb @@ -35,6 +35,12 @@ def update end end + def destroy + @product = Product.find(params[:id]) + @product.destroy + redirect_to admin_products_path + end + def index @products = Product.all end diff --git a/app/views/admin/products/index.html.erb b/app/views/admin/products/index.html.erb index 5677dd44..796a50af 100644 --- a/app/views/admin/products/index.html.erb +++ b/app/views/admin/products/index.html.erb @@ -1,7 +1,7 @@
      <% @products.each do |product| %>
    • - <%= link_to(product.title, admin_products_path(product)) %> | <%= link_to "EDIT", edit_admin_product_path(product) %> + <%= link_to(product.title, admin_products_path(product)) %> | <%= link_to "EDIT", edit_admin_product_path(product) %> | <%= link_to "刪除", admin_product_path(product), :method => :delete, :confirm => "確定刪除產品?" %>
    • <% end %> diff --git a/app/views/products/index.html.erb b/app/views/products/index.html.erb index d1476d2c..75f36843 100644 --- a/app/views/products/index.html.erb +++ b/app/views/products/index.html.erb @@ -4,6 +4,5 @@ <%= link_to(product.title, product_path(product)) %> - <%= image_tag product.photos.first.image_url(:thumb) %> <% end %>
    \ No newline at end of file diff --git a/todo b/todo index de3ccd65..1d02523c 100644 --- a/todo +++ b/todo @@ -15,11 +15,11 @@ [ 後台 ] ☐ 管理者登入後必須要有選單,以選擇想要進行的作業 - ☐ 管理商品 + ✔ 管理商品 @done (14-06-16 17:11) ✔ 顯示列表 @done (14-06-13 13:24) ✔ 新增 @done (14-06-13 13:23) - ☐ 修改 - ☐ 刪除 + ✔ 修改 @done (14-06-16 17:01) + ✔ 刪除 @done (14-06-16 17:11) ☐ 管理訂單 ☐ 顯示列表 ☐ 變更狀態 From 7d0040ce7eca76ebcd89cbb012f604b3d19dc45a Mon Sep 17 00:00:00 2001 From: FunnyQ Date: Mon, 16 Jun 2014 17:31:01 +0800 Subject: [PATCH 21/71] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=9C=96=E7=89=87?= =?UTF-8?q?=E9=A1=AF=E7=A4=BA=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/admin/products_controller.rb | 4 ++++ app/helpers/products_helper.rb | 9 +++++++++ app/models/product.rb | 5 +++++ app/views/admin/products/index.html.erb | 3 +++ app/views/products/show.html.erb | 2 +- todo | 5 ++++- 6 files changed, 26 insertions(+), 2 deletions(-) diff --git a/app/controllers/admin/products_controller.rb b/app/controllers/admin/products_controller.rb index 5a7acdbb..374dcfb3 100644 --- a/app/controllers/admin/products_controller.rb +++ b/app/controllers/admin/products_controller.rb @@ -45,6 +45,10 @@ def index @products = Product.all end + def show + @product = Product.find(params[:id]) + end + private def product_params diff --git a/app/helpers/products_helper.rb b/app/helpers/products_helper.rb index 50ad52c9..862d2fae 100644 --- a/app/helpers/products_helper.rb +++ b/app/helpers/products_helper.rb @@ -1,2 +1,11 @@ module ProductsHelper + + def render_product_photo(photo) + if photo.present? #產品是否有照片 + image_tag(photo.image.thumb.url) + else + "no pic" + end + end + end \ No newline at end of file diff --git a/app/models/product.rb b/app/models/product.rb index 1a2b4bcf..5272c4fb 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -9,4 +9,9 @@ class Product < ActiveRecord::Base # 驗證表單是否填妥 validates_presence_of :title, :quantity + # 定義最新的照片為 + def default_photo + photos.first + end + end diff --git a/app/views/admin/products/index.html.erb b/app/views/admin/products/index.html.erb index 796a50af..f652ff6f 100644 --- a/app/views/admin/products/index.html.erb +++ b/app/views/admin/products/index.html.erb @@ -2,6 +2,9 @@ <% @products.each do |product| %>
  • <%= link_to(product.title, admin_products_path(product)) %> | <%= link_to "EDIT", edit_admin_product_path(product) %> | <%= link_to "刪除", admin_product_path(product), :method => :delete, :confirm => "確定刪除產品?" %> +
    + <%= render_product_photo(product.default_photo) %> +

  • <% end %> diff --git a/app/views/products/show.html.erb b/app/views/products/show.html.erb index f9114ef5..9ddb5a2f 100644 --- a/app/views/products/show.html.erb +++ b/app/views/products/show.html.erb @@ -11,4 +11,4 @@

    尚有 <%= @product.quantity %> 件商品

    -<%= image_tag @product.photos.first.image_url(:thumb) %> \ No newline at end of file +<%= render_product_photo(@product.default_photo) %> \ No newline at end of file diff --git a/todo b/todo index 1d02523c..fec26b82 100644 --- a/todo +++ b/todo @@ -44,4 +44,7 @@ ✔ 實作圖片上傳 @done (14-06-16 02:06) ✔ 用 Bootstrap 做一個「有 navbar」的 layout @done (14-06-13 17:54) ✔ 使用 simple_form 換掉 form_for @done (14-06-10 21:44) - ✔ products/show, products, index 實作 @done (14-06-13 15:34) \ No newline at end of file + ✔ products/show, products, index 實作 @done (14-06-13 15:34) + ☐ 搞不定參考答案做的部分 + ✔ 修復圖片顯示功能 @done (14-06-16 17:27) + ✔ 資料庫中有「沒有圖片的產品」也要能正常顯示 index,要能判斷產品有沒有照片 @done (14-06-16 17:27) \ No newline at end of file From 3045b18cd86a5b6d2624402a40fa15b6b2498cb2 Mon Sep 17 00:00:00 2001 From: FunnyQ Date: Mon, 16 Jun 2014 17:56:22 +0800 Subject: [PATCH 22/71] update products view --- app/controllers/products_controller.rb | 3 ++- app/helpers/products_helper.rb | 2 +- app/views/products/index.html.erb | 5 +++++ app/views/products/show.html.erb | 5 ++++- todo | 2 +- 5 files changed, 13 insertions(+), 4 deletions(-) diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index fd7028d8..24e2a274 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -1,7 +1,8 @@ class ProductsController < ApplicationController def index - @products = Product.all + # 依 ID 降冪排序(最新的產品在最上面) + @products = Product.order("id DESC") end def show diff --git a/app/helpers/products_helper.rb b/app/helpers/products_helper.rb index 862d2fae..ff1daab8 100644 --- a/app/helpers/products_helper.rb +++ b/app/helpers/products_helper.rb @@ -4,7 +4,7 @@ def render_product_photo(photo) if photo.present? #產品是否有照片 image_tag(photo.image.thumb.url) else - "no pic" + image_tag("http://placehold.it/250x250&text=No Pic") end end diff --git a/app/views/products/index.html.erb b/app/views/products/index.html.erb index 75f36843..03b1f521 100644 --- a/app/views/products/index.html.erb +++ b/app/views/products/index.html.erb @@ -2,6 +2,11 @@ <% @products.each do |product| %>
  • <%= link_to(product.title, product_path(product)) %> +
    + <%= link_to product_path(product) do %> + <%= render_product_photo(product.default_photo) %> + <% end %> +

  • <% end %> diff --git a/app/views/products/show.html.erb b/app/views/products/show.html.erb index 9ddb5a2f..9889e55d 100644 --- a/app/views/products/show.html.erb +++ b/app/views/products/show.html.erb @@ -11,4 +11,7 @@

    尚有 <%= @product.quantity %> 件商品

    -<%= render_product_photo(@product.default_photo) %> \ No newline at end of file +<%= render_product_photo(@product.default_photo) %> +

    + <%= link_to "回到商品列表", products_path %> +

    diff --git a/todo b/todo index fec26b82..11fd948d 100644 --- a/todo +++ b/todo @@ -45,6 +45,6 @@ ✔ 用 Bootstrap 做一個「有 navbar」的 layout @done (14-06-13 17:54) ✔ 使用 simple_form 換掉 form_for @done (14-06-10 21:44) ✔ products/show, products, index 實作 @done (14-06-13 15:34) - ☐ 搞不定參考答案做的部分 + ✔ 搞不定參考答案做的部分 @done (14-06-16 17:53) ✔ 修復圖片顯示功能 @done (14-06-16 17:27) ✔ 資料庫中有「沒有圖片的產品」也要能正常顯示 index,要能判斷產品有沒有照片 @done (14-06-16 17:27) \ No newline at end of file From 261f8ff8d79190c823e0fe183854aba5451dad84 Mon Sep 17 00:00:00 2001 From: FunnyQ Date: Mon, 16 Jun 2014 18:14:49 +0800 Subject: [PATCH 23/71] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E8=B3=BC=E7=89=A9?= =?UTF-8?q?=E8=BB=8A=E6=8C=89=E9=88=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/views/common/_navbar.html.erb | 5 +++++ app/views/products/show.html.erb | 6 ++++++ config/routes.rb | 6 +++++- todo | 3 +++ 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/app/views/common/_navbar.html.erb b/app/views/common/_navbar.html.erb index dcdf7d06..a9ec7ad6 100644 --- a/app/views/common/_navbar.html.erb +++ b/app/views/common/_navbar.html.erb @@ -42,6 +42,11 @@