From 12f1cfd93e33c40d9ae2d05ae8c4e7052e50c6e4 Mon Sep 17 00:00:00 2001 From: colorbox Date: Tue, 2 May 2017 15:38:23 +0900 Subject: [PATCH 1/4] wip Add index action for lunchbox in admin --- app/assets/javascripts/admin/lunchbox.js | 2 + app/assets/stylesheets/admin/lunchbox.scss | 3 + app/assets/stylesheets/scaffolds.scss | 89 +++++++++++++++++++ .../admin/lunchboxes_controller.rb | 58 ++++++++++++ app/helpers/admin/lunchbox_helper.rb | 2 + app/helpers/admin/lunchboxes_helper.rb | 2 + app/models/admin.rb | 5 ++ app/views/admin/lunchboxes/_form.html.slim | 9 ++ app/views/admin/lunchboxes/edit.html.slim | 8 ++ app/views/admin/lunchboxes/index.html.slim | 21 +++++ app/views/admin/lunchboxes/new.html.slim | 5 ++ app/views/admin/lunchboxes/show.html.slim | 6 ++ config/routes.rb | 2 + .../admin/lunchbox_controller_test.rb | 7 ++ .../admin/lunchboxes_controller_test.rb | 48 ++++++++++ 15 files changed, 267 insertions(+) create mode 100644 app/assets/javascripts/admin/lunchbox.js create mode 100644 app/assets/stylesheets/admin/lunchbox.scss create mode 100644 app/assets/stylesheets/scaffolds.scss create mode 100644 app/controllers/admin/lunchboxes_controller.rb create mode 100644 app/helpers/admin/lunchbox_helper.rb create mode 100644 app/helpers/admin/lunchboxes_helper.rb create mode 100644 app/models/admin.rb create mode 100644 app/views/admin/lunchboxes/_form.html.slim create mode 100644 app/views/admin/lunchboxes/edit.html.slim create mode 100644 app/views/admin/lunchboxes/index.html.slim create mode 100644 app/views/admin/lunchboxes/new.html.slim create mode 100644 app/views/admin/lunchboxes/show.html.slim create mode 100644 test/controllers/admin/lunchbox_controller_test.rb create mode 100644 test/controllers/admin/lunchboxes_controller_test.rb diff --git a/app/assets/javascripts/admin/lunchbox.js b/app/assets/javascripts/admin/lunchbox.js new file mode 100644 index 0000000..dee720f --- /dev/null +++ b/app/assets/javascripts/admin/lunchbox.js @@ -0,0 +1,2 @@ +// Place all the behaviors and hooks related to the matching controller here. +// All this logic will automatically be available in application.js. diff --git a/app/assets/stylesheets/admin/lunchbox.scss b/app/assets/stylesheets/admin/lunchbox.scss new file mode 100644 index 0000000..6a19aeb --- /dev/null +++ b/app/assets/stylesheets/admin/lunchbox.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the admin/lunchbox controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/assets/stylesheets/scaffolds.scss b/app/assets/stylesheets/scaffolds.scss new file mode 100644 index 0000000..4ce4266 --- /dev/null +++ b/app/assets/stylesheets/scaffolds.scss @@ -0,0 +1,89 @@ +body { + background-color: #fff; + color: #333; + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; + margin: 33px; +} + +p, ol, ul, td { + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; + margin: 33px; +} + +pre { + background-color: #eee; + padding: 10px; + font-size: 11px; +} + +a { + color: #000; + + &:visited { + color: #666; + } + + &:hover { + color: #fff; + background-color: #000; + } +} + +th { + padding-bottom: 5px; +} + +td { + padding-bottom: 7px; + padding-left: 5px; + padding-right: 5px; +} + +div { + &.field, &.actions { + margin-bottom: 10px; + } +} + +#notice { + color: green; +} + +.field_with_errors { + padding: 2px; + background-color: red; + display: table; +} + +#error_explanation { + width: 450px; + border: 2px solid red; + padding: 7px; + padding-bottom: 0; + margin-bottom: 20px; + background-color: #f0f0f0; + + h2 { + text-align: left; + font-weight: bold; + padding: 5px 5px 5px 15px; + font-size: 12px; + margin: -7px; + margin-bottom: 0; + background-color: #c00; + color: #fff; + } + + ul li { + font-size: 12px; + list-style: square; + } +} + +label { + display: block; +} diff --git a/app/controllers/admin/lunchboxes_controller.rb b/app/controllers/admin/lunchboxes_controller.rb new file mode 100644 index 0000000..0554173 --- /dev/null +++ b/app/controllers/admin/lunchboxes_controller.rb @@ -0,0 +1,58 @@ +class Admin::LunchboxesController < ApplicationController + before_action :set_lunchbox, only: [:show, :edit, :update, :destroy] + + # GET /lunchboxes + def index + @lunchboxes = Lunchbox.all + end + + # GET /lunchboxes/1 + def show + end + + # GET /lunchboxes/new + def new + @lunchbox = Lunchbox.new + end + + # GET /lunchboxes/1/edit + def edit + end + + # POST /lunchboxes + def create + @lunchbox = Lunchbox.new(lunchbox_params) + + if @lunchbox.save + redirect_to @lunchbox, notice: 'Lunchbox was successfully created.' + else + render :new + end + end + + # PATCH/PUT /lunchboxes/1 + def update + if @lunchbox.update(lunchbox_params) + redirect_to @lunchbox, notice: 'Lunchbox was successfully updated.' + else + render :edit + end + end + + # DELETE /lunchboxes/1 + def destroy + @lunchbox.destroy + redirect_to lunchboxes_url, notice: 'Lunchbox was successfully destroyed.' + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_lunchbox + @lunchbox = Lunchbox.find(params[:id]) + end + + # Only allow a trusted parameter "white list" through. + def lunchbox_params + params.fetch(:lunchbox, {}) + end +end diff --git a/app/helpers/admin/lunchbox_helper.rb b/app/helpers/admin/lunchbox_helper.rb new file mode 100644 index 0000000..aa42d6e --- /dev/null +++ b/app/helpers/admin/lunchbox_helper.rb @@ -0,0 +1,2 @@ +module Admin::LunchboxHelper +end diff --git a/app/helpers/admin/lunchboxes_helper.rb b/app/helpers/admin/lunchboxes_helper.rb new file mode 100644 index 0000000..1f4030e --- /dev/null +++ b/app/helpers/admin/lunchboxes_helper.rb @@ -0,0 +1,2 @@ +module Admin::LunchboxesHelper +end diff --git a/app/models/admin.rb b/app/models/admin.rb new file mode 100644 index 0000000..a38e3f4 --- /dev/null +++ b/app/models/admin.rb @@ -0,0 +1,5 @@ +module Admin + def self.table_name_prefix + 'admin_' + end +end diff --git a/app/views/admin/lunchboxes/_form.html.slim b/app/views/admin/lunchboxes/_form.html.slim new file mode 100644 index 0000000..b3c8d9d --- /dev/null +++ b/app/views/admin/lunchboxes/_form.html.slim @@ -0,0 +1,9 @@ += form_for @lunchbox do |f| + - if @lunchbox.errors.any? + #error_explanation + h2 = "#{pluralize(@lunchbox.errors.count, "error")} prohibited this lunchbox from being saved:" + ul + - @lunchbox.errors.full_messages.each do |message| + li = message + + .actions = f.submit diff --git a/app/views/admin/lunchboxes/edit.html.slim b/app/views/admin/lunchboxes/edit.html.slim new file mode 100644 index 0000000..8b1ad1e --- /dev/null +++ b/app/views/admin/lunchboxes/edit.html.slim @@ -0,0 +1,8 @@ +h1 Editing lunchbox + +== render 'form' + += link_to 'Show', @lunchbox +' | += link_to 'Back', lunchboxes_path + diff --git a/app/views/admin/lunchboxes/index.html.slim b/app/views/admin/lunchboxes/index.html.slim new file mode 100644 index 0000000..5a8703f --- /dev/null +++ b/app/views/admin/lunchboxes/index.html.slim @@ -0,0 +1,21 @@ +h1 Listing lunchboxes + +table + thead + tr + th + th + th + th + + tbody + - @lunchboxes.each do |lunchbox| + tr + td = lunchbox.name + td = link_to 'Show', admin_lunchbox_path(lunchbox) + td = link_to 'Edit', edit_admin_lunchbox_path(lunchbox) + td = link_to 'Destroy', admin_lunchbox_path(lunchbox), data: { confirm: 'Are you sure?' }, method: :delete + +br + += link_to 'New Lunchbox', new_admin_lunchbox_path diff --git a/app/views/admin/lunchboxes/new.html.slim b/app/views/admin/lunchboxes/new.html.slim new file mode 100644 index 0000000..13681e8 --- /dev/null +++ b/app/views/admin/lunchboxes/new.html.slim @@ -0,0 +1,5 @@ +h1 New lunchbox + +== render 'form' + += link_to 'Back', lunchboxes_path diff --git a/app/views/admin/lunchboxes/show.html.slim b/app/views/admin/lunchboxes/show.html.slim new file mode 100644 index 0000000..06911d3 --- /dev/null +++ b/app/views/admin/lunchboxes/show.html.slim @@ -0,0 +1,6 @@ +p#notice = notice + + += link_to 'Edit', edit_lunchbox_path(@lunchbox) +' | += link_to 'Back', lunchboxes_path diff --git a/config/routes.rb b/config/routes.rb index 85aa1bb..2120906 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -10,6 +10,8 @@ get :todays_order, to: 'order_items#index' end end + resources :lunchboxes + # , only: %i(create) end resources :orders, only: %i(index) do diff --git a/test/controllers/admin/lunchbox_controller_test.rb b/test/controllers/admin/lunchbox_controller_test.rb new file mode 100644 index 0000000..33e8d07 --- /dev/null +++ b/test/controllers/admin/lunchbox_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class Admin::LunchboxControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end diff --git a/test/controllers/admin/lunchboxes_controller_test.rb b/test/controllers/admin/lunchboxes_controller_test.rb new file mode 100644 index 0000000..263490b --- /dev/null +++ b/test/controllers/admin/lunchboxes_controller_test.rb @@ -0,0 +1,48 @@ +require 'test_helper' + +class Admin::LunchboxesControllerTest < ActionDispatch::IntegrationTest + setup do + @lunchbox = lunchboxes(:one) + end + + test "should get index" do + get lunchboxes_url + assert_response :success + end + + test "should get new" do + get new_lunchbox_url + assert_response :success + end + + test "should create lunchbox" do + assert_difference('Lunchbox.count') do + post lunchboxes_url, params: { lunchbox: { } } + end + + assert_redirected_to lunchbox_url(Lunchbox.last) + end + + test "should show lunchbox" do + get lunchbox_url(@lunchbox) + assert_response :success + end + + test "should get edit" do + get edit_lunchbox_url(@lunchbox) + assert_response :success + end + + test "should update lunchbox" do + patch lunchbox_url(@lunchbox), params: { lunchbox: { } } + assert_redirected_to lunchbox_url(@lunchbox) + end + + test "should destroy lunchbox" do + assert_difference('Lunchbox.count', -1) do + delete lunchbox_url(@lunchbox) + end + + assert_redirected_to lunchboxes_url + end +end From 0b784009e9417963cfe9e16258f351ad409d05b7 Mon Sep 17 00:00:00 2001 From: colorbox Date: Tue, 2 May 2017 15:57:36 +0900 Subject: [PATCH 2/4] wip Add insert Lunchbox --- .../admin/lunchboxes_controller.rb | 11 +++++++--- app/views/admin/lunchboxes/new.html.slim | 21 +++++++++++++++++-- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/app/controllers/admin/lunchboxes_controller.rb b/app/controllers/admin/lunchboxes_controller.rb index 0554173..a7a33de 100644 --- a/app/controllers/admin/lunchboxes_controller.rb +++ b/app/controllers/admin/lunchboxes_controller.rb @@ -24,7 +24,7 @@ def create @lunchbox = Lunchbox.new(lunchbox_params) if @lunchbox.save - redirect_to @lunchbox, notice: 'Lunchbox was successfully created.' + redirect_to admin_lunchboxes_path, notice: 'Lunchbox was successfully created.' else render :new end @@ -51,8 +51,13 @@ def set_lunchbox @lunchbox = Lunchbox.find(params[:id]) end - # Only allow a trusted parameter "white list" through. + # # Only allow a trusted parameter "white list" through. + # def lunchbox_params + # params.fetch(:lunchbox, {}) + # end + def lunchbox_params - params.fetch(:lunchbox, {}) + params.require(:lunchbox).permit(:name, :price) end + end diff --git a/app/views/admin/lunchboxes/new.html.slim b/app/views/admin/lunchboxes/new.html.slim index 13681e8..051cc38 100644 --- a/app/views/admin/lunchboxes/new.html.slim +++ b/app/views/admin/lunchboxes/new.html.slim @@ -1,5 +1,22 @@ h1 New lunchbox -== render 'form' += form_for(@lunchbox, url: admin_lunchboxes_path) do |f| + - if @lunchbox.errors.any? + #error_explanation + h2 = "#{pluralize(@lunchbox.errors.count, "error")} prohibited this lunchbox from being saved:" + ul + - @lunchbox.errors.full_messages.each do |message| + li = message -= link_to 'Back', lunchboxes_path + fieldset + .form-group + = f.label :name + = f.text_field :name, class: 'form-control' + + .form-group + = f.label :price + = f.text_field :price, class: 'form-control' + + .actions = f.submit + +/= link_to 'Back', lunchboxes_path From f2a49542d07a94aaac092f0a91d064661c7a17ff Mon Sep 17 00:00:00 2001 From: colorbox Date: Tue, 2 May 2017 16:04:51 +0900 Subject: [PATCH 3/4] wip Add update lunchbox for admin --- app/controllers/admin/lunchboxes_controller.rb | 2 +- app/views/admin/lunchboxes/_form.html.slim | 11 ++++++++++- app/views/admin/lunchboxes/edit.html.slim | 6 +++--- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/app/controllers/admin/lunchboxes_controller.rb b/app/controllers/admin/lunchboxes_controller.rb index a7a33de..9b3403b 100644 --- a/app/controllers/admin/lunchboxes_controller.rb +++ b/app/controllers/admin/lunchboxes_controller.rb @@ -33,7 +33,7 @@ def create # PATCH/PUT /lunchboxes/1 def update if @lunchbox.update(lunchbox_params) - redirect_to @lunchbox, notice: 'Lunchbox was successfully updated.' + redirect_to admin_lunchboxes_path, notice: 'Lunchbox was successfully updated.' else render :edit end diff --git a/app/views/admin/lunchboxes/_form.html.slim b/app/views/admin/lunchboxes/_form.html.slim index b3c8d9d..c9bf59a 100644 --- a/app/views/admin/lunchboxes/_form.html.slim +++ b/app/views/admin/lunchboxes/_form.html.slim @@ -1,4 +1,4 @@ -= form_for @lunchbox do |f| += form_for(@lunchbox, url: admin_lunchbox_path(@lunchbox)) do |f| - if @lunchbox.errors.any? #error_explanation h2 = "#{pluralize(@lunchbox.errors.count, "error")} prohibited this lunchbox from being saved:" @@ -6,4 +6,13 @@ - @lunchbox.errors.full_messages.each do |message| li = message + fieldset + .form-group + = f.label :name + = f.text_field :name, class: 'form-control' + + .form-group + = f.label :price + = f.text_field :price, class: 'form-control' + .actions = f.submit diff --git a/app/views/admin/lunchboxes/edit.html.slim b/app/views/admin/lunchboxes/edit.html.slim index 8b1ad1e..a5064bf 100644 --- a/app/views/admin/lunchboxes/edit.html.slim +++ b/app/views/admin/lunchboxes/edit.html.slim @@ -2,7 +2,7 @@ h1 Editing lunchbox == render 'form' -= link_to 'Show', @lunchbox -' | -= link_to 'Back', lunchboxes_path +/= link_to 'Show', @lunchbox +/' | +/= link_to 'Back', lunchboxes_path From 8b03bd99ddfbabf387c87251c808bdbf25e244e8 Mon Sep 17 00:00:00 2001 From: colorbox Date: Tue, 2 May 2017 16:50:06 +0900 Subject: [PATCH 4/4] wip Add update bento action --- .../admin/lunchboxes_controller.rb | 6 ++++- app/models/lunchbox.rb | 23 +++++++++++++++++++ ...nt_update_future_reserved_lunchbox_spec.rb | 21 +++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 spec/features/admin/prevent_update_future_reserved_lunchbox_spec.rb diff --git a/app/controllers/admin/lunchboxes_controller.rb b/app/controllers/admin/lunchboxes_controller.rb index 9b3403b..7e792c7 100644 --- a/app/controllers/admin/lunchboxes_controller.rb +++ b/app/controllers/admin/lunchboxes_controller.rb @@ -32,10 +32,14 @@ def create # PATCH/PUT /lunchboxes/1 def update + if @lunchbox.update(lunchbox_params) redirect_to admin_lunchboxes_path, notice: 'Lunchbox was successfully updated.' else - render :edit + puts("#########") + puts("#########") + puts("#########") + render :edit, notice: '更新できませんでした' end end diff --git a/app/models/lunchbox.rb b/app/models/lunchbox.rb index 4a28630..2de43c0 100644 --- a/app/models/lunchbox.rb +++ b/app/models/lunchbox.rb @@ -10,4 +10,27 @@ # class Lunchbox < ApplicationRecord + has_many :order_items + has_many :orders, through: :order_items + + # validation + + validate :prevent_future_reserved_lunchbox, on: :update + + private + def prevent_future_reserved_lunchbox + # 弁当に紐づくオーダーを取ってくる + # 未来日のオーダーに自分が含まれているかを確認 + # 含まれている場合は更新させない + # puts orders.where("date > ?", Date.current).present? + # puts orders.first.date + # puts self.name + # puts Date.current + # puts"###################" + + if orders.where("date > ?", Date.current).present? + errors.add(:future_date, "未来日に予約された弁当は更新できません") + end + end + end diff --git a/spec/features/admin/prevent_update_future_reserved_lunchbox_spec.rb b/spec/features/admin/prevent_update_future_reserved_lunchbox_spec.rb new file mode 100644 index 0000000..6cd16a8 --- /dev/null +++ b/spec/features/admin/prevent_update_future_reserved_lunchbox_spec.rb @@ -0,0 +1,21 @@ +require 'rails_helper' + +RSpec.feature '未来日に予約されている弁当の更新を行う時にエラーにする', type: :feature do + given!(:order) { create(:order, date: Time.zone.local(2017, 2, 4)) } + given!(:lunchbox) { create(:lunchbox) } + + scenario '未来日に予約を入れた状態で、その弁当を更新する' do + Timecop.freeze(Time.zone.local(2017, 2, 1)) do + create(:order_item, lunchbox_id: lunchbox.id, order: order) + + visit edit_admin_lunchbox_path(lunchbox) + + fill_in 'Name', with: "new_lunchbox_name" + + click_button('Update Lunchbox') + + expect(page).to have_text('未来日に予約された弁当は更新できません') + # expect(page).not_to have_text('new_lunchbox_name') + end + end +end