Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP][Please don't review]62 admin update lunchbox master data #132

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/assets/javascripts/admin/lunchbox.js
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 3 additions & 0 deletions app/assets/stylesheets/admin/lunchbox.scss
Original file line number Diff line number Diff line change
@@ -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/
89 changes: 89 additions & 0 deletions app/assets/stylesheets/scaffolds.scss
Original file line number Diff line number Diff line change
@@ -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;
}
67 changes: 67 additions & 0 deletions app/controllers/admin/lunchboxes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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 admin_lunchboxes_path, notice: 'Lunchbox was successfully created.'
else
render :new
end
end

# PATCH/PUT /lunchboxes/1
def update

if @lunchbox.update(lunchbox_params)
redirect_to admin_lunchboxes_path, notice: 'Lunchbox was successfully updated.'
else
puts("#########")
puts("#########")
puts("#########")
render :edit, notice: '更新できませんでした'
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

def lunchbox_params
params.require(:lunchbox).permit(:name, :price)
end

end
2 changes: 2 additions & 0 deletions app/helpers/admin/lunchbox_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module Admin::LunchboxHelper
end
2 changes: 2 additions & 0 deletions app/helpers/admin/lunchboxes_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module Admin::LunchboxesHelper
end
5 changes: 5 additions & 0 deletions app/models/admin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Admin
def self.table_name_prefix
'admin_'
end
end
23 changes: 23 additions & 0 deletions app/models/lunchbox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 18 additions & 0 deletions app/views/admin/lunchboxes/_form.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
= 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:"
ul
- @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
8 changes: 8 additions & 0 deletions app/views/admin/lunchboxes/edit.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
h1 Editing lunchbox

== render 'form'

/= link_to 'Show', @lunchbox
/' |
/= link_to 'Back', lunchboxes_path

21 changes: 21 additions & 0 deletions app/views/admin/lunchboxes/index.html.slim
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions app/views/admin/lunchboxes/new.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
h1 New lunchbox

= 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

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
6 changes: 6 additions & 0 deletions app/views/admin/lunchboxes/show.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
p#notice = notice


= link_to 'Edit', edit_lunchbox_path(@lunchbox)
' |
= link_to 'Back', lunchboxes_path
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions test/controllers/admin/lunchbox_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class Admin::LunchboxControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end
48 changes: 48 additions & 0 deletions test/controllers/admin/lunchboxes_controller_test.rb
Original file line number Diff line number Diff line change
@@ -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