├── log └── .keep ├── tmp └── .keep ├── lib ├── assets │ └── .keep ├── tasks │ ├── .keep │ └── dev.rake └── templates │ └── erb │ └── scaffold │ └── _form.html.erb ├── public ├── favicon.ico ├── apple-touch-icon.png ├── apple-touch-icon-precomposed.png ├── robots.txt ├── 500.html ├── 422.html └── 404.html ├── test ├── helpers │ └── .keep ├── mailers │ ├── .keep │ ├── order_mailer_test.rb │ └── previews │ │ └── order_mailer_preview.rb ├── models │ ├── .keep │ ├── cart_test.rb │ ├── order_test.rb │ ├── user_test.rb │ ├── product_test.rb │ ├── cart_item_test.rb │ └── product_list_test.rb ├── controllers │ ├── .keep │ ├── carts_controller_test.rb │ ├── orders_controller_test.rb │ ├── products_controller_test.rb │ ├── welcome_controller_test.rb │ ├── cart_items_controller_test.rb │ ├── admin │ │ ├── orders_controller_test.rb │ │ └── products_controller_test.rb │ └── account │ │ └── orders_controller_test.rb ├── fixtures │ ├── .keep │ ├── files │ │ └── .keep │ ├── products.yml │ ├── carts.yml │ ├── orders.yml │ ├── users.yml │ ├── cart_items.yml │ └── product_lists.yml ├── integration │ └── .keep └── test_helper.rb ├── .ruby-version ├── app ├── assets │ ├── images │ │ └── .keep │ ├── javascripts │ │ ├── channels │ │ │ └── .keep │ │ ├── carts.coffee │ │ ├── cart_items.coffee │ │ ├── orders.coffee │ │ ├── products.coffee │ │ ├── welcome.coffee │ │ ├── account │ │ │ └── orders.coffee │ │ ├── admin │ │ │ ├── orders.coffee │ │ │ └── products.coffee │ │ ├── cable.js │ │ └── application.js │ ├── config │ │ └── manifest.js │ └── stylesheets │ │ ├── carts.scss │ │ ├── orders.scss │ │ ├── products.scss │ │ ├── welcome.scss │ │ ├── cart_items.scss │ │ ├── admin │ │ ├── orders.scss │ │ └── products.scss │ │ ├── account │ │ └── orders.scss │ │ └── application.css ├── models │ ├── concerns │ │ └── .keep │ ├── product_list.rb │ ├── application_record.rb │ ├── product.rb │ ├── cart_item.rb │ ├── user.rb │ ├── cart.rb │ └── order.rb ├── controllers │ ├── concerns │ │ └── .keep │ ├── welcome_controller.rb │ ├── account │ │ └── orders_controller.rb │ ├── carts_controller.rb │ ├── products_controller.rb │ ├── application_controller.rb │ ├── cart_items_controller.rb │ ├── admin │ │ ├── orders_controller.rb │ │ └── products_controller.rb │ └── orders_controller.rb ├── views │ ├── layouts │ │ ├── mailer.text.erb │ │ ├── mailer.html.erb │ │ ├── application.html.erb │ │ └── admin.html.erb │ ├── welcome │ │ └── index.html.erb │ ├── common │ │ ├── _footer.html.erb │ │ ├── _flashes.html.erb │ │ └── _navbar.html.erb │ ├── account │ │ └── orders │ │ │ └── index.html.erb │ ├── products │ │ ├── index.html.erb │ │ └── show.html.erb │ ├── admin │ │ ├── orders │ │ │ ├── index.html.erb │ │ │ ├── _state_option.html.erb │ │ │ └── show.html.erb │ │ └── products │ │ │ ├── new.html.erb │ │ │ ├── edit.html.erb │ │ │ └── index.html.erb │ ├── order_mailer │ │ ├── notify_ship.html.erb │ │ ├── apply_cancel.html.erb │ │ ├── notify_cancel.html.erb │ │ └── notify_order_placed.html.erb │ ├── carts │ │ ├── checkout.html.erb │ │ └── show.html.erb │ └── orders │ │ └── show.html.erb ├── helpers │ ├── carts_helper.rb │ ├── welcome_helper.rb │ ├── cart_items_helper.rb │ ├── products_helper.rb │ ├── admin │ │ ├── orders_helper.rb │ │ └── products_helper.rb │ ├── application_helper.rb │ ├── account │ │ └── orders_helper.rb │ ├── orders_helper.rb │ └── flashes_helper.rb ├── jobs │ └── application_job.rb ├── channels │ └── application_cable │ │ ├── channel.rb │ │ └── connection.rb ├── mailers │ ├── application_mailer.rb │ └── order_mailer.rb └── uploaders │ └── image_uploader.rb ├── vendor └── assets │ ├── javascripts │ └── .keep │ └── stylesheets │ └── .keep ├── bin ├── bundle ├── rake ├── rails ├── spring ├── update └── setup ├── config ├── spring.rb ├── boot.rb ├── environment.rb ├── cable.yml ├── initializers │ ├── session_store.rb │ ├── mime_types.rb │ ├── application_controller_renderer.rb │ ├── filter_parameter_logging.rb │ ├── cookies_serializer.rb │ ├── backtrace_silencers.rb │ ├── assets.rb │ ├── wrap_parameters.rb │ ├── inflections.rb │ ├── new_framework_defaults.rb │ ├── simple_form_bootstrap.rb │ ├── simple_form.rb │ └── devise.rb ├── application.rb ├── database.yml ├── locales │ ├── en.yml │ ├── simple_form.en.yml │ └── devise.en.yml ├── routes.rb ├── secrets.yml ├── environments │ ├── test.rb │ ├── development.rb │ └── production.rb └── puma.rb ├── config.ru ├── db ├── migrate │ ├── 20170215062750_add_token_to_order.rb │ ├── 20170215051214_add_role_to_user.rb │ ├── 20170215052228_add_image_to_product.rb │ ├── 20170215063436_add_paid_at_to_order.rb │ ├── 20170215053755_create_carts.rb │ ├── 20170215063509_add_payment_method_to_order.rb │ ├── 20170216105928_add_is_paid_to_orders.rb │ ├── 20170215064246_add_aasm_state_to_order.rb │ ├── 20170215050108_create_products.rb │ ├── 20170215053757_create_cart_items.rb │ ├── 20170215062258_create_product_lists.rb │ ├── 20170215062016_create_orders.rb │ └── 20170215044850_devise_create_users.rb ├── seeds.rb └── schema.rb ├── Rakefile ├── .gitignore ├── Gemfile ├── Gemfile.lock └── README.md /log/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tmp/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /lib/assets/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /lib/tasks/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/helpers/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/mailers/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/models/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.ruby-version: -------------------------------------------------------------------------------- 1 | 2.3.3 2 | -------------------------------------------------------------------------------- /app/assets/images/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/controllers/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/fixtures/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/integration/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /app/models/concerns/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /public/apple-touch-icon.png: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/fixtures/files/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /app/controllers/concerns/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /vendor/assets/javascripts/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /vendor/assets/stylesheets/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /app/assets/javascripts/channels/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /public/apple-touch-icon-precomposed.png: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /app/views/layouts/mailer.text.erb: -------------------------------------------------------------------------------- 1 | <%= yield %> 2 | -------------------------------------------------------------------------------- /app/helpers/carts_helper.rb: -------------------------------------------------------------------------------- 1 | module CartsHelper 2 | end 3 | -------------------------------------------------------------------------------- /app/helpers/welcome_helper.rb: -------------------------------------------------------------------------------- 1 | module WelcomeHelper 2 | end 3 | -------------------------------------------------------------------------------- /app/views/welcome/index.html.erb: -------------------------------------------------------------------------------- 1 |

Hello World!

2 | -------------------------------------------------------------------------------- /app/helpers/cart_items_helper.rb: -------------------------------------------------------------------------------- 1 | module CartItemsHelper 2 | end 3 | -------------------------------------------------------------------------------- /app/helpers/products_helper.rb: -------------------------------------------------------------------------------- 1 | module ProductsHelper 2 | end 3 | -------------------------------------------------------------------------------- /app/helpers/admin/orders_helper.rb: -------------------------------------------------------------------------------- 1 | module Admin::OrdersHelper 2 | end 3 | -------------------------------------------------------------------------------- /app/helpers/application_helper.rb: -------------------------------------------------------------------------------- 1 | module ApplicationHelper 2 | end 3 | -------------------------------------------------------------------------------- /app/helpers/account/orders_helper.rb: -------------------------------------------------------------------------------- 1 | module Account::OrdersHelper 2 | end 3 | -------------------------------------------------------------------------------- /app/helpers/admin/products_helper.rb: -------------------------------------------------------------------------------- 1 | module Admin::ProductsHelper 2 | end 3 | -------------------------------------------------------------------------------- /app/jobs/application_job.rb: -------------------------------------------------------------------------------- 1 | class ApplicationJob < ActiveJob::Base 2 | end 3 | -------------------------------------------------------------------------------- /app/models/product_list.rb: -------------------------------------------------------------------------------- 1 | class ProductList < ApplicationRecord 2 | 3 | belongs_to :order 4 | 5 | end 6 | -------------------------------------------------------------------------------- /app/models/application_record.rb: -------------------------------------------------------------------------------- 1 | class ApplicationRecord < ActiveRecord::Base 2 | self.abstract_class = true 3 | end 4 | -------------------------------------------------------------------------------- /app/models/product.rb: -------------------------------------------------------------------------------- 1 | class Product < ApplicationRecord 2 | 3 | mount_uploader :image, ImageUploader 4 | 5 | end 6 | -------------------------------------------------------------------------------- /app/models/cart_item.rb: -------------------------------------------------------------------------------- 1 | class CartItem < ApplicationRecord 2 | 3 | belongs_to :cart 4 | belongs_to :product 5 | 6 | end 7 | -------------------------------------------------------------------------------- /app/channels/application_cable/channel.rb: -------------------------------------------------------------------------------- 1 | module ApplicationCable 2 | class Channel < ActionCable::Channel::Base 3 | end 4 | end 5 | -------------------------------------------------------------------------------- /app/assets/config/manifest.js: -------------------------------------------------------------------------------- 1 | //= link_tree ../images 2 | //= link_directory ../javascripts .js 3 | //= link_directory ../stylesheets .css 4 | -------------------------------------------------------------------------------- /app/channels/application_cable/connection.rb: -------------------------------------------------------------------------------- 1 | module ApplicationCable 2 | class Connection < ActionCable::Connection::Base 3 | end 4 | end 5 | -------------------------------------------------------------------------------- /bin/bundle: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) 3 | load Gem.bin_path('bundler', 'bundle') 4 | -------------------------------------------------------------------------------- /config/spring.rb: -------------------------------------------------------------------------------- 1 | %w( 2 | .ruby-version 3 | .rbenv-vars 4 | tmp/restart.txt 5 | tmp/caching-dev.txt 6 | ).each { |path| Spring.watch(path) } 7 | -------------------------------------------------------------------------------- /config/boot.rb: -------------------------------------------------------------------------------- 1 | ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__) 2 | 3 | require 'bundler/setup' # Set up gems listed in the Gemfile. 4 | -------------------------------------------------------------------------------- /config.ru: -------------------------------------------------------------------------------- 1 | # This file is used by Rack-based servers to start the application. 2 | 3 | require_relative 'config/environment' 4 | 5 | run Rails.application 6 | -------------------------------------------------------------------------------- /app/mailers/application_mailer.rb: -------------------------------------------------------------------------------- 1 | class ApplicationMailer < ActionMailer::Base 2 | 3 | default from: 'service@jdstore.com' 4 | layout 'mailer' 5 | 6 | end 7 | -------------------------------------------------------------------------------- /app/controllers/welcome_controller.rb: -------------------------------------------------------------------------------- 1 | class WelcomeController < ApplicationController 2 | 3 | def index 4 | # flash[:notice] = "早安!你好!" 5 | end 6 | 7 | end 8 | -------------------------------------------------------------------------------- /config/environment.rb: -------------------------------------------------------------------------------- 1 | # Load the Rails application. 2 | require_relative 'application' 3 | 4 | # Initialize the Rails application. 5 | Rails.application.initialize! 6 | -------------------------------------------------------------------------------- /test/models/cart_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class CartTest < ActiveSupport::TestCase 4 | # test "the truth" do 5 | # assert true 6 | # end 7 | end 8 | -------------------------------------------------------------------------------- /test/models/order_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class OrderTest < ActiveSupport::TestCase 4 | # test "the truth" do 5 | # assert true 6 | # end 7 | end 8 | -------------------------------------------------------------------------------- /test/models/user_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class UserTest < ActiveSupport::TestCase 4 | # test "the truth" do 5 | # assert true 6 | # end 7 | end 8 | -------------------------------------------------------------------------------- /config/cable.yml: -------------------------------------------------------------------------------- 1 | development: 2 | adapter: async 3 | 4 | test: 5 | adapter: async 6 | 7 | production: 8 | adapter: redis 9 | url: redis://localhost:6379/1 10 | -------------------------------------------------------------------------------- /test/models/product_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class ProductTest < ActiveSupport::TestCase 4 | # test "the truth" do 5 | # assert true 6 | # end 7 | end 8 | -------------------------------------------------------------------------------- /db/migrate/20170215062750_add_token_to_order.rb: -------------------------------------------------------------------------------- 1 | class AddTokenToOrder < ActiveRecord::Migration[5.0] 2 | def change 3 | add_column :orders, :token, :string 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /test/models/cart_item_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class CartItemTest < ActiveSupport::TestCase 4 | # test "the truth" do 5 | # assert true 6 | # end 7 | end 8 | -------------------------------------------------------------------------------- /config/initializers/session_store.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | Rails.application.config.session_store :cookie_store, key: '_jdstore_session' 4 | -------------------------------------------------------------------------------- /db/migrate/20170215051214_add_role_to_user.rb: -------------------------------------------------------------------------------- 1 | class AddRoleToUser < ActiveRecord::Migration[5.0] 2 | 3 | def change 4 | add_column :users, :role, :string 5 | end 6 | 7 | end 8 | -------------------------------------------------------------------------------- /test/mailers/order_mailer_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class OrderMailerTest < ActionMailer::TestCase 4 | # test "the truth" do 5 | # assert true 6 | # end 7 | end 8 | -------------------------------------------------------------------------------- /test/mailers/previews/order_mailer_preview.rb: -------------------------------------------------------------------------------- 1 | # Preview all emails at http://localhost:3000/rails/mailers/order_mailer 2 | class OrderMailerPreview < ActionMailer::Preview 3 | 4 | end 5 | -------------------------------------------------------------------------------- /test/models/product_list_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class ProductListTest < ActiveSupport::TestCase 4 | # test "the truth" do 5 | # assert true 6 | # end 7 | end 8 | -------------------------------------------------------------------------------- /db/migrate/20170215052228_add_image_to_product.rb: -------------------------------------------------------------------------------- 1 | class AddImageToProduct < ActiveRecord::Migration[5.0] 2 | def change 3 | add_column :products, :image, :string 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /db/migrate/20170215063436_add_paid_at_to_order.rb: -------------------------------------------------------------------------------- 1 | class AddPaidAtToOrder < ActiveRecord::Migration[5.0] 2 | def change 3 | add_column :orders, :paid_at, :datetime 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /config/initializers/mime_types.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Add new mime types for use in respond_to blocks: 4 | # Mime::Type.register "text/richtext", :rtf 5 | -------------------------------------------------------------------------------- /db/migrate/20170215053755_create_carts.rb: -------------------------------------------------------------------------------- 1 | class CreateCarts < ActiveRecord::Migration[5.0] 2 | def change 3 | create_table :carts do |t| 4 | 5 | t.timestamps 6 | end 7 | end 8 | end 9 | -------------------------------------------------------------------------------- /test/controllers/carts_controller_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class CartsControllerTest < ActionDispatch::IntegrationTest 4 | # test "the truth" do 5 | # assert true 6 | # end 7 | end 8 | -------------------------------------------------------------------------------- /test/controllers/orders_controller_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class OrdersControllerTest < ActionDispatch::IntegrationTest 4 | # test "the truth" do 5 | # assert true 6 | # end 7 | end 8 | -------------------------------------------------------------------------------- /app/views/common/_footer.html.erb: -------------------------------------------------------------------------------- 1 | 7 | -------------------------------------------------------------------------------- /db/migrate/20170215063509_add_payment_method_to_order.rb: -------------------------------------------------------------------------------- 1 | class AddPaymentMethodToOrder < ActiveRecord::Migration[5.0] 2 | def change 3 | add_column :orders, :payment_method, :string 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /test/controllers/products_controller_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class ProductsControllerTest < ActionDispatch::IntegrationTest 4 | # test "the truth" do 5 | # assert true 6 | # end 7 | end 8 | -------------------------------------------------------------------------------- /test/controllers/welcome_controller_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class WelcomeControllerTest < ActionDispatch::IntegrationTest 4 | # test "the truth" do 5 | # assert true 6 | # end 7 | end 8 | -------------------------------------------------------------------------------- /test/controllers/cart_items_controller_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class CartItemsControllerTest < ActionDispatch::IntegrationTest 4 | # test "the truth" do 5 | # assert true 6 | # end 7 | end 8 | -------------------------------------------------------------------------------- /app/assets/stylesheets/carts.scss: -------------------------------------------------------------------------------- 1 | // Place all the styles related to the carts controller here. 2 | // They will automatically be included in application.css. 3 | // You can use Sass (SCSS) here: http://sass-lang.com/ 4 | -------------------------------------------------------------------------------- /app/assets/stylesheets/orders.scss: -------------------------------------------------------------------------------- 1 | // Place all the styles related to the orders controller here. 2 | // They will automatically be included in application.css. 3 | // You can use Sass (SCSS) here: http://sass-lang.com/ 4 | -------------------------------------------------------------------------------- /app/helpers/orders_helper.rb: -------------------------------------------------------------------------------- 1 | module OrdersHelper 2 | 3 | def render_order_paid_state(order) 4 | if order.is_paid 5 | "已付款" 6 | else 7 | "未付款" 8 | end 9 | end 10 | 11 | end 12 | -------------------------------------------------------------------------------- /test/controllers/admin/orders_controller_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class Admin::OrdersControllerTest < ActionDispatch::IntegrationTest 4 | # test "the truth" do 5 | # assert true 6 | # end 7 | end 8 | -------------------------------------------------------------------------------- /app/assets/stylesheets/products.scss: -------------------------------------------------------------------------------- 1 | // Place all the styles related to the products controller here. 2 | // They will automatically be included in application.css. 3 | // You can use Sass (SCSS) here: http://sass-lang.com/ 4 | -------------------------------------------------------------------------------- /app/assets/stylesheets/welcome.scss: -------------------------------------------------------------------------------- 1 | // Place all the styles related to the welcome controller here. 2 | // They will automatically be included in application.css. 3 | // You can use Sass (SCSS) here: http://sass-lang.com/ 4 | -------------------------------------------------------------------------------- /test/controllers/account/orders_controller_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class Account::OrdersControllerTest < ActionDispatch::IntegrationTest 4 | # test "the truth" do 5 | # assert true 6 | # end 7 | end 8 | -------------------------------------------------------------------------------- /test/controllers/admin/products_controller_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class Admin::ProductsControllerTest < ActionDispatch::IntegrationTest 4 | # test "the truth" do 5 | # assert true 6 | # end 7 | end 8 | -------------------------------------------------------------------------------- /app/assets/stylesheets/cart_items.scss: -------------------------------------------------------------------------------- 1 | // Place all the styles related to the cart_items controller here. 2 | // They will automatically be included in application.css. 3 | // You can use Sass (SCSS) here: http://sass-lang.com/ 4 | -------------------------------------------------------------------------------- /app/assets/stylesheets/admin/orders.scss: -------------------------------------------------------------------------------- 1 | // Place all the styles related to the admin::orders controller here. 2 | // They will automatically be included in application.css. 3 | // You can use Sass (SCSS) here: http://sass-lang.com/ 4 | -------------------------------------------------------------------------------- /app/assets/stylesheets/account/orders.scss: -------------------------------------------------------------------------------- 1 | // Place all the styles related to the account::orders controller here. 2 | // They will automatically be included in application.css. 3 | // You can use Sass (SCSS) here: http://sass-lang.com/ 4 | -------------------------------------------------------------------------------- /app/assets/stylesheets/admin/products.scss: -------------------------------------------------------------------------------- 1 | // Place all the styles related to the admin::products controller here. 2 | // They will automatically be included in application.css. 3 | // You can use Sass (SCSS) here: http://sass-lang.com/ 4 | -------------------------------------------------------------------------------- /config/initializers/application_controller_renderer.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # ApplicationController.renderer.defaults.merge!( 4 | # http_host: 'example.org', 5 | # https: false 6 | # ) 7 | -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # See http://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file 2 | # 3 | # To ban all spiders from the entire site uncomment the next two lines: 4 | # User-agent: * 5 | # Disallow: / 6 | -------------------------------------------------------------------------------- /db/migrate/20170216105928_add_is_paid_to_orders.rb: -------------------------------------------------------------------------------- 1 | class AddIsPaidToOrders < ActiveRecord::Migration[5.0] 2 | def change 3 | remove_column :orders, :paid_at 4 | add_column :orders, :is_paid, :boolean, :default => false 5 | end 6 | end 7 | -------------------------------------------------------------------------------- /app/assets/javascripts/carts.coffee: -------------------------------------------------------------------------------- 1 | # Place all the behaviors and hooks related to the matching controller here. 2 | # All this logic will automatically be available in application.js. 3 | # You can use CoffeeScript in this file: http://coffeescript.org/ 4 | -------------------------------------------------------------------------------- /app/controllers/account/orders_controller.rb: -------------------------------------------------------------------------------- 1 | class Account::OrdersController < ApplicationController 2 | 3 | before_action :authenticate_user! 4 | 5 | def index 6 | @orders = current_user.orders.order("id DESC") 7 | end 8 | 9 | end 10 | -------------------------------------------------------------------------------- /config/initializers/filter_parameter_logging.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Configure sensitive parameters which will be filtered from the log file. 4 | Rails.application.config.filter_parameters += [:password] 5 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | # Add your own tasks in files placed in lib/tasks ending in .rake, 2 | # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. 3 | 4 | require_relative 'config/application' 5 | 6 | Rails.application.load_tasks 7 | -------------------------------------------------------------------------------- /app/assets/javascripts/cart_items.coffee: -------------------------------------------------------------------------------- 1 | # Place all the behaviors and hooks related to the matching controller here. 2 | # All this logic will automatically be available in application.js. 3 | # You can use CoffeeScript in this file: http://coffeescript.org/ 4 | -------------------------------------------------------------------------------- /app/assets/javascripts/orders.coffee: -------------------------------------------------------------------------------- 1 | # Place all the behaviors and hooks related to the matching controller here. 2 | # All this logic will automatically be available in application.js. 3 | # You can use CoffeeScript in this file: http://coffeescript.org/ 4 | -------------------------------------------------------------------------------- /app/assets/javascripts/products.coffee: -------------------------------------------------------------------------------- 1 | # Place all the behaviors and hooks related to the matching controller here. 2 | # All this logic will automatically be available in application.js. 3 | # You can use CoffeeScript in this file: http://coffeescript.org/ 4 | -------------------------------------------------------------------------------- /app/assets/javascripts/welcome.coffee: -------------------------------------------------------------------------------- 1 | # Place all the behaviors and hooks related to the matching controller here. 2 | # All this logic will automatically be available in application.js. 3 | # You can use CoffeeScript in this file: http://coffeescript.org/ 4 | -------------------------------------------------------------------------------- /db/migrate/20170215064246_add_aasm_state_to_order.rb: -------------------------------------------------------------------------------- 1 | class AddAasmStateToOrder < ActiveRecord::Migration[5.0] 2 | def change 3 | add_column :orders, :aasm_state, :string, default: "order_placed" 4 | add_index :orders, :aasm_state 5 | end 6 | end 7 | -------------------------------------------------------------------------------- /app/assets/javascripts/account/orders.coffee: -------------------------------------------------------------------------------- 1 | # Place all the behaviors and hooks related to the matching controller here. 2 | # All this logic will automatically be available in application.js. 3 | # You can use CoffeeScript in this file: http://coffeescript.org/ 4 | -------------------------------------------------------------------------------- /app/assets/javascripts/admin/orders.coffee: -------------------------------------------------------------------------------- 1 | # Place all the behaviors and hooks related to the matching controller here. 2 | # All this logic will automatically be available in application.js. 3 | # You can use CoffeeScript in this file: http://coffeescript.org/ 4 | -------------------------------------------------------------------------------- /app/assets/javascripts/admin/products.coffee: -------------------------------------------------------------------------------- 1 | # Place all the behaviors and hooks related to the matching controller here. 2 | # All this logic will automatically be available in application.js. 3 | # You can use CoffeeScript in this file: http://coffeescript.org/ 4 | -------------------------------------------------------------------------------- /bin/rake: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | begin 3 | load File.expand_path('../spring', __FILE__) 4 | rescue LoadError => e 5 | raise unless e.message.include?('spring') 6 | end 7 | require_relative '../config/boot' 8 | require 'rake' 9 | Rake.application.run 10 | -------------------------------------------------------------------------------- /app/controllers/carts_controller.rb: -------------------------------------------------------------------------------- 1 | class CartsController < ApplicationController 2 | 3 | def checkout 4 | @order = Order.new 5 | end 6 | 7 | def clean 8 | current_cart.clean! 9 | flash[:warning] = "已清空购物车" 10 | redirect_to cart_path 11 | end 12 | 13 | end 14 | -------------------------------------------------------------------------------- /config/initializers/cookies_serializer.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Specify a serializer for the signed and encrypted cookie jars. 4 | # Valid options are :json, :marshal, and :hybrid. 5 | Rails.application.config.action_dispatch.cookies_serializer = :json 6 | -------------------------------------------------------------------------------- /app/views/common/_flashes.html.erb: -------------------------------------------------------------------------------- 1 | <% if flash.any? %> 2 | <% user_facing_flashes.each do |key, value| %> 3 |
4 | 5 | <%= value %> 6 |
7 | <% end %> 8 | <% end %> 9 | -------------------------------------------------------------------------------- /bin/rails: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | begin 3 | load File.expand_path('../spring', __FILE__) 4 | rescue LoadError => e 5 | raise unless e.message.include?('spring') 6 | end 7 | APP_PATH = File.expand_path('../config/application', __dir__) 8 | require_relative '../config/boot' 9 | require 'rails/commands' 10 | -------------------------------------------------------------------------------- /test/fixtures/products.yml: -------------------------------------------------------------------------------- 1 | # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html 2 | 3 | one: 4 | title: MyString 5 | description: MyText 6 | quantity: 1 7 | price: 1 8 | 9 | two: 10 | title: MyString 11 | description: MyText 12 | quantity: 1 13 | price: 1 14 | -------------------------------------------------------------------------------- /app/views/layouts/mailer.html.erb: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 8 | 9 | 10 | 11 | <%= yield %> 12 | 13 | 14 | -------------------------------------------------------------------------------- /db/migrate/20170215050108_create_products.rb: -------------------------------------------------------------------------------- 1 | class CreateProducts < ActiveRecord::Migration[5.0] 2 | def change 3 | create_table :products do |t| 4 | t.string :title 5 | t.text :description 6 | t.integer :quantity 7 | t.integer :price 8 | 9 | t.timestamps 10 | end 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /db/migrate/20170215053757_create_cart_items.rb: -------------------------------------------------------------------------------- 1 | class CreateCartItems < ActiveRecord::Migration[5.0] 2 | def change 3 | create_table :cart_items do |t| 4 | 5 | t.integer :cart_id 6 | t.integer :product_id 7 | t.integer :quantity, default: 1 8 | 9 | t.timestamps 10 | end 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /app/helpers/flashes_helper.rb: -------------------------------------------------------------------------------- 1 | module FlashesHelper 2 | FLASH_CLASSES = { alert: "danger", notice: "success", warning: "warning"}.freeze 3 | 4 | def flash_class(key) 5 | FLASH_CLASSES.fetch key.to_sym, key 6 | end 7 | 8 | def user_facing_flashes 9 | flash.to_hash.slice "alert", "notice","warning" 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /db/migrate/20170215062258_create_product_lists.rb: -------------------------------------------------------------------------------- 1 | class CreateProductLists < ActiveRecord::Migration[5.0] 2 | def change 3 | create_table :product_lists do |t| 4 | 5 | t.integer :order_id 6 | t.string :product_name 7 | t.integer :product_price 8 | t.integer :quantity 9 | 10 | t.timestamps 11 | end 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /test/test_helper.rb: -------------------------------------------------------------------------------- 1 | ENV['RAILS_ENV'] ||= 'test' 2 | require File.expand_path('../../config/environment', __FILE__) 3 | require 'rails/test_help' 4 | 5 | class ActiveSupport::TestCase 6 | # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. 7 | fixtures :all 8 | 9 | # Add more helper methods to be used by all tests here... 10 | end 11 | -------------------------------------------------------------------------------- /app/models/user.rb: -------------------------------------------------------------------------------- 1 | class User < ApplicationRecord 2 | # Include default devise modules. Others available are: 3 | # :confirmable, :lockable, :timeoutable and :omniauthable 4 | devise :database_authenticatable, :registerable, 5 | :recoverable, :rememberable, :trackable, :validatable 6 | 7 | has_many :orders 8 | 9 | def admin? 10 | (role == "admin") 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /test/fixtures/carts.yml: -------------------------------------------------------------------------------- 1 | # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html 2 | 3 | # This model initially had no columns defined. If you add columns to the 4 | # model remove the '{}' from the fixture names and add the columns immediately 5 | # below each fixture, per the syntax in the comments below 6 | # 7 | one: {} 8 | # column: value 9 | # 10 | two: {} 11 | # column: value 12 | -------------------------------------------------------------------------------- /test/fixtures/orders.yml: -------------------------------------------------------------------------------- 1 | # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html 2 | 3 | # This model initially had no columns defined. If you add columns to the 4 | # model remove the '{}' from the fixture names and add the columns immediately 5 | # below each fixture, per the syntax in the comments below 6 | # 7 | one: {} 8 | # column: value 9 | # 10 | two: {} 11 | # column: value 12 | -------------------------------------------------------------------------------- /test/fixtures/users.yml: -------------------------------------------------------------------------------- 1 | # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html 2 | 3 | # This model initially had no columns defined. If you add columns to the 4 | # model remove the '{}' from the fixture names and add the columns immediately 5 | # below each fixture, per the syntax in the comments below 6 | # 7 | one: {} 8 | # column: value 9 | # 10 | two: {} 11 | # column: value 12 | -------------------------------------------------------------------------------- /db/migrate/20170215062016_create_orders.rb: -------------------------------------------------------------------------------- 1 | class CreateOrders < ActiveRecord::Migration[5.0] 2 | def change 3 | create_table :orders do |t| 4 | 5 | t.integer :total, default: 0 6 | t.integer :user_id 7 | t.string :billing_name 8 | t.string :billing_address 9 | t.string :shipping_name 10 | t.string :shipping_address 11 | 12 | t.timestamps 13 | end 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /test/fixtures/cart_items.yml: -------------------------------------------------------------------------------- 1 | # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html 2 | 3 | # This model initially had no columns defined. If you add columns to the 4 | # model remove the '{}' from the fixture names and add the columns immediately 5 | # below each fixture, per the syntax in the comments below 6 | # 7 | one: {} 8 | # column: value 9 | # 10 | two: {} 11 | # column: value 12 | -------------------------------------------------------------------------------- /test/fixtures/product_lists.yml: -------------------------------------------------------------------------------- 1 | # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html 2 | 3 | # This model initially had no columns defined. If you add columns to the 4 | # model remove the '{}' from the fixture names and add the columns immediately 5 | # below each fixture, per the syntax in the comments below 6 | # 7 | one: {} 8 | # column: value 9 | # 10 | two: {} 11 | # column: value 12 | -------------------------------------------------------------------------------- /app/assets/javascripts/cable.js: -------------------------------------------------------------------------------- 1 | // Action Cable provides the framework to deal with WebSockets in Rails. 2 | // You can generate new channels where WebSocket features live using the rails generate channel command. 3 | // 4 | //= require action_cable 5 | //= require_self 6 | //= require_tree ./channels 7 | 8 | (function() { 9 | this.App || (this.App = {}); 10 | 11 | App.cable = ActionCable.createConsumer(); 12 | 13 | }).call(this); 14 | -------------------------------------------------------------------------------- /lib/templates/erb/scaffold/_form.html.erb: -------------------------------------------------------------------------------- 1 | <%%= simple_form_for(@<%= singular_table_name %>) do |f| %> 2 | <%%= f.error_notification %> 3 | 4 |
5 | <%- attributes.each do |attribute| -%> 6 | <%%= f.<%= attribute.reference? ? :association : :input %> :<%= attribute.name %> %> 7 | <%- end -%> 8 |
9 | 10 |
11 | <%%= f.button :submit %> 12 |
13 | <%% end %> 14 | -------------------------------------------------------------------------------- /config/initializers/backtrace_silencers.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces. 4 | # Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ } 5 | 6 | # You can also remove all the silencers if you're trying to debug a problem that might stem from framework code. 7 | # Rails.backtrace_cleaner.remove_silencers! 8 | -------------------------------------------------------------------------------- /app/views/account/orders/index.html.erb: -------------------------------------------------------------------------------- 1 |

订单列表

2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | <% @orders.each do |order| %> 14 | 15 | 16 | 17 | 18 | <% end %> 19 | 20 | 21 |
#生成时间
<%= link_to(order.id, order_path(order.token)) %> <%= order.created_at.to_s(:long) %>
22 | -------------------------------------------------------------------------------- /db/seeds.rb: -------------------------------------------------------------------------------- 1 | # This file should contain all the record creation needed to seed the database with its default values. 2 | # The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup). 3 | # 4 | # Examples: 5 | # 6 | # movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }]) 7 | # Character.create(name: 'Luke', movie: movies.first) 8 | 9 | User.create!( :email => "admin@test.com", :password => "12345678", :role => "admin") 10 | -------------------------------------------------------------------------------- /app/models/cart.rb: -------------------------------------------------------------------------------- 1 | class Cart < ApplicationRecord 2 | 3 | has_many :cart_items 4 | has_many :products, through: :cart_items, source: :product 5 | 6 | def clean! 7 | cart_items.destroy_all 8 | end 9 | 10 | def add_product_to_cart(product) 11 | ci = cart_items.build 12 | ci.product = product 13 | ci.quantity = 1 14 | ci.save 15 | end 16 | 17 | def total_price 18 | cart_items.map{ |ci| ci.quantity * ci.product.price.to_i }.sum 19 | end 20 | 21 | end 22 | -------------------------------------------------------------------------------- /config/initializers/assets.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Version of your assets, change this if you want to expire all your assets. 4 | Rails.application.config.assets.version = '1.0' 5 | 6 | # Add additional assets to the asset load path 7 | # Rails.application.config.assets.paths << Emoji.images_path 8 | 9 | # Precompile additional assets. 10 | # application.js, application.css, and all non-JS/CSS in app/assets folder are already added. 11 | # Rails.application.config.assets.precompile += %w( search.js ) 12 | -------------------------------------------------------------------------------- /app/views/products/index.html.erb: -------------------------------------------------------------------------------- 1 |
2 | <% @products.each do |product| %> 3 |
4 | <%= link_to product_path(product) do %> 5 | <% if product.image.present? %> 6 | <%= image_tag(product.image.thumb.url, class: "thumbnail") %> 7 | <% else %> 8 | <%= image_tag("http://placehold.it/200x200&text=No Pic", class: "thumbnail") %> 9 | <% end %> 10 | <% end %> 11 | <%= product.title %> ¥ <%= product.price %> 12 |
13 | <% end %> 14 |
15 | -------------------------------------------------------------------------------- /config/application.rb: -------------------------------------------------------------------------------- 1 | require_relative 'boot' 2 | 3 | require 'rails/all' 4 | 5 | # Require the gems listed in Gemfile, including any gems 6 | # you've limited to :test, :development, or :production. 7 | Bundler.require(*Rails.groups) 8 | 9 | module Jdstore 10 | class Application < Rails::Application 11 | # Settings in config/environments/* take precedence over those specified here. 12 | # Application configuration should go into files in config/initializers 13 | # -- all .rb files in that directory are automatically loaded. 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /config/initializers/wrap_parameters.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # This file contains settings for ActionController::ParamsWrapper which 4 | # is enabled by default. 5 | 6 | # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array. 7 | ActiveSupport.on_load(:action_controller) do 8 | wrap_parameters format: [:json] 9 | end 10 | 11 | # To enable root element in JSON for ActiveRecord objects. 12 | # ActiveSupport.on_load(:active_record) do 13 | # self.include_root_in_json = true 14 | # end 15 | -------------------------------------------------------------------------------- /bin/spring: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | # This file loads spring without using Bundler, in order to be fast. 4 | # It gets overwritten when you run the `spring binstub` command. 5 | 6 | unless defined?(Spring) 7 | require 'rubygems' 8 | require 'bundler' 9 | 10 | lockfile = Bundler::LockfileParser.new(Bundler.default_lockfile.read) 11 | spring = lockfile.specs.detect { |spec| spec.name == "spring" } 12 | if spring 13 | Gem.use_paths Gem.dir, Bundler.bundle_path.to_s, *Gem.path 14 | gem 'spring', spring.version 15 | require 'spring/binstub' 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /app/controllers/products_controller.rb: -------------------------------------------------------------------------------- 1 | class ProductsController < ApplicationController 2 | 3 | def index 4 | @products = Product.all 5 | end 6 | 7 | def show 8 | @product = Product.find(params[:id]) 9 | end 10 | 11 | def add_to_cart 12 | @product = Product.find(params[:id]) 13 | 14 | if !current_cart.products.include?(@product) 15 | current_cart.add_product_to_cart(@product) 16 | flash[:notice] = "你已成功将 #{@product.title} 加入购物车" 17 | else 18 | flash[:warning] = "你的购物车内已有此物品" 19 | end 20 | 21 | redirect_to :back 22 | end 23 | 24 | end 25 | -------------------------------------------------------------------------------- /app/views/layouts/application.html.erb: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Jdstore 5 | <%= csrf_meta_tags %> 6 | 7 | <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> 8 | <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %> 9 | 10 | 11 | 12 |
13 | <%= render "common/navbar" %> 14 | <%= render "common/flashes" %> 15 | <%= yield %> 16 |
17 | 18 | <%= render "common/footer" %> 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/views/admin/orders/index.html.erb: -------------------------------------------------------------------------------- 1 |

订单列表

2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | <% @orders.each do |order| %> 14 | 15 | 16 | 17 | 18 | 19 | 20 | <% end %> 21 | 22 | 23 |
#生成时间订购者订单状态
<%= link_to(order.id, admin_order_path(order) ) %> <%= order.created_at.to_s(:long) %> <%= order.user.email %> <%= order.aasm_state %>
24 | 25 | <%= will_paginate @orders %> 26 | -------------------------------------------------------------------------------- /app/controllers/application_controller.rb: -------------------------------------------------------------------------------- 1 | class ApplicationController < ActionController::Base 2 | protect_from_forgery with: :exception 3 | 4 | helper_method :current_cart 5 | 6 | def current_cart 7 | @current_cart ||= find_cart 8 | end 9 | 10 | private 11 | 12 | def find_cart 13 | cart = Cart.find_by(id: session[:cart_id]) 14 | if cart.blank? 15 | cart = Cart.create 16 | end 17 | session[:cart_id] = cart.id 18 | return cart 19 | end 20 | 21 | def admin_required 22 | if !current_user.admin? 23 | redirect_to "/", alert: "You are not admin." 24 | end 25 | end 26 | 27 | end 28 | -------------------------------------------------------------------------------- /app/views/admin/products/new.html.erb: -------------------------------------------------------------------------------- 1 |

New Product

2 | 3 | <%= simple_form_for [:admin, @product] do |f| %> 4 | 5 |
6 | <%= f.input :title %> 7 |
8 | 9 |
10 | <%= f.input :description %> 11 |
12 | 13 |
14 | <%= f.input :quantity %> 15 |
16 | 17 |
18 | <%= f.input :price %> 19 |
20 | 21 |
22 | <%= f.input :image, as: :file %> 23 |
24 | 25 | <%= f.submit "Submit", data: { disable_with: "Submitting..." }, :class => "btn btn-primary" %> 26 | 27 | <% end %> 28 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files for more about ignoring files. 2 | # 3 | # If you find yourself ignoring temporary files generated by your text editor 4 | # or operating system, you probably want to add a global ignore instead: 5 | # git config --global core.excludesfile '~/.gitignore_global' 6 | 7 | # Ignore bundler config. 8 | /.bundle 9 | 10 | # Ignore the default SQLite database. 11 | /db/*.sqlite3 12 | /db/*.sqlite3-journal 13 | 14 | # Ignore all logfiles and tempfiles. 15 | /log/* 16 | /tmp/* 17 | !/log/.keep 18 | !/tmp/.keep 19 | 20 | # Ignore Byebug command history file. 21 | .byebug_history 22 | 23 | public/uploads 24 | -------------------------------------------------------------------------------- /config/database.yml: -------------------------------------------------------------------------------- 1 | # SQLite version 3.x 2 | # gem install sqlite3 3 | # 4 | # Ensure the SQLite 3 gem is defined in your Gemfile 5 | # gem 'sqlite3' 6 | # 7 | default: &default 8 | adapter: sqlite3 9 | pool: 5 10 | timeout: 5000 11 | 12 | development: 13 | <<: *default 14 | database: db/development.sqlite3 15 | 16 | # Warning: The database defined as "test" will be erased and 17 | # re-generated from your development database when you run "rake". 18 | # Do not set this db to the same as development or production. 19 | test: 20 | <<: *default 21 | database: db/test.sqlite3 22 | 23 | production: 24 | <<: *default 25 | database: db/production.sqlite3 26 | -------------------------------------------------------------------------------- /config/initializers/inflections.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Add new inflection rules using the following format. Inflections 4 | # are locale specific, and you may define rules for as many different 5 | # locales as you wish. All of these examples are active by default: 6 | # ActiveSupport::Inflector.inflections(:en) do |inflect| 7 | # inflect.plural /^(ox)$/i, '\1en' 8 | # inflect.singular /^(ox)en/i, '\1' 9 | # inflect.irregular 'person', 'people' 10 | # inflect.uncountable %w( fish sheep ) 11 | # end 12 | 13 | # These inflection rules are supported but not enabled by default: 14 | # ActiveSupport::Inflector.inflections(:en) do |inflect| 15 | # inflect.acronym 'RESTful' 16 | # end 17 | -------------------------------------------------------------------------------- /config/locales/en.yml: -------------------------------------------------------------------------------- 1 | # Files in the config/locales directory are used for internationalization 2 | # and are automatically loaded by Rails. If you want to use locales other 3 | # than English, add the necessary files in this directory. 4 | # 5 | # To use the locales, use `I18n.t`: 6 | # 7 | # I18n.t 'hello' 8 | # 9 | # In views, this is aliased to just `t`: 10 | # 11 | # <%= t('hello') %> 12 | # 13 | # To use a different locale, set it with `I18n.locale`: 14 | # 15 | # I18n.locale = :es 16 | # 17 | # This would use the information in config/locales/es.yml. 18 | # 19 | # To learn more, please read the Rails Internationalization guide 20 | # available at http://guides.rubyonrails.org/i18n.html. 21 | 22 | en: 23 | hello: "Hello world" 24 | -------------------------------------------------------------------------------- /app/views/admin/products/edit.html.erb: -------------------------------------------------------------------------------- 1 |

Edit Product

2 | 3 | <%= simple_form_for [:admin, @product] do |f| %> 4 | 5 |
6 | <%= f.input :title %> 7 |
8 | 9 |
10 | <%= f.input :description %> 11 |
12 | 13 |
14 | <%= f.input :quantity %> 15 |
16 | 17 |
18 | <%= f.input :price %> 19 |
20 | 21 | <% if @product.image.present? %> 22 | 目前商品图
23 | <%= image_tag(@product.image.thumb.url) %> 24 | <% end %> 25 | 26 |
27 | <%= f.input :image, as: :file %> 28 |
29 | 30 | <%= f.submit "Submit", data: { disable_with: "Submitting..." }, :class => "btn btn-primary" %> 31 | 32 | <% end %> 33 | -------------------------------------------------------------------------------- /app/assets/stylesheets/application.css: -------------------------------------------------------------------------------- 1 | /* 2 | * This is a manifest file that'll be compiled into application.css, which will include all the files 3 | * listed below. 4 | * 5 | * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets, 6 | * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path. 7 | * 8 | * You're free to add application-wide styles to this file and they'll appear at the bottom of the 9 | * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS 10 | * files in this directory. Styles in this file should be added after the last require_* statement. 11 | * It is generally better to create a new file per style scope. 12 | * 13 | *= require_tree . 14 | *= require_self 15 | */ -------------------------------------------------------------------------------- /app/assets/javascripts/application.js: -------------------------------------------------------------------------------- 1 | // This is a manifest file that'll be compiled into application.js, which will include all the files 2 | // listed below. 3 | // 4 | // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts, 5 | // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path. 6 | // 7 | // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the 8 | // compiled file. JavaScript code in this file should be added after the last require_* statement. 9 | // 10 | // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details 11 | // about supported directives. 12 | // 13 | //= require jquery 14 | //= require jquery_ujs 15 | //= require turbolinks 16 | //= require_tree . 17 | -------------------------------------------------------------------------------- /app/views/layouts/admin.html.erb: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | JDstore 后台 5 | <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %> 6 | <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %> 7 | <%= csrf_meta_tags %> 8 | 9 | 10 |
11 | <%= render "common/navbar" %> 12 |
13 |
14 | 18 |
19 |
20 | <%= yield %> 21 |
22 |
23 |
24 | 25 | 26 | -------------------------------------------------------------------------------- /app/controllers/cart_items_controller.rb: -------------------------------------------------------------------------------- 1 | class CartItemsController < ApplicationController 2 | 3 | def update 4 | @cart = current_cart 5 | @cart_item = @cart.cart_items.find_by(product_id: params[:id]) 6 | 7 | if @cart_item.product.quantity >= cart_item_params[:quantity].to_i 8 | @cart_item.update(cart_item_params) 9 | flash[:notice] = "成功变更数量" 10 | else 11 | flash[:warning] = "数量不足以加入购物车" 12 | end 13 | 14 | redirect_to cart_path 15 | end 16 | 17 | def destroy 18 | @cart = current_cart 19 | @cart_item = @cart.cart_items.find_by(product_id: params[:id]) 20 | @product = @cart_item.product 21 | @cart_item.destroy 22 | 23 | flash[:warning] = "成功将 #{@product.title} 从购物车删除!" 24 | redirect_to :back 25 | end 26 | 27 | private 28 | 29 | def cart_item_params 30 | params.require(:cart_item).permit(:quantity) 31 | end 32 | 33 | end 34 | -------------------------------------------------------------------------------- /bin/update: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | require 'pathname' 3 | require 'fileutils' 4 | include FileUtils 5 | 6 | # path to your application root. 7 | APP_ROOT = Pathname.new File.expand_path('../../', __FILE__) 8 | 9 | def system!(*args) 10 | system(*args) || abort("\n== Command #{args} failed ==") 11 | end 12 | 13 | chdir APP_ROOT do 14 | # This script is a way to update your development environment automatically. 15 | # Add necessary update steps to this file. 16 | 17 | puts '== Installing dependencies ==' 18 | system! 'gem install bundler --conservative' 19 | system('bundle check') || system!('bundle install') 20 | 21 | puts "\n== Updating database ==" 22 | system! 'bin/rails db:migrate' 23 | 24 | puts "\n== Removing old logs and tempfiles ==" 25 | system! 'bin/rails log:clear tmp:clear' 26 | 27 | puts "\n== Restarting application server ==" 28 | system! 'bin/rails restart' 29 | end 30 | -------------------------------------------------------------------------------- /config/routes.rb: -------------------------------------------------------------------------------- 1 | Rails.application.routes.draw do 2 | devise_for :users 3 | # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 4 | 5 | resources :orders do 6 | member do 7 | post :pay_with_alipay 8 | post :pay_with_wechat 9 | post :apply_to_cancel 10 | end 11 | end 12 | 13 | namespace :account do 14 | resources :orders 15 | end 16 | 17 | resource :cart do 18 | collection do 19 | post :clean 20 | post :checkout 21 | end 22 | end 23 | 24 | resources :cart_items 25 | 26 | resources :products do 27 | member do 28 | post :add_to_cart 29 | end 30 | end 31 | 32 | namespace :admin do 33 | resources :products 34 | resources :orders do 35 | member do 36 | post :cancel 37 | post :ship 38 | post :shipped 39 | post :return 40 | end 41 | end 42 | end 43 | 44 | root "products#index" 45 | 46 | end 47 | -------------------------------------------------------------------------------- /config/locales/simple_form.en.yml: -------------------------------------------------------------------------------- 1 | en: 2 | simple_form: 3 | "yes": 'Yes' 4 | "no": 'No' 5 | required: 6 | text: 'required' 7 | mark: '*' 8 | # You can uncomment the line below if you need to overwrite the whole required html. 9 | # When using html, text and mark won't be used. 10 | # html: '*' 11 | error_notification: 12 | default_message: "Please review the problems below:" 13 | # Examples 14 | # labels: 15 | # defaults: 16 | # password: 'Password' 17 | # user: 18 | # new: 19 | # email: 'E-mail to sign in.' 20 | # edit: 21 | # email: 'E-mail.' 22 | # hints: 23 | # defaults: 24 | # username: 'User name to sign in.' 25 | # password: 'No special characters, please.' 26 | # include_blanks: 27 | # defaults: 28 | # age: 'Rather not say' 29 | # prompts: 30 | # defaults: 31 | # age: 'Select your age' 32 | -------------------------------------------------------------------------------- /app/views/products/show.html.erb: -------------------------------------------------------------------------------- 1 |
2 |
3 | <% if @product.image.present? %> 4 | <%= image_tag(@product.image.medium.url, class: "thumbnail") %> 5 | <% else %> 6 | <%= image_tag("http://placehold.it/400x400&text=No Pic", class: "thumbnail") %> 7 | <% end %> 8 |
9 |
10 |

<%= @product.title %>

11 |
12 |

13 | <%= @product.description %> 14 |

15 |
16 |
数量 : <%= @product.quantity %>
17 |
¥ <%= @product.price %>
18 |
19 | 20 | <% if @product.quantity.present? && @product.quantity > 0 %> 21 | <%= link_to("加入购物车", add_to_cart_product_path(@product), method: :post, 22 | class: "btn btn-lg btn-danger") %> 23 | <% else %> 24 | 已销售一空,无法购买 25 | <% end %> 26 | 27 |
28 |
29 |
30 | -------------------------------------------------------------------------------- /config/secrets.yml: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Your secret key is used for verifying the integrity of signed cookies. 4 | # If you change this key, all old signed cookies will become invalid! 5 | 6 | # Make sure the secret is at least 30 characters and all random, 7 | # no regular words or you'll be exposed to dictionary attacks. 8 | # You can use `rails secret` to generate a secure secret key. 9 | 10 | # Make sure the secrets in this file are kept private 11 | # if you're sharing your code publicly. 12 | 13 | development: 14 | secret_key_base: 544c5c1b71d459dd7656a59e1a9ac631bb310248355e87c60aa85540fafcd458d7049cd31e184e441896e126149b502d07397f379bd5344352c3e55dd836375c 15 | 16 | test: 17 | secret_key_base: d8b1251b470d4003fba04241d1708eb9efd654cb3e8481dcd7eb6cf6434745ca1d27ccf48847dfe3c2f591fd1c7fc3b9e566169eb8a64a0040e4ab0edb75747a 18 | 19 | # Do not keep production secrets in the repository, 20 | # instead read values from the environment. 21 | production: 22 | secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> 23 | -------------------------------------------------------------------------------- /app/controllers/admin/orders_controller.rb: -------------------------------------------------------------------------------- 1 | class Admin::OrdersController < ApplicationController 2 | layout "admin" 3 | 4 | before_action :authenticate_user! 5 | before_action :admin_required 6 | 7 | def index 8 | @orders = Order.order("id DESC").paginate(:page => params[:page]) 9 | end 10 | 11 | def show 12 | @order = Order.find(params[:id]) 13 | @product_lists = @order.product_lists 14 | end 15 | 16 | def ship 17 | @order = Order.find(params[:id]) 18 | @order.ship! 19 | 20 | OrderMailer.notify_ship(@order).deliver! 21 | 22 | redirect_to :back 23 | end 24 | 25 | def shipped 26 | @order = Order.find(params[:id]) 27 | @order.deliver! 28 | redirect_to :back 29 | end 30 | 31 | def cancel 32 | @order = Order.find(params[:id]) 33 | @order.cancell_order! 34 | 35 | OrderMailer.notify_cancel(@order).deliver! 36 | 37 | redirect_to :back 38 | end 39 | 40 | def return 41 | @order = Order.find(params[:id]) 42 | @order.return_good! 43 | redirect_to :back 44 | end 45 | 46 | end 47 | -------------------------------------------------------------------------------- /bin/setup: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | require 'pathname' 3 | require 'fileutils' 4 | include FileUtils 5 | 6 | # path to your application root. 7 | APP_ROOT = Pathname.new File.expand_path('../../', __FILE__) 8 | 9 | def system!(*args) 10 | system(*args) || abort("\n== Command #{args} failed ==") 11 | end 12 | 13 | chdir APP_ROOT do 14 | # This script is a starting point to setup your application. 15 | # Add necessary setup steps to this file. 16 | 17 | puts '== Installing dependencies ==' 18 | system! 'gem install bundler --conservative' 19 | system('bundle check') || system!('bundle install') 20 | 21 | # puts "\n== Copying sample files ==" 22 | # unless File.exist?('config/database.yml') 23 | # cp 'config/database.yml.sample', 'config/database.yml' 24 | # end 25 | 26 | puts "\n== Preparing database ==" 27 | system! 'bin/rails db:setup' 28 | 29 | puts "\n== Removing old logs and tempfiles ==" 30 | system! 'bin/rails log:clear tmp:clear' 31 | 32 | puts "\n== Restarting application server ==" 33 | system! 'bin/rails restart' 34 | end 35 | -------------------------------------------------------------------------------- /app/controllers/admin/products_controller.rb: -------------------------------------------------------------------------------- 1 | class Admin::ProductsController < ApplicationController 2 | 3 | layout "admin" 4 | 5 | before_action :authenticate_user! 6 | before_action :admin_required 7 | 8 | def index 9 | @products = Product.all 10 | end 11 | 12 | def show 13 | @product = Product.find(params[:id]) 14 | end 15 | 16 | def new 17 | @product = Product.new 18 | end 19 | 20 | def create 21 | @product = Product.new(product_params) 22 | 23 | if @product.save 24 | redirect_to admin_products_path 25 | else 26 | render :new 27 | end 28 | end 29 | 30 | def edit 31 | @product = Product.find(params[:id]) 32 | end 33 | 34 | def update 35 | @product = Product.find(params[:id]) 36 | 37 | if @product.update(product_params) 38 | redirect_to admin_products_path 39 | else 40 | render :edit 41 | end 42 | end 43 | 44 | private 45 | 46 | def product_params 47 | params.require(:product).permit(:title, :description, :quantity, :price, :image) 48 | end 49 | 50 | end 51 | -------------------------------------------------------------------------------- /app/mailers/order_mailer.rb: -------------------------------------------------------------------------------- 1 | class OrderMailer < ApplicationMailer 2 | 3 | def notify_order_placed(order) 4 | @order = order 5 | @user = order.user 6 | @product_lists = @order.product_lists 7 | 8 | mail(to: @user.email , subject: "[JDstore] 感谢您完成本次的下单,以下是您这次购物明细 #{order.token}") 9 | end 10 | 11 | def apply_cancel(order) 12 | @order = order 13 | @user = order.user 14 | @product_lists = @order.product_lists 15 | 16 | mail(to: "admin@test.com" , subject: "[JDStore] 用户#{order.user.email}申请取消订单 #{order.token}") 17 | end 18 | 19 | def notify_ship(order) 20 | @order = order 21 | @user = order.user 22 | @product_lists = @order.product_lists 23 | 24 | mail(to: @user.email, subject: "[JDStore] 您的订单 #{order.token}已发货") 25 | end 26 | 27 | def notify_cancel(order) 28 | @order = order 29 | @user = order.user 30 | @product_lists = @order.product_lists 31 | 32 | mail(to: @user.email, subject: "[JDStore] 您的订单 #{order.token}已取消") 33 | end 34 | 35 | end 36 | -------------------------------------------------------------------------------- /app/views/admin/orders/_state_option.html.erb: -------------------------------------------------------------------------------- 1 |
2 | 3 | <% case order.aasm_state %> 4 | <% when "order_placed" %> 5 | <%= link_to("取消订单", 6 | cancel_admin_order_path(order), 7 | class: "btn btn-default btn-sm", method: :post) %> 8 | 9 | <% when "paid" %> 10 | <%= link_to("取消订单", 11 | cancel_admin_order_path(order), 12 | class: "btn btn-default btn-sm", method: :post) %> 13 | <%= link_to("出货", 14 | ship_admin_order_path(order), 15 | class: "btn btn-default btn-sm", method: :post) %> 16 | 17 | <% when "shipping" %> 18 | <%= link_to("设为已出货", 19 | shipped_admin_order_path(order), 20 | class: "btn btn-default btn-sm", method: :post) %> 21 | 22 | <% when "shipped" %> 23 | <%= link_to("退货", 24 | return_admin_order_path(order), 25 | class: "btn btn-default btn-sm", method: :post) %> 26 | 27 | <% when "order_cancelled" %> 28 | 订单已取消 29 | 30 | <% when "good_returned" %> 31 | 已退货 32 | 33 | <% end %> 34 | 35 |
36 | -------------------------------------------------------------------------------- /config/initializers/new_framework_defaults.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | # 3 | # This file contains migration options to ease your Rails 5.0 upgrade. 4 | # 5 | # Read the Guide for Upgrading Ruby on Rails for more info on each option. 6 | 7 | # Enable per-form CSRF tokens. Previous versions had false. 8 | Rails.application.config.action_controller.per_form_csrf_tokens = true 9 | 10 | # Enable origin-checking CSRF mitigation. Previous versions had false. 11 | Rails.application.config.action_controller.forgery_protection_origin_check = true 12 | 13 | # Make Ruby 2.4 preserve the timezone of the receiver when calling `to_time`. 14 | # Previous versions had false. 15 | ActiveSupport.to_time_preserves_timezone = true 16 | 17 | # Require `belongs_to` associations by default. Previous versions had false. 18 | Rails.application.config.active_record.belongs_to_required_by_default = true 19 | 20 | # Do not halt callback chains when a callback returns false. Previous versions had true. 21 | ActiveSupport.halt_callback_chains_on_return_false = false 22 | 23 | # Configure SSL options to enable HSTS with subdomains. Previous versions had false. 24 | Rails.application.config.ssl_options = { hsts: { subdomains: true } } 25 | -------------------------------------------------------------------------------- /app/views/admin/products/index.html.erb: -------------------------------------------------------------------------------- 1 |

Product List

2 |
3 | <%= link_to("新增产品", new_admin_product_path, class: "btn btn-primary btn-sm") %> 4 |
5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | <% @products.each do |product| %> 17 | 18 | 21 | 30 | 33 | 36 | 39 | 40 | <% end %> 41 | 42 |
#Product PicNamePrice Options
19 | <%= product.id %> 20 | 22 | <%= link_to product_path(product) do %> 23 | <% if product.image.present? %> 24 | <%= image_tag(product.image.thumb.url, class: "thumbnail") %> 25 | <% else %> 26 | <%= image_tag("http://placehold.it/200x200&text=No Pic", class: "thumbnail") %> 27 | <% end %> 28 | <% end %> 29 | 31 | <%= product.title %> 32 | 34 | <%= product.price %> 35 | 37 | <%= link_to("Edit", edit_admin_product_path(product)) %> 38 |
43 | -------------------------------------------------------------------------------- /app/models/order.rb: -------------------------------------------------------------------------------- 1 | class Order < ApplicationRecord 2 | 3 | belongs_to :user 4 | has_many :product_lists 5 | 6 | validates :billing_name, presence: true 7 | validates :billing_address, presence: true 8 | validates :shipping_name, presence: true 9 | validates :shipping_address, presence: true 10 | 11 | before_create :generate_token 12 | 13 | include AASM 14 | aasm do 15 | state :order_placed, initial: true 16 | state :paid 17 | state :shipping 18 | state :shipped 19 | state :order_cancelled 20 | state :good_returned 21 | 22 | event :make_payment, after_commit: :pay! do 23 | transitions from: :order_placed, to: :paid 24 | end 25 | 26 | event :ship do 27 | transitions from: :paid, to: :shipping 28 | end 29 | 30 | event :deliver do 31 | transitions from: :shipping, to: :shipped 32 | end 33 | 34 | event :return_good do 35 | transitions from: :shipped, to: :good_returned 36 | end 37 | 38 | event :cancell_order do 39 | transitions from: [:order_placed, :paid], to: :order_cancelled 40 | end 41 | end 42 | 43 | def set_payment_with!(method) 44 | self.update_columns(payment_method: method ) 45 | end 46 | 47 | def pay! 48 | self.update_columns(is_paid: true) 49 | end 50 | 51 | def generate_token 52 | self.token = SecureRandom.uuid 53 | end 54 | 55 | end 56 | -------------------------------------------------------------------------------- /db/migrate/20170215044850_devise_create_users.rb: -------------------------------------------------------------------------------- 1 | class DeviseCreateUsers < ActiveRecord::Migration[5.0] 2 | def change 3 | create_table :users do |t| 4 | ## Database authenticatable 5 | t.string :email, null: false, default: "" 6 | t.string :encrypted_password, null: false, default: "" 7 | 8 | ## Recoverable 9 | t.string :reset_password_token 10 | t.datetime :reset_password_sent_at 11 | 12 | ## Rememberable 13 | t.datetime :remember_created_at 14 | 15 | ## Trackable 16 | t.integer :sign_in_count, default: 0, null: false 17 | t.datetime :current_sign_in_at 18 | t.datetime :last_sign_in_at 19 | t.string :current_sign_in_ip 20 | t.string :last_sign_in_ip 21 | 22 | ## Confirmable 23 | # t.string :confirmation_token 24 | # t.datetime :confirmed_at 25 | # t.datetime :confirmation_sent_at 26 | # t.string :unconfirmed_email # Only if using reconfirmable 27 | 28 | ## Lockable 29 | # t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts 30 | # t.string :unlock_token # Only if unlock strategy is :email or :both 31 | # t.datetime :locked_at 32 | 33 | 34 | t.timestamps null: false 35 | end 36 | 37 | add_index :users, :email, unique: true 38 | add_index :users, :reset_password_token, unique: true 39 | # add_index :users, :confirmation_token, unique: true 40 | # add_index :users, :unlock_token, unique: true 41 | end 42 | end 43 | -------------------------------------------------------------------------------- /app/views/admin/orders/show.html.erb: -------------------------------------------------------------------------------- 1 |
2 |
3 | 4 |

订单明细 (<%= render_order_paid_state(@order) %>)

5 | 6 | <%= render "admin/orders/state_option", order: @order %> 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | <% @product_lists.each do |product_list| %> 19 | 20 | 23 | 26 | 29 | 30 | <% end %> 31 | 32 | 33 |
商品明细单价数量
21 | <%= product_list.product_name %> 22 | 24 | <%= product_list.product_price %> 25 | 27 | <%= product_list.quantity %> 28 |
34 | 35 |
36 | 37 | 总计 <%= @order.total %> CNY 38 | 39 |
40 | 41 |
42 | 43 |

寄送资讯

44 | 45 | 46 | 47 | 48 | 51 | 52 | 53 | 56 | 57 | 58 | 61 | 62 | 63 | 66 | 67 | 68 |
49 | 订购人 50 |
54 | <%= @order.billing_name %> - <%= @order.billing_address %> 55 |
59 | 收件人 60 |
64 | <%= @order.shipping_name %> - <%= @order.shipping_address %> 65 |
69 | 70 |
71 |
72 | -------------------------------------------------------------------------------- /public/500.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | We're sorry, but something went wrong (500) 5 | 6 | 55 | 56 | 57 | 58 | 59 |
60 |
61 |

We're sorry, but something went wrong.

62 |
63 |

If you are the application owner check the logs for more information.

64 |
65 | 66 | 67 | -------------------------------------------------------------------------------- /app/views/order_mailer/notify_ship.html.erb: -------------------------------------------------------------------------------- 1 |
2 |
3 |

4 | 订单明细
5 | 6 | <%= link_to("订单连结", order_url(@order.token)) %> 7 | 8 |

9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | <% @product_lists.each do |product_list| %> 21 | 22 | 25 | 28 | 31 | 34 | 35 | <% end %> 36 | 37 | 38 |
商品明细单价数量小记
23 | <%= product_list.product_name %> 24 | 26 | <%= product_list.product_price %> 27 | 29 | <%= product_list.quantity %> 30 | 32 | <%= product_list.quantity * product_list.product_price %> 33 |
39 | 40 |
41 |

42 | 总计 <%= @order.total %> CNY 43 |

44 |
45 | 46 |
47 | 48 |

寄送资讯

49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 59 | 60 | 61 | 62 | 63 | 64 | 67 | 68 | 69 |
订购人
57 | <%= @order.billing_name %> - <%= @order.billing_address %> 58 |
收件者
65 | <%= @order.shipping_name %> - <%= @order.shipping_address %> 66 |
70 |
71 |
72 | -------------------------------------------------------------------------------- /public/422.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | The change you wanted was rejected (422) 5 | 6 | 55 | 56 | 57 | 58 | 59 |
60 |
61 |

The change you wanted was rejected.

62 |

Maybe you tried to change something you didn't have access to.

63 |
64 |

If you are the application owner check the logs for more information.

65 |
66 | 67 | 68 | -------------------------------------------------------------------------------- /app/views/order_mailer/apply_cancel.html.erb: -------------------------------------------------------------------------------- 1 |
2 |
3 |

4 | 订单明细
5 | 6 | <%= link_to("订单连结", order_url(@order.token)) %> 7 | 8 |

9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | <% @product_lists.each do |product_list| %> 21 | 22 | 25 | 28 | 31 | 34 | 35 | <% end %> 36 | 37 | 38 |
商品明细单价数量小记
23 | <%= product_list.product_name %> 24 | 26 | <%= product_list.product_price %> 27 | 29 | <%= product_list.quantity %> 30 | 32 | <%= product_list.quantity * product_list.product_price %> 33 |
39 | 40 |
41 |

42 | 总计 <%= @order.total %> CNY 43 |

44 |
45 | 46 |
47 | 48 |

寄送资讯

49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 59 | 60 | 61 | 62 | 63 | 64 | 67 | 68 | 69 |
订购人
57 | <%= @order.billing_name %> - <%= @order.billing_address %> 58 |
收件者
65 | <%= @order.shipping_name %> - <%= @order.shipping_address %> 66 |
70 |
71 |
72 | -------------------------------------------------------------------------------- /app/views/order_mailer/notify_cancel.html.erb: -------------------------------------------------------------------------------- 1 |
2 |
3 |

4 | 订单明细
5 | 6 | <%= link_to("订单连结", order_url(@order.token)) %> 7 | 8 |

9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | <% @product_lists.each do |product_list| %> 21 | 22 | 25 | 28 | 31 | 34 | 35 | <% end %> 36 | 37 | 38 |
商品明细单价数量小记
23 | <%= product_list.product_name %> 24 | 26 | <%= product_list.product_price %> 27 | 29 | <%= product_list.quantity %> 30 | 32 | <%= product_list.quantity * product_list.product_price %> 33 |
39 | 40 |
41 |

42 | 总计 <%= @order.total %> CNY 43 |

44 |
45 | 46 |
47 | 48 |

寄送资讯

49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 59 | 60 | 61 | 62 | 63 | 64 | 67 | 68 | 69 |
订购人
57 | <%= @order.billing_name %> - <%= @order.billing_address %> 58 |
收件者
65 | <%= @order.shipping_name %> - <%= @order.shipping_address %> 66 |
70 |
71 |
72 | -------------------------------------------------------------------------------- /app/views/order_mailer/notify_order_placed.html.erb: -------------------------------------------------------------------------------- 1 |
2 |
3 |

4 | 订单明细
5 | 6 | <%= link_to("订单连结", order_url(@order.token)) %> 7 | 8 |

9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | <% @product_lists.each do |product_list| %> 21 | 22 | 25 | 28 | 31 | 34 | 35 | <% end %> 36 | 37 | 38 |
商品明细单价数量小记
23 | <%= product_list.product_name %> 24 | 26 | <%= product_list.product_price %> 27 | 29 | <%= product_list.quantity %> 30 | 32 | <%= product_list.quantity * product_list.product_price %> 33 |
39 | 40 |
41 |

42 | 总计 <%= @order.total %> CNY 43 |

44 |
45 | 46 |
47 | 48 |

寄送资讯

49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 59 | 60 | 61 | 62 | 63 | 64 | 67 | 68 | 69 |
订购人
57 | <%= @order.billing_name %> - <%= @order.billing_address %> 58 |
收件者
65 | <%= @order.shipping_name %> - <%= @order.shipping_address %> 66 |
70 |
71 |
72 | -------------------------------------------------------------------------------- /public/404.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | The page you were looking for doesn't exist (404) 5 | 6 | 55 | 56 | 57 | 58 | 59 |
60 |
61 |

The page you were looking for doesn't exist.

62 |

You may have mistyped the address or the page may have moved.

63 |
64 |

If you are the application owner check the logs for more information.

65 |
66 | 67 | 68 | -------------------------------------------------------------------------------- /app/controllers/orders_controller.rb: -------------------------------------------------------------------------------- 1 | class OrdersController < ApplicationController 2 | 3 | before_action :authenticate_user!, only: [:create] 4 | 5 | def show 6 | @order = Order.find_by_token(params[:id]) 7 | @product_lists = @order.product_lists 8 | end 9 | 10 | def create 11 | @order = Order.new(order_params) 12 | @order.user = current_user 13 | @order.total = current_cart.total_price 14 | 15 | if @order.save 16 | 17 | current_cart.cart_items.each do |cart_item| 18 | product_list = ProductList.new 19 | product_list.order = @order 20 | product_list.product_name = cart_item.product.title 21 | product_list.product_price = cart_item.product.price 22 | product_list.quantity = cart_item.quantity 23 | product_list.save 24 | end 25 | 26 | current_cart.clean! 27 | OrderMailer.notify_order_placed(@order).deliver! 28 | 29 | redirect_to order_path(@order.token) 30 | else 31 | render 'carts/checkout' 32 | end 33 | end 34 | 35 | def pay_with_alipay 36 | @order = Order.find_by_token(params[:id]) 37 | @order.set_payment_with!("alipay") 38 | @order.make_payment! 39 | 40 | redirect_to order_path(@order.token), notice: "使用支付宝成功完成付款" 41 | end 42 | 43 | def pay_with_wechat 44 | @order = Order.find_by_token(params[:id]) 45 | @order.set_payment_with!("wechat") 46 | @order.make_payment! 47 | 48 | redirect_to order_path(@order.token), notice: "使用微信支付成功完成付款" 49 | end 50 | 51 | def apply_to_cancel 52 | @order = Order.find(params[:id]) 53 | OrderMailer.apply_cancel(@order).deliver! 54 | flash[:notice] = "已提交申请" 55 | redirect_to :back 56 | end 57 | 58 | private 59 | 60 | def order_params 61 | params.require(:order).permit(:billing_name, :billing_address, :shipping_name, :shipping_address) 62 | end 63 | 64 | end 65 | -------------------------------------------------------------------------------- /lib/tasks/dev.rake: -------------------------------------------------------------------------------- 1 | namespace :dev do 2 | 3 | task :fake => [:fake_products, :fake_users, :fake_orders] 4 | 5 | task :fake_products => :environment do 6 | 10.times do 7 | Product.create!( :title => Faker::Commerce.product_name, 8 | :description => Faker::Lorem.paragraph, 9 | :quantity => rand(100), 10 | :price => ( rand(100)+1 ) * 10 ) 11 | end 12 | end 13 | 14 | task :fake_users => :environment do 15 | 10.times do 16 | User.create!( :email => Faker::Internet.email, :password => "12345678") 17 | end 18 | end 19 | 20 | task :fake_orders => :environment do 21 | users = User.all 22 | products = Product.all 23 | 24 | 100.times do 25 | order = Order.new( 26 | :user => users.sample, 27 | :billing_name => Faker::Name.name, 28 | :billing_address => Faker::Address.street_address, 29 | :shipping_name => Faker::Name.name, 30 | :shipping_address => Faker::Address.street_address, 31 | ) 32 | 33 | products.sample( rand(3)+1 ).each do |p| 34 | order.product_lists.build( :product_name => p.title, 35 | :product_price => p.price, 36 | :quantity => rand(5)+1, 37 | ) 38 | end 39 | 40 | order.total = order.product_lists.map{ |p| p.product_price * p.quantity }.sum 41 | order.created_at = Time.now - (rand(100)+1) * 3600 42 | order.save! 43 | end 44 | 45 | 46 | %w[ paid shipping shipped order_cancelled good_returned ].each do |state| 47 | Order.all.sample(10).each do |o| 48 | o.update_columns( :aasm_state => state, 49 | :is_paid => [true, false].sample, 50 | :payment_method => ["alipay", "wechat"].sample ) 51 | end 52 | end 53 | 54 | end 55 | end 56 | -------------------------------------------------------------------------------- /app/uploaders/image_uploader.rb: -------------------------------------------------------------------------------- 1 | class ImageUploader < CarrierWave::Uploader::Base 2 | 3 | # Include RMagick or MiniMagick support: 4 | # include CarrierWave::RMagick 5 | include CarrierWave::MiniMagick 6 | 7 | # Choose what kind of storage to use for this uploader: 8 | storage :file 9 | # storage :fog 10 | 11 | # Override the directory where uploaded files will be stored. 12 | # This is a sensible default for uploaders that are meant to be mounted: 13 | def store_dir 14 | "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 15 | end 16 | 17 | process resize_to_fit: [800, 800] 18 | 19 | version :thumb do 20 | process resize_to_fill: [200,200] 21 | end 22 | 23 | version :medium do 24 | process resize_to_fill: [400,400] 25 | end 26 | 27 | # Provide a default URL as a default if there hasn't been a file uploaded: 28 | # def default_url 29 | # # For Rails 3.1+ asset pipeline compatibility: 30 | # # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_')) 31 | # 32 | # "/images/fallback/" + [version_name, "default.png"].compact.join('_') 33 | # end 34 | 35 | # Process files as they are uploaded: 36 | # process scale: [200, 300] 37 | # 38 | # def scale(width, height) 39 | # # do something 40 | # end 41 | 42 | # Create different versions of your uploaded files: 43 | # version :thumb do 44 | # process resize_to_fit: [50, 50] 45 | # end 46 | 47 | # Add a white list of extensions which are allowed to be uploaded. 48 | # For images you might use something like this: 49 | # def extension_whitelist 50 | # %w(jpg jpeg gif png) 51 | # end 52 | 53 | # Override the filename of the uploaded files: 54 | # Avoid using model.id or version_name here, see uploader/store.rb for details. 55 | # def filename 56 | # "something.jpg" if original_filename 57 | # end 58 | 59 | end 60 | -------------------------------------------------------------------------------- /config/environments/test.rb: -------------------------------------------------------------------------------- 1 | Rails.application.configure do 2 | # Settings specified here will take precedence over those in config/application.rb. 3 | 4 | # The test environment is used exclusively to run your application's 5 | # test suite. You never need to work with it otherwise. Remember that 6 | # your test database is "scratch space" for the test suite and is wiped 7 | # and recreated between test runs. Don't rely on the data there! 8 | config.cache_classes = true 9 | 10 | # Do not eager load code on boot. This avoids loading your whole application 11 | # just for the purpose of running a single test. If you are using a tool that 12 | # preloads Rails for running tests, you may have to set it to true. 13 | config.eager_load = false 14 | 15 | # Configure public file server for tests with Cache-Control for performance. 16 | config.public_file_server.enabled = true 17 | config.public_file_server.headers = { 18 | 'Cache-Control' => 'public, max-age=3600' 19 | } 20 | 21 | # Show full error reports and disable caching. 22 | config.consider_all_requests_local = true 23 | config.action_controller.perform_caching = false 24 | 25 | # Raise exceptions instead of rendering exception templates. 26 | config.action_dispatch.show_exceptions = false 27 | 28 | # Disable request forgery protection in test environment. 29 | config.action_controller.allow_forgery_protection = false 30 | config.action_mailer.perform_caching = false 31 | 32 | # Tell Action Mailer not to deliver emails to the real world. 33 | # The :test delivery method accumulates sent emails in the 34 | # ActionMailer::Base.deliveries array. 35 | config.action_mailer.delivery_method = :test 36 | 37 | # Print deprecation notices to the stderr. 38 | config.active_support.deprecation = :stderr 39 | 40 | # Raises error for missing translations 41 | # config.action_view.raise_on_missing_translations = true 42 | end 43 | -------------------------------------------------------------------------------- /app/views/carts/checkout.html.erb: -------------------------------------------------------------------------------- 1 |
2 |
3 | 4 |

购物明细

5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | <% current_cart.cart_items.each do |cart_item| %> 17 | 18 | 21 | 24 | 25 | 28 | 29 | 30 | <% end %> 31 | 32 | 33 |
商品明细单价数量
19 | <%= link_to(cart_item.product.title, product_path(cart_item.product)) %> 20 | 22 | <%= cart_item.product.price %> 23 | 26 | <%= cart_item.quantity %> 27 |
34 | 35 |
36 | 37 | 总计 <%= current_cart.total_price %> CNY 38 | 39 |
40 | 41 |
42 | 43 |

订单资讯

44 | 45 |
46 | 47 | <%= simple_form_for @order do |f| %> 48 | 49 | 50 | 51 | 订购人 52 |
53 | <%= f.input :billing_name %> 54 |
55 |
56 | <%= f.input :billing_address %> 57 |
58 | 59 | 收货人 60 |
61 | <%= f.input :shipping_name %> 62 |
63 |
64 | <%= f.input :shipping_address %> 65 |
66 | 67 |
68 | <%= f.submit "生成订单", class: "btn btn-lg btn-danger pull-right", 69 | data: { disable_with: "Submitting..." } %> 70 |
71 | <% end %> 72 | 73 |
74 |
75 |
76 | -------------------------------------------------------------------------------- /app/views/orders/show.html.erb: -------------------------------------------------------------------------------- 1 |
2 |
3 | 4 |

订单明细

5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | <% @product_lists.each do |product_list| %> 16 | 17 | 20 | 23 | 24 | <% end %> 25 | 26 | 27 |
商品明细单价
18 | <%= product_list.product_name %> 19 | 21 | <%= product_list.product_price %> 22 |
28 | 29 |
30 | 31 | 总计 <%= @order.total %> CNY 32 | 33 |
34 | 35 |
36 | 37 |

寄送资讯

38 | 39 | 40 | 41 | 42 | 45 | 46 | 47 | 50 | 51 | 52 | 55 | 56 | 57 | 60 | 61 | 62 |
43 | 订购人 44 |
48 | <%= @order.billing_name %> - <%= @order.billing_address %> 49 |
53 | 收件人 54 |
58 | <%= @order.shipping_name %> - <%= @order.shipping_address %> 59 |
63 | 64 |
65 | <% if @order.order_placed? || @order.paid? %> 66 | <%= link_to("申请取消订单", apply_to_cancel_order_path(@order), method: :post, class: "btn btn-info") %> 67 | <% end %> 68 |
69 | 70 | <% if !@order.is_paid %> 71 |
72 | <%= link_to("以支付宝支付", pay_with_alipay_order_path(@order.token), :method => :post, :class => "btn btn-danger") %> 73 | <%= link_to("以微信支付", pay_with_wechat_order_path(@order.token), :method => :post, :class => "btn btn-danger") %> 74 |
75 | <% else %> 76 |

此订单已完成付款

77 | <% end %> 78 | 79 |
80 |
81 | -------------------------------------------------------------------------------- /config/puma.rb: -------------------------------------------------------------------------------- 1 | # Puma can serve each request in a thread from an internal thread pool. 2 | # The `threads` method setting takes two numbers a minimum and maximum. 3 | # Any libraries that use thread pools should be configured to match 4 | # the maximum value specified for Puma. Default is set to 5 threads for minimum 5 | # and maximum, this matches the default thread size of Active Record. 6 | # 7 | threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }.to_i 8 | threads threads_count, threads_count 9 | 10 | # Specifies the `port` that Puma will listen on to receive requests, default is 3000. 11 | # 12 | port ENV.fetch("PORT") { 3000 } 13 | 14 | # Specifies the `environment` that Puma will run in. 15 | # 16 | environment ENV.fetch("RAILS_ENV") { "development" } 17 | 18 | # Specifies the number of `workers` to boot in clustered mode. 19 | # Workers are forked webserver processes. If using threads and workers together 20 | # the concurrency of the application would be max `threads` * `workers`. 21 | # Workers do not work on JRuby or Windows (both of which do not support 22 | # processes). 23 | # 24 | # workers ENV.fetch("WEB_CONCURRENCY") { 2 } 25 | 26 | # Use the `preload_app!` method when specifying a `workers` number. 27 | # This directive tells Puma to first boot the application and load code 28 | # before forking the application. This takes advantage of Copy On Write 29 | # process behavior so workers use less memory. If you use this option 30 | # you need to make sure to reconnect any threads in the `on_worker_boot` 31 | # block. 32 | # 33 | # preload_app! 34 | 35 | # The code in the `on_worker_boot` will be called if you are using 36 | # clustered mode by specifying a number of `workers`. After each worker 37 | # process is booted this block will be run, if you are using `preload_app!` 38 | # option you will want to use this block to reconnect to any threads 39 | # or connections that may have been created at application boot, Ruby 40 | # cannot share connections between processes. 41 | # 42 | # on_worker_boot do 43 | # ActiveRecord::Base.establish_connection if defined?(ActiveRecord) 44 | # end 45 | 46 | # Allow puma to be restarted by `rails restart` command. 47 | plugin :tmp_restart 48 | -------------------------------------------------------------------------------- /config/environments/development.rb: -------------------------------------------------------------------------------- 1 | Rails.application.configure do 2 | # Settings specified here will take precedence over those in config/application.rb. 3 | 4 | # In the development environment your application's code is reloaded on 5 | # every request. This slows down response time but is perfect for development 6 | # since you don't have to restart the web server when you make code changes. 7 | config.cache_classes = false 8 | 9 | # Do not eager load code on boot. 10 | config.eager_load = false 11 | 12 | # Show full error reports. 13 | config.consider_all_requests_local = true 14 | 15 | # Enable/disable caching. By default caching is disabled. 16 | if Rails.root.join('tmp/caching-dev.txt').exist? 17 | config.action_controller.perform_caching = true 18 | 19 | config.cache_store = :memory_store 20 | config.public_file_server.headers = { 21 | 'Cache-Control' => 'public, max-age=172800' 22 | } 23 | else 24 | config.action_controller.perform_caching = false 25 | 26 | config.cache_store = :null_store 27 | end 28 | 29 | # Don't care if the mailer can't send. 30 | config.action_mailer.raise_delivery_errors = false 31 | 32 | config.action_mailer.perform_caching = false 33 | 34 | # Print deprecation notices to the Rails logger. 35 | config.active_support.deprecation = :log 36 | 37 | # Raise an error on page load if there are pending migrations. 38 | config.active_record.migration_error = :page_load 39 | 40 | # Debug mode disables concatenation and preprocessing of assets. 41 | # This option may cause significant delays in view rendering with a large 42 | # number of complex assets. 43 | config.assets.debug = true 44 | 45 | # Suppress logger output for asset requests. 46 | config.assets.quiet = true 47 | 48 | # Raises error for missing translations 49 | # config.action_view.raise_on_missing_translations = true 50 | 51 | # Use an evented file watcher to asynchronously detect changes in source code, 52 | # routes, locales, etc. This feature depends on the listen gem. 53 | config.file_watcher = ActiveSupport::EventedFileUpdateChecker 54 | 55 | config.action_mailer.default_url_options = { host: 'localhost:3000' } 56 | config.action_mailer.delivery_method = :letter_opener 57 | 58 | end 59 | -------------------------------------------------------------------------------- /app/views/common/_navbar.html.erb: -------------------------------------------------------------------------------- 1 | 55 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | git_source(:github) do |repo_name| 4 | repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/") 5 | "https://github.com/#{repo_name}.git" 6 | end 7 | 8 | 9 | # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' 10 | gem 'rails', '~> 5.0.1' 11 | # Use sqlite3 as the database for Active Record 12 | gem 'sqlite3' 13 | # Use Puma as the app server 14 | gem 'puma', '~> 3.0' 15 | # Use SCSS for stylesheets 16 | gem 'sass-rails', '~> 5.0' 17 | # Use Uglifier as compressor for JavaScript assets 18 | gem 'uglifier', '>= 1.3.0' 19 | # Use CoffeeScript for .coffee assets and views 20 | gem 'coffee-rails', '~> 4.2' 21 | # See https://github.com/rails/execjs#readme for more supported runtimes 22 | # gem 'therubyracer', platforms: :ruby 23 | 24 | gem 'devise' 25 | 26 | #gem 'bootstrap-sass' 27 | #gem 'font-awesome-rails' 28 | 29 | gem 'simple_form' 30 | 31 | gem 'carrierwave' 32 | gem 'mini_magick' 33 | 34 | gem 'aasm' 35 | gem 'will_paginate' 36 | 37 | gem 'faker' 38 | 39 | # Use jquery as the JavaScript library 40 | gem 'jquery-rails' 41 | # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks 42 | gem 'turbolinks', '~> 5' 43 | # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder 44 | gem 'jbuilder', '~> 2.5' 45 | # Use Redis adapter to run Action Cable in production 46 | # gem 'redis', '~> 3.0' 47 | # Use ActiveModel has_secure_password 48 | # gem 'bcrypt', '~> 3.1.7' 49 | 50 | # Use Capistrano for deployment 51 | # gem 'capistrano-rails', group: :development 52 | 53 | group :development, :test do 54 | # Call 'byebug' anywhere in the code to stop execution and get a debugger console 55 | gem 'byebug', platform: :mri 56 | end 57 | 58 | group :development do 59 | # Access an IRB console on exception pages or by using <%= console %> anywhere in the code. 60 | gem 'web-console', '>= 3.3.0' 61 | gem 'listen', '~> 3.0.5' 62 | # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring 63 | gem 'spring' 64 | gem 'spring-watcher-listen', '~> 2.0.0' 65 | 66 | gem 'letter_opener' 67 | end 68 | 69 | # Windows does not include zoneinfo files, so bundle the tzinfo-data gem 70 | gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] 71 | -------------------------------------------------------------------------------- /app/views/carts/show.html.erb: -------------------------------------------------------------------------------- 1 |
2 |
3 | 4 | <%= link_to("清空购物车", clean_cart_path , 5 | method: :post , class: "pull-right", 6 | style: "text-decoration: underline;", 7 | data: { confirm: "你确定要清空整个购物车吗?"} )%> 8 | 9 |

购物车

10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | <% current_cart.cart_items.each do |cart_item| %> 23 | 24 | 33 | 36 | 39 | 47 | 52 | 53 | <% end %> 54 | 55 | 56 |
商品资讯单价数量操作选项
25 | <%= link_to product_path(cart_item.product) do %> 26 | <% if cart_item.product.image.present? %> 27 | <%= image_tag(cart_item.product.image.thumb.url, class: "thumbnail") %> 28 | <% else %> 29 | <%= image_tag("http://placehold.it/200x200&text=No Pic", class: "thumbnail") %> 30 | <% end %> 31 | <% end %> 32 | 34 | <%= link_to(cart_item.product.title, product_path(cart_item.product)) %> 35 | 37 | <%= cart_item.product.price %> 38 | 40 | <% cart_item = current_cart.cart_items.find_by(product_id: cart_item.product_id) %> 41 | 42 | <%= form_for cart_item, url: cart_item_path(cart_item.product_id) do |f| %> 43 | <%= f.select :quantity, 1..cart_item.product.quantity %> 44 | <%= f.submit "更新", data: { disable_with: "Submiting..." } %> 45 | <% end %> 46 | 48 | <%= link_to cart_item_path(cart_item.product_id), method: :delete do %> 49 | 50 | <% end %> 51 |
57 | 58 |
59 | 60 |
61 | 62 | 总计 <%= current_cart.total_price %> RMB 63 | 64 |
65 | 66 |
67 | 68 |
69 | <%= link_to("确认结账", checkout_cart_path, method: :post, class: "btn btn-lg btn-danger pull-right") %> 70 |
71 |
72 |
73 | -------------------------------------------------------------------------------- /db/schema.rb: -------------------------------------------------------------------------------- 1 | # This file is auto-generated from the current state of the database. Instead 2 | # of editing this file, please use the migrations feature of Active Record to 3 | # incrementally modify your database, and then regenerate this schema definition. 4 | # 5 | # Note that this schema.rb definition is the authoritative source for your 6 | # database schema. If you need to create the application database on another 7 | # system, you should be using db:schema:load, not running all the migrations 8 | # from scratch. The latter is a flawed and unsustainable approach (the more migrations 9 | # you'll amass, the slower it'll run and the greater likelihood for issues). 10 | # 11 | # It's strongly recommended that you check this file into your version control system. 12 | 13 | ActiveRecord::Schema.define(version: 20170216105928) do 14 | 15 | create_table "cart_items", force: :cascade do |t| 16 | t.integer "cart_id" 17 | t.integer "product_id" 18 | t.integer "quantity", default: 1 19 | t.datetime "created_at", null: false 20 | t.datetime "updated_at", null: false 21 | end 22 | 23 | create_table "carts", force: :cascade do |t| 24 | t.datetime "created_at", null: false 25 | t.datetime "updated_at", null: false 26 | end 27 | 28 | create_table "orders", force: :cascade do |t| 29 | t.integer "total", default: 0 30 | t.integer "user_id" 31 | t.string "billing_name" 32 | t.string "billing_address" 33 | t.string "shipping_name" 34 | t.string "shipping_address" 35 | t.datetime "created_at", null: false 36 | t.datetime "updated_at", null: false 37 | t.string "token" 38 | t.string "payment_method" 39 | t.string "aasm_state", default: "order_placed" 40 | t.boolean "is_paid", default: false 41 | t.index ["aasm_state"], name: "index_orders_on_aasm_state" 42 | end 43 | 44 | create_table "product_lists", force: :cascade do |t| 45 | t.integer "order_id" 46 | t.string "product_name" 47 | t.integer "product_price" 48 | t.integer "quantity" 49 | t.datetime "created_at", null: false 50 | t.datetime "updated_at", null: false 51 | end 52 | 53 | create_table "products", force: :cascade do |t| 54 | t.string "title" 55 | t.text "description" 56 | t.integer "quantity" 57 | t.integer "price" 58 | t.datetime "created_at", null: false 59 | t.datetime "updated_at", null: false 60 | t.string "image" 61 | end 62 | 63 | create_table "users", force: :cascade do |t| 64 | t.string "email", default: "", null: false 65 | t.string "encrypted_password", default: "", null: false 66 | t.string "reset_password_token" 67 | t.datetime "reset_password_sent_at" 68 | t.datetime "remember_created_at" 69 | t.integer "sign_in_count", default: 0, null: false 70 | t.datetime "current_sign_in_at" 71 | t.datetime "last_sign_in_at" 72 | t.string "current_sign_in_ip" 73 | t.string "last_sign_in_ip" 74 | t.datetime "created_at", null: false 75 | t.datetime "updated_at", null: false 76 | t.string "role" 77 | t.index ["email"], name: "index_users_on_email", unique: true 78 | t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true 79 | end 80 | 81 | end 82 | -------------------------------------------------------------------------------- /config/environments/production.rb: -------------------------------------------------------------------------------- 1 | Rails.application.configure do 2 | # Settings specified here will take precedence over those in config/application.rb. 3 | 4 | # Code is not reloaded between requests. 5 | config.cache_classes = true 6 | 7 | # Eager load code on boot. This eager loads most of Rails and 8 | # your application in memory, allowing both threaded web servers 9 | # and those relying on copy on write to perform better. 10 | # Rake tasks automatically ignore this option for performance. 11 | config.eager_load = true 12 | 13 | # Full error reports are disabled and caching is turned on. 14 | config.consider_all_requests_local = false 15 | config.action_controller.perform_caching = true 16 | 17 | # Disable serving static files from the `/public` folder by default since 18 | # Apache or NGINX already handles this. 19 | config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present? 20 | 21 | # Compress JavaScripts and CSS. 22 | config.assets.js_compressor = :uglifier 23 | # config.assets.css_compressor = :sass 24 | 25 | # Do not fallback to assets pipeline if a precompiled asset is missed. 26 | config.assets.compile = false 27 | 28 | # `config.assets.precompile` and `config.assets.version` have moved to config/initializers/assets.rb 29 | 30 | # Enable serving of images, stylesheets, and JavaScripts from an asset server. 31 | # config.action_controller.asset_host = 'http://assets.example.com' 32 | 33 | # Specifies the header that your server uses for sending files. 34 | # config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache 35 | # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX 36 | 37 | # Mount Action Cable outside main process or domain 38 | # config.action_cable.mount_path = nil 39 | # config.action_cable.url = 'wss://example.com/cable' 40 | # config.action_cable.allowed_request_origins = [ 'http://example.com', /http:\/\/example.*/ ] 41 | 42 | # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. 43 | # config.force_ssl = true 44 | 45 | # Use the lowest log level to ensure availability of diagnostic information 46 | # when problems arise. 47 | config.log_level = :debug 48 | 49 | # Prepend all log lines with the following tags. 50 | config.log_tags = [ :request_id ] 51 | 52 | # Use a different cache store in production. 53 | # config.cache_store = :mem_cache_store 54 | 55 | # Use a real queuing backend for Active Job (and separate queues per environment) 56 | # config.active_job.queue_adapter = :resque 57 | # config.active_job.queue_name_prefix = "jdstore_#{Rails.env}" 58 | config.action_mailer.perform_caching = false 59 | 60 | # Ignore bad email addresses and do not raise email delivery errors. 61 | # Set this to true and configure the email server for immediate delivery to raise delivery errors. 62 | # config.action_mailer.raise_delivery_errors = false 63 | 64 | # Enable locale fallbacks for I18n (makes lookups for any locale fall back to 65 | # the I18n.default_locale when a translation cannot be found). 66 | config.i18n.fallbacks = true 67 | 68 | # Send deprecation notices to registered listeners. 69 | config.active_support.deprecation = :notify 70 | 71 | # Use default logging formatter so that PID and timestamp are not suppressed. 72 | config.log_formatter = ::Logger::Formatter.new 73 | 74 | # Use a different logger for distributed setups. 75 | # require 'syslog/logger' 76 | # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name') 77 | 78 | if ENV["RAILS_LOG_TO_STDOUT"].present? 79 | logger = ActiveSupport::Logger.new(STDOUT) 80 | logger.formatter = config.log_formatter 81 | config.logger = ActiveSupport::TaggedLogging.new(logger) 82 | end 83 | 84 | # Do not dump schema after migrations. 85 | config.active_record.dump_schema_after_migration = false 86 | end 87 | -------------------------------------------------------------------------------- /config/locales/devise.en.yml: -------------------------------------------------------------------------------- 1 | # Additional translations at https://github.com/plataformatec/devise/wiki/I18n 2 | 3 | en: 4 | devise: 5 | confirmations: 6 | confirmed: "Your email address has been successfully confirmed." 7 | send_instructions: "You will receive an email with instructions for how to confirm your email address in a few minutes." 8 | send_paranoid_instructions: "If your email address exists in our database, you will receive an email with instructions for how to confirm your email address in a few minutes." 9 | failure: 10 | already_authenticated: "You are already signed in." 11 | inactive: "Your account is not activated yet." 12 | invalid: "Invalid %{authentication_keys} or password." 13 | locked: "Your account is locked." 14 | last_attempt: "You have one more attempt before your account is locked." 15 | not_found_in_database: "Invalid %{authentication_keys} or password." 16 | timeout: "Your session expired. Please sign in again to continue." 17 | unauthenticated: "You need to sign in or sign up before continuing." 18 | unconfirmed: "You have to confirm your email address before continuing." 19 | mailer: 20 | confirmation_instructions: 21 | subject: "Confirmation instructions" 22 | reset_password_instructions: 23 | subject: "Reset password instructions" 24 | unlock_instructions: 25 | subject: "Unlock instructions" 26 | password_change: 27 | subject: "Password Changed" 28 | omniauth_callbacks: 29 | failure: "Could not authenticate you from %{kind} because \"%{reason}\"." 30 | success: "Successfully authenticated from %{kind} account." 31 | passwords: 32 | 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." 33 | send_instructions: "You will receive an email with instructions on how to reset your password in a few minutes." 34 | 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." 35 | updated: "Your password has been changed successfully. You are now signed in." 36 | updated_not_active: "Your password has been changed successfully." 37 | registrations: 38 | destroyed: "Bye! Your account has been successfully cancelled. We hope to see you again soon." 39 | signed_up: "Welcome! You have signed up successfully." 40 | signed_up_but_inactive: "You have signed up successfully. However, we could not sign you in because your account is not yet activated." 41 | signed_up_but_locked: "You have signed up successfully. However, we could not sign you in because your account is locked." 42 | signed_up_but_unconfirmed: "A message with a confirmation link has been sent to your email address. Please follow the link to activate your account." 43 | update_needs_confirmation: "You updated your account successfully, but we need to verify your new email address. Please check your email and follow the confirm link to confirm your new email address." 44 | updated: "Your account has been updated successfully." 45 | sessions: 46 | signed_in: "Signed in successfully." 47 | signed_out: "Signed out successfully." 48 | already_signed_out: "Signed out successfully." 49 | unlocks: 50 | send_instructions: "You will receive an email with instructions for how to unlock your account in a few minutes." 51 | send_paranoid_instructions: "If your account exists, you will receive an email with instructions for how to unlock it in a few minutes." 52 | unlocked: "Your account has been unlocked successfully. Please sign in to continue." 53 | errors: 54 | messages: 55 | already_confirmed: "was already confirmed, please try signing in" 56 | confirmation_period_expired: "needs to be confirmed within %{period}, please request a new one" 57 | expired: "has expired, please request a new one" 58 | not_found: "not found" 59 | not_locked: "was not locked" 60 | not_saved: 61 | one: "1 error prohibited this %{resource} from being saved:" 62 | other: "%{count} errors prohibited this %{resource} from being saved:" 63 | -------------------------------------------------------------------------------- /config/initializers/simple_form_bootstrap.rb: -------------------------------------------------------------------------------- 1 | # Use this setup block to configure all options available in SimpleForm. 2 | SimpleForm.setup do |config| 3 | config.error_notification_class = 'alert alert-danger' 4 | config.button_class = 'btn btn-default' 5 | config.boolean_label_class = nil 6 | 7 | config.wrappers :vertical_form, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| 8 | b.use :html5 9 | b.use :placeholder 10 | b.optional :maxlength 11 | b.optional :minlength 12 | b.optional :pattern 13 | b.optional :min_max 14 | b.optional :readonly 15 | b.use :label, class: 'control-label' 16 | 17 | b.use :input, class: 'form-control' 18 | b.use :error, wrap_with: { tag: 'span', class: 'help-block' } 19 | b.use :hint, wrap_with: { tag: 'p', class: 'help-block' } 20 | end 21 | 22 | config.wrappers :vertical_file_input, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| 23 | b.use :html5 24 | b.use :placeholder 25 | b.optional :maxlength 26 | b.optional :minlength 27 | b.optional :readonly 28 | b.use :label, class: 'control-label' 29 | 30 | b.use :input 31 | b.use :error, wrap_with: { tag: 'span', class: 'help-block' } 32 | b.use :hint, wrap_with: { tag: 'p', class: 'help-block' } 33 | end 34 | 35 | config.wrappers :vertical_boolean, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| 36 | b.use :html5 37 | b.optional :readonly 38 | 39 | b.wrapper tag: 'div', class: 'checkbox' do |ba| 40 | ba.use :label_input 41 | end 42 | 43 | b.use :error, wrap_with: { tag: 'span', class: 'help-block' } 44 | b.use :hint, wrap_with: { tag: 'p', class: 'help-block' } 45 | end 46 | 47 | config.wrappers :vertical_radio_and_checkboxes, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| 48 | b.use :html5 49 | b.optional :readonly 50 | b.use :label, class: 'control-label' 51 | b.use :input 52 | b.use :error, wrap_with: { tag: 'span', class: 'help-block' } 53 | b.use :hint, wrap_with: { tag: 'p', class: 'help-block' } 54 | end 55 | 56 | config.wrappers :horizontal_form, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| 57 | b.use :html5 58 | b.use :placeholder 59 | b.optional :maxlength 60 | b.optional :minlength 61 | b.optional :pattern 62 | b.optional :min_max 63 | b.optional :readonly 64 | b.use :label, class: 'col-sm-3 control-label' 65 | 66 | b.wrapper tag: 'div', class: 'col-sm-9' do |ba| 67 | ba.use :input, class: 'form-control' 68 | ba.use :error, wrap_with: { tag: 'span', class: 'help-block' } 69 | ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' } 70 | end 71 | end 72 | 73 | config.wrappers :horizontal_file_input, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| 74 | b.use :html5 75 | b.use :placeholder 76 | b.optional :maxlength 77 | b.optional :minlength 78 | b.optional :readonly 79 | b.use :label, class: 'col-sm-3 control-label' 80 | 81 | b.wrapper tag: 'div', class: 'col-sm-9' do |ba| 82 | ba.use :input 83 | ba.use :error, wrap_with: { tag: 'span', class: 'help-block' } 84 | ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' } 85 | end 86 | end 87 | 88 | config.wrappers :horizontal_boolean, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| 89 | b.use :html5 90 | b.optional :readonly 91 | 92 | b.wrapper tag: 'div', class: 'col-sm-offset-3 col-sm-9' do |wr| 93 | wr.wrapper tag: 'div', class: 'checkbox' do |ba| 94 | ba.use :label_input 95 | end 96 | 97 | wr.use :error, wrap_with: { tag: 'span', class: 'help-block' } 98 | wr.use :hint, wrap_with: { tag: 'p', class: 'help-block' } 99 | end 100 | end 101 | 102 | config.wrappers :horizontal_radio_and_checkboxes, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| 103 | b.use :html5 104 | b.optional :readonly 105 | 106 | b.use :label, class: 'col-sm-3 control-label' 107 | 108 | b.wrapper tag: 'div', class: 'col-sm-9' do |ba| 109 | ba.use :input 110 | ba.use :error, wrap_with: { tag: 'span', class: 'help-block' } 111 | ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' } 112 | end 113 | end 114 | 115 | config.wrappers :inline_form, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| 116 | b.use :html5 117 | b.use :placeholder 118 | b.optional :maxlength 119 | b.optional :minlength 120 | b.optional :pattern 121 | b.optional :min_max 122 | b.optional :readonly 123 | b.use :label, class: 'sr-only' 124 | 125 | b.use :input, class: 'form-control' 126 | b.use :error, wrap_with: { tag: 'span', class: 'help-block' } 127 | b.use :hint, wrap_with: { tag: 'p', class: 'help-block' } 128 | end 129 | 130 | config.wrappers :multi_select, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| 131 | b.use :html5 132 | b.optional :readonly 133 | b.use :label, class: 'control-label' 134 | b.wrapper tag: 'div', class: 'form-inline' do |ba| 135 | ba.use :input, class: 'form-control' 136 | ba.use :error, wrap_with: { tag: 'span', class: 'help-block' } 137 | ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' } 138 | end 139 | end 140 | # Wrappers for forms and inputs using the Bootstrap toolkit. 141 | # Check the Bootstrap docs (http://getbootstrap.com) 142 | # to learn about the different styles for forms and inputs, 143 | # buttons and other elements. 144 | config.default_wrapper = :vertical_form 145 | config.wrapper_mappings = { 146 | check_boxes: :vertical_radio_and_checkboxes, 147 | radio_buttons: :vertical_radio_and_checkboxes, 148 | file: :vertical_file_input, 149 | boolean: :vertical_boolean, 150 | datetime: :multi_select, 151 | date: :multi_select, 152 | time: :multi_select 153 | } 154 | end 155 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: https://rubygems.org/ 3 | specs: 4 | aasm (4.11.1) 5 | actioncable (5.0.1) 6 | actionpack (= 5.0.1) 7 | nio4r (~> 1.2) 8 | websocket-driver (~> 0.6.1) 9 | actionmailer (5.0.1) 10 | actionpack (= 5.0.1) 11 | actionview (= 5.0.1) 12 | activejob (= 5.0.1) 13 | mail (~> 2.5, >= 2.5.4) 14 | rails-dom-testing (~> 2.0) 15 | actionpack (5.0.1) 16 | actionview (= 5.0.1) 17 | activesupport (= 5.0.1) 18 | rack (~> 2.0) 19 | rack-test (~> 0.6.3) 20 | rails-dom-testing (~> 2.0) 21 | rails-html-sanitizer (~> 1.0, >= 1.0.2) 22 | actionview (5.0.1) 23 | activesupport (= 5.0.1) 24 | builder (~> 3.1) 25 | erubis (~> 2.7.0) 26 | rails-dom-testing (~> 2.0) 27 | rails-html-sanitizer (~> 1.0, >= 1.0.2) 28 | activejob (5.0.1) 29 | activesupport (= 5.0.1) 30 | globalid (>= 0.3.6) 31 | activemodel (5.0.1) 32 | activesupport (= 5.0.1) 33 | activerecord (5.0.1) 34 | activemodel (= 5.0.1) 35 | activesupport (= 5.0.1) 36 | arel (~> 7.0) 37 | activesupport (5.0.1) 38 | concurrent-ruby (~> 1.0, >= 1.0.2) 39 | i18n (~> 0.7) 40 | minitest (~> 5.1) 41 | tzinfo (~> 1.1) 42 | addressable (2.4.0) 43 | arel (7.1.4) 44 | bcrypt (3.1.11) 45 | builder (3.2.3) 46 | byebug (9.0.6) 47 | carrierwave (1.0.0) 48 | activemodel (>= 4.0.0) 49 | activesupport (>= 4.0.0) 50 | mime-types (>= 1.16) 51 | coffee-rails (4.2.1) 52 | coffee-script (>= 2.2.0) 53 | railties (>= 4.0.0, < 5.2.x) 54 | coffee-script (2.4.1) 55 | coffee-script-source 56 | execjs 57 | coffee-script-source (1.12.2) 58 | concurrent-ruby (1.0.4) 59 | debug_inspector (0.0.2) 60 | devise (4.2.0) 61 | bcrypt (~> 3.0) 62 | orm_adapter (~> 0.1) 63 | railties (>= 4.1.0, < 5.1) 64 | responders 65 | warden (~> 1.2.3) 66 | erubis (2.7.0) 67 | execjs (2.7.0) 68 | faker (1.7.3) 69 | i18n (~> 0.5) 70 | ffi (1.9.17) 71 | globalid (0.3.7) 72 | activesupport (>= 4.1.0) 73 | i18n (0.8.0) 74 | jbuilder (2.6.1) 75 | activesupport (>= 3.0.0, < 5.1) 76 | multi_json (~> 1.2) 77 | jquery-rails (4.2.2) 78 | rails-dom-testing (>= 1, < 3) 79 | railties (>= 4.2.0) 80 | thor (>= 0.14, < 2.0) 81 | launchy (2.4.3) 82 | addressable (~> 2.3) 83 | letter_opener (1.4.1) 84 | launchy (~> 2.2) 85 | listen (3.0.8) 86 | rb-fsevent (~> 0.9, >= 0.9.4) 87 | rb-inotify (~> 0.9, >= 0.9.7) 88 | loofah (2.0.3) 89 | nokogiri (>= 1.5.9) 90 | mail (2.6.4) 91 | mime-types (>= 1.16, < 4) 92 | method_source (0.8.2) 93 | mime-types (3.1) 94 | mime-types-data (~> 3.2015) 95 | mime-types-data (3.2016.0521) 96 | mini_magick (4.6.1) 97 | mini_portile2 (2.1.0) 98 | minitest (5.10.1) 99 | multi_json (1.12.1) 100 | nio4r (1.2.1) 101 | nokogiri (1.7.0.1) 102 | mini_portile2 (~> 2.1.0) 103 | orm_adapter (0.5.0) 104 | puma (3.7.0) 105 | rack (2.0.1) 106 | rack-test (0.6.3) 107 | rack (>= 1.0) 108 | rails (5.0.1) 109 | actioncable (= 5.0.1) 110 | actionmailer (= 5.0.1) 111 | actionpack (= 5.0.1) 112 | actionview (= 5.0.1) 113 | activejob (= 5.0.1) 114 | activemodel (= 5.0.1) 115 | activerecord (= 5.0.1) 116 | activesupport (= 5.0.1) 117 | bundler (>= 1.3.0, < 2.0) 118 | railties (= 5.0.1) 119 | sprockets-rails (>= 2.0.0) 120 | rails-dom-testing (2.0.2) 121 | activesupport (>= 4.2.0, < 6.0) 122 | nokogiri (~> 1.6) 123 | rails-html-sanitizer (1.0.3) 124 | loofah (~> 2.0) 125 | railties (5.0.1) 126 | actionpack (= 5.0.1) 127 | activesupport (= 5.0.1) 128 | method_source 129 | rake (>= 0.8.7) 130 | thor (>= 0.18.1, < 2.0) 131 | rake (12.0.0) 132 | rb-fsevent (0.9.8) 133 | rb-inotify (0.9.8) 134 | ffi (>= 0.5.0) 135 | responders (2.3.0) 136 | railties (>= 4.2.0, < 5.1) 137 | sass (3.4.23) 138 | sass-rails (5.0.6) 139 | railties (>= 4.0.0, < 6) 140 | sass (~> 3.1) 141 | sprockets (>= 2.8, < 4.0) 142 | sprockets-rails (>= 2.0, < 4.0) 143 | tilt (>= 1.1, < 3) 144 | simple_form (3.4.0) 145 | actionpack (> 4, < 5.1) 146 | activemodel (> 4, < 5.1) 147 | spring (2.0.1) 148 | activesupport (>= 4.2) 149 | spring-watcher-listen (2.0.1) 150 | listen (>= 2.7, < 4.0) 151 | spring (>= 1.2, < 3.0) 152 | sprockets (3.7.1) 153 | concurrent-ruby (~> 1.0) 154 | rack (> 1, < 3) 155 | sprockets-rails (3.2.0) 156 | actionpack (>= 4.0) 157 | activesupport (>= 4.0) 158 | sprockets (>= 3.0.0) 159 | sqlite3 (1.3.13) 160 | thor (0.19.4) 161 | thread_safe (0.3.5) 162 | tilt (2.0.6) 163 | turbolinks (5.0.1) 164 | turbolinks-source (~> 5) 165 | turbolinks-source (5.0.0) 166 | tzinfo (1.2.2) 167 | thread_safe (~> 0.1) 168 | uglifier (3.0.4) 169 | execjs (>= 0.3.0, < 3) 170 | warden (1.2.6) 171 | rack (>= 1.0) 172 | web-console (3.4.0) 173 | actionview (>= 5.0) 174 | activemodel (>= 5.0) 175 | debug_inspector 176 | railties (>= 5.0) 177 | websocket-driver (0.6.5) 178 | websocket-extensions (>= 0.1.0) 179 | websocket-extensions (0.1.2) 180 | will_paginate (3.1.5) 181 | 182 | PLATFORMS 183 | ruby 184 | 185 | DEPENDENCIES 186 | aasm 187 | byebug 188 | carrierwave 189 | coffee-rails (~> 4.2) 190 | devise 191 | faker 192 | jbuilder (~> 2.5) 193 | jquery-rails 194 | letter_opener 195 | listen (~> 3.0.5) 196 | mini_magick 197 | puma (~> 3.0) 198 | rails (~> 5.0.1) 199 | sass-rails (~> 5.0) 200 | simple_form 201 | spring 202 | spring-watcher-listen (~> 2.0.0) 203 | sqlite3 204 | turbolinks (~> 5) 205 | tzinfo-data 206 | uglifier (>= 1.3.0) 207 | web-console (>= 3.3.0) 208 | will_paginate 209 | 210 | BUNDLED WITH 211 | 1.14.6 212 | -------------------------------------------------------------------------------- /config/initializers/simple_form.rb: -------------------------------------------------------------------------------- 1 | # Use this setup block to configure all options available in SimpleForm. 2 | SimpleForm.setup do |config| 3 | # Wrappers are used by the form builder to generate a 4 | # complete input. You can remove any component from the 5 | # wrapper, change the order or even add your own to the 6 | # stack. The options given below are used to wrap the 7 | # whole input. 8 | config.wrappers :default, class: :input, 9 | hint_class: :field_with_hint, error_class: :field_with_errors do |b| 10 | ## Extensions enabled by default 11 | # Any of these extensions can be disabled for a 12 | # given input by passing: `f.input EXTENSION_NAME => false`. 13 | # You can make any of these extensions optional by 14 | # renaming `b.use` to `b.optional`. 15 | 16 | # Determines whether to use HTML5 (:email, :url, ...) 17 | # and required attributes 18 | b.use :html5 19 | 20 | # Calculates placeholders automatically from I18n 21 | # You can also pass a string as f.input placeholder: "Placeholder" 22 | b.use :placeholder 23 | 24 | ## Optional extensions 25 | # They are disabled unless you pass `f.input EXTENSION_NAME => true` 26 | # to the input. If so, they will retrieve the values from the model 27 | # if any exists. If you want to enable any of those 28 | # extensions by default, you can change `b.optional` to `b.use`. 29 | 30 | # Calculates maxlength from length validations for string inputs 31 | # and/or database column lengths 32 | b.optional :maxlength 33 | 34 | # Calculate minlength from length validations for string inputs 35 | b.optional :minlength 36 | 37 | # Calculates pattern from format validations for string inputs 38 | b.optional :pattern 39 | 40 | # Calculates min and max from length validations for numeric inputs 41 | b.optional :min_max 42 | 43 | # Calculates readonly automatically from readonly attributes 44 | b.optional :readonly 45 | 46 | ## Inputs 47 | b.use :label_input 48 | b.use :hint, wrap_with: { tag: :span, class: :hint } 49 | b.use :error, wrap_with: { tag: :span, class: :error } 50 | 51 | ## full_messages_for 52 | # If you want to display the full error message for the attribute, you can 53 | # use the component :full_error, like: 54 | # 55 | # b.use :full_error, wrap_with: { tag: :span, class: :error } 56 | end 57 | 58 | # The default wrapper to be used by the FormBuilder. 59 | config.default_wrapper = :default 60 | 61 | # Define the way to render check boxes / radio buttons with labels. 62 | # Defaults to :nested for bootstrap config. 63 | # inline: input + label 64 | # nested: label > input 65 | config.boolean_style = :nested 66 | 67 | # Default class for buttons 68 | config.button_class = 'btn' 69 | 70 | # Method used to tidy up errors. Specify any Rails Array method. 71 | # :first lists the first message for each field. 72 | # Use :to_sentence to list all errors for each field. 73 | # config.error_method = :first 74 | 75 | # Default tag used for error notification helper. 76 | config.error_notification_tag = :div 77 | 78 | # CSS class to add for error notification helper. 79 | config.error_notification_class = 'error_notification' 80 | 81 | # ID to add for error notification helper. 82 | # config.error_notification_id = nil 83 | 84 | # Series of attempts to detect a default label method for collection. 85 | # config.collection_label_methods = [ :to_label, :name, :title, :to_s ] 86 | 87 | # Series of attempts to detect a default value method for collection. 88 | # config.collection_value_methods = [ :id, :to_s ] 89 | 90 | # You can wrap a collection of radio/check boxes in a pre-defined tag, defaulting to none. 91 | # config.collection_wrapper_tag = nil 92 | 93 | # You can define the class to use on all collection wrappers. Defaulting to none. 94 | # config.collection_wrapper_class = nil 95 | 96 | # You can wrap each item in a collection of radio/check boxes with a tag, 97 | # defaulting to :span. 98 | # config.item_wrapper_tag = :span 99 | 100 | # You can define a class to use in all item wrappers. Defaulting to none. 101 | # config.item_wrapper_class = nil 102 | 103 | # How the label text should be generated altogether with the required text. 104 | # config.label_text = lambda { |label, required, explicit_label| "#{required} #{label}" } 105 | 106 | # You can define the class to use on all labels. Default is nil. 107 | # config.label_class = nil 108 | 109 | # You can define the default class to be used on forms. Can be overriden 110 | # with `html: { :class }`. Defaulting to none. 111 | # config.default_form_class = nil 112 | 113 | # You can define which elements should obtain additional classes 114 | # config.generate_additional_classes_for = [:wrapper, :label, :input] 115 | 116 | # Whether attributes are required by default (or not). Default is true. 117 | # config.required_by_default = true 118 | 119 | # Tell browsers whether to use the native HTML5 validations (novalidate form option). 120 | # These validations are enabled in SimpleForm's internal config but disabled by default 121 | # in this configuration, which is recommended due to some quirks from different browsers. 122 | # To stop SimpleForm from generating the novalidate option, enabling the HTML5 validations, 123 | # change this configuration to true. 124 | config.browser_validations = false 125 | 126 | # Collection of methods to detect if a file type was given. 127 | # config.file_methods = [ :mounted_as, :file?, :public_filename ] 128 | 129 | # Custom mappings for input types. This should be a hash containing a regexp 130 | # to match as key, and the input type that will be used when the field name 131 | # matches the regexp as value. 132 | # config.input_mappings = { /count/ => :integer } 133 | 134 | # Custom wrappers for input types. This should be a hash containing an input 135 | # type as key and the wrapper that will be used for all inputs with specified type. 136 | # config.wrapper_mappings = { string: :prepend } 137 | 138 | # Namespaces where SimpleForm should look for custom input classes that 139 | # override default inputs. 140 | # config.custom_inputs_namespaces << "CustomInputs" 141 | 142 | # Default priority for time_zone inputs. 143 | # config.time_zone_priority = nil 144 | 145 | # Default priority for country inputs. 146 | # config.country_priority = nil 147 | 148 | # When false, do not use translations for labels. 149 | # config.translate_labels = true 150 | 151 | # Automatically discover new inputs in Rails' autoload path. 152 | # config.inputs_discovery = true 153 | 154 | # Cache SimpleForm inputs discovery 155 | # config.cache_discovery = !Rails.env.development? 156 | 157 | # Default class for inputs 158 | # config.input_class = nil 159 | 160 | # Define the default class of the input wrapper of the boolean input. 161 | config.boolean_label_class = 'checkbox' 162 | 163 | # Defines if the default input wrapper class should be included in radio 164 | # collection wrappers. 165 | # config.include_default_input_wrapper_class = true 166 | 167 | # Defines which i18n scope will be used in Simple Form. 168 | # config.i18n_scope = 'simple_form' 169 | end 170 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 范例专案: https://github.com/ihower/jdstore 2 | 参考资料: https://ihower.tw/rails/ 3 | 4 | ## 范例安装指南 5 | 6 | * `git clone https://github.com/ihower/jdstore.git ihower-jdstore` 7 | * `cd ihower-jdstore` 8 | * `bundle` 9 | * `rake db:migrate` 10 | * `rake db:seed` 11 | * `rake dev:fake` 这会产生假用户、产品和订单 12 | * `rails s` 13 | 14 | # 3/9 前端补充知识(Asset Pipeline) 15 | 16 | ## Asset Pipeline 简介 17 | 18 | * 静态档案(static file,又叫做 asset) 指的是 CSS, JavaScript 和图档(images) 等:无论哪个用户、所有浏览器拿到的档案都一样 19 | * 相对于动态档案:动态档案指的是经过 Rails 程序产生的 HTML 页面(xxx.html.erb) 20 | * 在 Rails 目录中,放在 `public` 目录下的是静态档案,浏览器可以直接读取,不会经过 Rails 程序 🖥 21 | * 在 `public` 目录找不到檔案的話才會進到 Rails Router 路由系統 22 | * 除了 `public` 目录,Rails 也可将静态档案放在 `app/assets` 目录下,由 Rails 统一管理打包这些静态档案,这功能叫做 Asset Pipeline。用户浏览器不能直接访问 `app/assets` 目录,在本地开发的时候,会经过 Rails 程序回传这些静态档案。部署上 production 服务器时,会先执行 `rake assets:precompile` 产生静态档案放在 `public/assets` 目录下,让浏览器可以访问。 23 | 24 | > 如果用 Capistrano 部署在 Linode 服务器,会在 `cap production deploy` 过程中在服务器上执行 `rake assets:precompile`。如果用Heroku 请参考[Rails Asset Pipeline on Heroku Cedar](https://devcenter.heroku.com/articles/rails-asset-pipeline#the-rails-4-asset-pipeline) 说明,在本地执行`rake assets :precompile` 将产生的`public/assets` 目录commit 进 git 库,再 push 上 Heroku。 25 | 26 | ## Asset Pipeline 用法 27 | 28 | * Manifest 档案是进入点,这个档案会列出要载入哪些档案,预设是: 29 | * `app/assets/javascripts/application.js` 30 | * `app/assets/stylesheets/application.css` 31 | * 接著在 `layout/application.html.erb` 中,会用以下这两行来载入进入点 🖥 32 | * `<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>` 33 | * `<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>` 34 | * 在 Manifest 档案中,会用 `//= require` 写法列出要载入的 css 和 js 档案 🖥 35 | * 尽量不要用 `require_tree`,因为 css 和 js 是会依照载入顺序执行的,后载入的会覆盖前面的,载入顺序很重要 ⚠️ 36 | * 不只 `app/assets/` 可以载入得到、放 `lib/assets` 和 `vendor/assets` 也可以载入得到,那些放在 gem 库里面的也载入得到。我们会将第三方的 css/js 库的源码放在 `vendor/assets` 下来区别 🖥 37 | * 放`app/assets/images` 的图档,也必须透过 Rails helper 才能够访问到 (因为有 fingerprint 的关系,不透过 Rails helper 你不知道最后的档名) 🖥 38 | * 在 erb 中用 `image_tag` 或 `asset_path` 39 | * 在 js 需要改档名为`js.erb` 就可以用 rails helper 40 | * 在 Sass 中可以用 `image-url` 41 | * Asset Pipeline 在本地开发 development 和部署上 production 的实际运作不一样:本机开发时是拆开载入,方便除错。上 production 时才会打包压缩。 42 | * ⚠️ 因此专案千万不要最后一天才部署上 Production,因为有些前端问题是在 Production 环境上才会发生,不要拖到最后一天才发现!!!! 会逼死人。 43 | 44 | ## Asset Pipleline 这功能的的目的是? 45 | 46 | 1. 方便装 gem (Ruby的库) 进行管理,不需要将 gem 里面的静态档案也搬进 `public` 目录下搅在一起。 47 | 2. 上 production 部署时,会打包压缩成为一个档案,加速浏览器下载 🖥 48 | 3. 档名会有 fingerprint,一但内容有修改就会变动,避免浏览器缓存,让用户总是访问到最新的档案 🖥 49 | 4. pre-processing 功能: [Sass](http://sass-lang.com) 和 [CoffeeScript](http://coffeescript.org),可以用其他语言写 CSS 和 JavaScript。 50 | 51 | ## Q: 如何安装和使用第三方前端套件? 52 | 53 | 前端世界五花八门,充满各式各样的 CSS/JavaScript 套件,我们可以从 jQuery plugins 开始 google 起,再 google 找找看有没有 Bootstrap 样式。接下来.... 54 | 55 | 1. 看看官网的文件,是否满足需求,合用吗? 56 | 1. google 看看有没有包好的 gem 在 github 上 57 | 2. 观察看看这个 gem 包的版本是? gem 的版本和前端库的版本,是两回事。检查看看最后的 commit 时间、有没有人关注、有没有人维护啊 58 | 3. 如果版本过旧没人维护,就不要用这个 gem 了。其实大部分前端用途的 gem 只是包裹 js/css 而已,你可以直接拿 js/css 源码来用,方法如下: 59 | 60 | 如果这个套件全站常用,建议可以一起打包进 Asset Pipeline: 61 | 62 | * 把 css/js 源码放到 `vendor/assets` 下,就可以 `require` 载入到了 63 | * 如果 css 內有用到图档,建议不要放进 `app/assets/images` 里面,因为这样要改 `css` 很麻烦。 64 | 65 | 如果这个套件只是少数页面用到而且档案大小超过数百Kb,建议就不经过 Asset Pipeline 了: 66 | 67 | * 可把 css/js 代码放 `public` 目录下,在HTML 里直接用`` 和`` 就可以访问到了。 68 | * 或是找免费的 CDN 服务提供静态档案。 CDN (Content Delivery Network) 看名子好像很厉害,就是用别人的服务器的意思。别人的服务器可能离用户更近、频宽更大、下载更快。记得要挑有国内服务器的 CDN 服务,例如 [BootCDN](http://www.bootcdn.cn/) 或 [Staticfile CDN](https://www.staticfile.org),不要傻傻地複製國外官網上的CDN位址。 69 | 70 | 以下我们藉由案例来实际说明如何安装使用: 71 | 72 | ## Bootstrap 73 | 74 | Bootstrap 在教材中装过了,这里我们很快地示范一遍,说明其中的差异: 75 | 76 | * 77 | * 78 | 79 | * 因为这个 gem 是用 Sass 写的,所以步骤中将 `application.css` 改名 `application.scss` 了。请统一用 `@import` 语法载入 ⚠️ 注意副档名不要加 `.css`,最后要加 `;`。 80 | * js 部分可以直接 `//= require bootstrap-sprockets` 就会载入全部的 bootstramp 组件,可以不需要逐笔载入,例如 `//= require bootstrap/modal`、`//= require bootstrap/alert` 等等 81 | 82 | ## Font Awesome 83 | 84 | Bootstrap 3 里面虽然也有 Font Icon,但不够用而且之后的版本拿掉了。建议都改用 Font Awesome。 85 | 86 | * 87 | * 88 | 89 | ## Select2 厉害的下拉选单 90 | 91 | Select2 是一个非常好用的单选、多选选单,非常适合选项非常多的情境,这里示范如何实作单选、多选。 92 | 93 | * 94 | * 95 | 96 | ## Date Picker 选日期介面 97 | 98 | Rails 内建的选日期是三个下拉选年、月、日。可以用这个日历套件有更好的用户介面: 99 | 100 | * 101 | * 102 | 103 | 注意格式要指定以配合 Rails: `$("#product_publish_on").datepicker({ format: "yyyy/mm/dd" });` 104 | 105 | 如果要日期和时间: 106 | 107 | * 108 | * 109 | 110 | ## Autosize 自动调整输入框大小 111 | 112 | * 113 | * (outdated 没在维护了) 114 | 115 | 发现 gem 版过期了,决定不要用 gem 装,把 js 源码抓下来放 `vendor/assets/` 自行载入。 116 | 117 | ## Chart.js 图表 118 | 119 | * 120 | * 中文文档 121 | * 是有 gem 可以用,但是只有后台报表为用到,决定不包进 asset pipeline 让所有用户下载,来用 CDN 版本。在 [BootCDN](http://www.bootcdn.cn/Chart.js/) 上找到 Chart.js,将以下 code 贴到页面上就载入了: 122 | 123 | `` 124 | 125 | ## Turbolinks 大坑 126 | 127 | [Turbolinks](https://github.com/turbolinks/turbolinks) 是一个 Rails 内建的页面加速工具,在 `Gemfile` 和 `application.js` 可以发现它的踪迹。这是一个 Javascript 套件会在换页的时候,不重新载入 HTML 的 `head`,只载入新的 `body`,来加速换页。 128 | 129 | 虽然有加速的效果,但是却很干扰其他 `javascript` 源码的载入,具体来说,有两个坑: 130 | 131 | * 网上所有 jQuery 的教学文章,都是用`$(document).ready(function(){...})` 或`$(function(){...})`,在 HTML 载入完毕后执行 js 源码。但是用了 Turbolink 只会触发第一次而已,换页时不会再执行。 🖥 132 |   * 解法是全部都要改 `$(document).on("turbolinks:load", function(){...})` 133 | * 只有单页(page-specific) 用到的 javascript 代码,如果写在 `body` 里面,跳页回来时,会触发两次。某些 js code 重复执行两次没关系,但有些会有问题。 🖥 134 | * 简单解法一:关掉 Turbolinks 的缓存功能,把 `` 放到 layout 的 `head` 里面。 135 | * 补充解法二:把layout 的`` 改成`">`,这样就可以在全局载入的` application.js` 中指定只有这一页才执行的js code,例如: 136 | 137 | 138 | $(document).on("turbolinks:load", function() { 139 | if ( $("#products-show").length > 0 ) { 140 | console.log("product-show"); 141 | } 142 | }) 143 | 144 | 145 | ⚠️ 同学们也大可以选择直接绕过这个大坑,如果你碰到 js 灵异现象(贴上来的 js code 换页回來后不执行,但是重新整理就没问题。或是跳页回來重复执行了两次等等),可以试试看拆掉 Turbolinks:把 `Gemfile` 跟 `applicatio.js` 里面的 Turbolink 代码拿掉即可。 146 | 147 | ## 套现成的 Bootstrap Theme 148 | 149 | google "bootstrap theme" 可以找到一堆 Bootstrap Theme,有免费也有付费的,例如: 150 | 151 | * 152 | * 153 | * 154 | 155 | 这里以 为例。秘诀是: 156 | 157 | * jQuery, Bootstrap, font-awesome 我们已经有装了,不要重复载入。重复载入不但浪费用户下载时间,也容易造成 js 执行错误。 158 | * 图档不要放 asset pipeline,放 `public` 目录下。这样 CSS 才可以无痛衔接上,并检查路径一律是 `/` 开头用绝对路径即可。 159 | * `$(document).ready(function(){...})` 要配合 Turbolinks 处理,或是拆掉 Turbolinks。 160 | 161 | ## 前后台 css/js 如何拆开? 162 | 163 | 我们学过拆 layout 了,例如前台用 `app/views/layouts/application.html.erb`,后台用 `app/views/layouts/admin.html.erb`。那 css/js 也可以拆开,方法如下: 164 | 165 | * 新增 `app/assets/` 下的 manifest 档案,例如 `app/assets/stylesheets/admin.scss` 和 `app/assets/javascripts/admin.js` 166 | * 修改 `config/initializers/assets.rb` 的 `Rails.application.config.assets.precompile += %w( admin.css admin.js )` 告诉 Rails 编译 assets 时要多找这两个进入点,接著重啟 Rails 服務器 167 | * 修改 `app/views/layouts/admin.html.erb` 换成载入 admin css 和 js 168 | 169 | 170 | ----- 171 | 172 | 173 | # 2/16 补充知识 174 | 175 | ## ActiveRecord Query: where 用法 176 | 177 | * 找出某一天的订单 178 | * 根据指定状态: 所有尚未处理和已处理订单 179 | * 根据(多个)订单号码 180 | * 根据特定金额以上 181 | 182 | ## has_many :through, :source 关联参数释疑 183 | 184 | * 关联的名称可以不一样 185 | * 关联可以有条件 186 | 187 | ## cookies/session 释疑 188 | 189 | * 用 cookie 让浏览器对这个网站记住资料 190 | * 用 Chrome DevTools 观察 cookie 191 | * session 是基于 cookie 机制的储存空间,但是让客户端不能读取修改 192 | * session 设定: config/initializers/session_store.rb 和 config/secret.yml 193 | 194 | ## routing 释疑 195 | 196 | * 决定 URL path 进到哪一个 controller,以及 URL helper 长怎样 197 | * 用 rake routes 观察 198 | * namespace 和 scope(module, as, path 参数) 199 | * resource 单数用法 (carts 可改 cart) 200 | 201 | ## State Machine 释疑: state, event 和 callbacks 202 | 203 | * https://fullstack.xinshengdaxue.com/posts/237 204 | * https://github.com/aasm/aasm 用法 205 | 206 | -------------------------------------------------------------------------------- /config/initializers/devise.rb: -------------------------------------------------------------------------------- 1 | # Use this hook to configure devise mailer, warden hooks and so forth. 2 | # Many of these configuration options can be set straight in your model. 3 | Devise.setup do |config| 4 | # The secret key used by Devise. Devise uses this key to generate 5 | # random tokens. Changing this key will render invalid all existing 6 | # confirmation, reset password and unlock tokens in the database. 7 | # Devise will use the `secret_key_base` as its `secret_key` 8 | # by default. You can change it below and use your own secret key. 9 | # config.secret_key = '26c1cd49705549d822c1d26c1ceaa59dffbb224937e28b11abc34199f08dc07d97f92b6bd672c8ac33a8f98574eec2bdaf03dc14cce870ee0a0550f62c64c261' 10 | 11 | # ==> Mailer Configuration 12 | # Configure the e-mail address which will be shown in Devise::Mailer, 13 | # note that it will be overwritten if you use your own mailer class 14 | # with default "from" parameter. 15 | config.mailer_sender = 'please-change-me-at-config-initializers-devise@example.com' 16 | 17 | # Configure the class responsible to send e-mails. 18 | # config.mailer = 'Devise::Mailer' 19 | 20 | # Configure the parent class responsible to send e-mails. 21 | # config.parent_mailer = 'ActionMailer::Base' 22 | 23 | # ==> ORM configuration 24 | # Load and configure the ORM. Supports :active_record (default) and 25 | # :mongoid (bson_ext recommended) by default. Other ORMs may be 26 | # available as additional gems. 27 | require 'devise/orm/active_record' 28 | 29 | # ==> Configuration for any authentication mechanism 30 | # Configure which keys are used when authenticating a user. The default is 31 | # just :email. You can configure it to use [:username, :subdomain], so for 32 | # authenticating a user, both parameters are required. Remember that those 33 | # parameters are used only when authenticating and not when retrieving from 34 | # session. If you need permissions, you should implement that in a before filter. 35 | # You can also supply a hash where the value is a boolean determining whether 36 | # or not authentication should be aborted when the value is not present. 37 | # config.authentication_keys = [:email] 38 | 39 | # Configure parameters from the request object used for authentication. Each entry 40 | # given should be a request method and it will automatically be passed to the 41 | # find_for_authentication method and considered in your model lookup. For instance, 42 | # if you set :request_keys to [:subdomain], :subdomain will be used on authentication. 43 | # The same considerations mentioned for authentication_keys also apply to request_keys. 44 | # config.request_keys = [] 45 | 46 | # Configure which authentication keys should be case-insensitive. 47 | # These keys will be downcased upon creating or modifying a user and when used 48 | # to authenticate or find a user. Default is :email. 49 | config.case_insensitive_keys = [:email] 50 | 51 | # Configure which authentication keys should have whitespace stripped. 52 | # These keys will have whitespace before and after removed upon creating or 53 | # modifying a user and when used to authenticate or find a user. Default is :email. 54 | config.strip_whitespace_keys = [:email] 55 | 56 | # Tell if authentication through request.params is enabled. True by default. 57 | # It can be set to an array that will enable params authentication only for the 58 | # given strategies, for example, `config.params_authenticatable = [:database]` will 59 | # enable it only for database (email + password) authentication. 60 | # config.params_authenticatable = true 61 | 62 | # Tell if authentication through HTTP Auth is enabled. False by default. 63 | # It can be set to an array that will enable http authentication only for the 64 | # given strategies, for example, `config.http_authenticatable = [:database]` will 65 | # enable it only for database authentication. The supported strategies are: 66 | # :database = Support basic authentication with authentication key + password 67 | # config.http_authenticatable = false 68 | 69 | # If 401 status code should be returned for AJAX requests. True by default. 70 | # config.http_authenticatable_on_xhr = true 71 | 72 | # The realm used in Http Basic Authentication. 'Application' by default. 73 | # config.http_authentication_realm = 'Application' 74 | 75 | # It will change confirmation, password recovery and other workflows 76 | # to behave the same regardless if the e-mail provided was right or wrong. 77 | # Does not affect registerable. 78 | # config.paranoid = true 79 | 80 | # By default Devise will store the user in session. You can skip storage for 81 | # particular strategies by setting this option. 82 | # Notice that if you are skipping storage for all authentication paths, you 83 | # may want to disable generating routes to Devise's sessions controller by 84 | # passing skip: :sessions to `devise_for` in your config/routes.rb 85 | config.skip_session_storage = [:http_auth] 86 | 87 | # By default, Devise cleans up the CSRF token on authentication to 88 | # avoid CSRF token fixation attacks. This means that, when using AJAX 89 | # requests for sign in and sign up, you need to get a new CSRF token 90 | # from the server. You can disable this option at your own risk. 91 | # config.clean_up_csrf_token_on_authentication = true 92 | 93 | # When false, Devise will not attempt to reload routes on eager load. 94 | # This can reduce the time taken to boot the app but if your application 95 | # requires the Devise mappings to be loaded during boot time the application 96 | # won't boot properly. 97 | # config.reload_routes = true 98 | 99 | # ==> Configuration for :database_authenticatable 100 | # For bcrypt, this is the cost for hashing the password and defaults to 11. If 101 | # using other algorithms, it sets how many times you want the password to be hashed. 102 | # 103 | # Limiting the stretches to just one in testing will increase the performance of 104 | # your test suite dramatically. However, it is STRONGLY RECOMMENDED to not use 105 | # a value less than 10 in other environments. Note that, for bcrypt (the default 106 | # algorithm), the cost increases exponentially with the number of stretches (e.g. 107 | # a value of 20 is already extremely slow: approx. 60 seconds for 1 calculation). 108 | config.stretches = Rails.env.test? ? 1 : 11 109 | 110 | # Set up a pepper to generate the hashed password. 111 | # config.pepper = 'd8b412a82d714b5ca035cc5071767369cf8eac73aac3ee2bf9da88f9cd54d560685a592e126392c8de0974d242056d7099e4d2a01ba25d8c67c943e45ebd3680' 112 | 113 | # Send a notification email when the user's password is changed 114 | # config.send_password_change_notification = false 115 | 116 | # ==> Configuration for :confirmable 117 | # A period that the user is allowed to access the website even without 118 | # confirming their account. For instance, if set to 2.days, the user will be 119 | # able to access the website for two days without confirming their account, 120 | # access will be blocked just in the third day. Default is 0.days, meaning 121 | # the user cannot access the website without confirming their account. 122 | # config.allow_unconfirmed_access_for = 2.days 123 | 124 | # A period that the user is allowed to confirm their account before their 125 | # token becomes invalid. For example, if set to 3.days, the user can confirm 126 | # their account within 3 days after the mail was sent, but on the fourth day 127 | # their account can't be confirmed with the token any more. 128 | # Default is nil, meaning there is no restriction on how long a user can take 129 | # before confirming their account. 130 | # config.confirm_within = 3.days 131 | 132 | # If true, requires any email changes to be confirmed (exactly the same way as 133 | # initial account confirmation) to be applied. Requires additional unconfirmed_email 134 | # db field (see migrations). Until confirmed, new email is stored in 135 | # unconfirmed_email column, and copied to email column on successful confirmation. 136 | config.reconfirmable = true 137 | 138 | # Defines which key will be used when confirming an account 139 | # config.confirmation_keys = [:email] 140 | 141 | # ==> Configuration for :rememberable 142 | # The time the user will be remembered without asking for credentials again. 143 | # config.remember_for = 2.weeks 144 | 145 | # Invalidates all the remember me tokens when the user signs out. 146 | config.expire_all_remember_me_on_sign_out = true 147 | 148 | # If true, extends the user's remember period when remembered via cookie. 149 | # config.extend_remember_period = false 150 | 151 | # Options to be passed to the created cookie. For instance, you can set 152 | # secure: true in order to force SSL only cookies. 153 | # config.rememberable_options = {} 154 | 155 | # ==> Configuration for :validatable 156 | # Range for password length. 157 | config.password_length = 6..128 158 | 159 | # Email regex used to validate email formats. It simply asserts that 160 | # one (and only one) @ exists in the given string. This is mainly 161 | # to give user feedback and not to assert the e-mail validity. 162 | config.email_regexp = /\A[^@\s]+@[^@\s]+\z/ 163 | 164 | # ==> Configuration for :timeoutable 165 | # The time you want to timeout the user session without activity. After this 166 | # time the user will be asked for credentials again. Default is 30 minutes. 167 | # config.timeout_in = 30.minutes 168 | 169 | # ==> Configuration for :lockable 170 | # Defines which strategy will be used to lock an account. 171 | # :failed_attempts = Locks an account after a number of failed attempts to sign in. 172 | # :none = No lock strategy. You should handle locking by yourself. 173 | # config.lock_strategy = :failed_attempts 174 | 175 | # Defines which key will be used when locking and unlocking an account 176 | # config.unlock_keys = [:email] 177 | 178 | # Defines which strategy will be used to unlock an account. 179 | # :email = Sends an unlock link to the user email 180 | # :time = Re-enables login after a certain amount of time (see :unlock_in below) 181 | # :both = Enables both strategies 182 | # :none = No unlock strategy. You should handle unlocking by yourself. 183 | # config.unlock_strategy = :both 184 | 185 | # Number of authentication tries before locking an account if lock_strategy 186 | # is failed attempts. 187 | # config.maximum_attempts = 20 188 | 189 | # Time interval to unlock the account if :time is enabled as unlock_strategy. 190 | # config.unlock_in = 1.hour 191 | 192 | # Warn on the last attempt before the account is locked. 193 | # config.last_attempt_warning = true 194 | 195 | # ==> Configuration for :recoverable 196 | # 197 | # Defines which key will be used when recovering the password for an account 198 | # config.reset_password_keys = [:email] 199 | 200 | # Time interval you can reset your password with a reset password key. 201 | # Don't put a too small interval or your users won't have the time to 202 | # change their passwords. 203 | config.reset_password_within = 6.hours 204 | 205 | # When set to false, does not sign a user in automatically after their password is 206 | # reset. Defaults to true, so a user is signed in automatically after a reset. 207 | # config.sign_in_after_reset_password = true 208 | 209 | # ==> Configuration for :encryptable 210 | # Allow you to use another hashing or encryption algorithm besides bcrypt (default). 211 | # You can use :sha1, :sha512 or algorithms from others authentication tools as 212 | # :clearance_sha1, :authlogic_sha512 (then you should set stretches above to 20 213 | # for default behavior) and :restful_authentication_sha1 (then you should set 214 | # stretches to 10, and copy REST_AUTH_SITE_KEY to pepper). 215 | # 216 | # Require the `devise-encryptable` gem when using anything other than bcrypt 217 | # config.encryptor = :sha512 218 | 219 | # ==> Scopes configuration 220 | # Turn scoped views on. Before rendering "sessions/new", it will first check for 221 | # "users/sessions/new". It's turned off by default because it's slower if you 222 | # are using only default views. 223 | # config.scoped_views = false 224 | 225 | # Configure the default scope given to Warden. By default it's the first 226 | # devise role declared in your routes (usually :user). 227 | # config.default_scope = :user 228 | 229 | # Set this configuration to false if you want /users/sign_out to sign out 230 | # only the current scope. By default, Devise signs out all scopes. 231 | # config.sign_out_all_scopes = true 232 | 233 | # ==> Navigation configuration 234 | # Lists the formats that should be treated as navigational. Formats like 235 | # :html, should redirect to the sign in page when the user does not have 236 | # access, but formats like :xml or :json, should return 401. 237 | # 238 | # If you have any extra navigational formats, like :iphone or :mobile, you 239 | # should add them to the navigational formats lists. 240 | # 241 | # The "*/*" below is required to match Internet Explorer requests. 242 | # config.navigational_formats = ['*/*', :html] 243 | 244 | # The default HTTP method used to sign out a resource. Default is :delete. 245 | config.sign_out_via = :delete 246 | 247 | # ==> OmniAuth 248 | # Add a new OmniAuth provider. Check the wiki for more information on setting 249 | # up on your models and hooks. 250 | # config.omniauth :github, 'APP_ID', 'APP_SECRET', scope: 'user,public_repo' 251 | 252 | # ==> Warden configuration 253 | # If you want to use other strategies, that are not supported by Devise, or 254 | # change the failure app, you can configure them inside the config.warden block. 255 | # 256 | # config.warden do |manager| 257 | # manager.intercept_401 = false 258 | # manager.default_strategies(scope: :user).unshift :some_external_strategy 259 | # end 260 | 261 | # ==> Mountable engine configurations 262 | # When using Devise inside an engine, let's call it `MyEngine`, and this engine 263 | # is mountable, there are some extra configurations to be taken into account. 264 | # The following options are available, assuming the engine is mounted as: 265 | # 266 | # mount MyEngine, at: '/my_engine' 267 | # 268 | # The router that invoked `devise_for`, in the example above, would be: 269 | # config.router_name = :my_engine 270 | # 271 | # When using OmniAuth, Devise cannot automatically set OmniAuth path, 272 | # so you need to do it manually. For the users scope, it would be: 273 | # config.omniauth_path_prefix = '/my_engine/users/auth' 274 | end 275 | --------------------------------------------------------------------------------