add item set with scaffold

This commit is contained in:
Yan
2017-08-11 17:41:10 +06:30
parent be71d187b0
commit 03a6208a76
26 changed files with 476 additions and 1 deletions

View File

@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/

View File

@@ -0,0 +1,3 @@
// Place all the styles related to the Settings/ItemSets controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/

View File

@@ -0,0 +1,74 @@
class Settings::ItemSetsController < ApplicationController
before_action :set_settings_item_set, only: [:show, :edit, :update, :destroy]
# GET /settings/item_sets
# GET /settings/item_sets.json
def index
@settings_item_sets = ItemSet.all
end
# GET /settings/item_sets/1
# GET /settings/item_sets/1.json
def show
end
# GET /settings/item_sets/new
def new
@settings_item_set = ItemSet.new
end
# GET /settings/item_sets/1/edit
def edit
end
# POST /settings/item_sets
# POST /settings/item_sets.json
def create
@settings_item_set = ItemSet.new(settings_item_set_params)
respond_to do |format|
if @settings_item_set.save
format.html { redirect_to @settings_item_set, notice: 'Item set was successfully created.' }
format.json { render :show, status: :created, location: @settings_item_set }
else
format.html { render :new }
format.json { render json: @settings_item_set.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /settings/item_sets/1
# PATCH/PUT /settings/item_sets/1.json
def update
respond_to do |format|
if @settings_item_set.update(settings_item_set_params)
format.html { redirect_to @settings_item_set, notice: 'Item set was successfully updated.' }
format.json { render :show, status: :ok, location: @settings_item_set }
else
format.html { render :edit }
format.json { render json: @settings_item_set.errors, status: :unprocessable_entity }
end
end
end
# DELETE /settings/item_sets/1
# DELETE /settings/item_sets/1.json
def destroy
@settings_item_set.destroy
respond_to do |format|
format.html { redirect_to settings_item_sets_url, notice: 'Item set was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_settings_item_set
@settings_item_set = ItemSet.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def settings_item_set_params
params.require(:settings_item_set).permit(:name, :min_selectable_qty, :max_selectable_qty)
end
end

View File

@@ -0,0 +1,2 @@
module Settings::ItemSetsHelper
end

View File

@@ -3,5 +3,5 @@ class ItemSet < ApplicationRecord
has_many :menu_items, through: :menu_item_sets
has_many :menu_instance_item_sets
has_many :menu_item_instances, through: :menu_item_sets
has_many :menu_item_instances, through: :menu_instance_item_sets
end

View File

@@ -0,0 +1,2 @@
class Settings::ItemSet < ApplicationRecord
end

View File

@@ -0,0 +1,13 @@
<%= simple_form_for(@settings_item_set) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :name %>
<%= f.input :min_selectable_qty %>
<%= f.input :max_selectable_qty %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>

View File

@@ -0,0 +1,2 @@
json.extract! settings_item_set, :id, :name, :min_selectable_qty, :max_selectable_qty, :created_at, :updated_at
json.url settings_item_set_url(settings_item_set, format: :json)

View File

@@ -0,0 +1,6 @@
<h1>Editing Settings Item Set</h1>
<%= render 'form', settings_item_set: @settings_item_set %>
<%= link_to 'Show', @settings_item_set %> |
<%= link_to 'Back', settings_item_sets_path %>

View File

@@ -0,0 +1,31 @@
<p id="notice"><%= notice %></p>
<h1>Settings Item Sets</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Min selectable qty</th>
<th>Max selectable qty</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @settings_item_sets.each do |settings_item_set| %>
<tr>
<td><%= settings_item_set.name %></td>
<td><%= settings_item_set.min_selectable_qty %></td>
<td><%= settings_item_set.max_selectable_qty %></td>
<td><%= link_to 'Show', settings_item_set %></td>
<td><%= link_to 'Edit', edit_settings_item_set_path(settings_item_set) %></td>
<td><%= link_to 'Destroy', settings_item_set, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Settings Item Set', new_settings_item_set_path %>

View File

@@ -0,0 +1 @@
json.array! @settings_item_sets, partial: 'settings_item_sets/settings_item_set', as: :settings_item_set

View File

@@ -0,0 +1,5 @@
<h1>New Settings Item Set</h1>
<%= render 'form', settings_item_set: @settings_item_set %>
<%= link_to 'Back', settings_item_sets_path %>

View File

@@ -0,0 +1,19 @@
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= @settings_item_set.name %>
</p>
<p>
<strong>Min selectable qty:</strong>
<%= @settings_item_set.min_selectable_qty %>
</p>
<p>
<strong>Max selectable qty:</strong>
<%= @settings_item_set.max_selectable_qty %>
</p>
<%= link_to 'Edit', edit_settings_item_set_path(@settings_item_set) %> |
<%= link_to 'Back', settings_item_sets_path %>

View File

@@ -0,0 +1 @@
json.partial! "settings_item_sets/settings_item_set", settings_item_set: @settings_item_set

View File

@@ -2,6 +2,9 @@ require 'sidekiq/web'
Rails.application.routes.draw do
namespace :settings do
resources :item_sets
end
root 'home#index'
mount Sidekiq::Web => '/kiq'

View File

@@ -0,0 +1,141 @@
require 'rails_helper'
# This spec was generated by rspec-rails when you ran the scaffold generator.
# It demonstrates how one might use RSpec to specify the controller code that
# was generated by Rails when you ran the scaffold generator.
#
# It assumes that the implementation code is generated by the rails scaffold
# generator. If you are using any extension libraries to generate different
# controller code, this generated spec may or may not pass.
#
# It only uses APIs available in rails and/or rspec-rails. There are a number
# of tools you can use to make these specs even more expressive, but we're
# sticking to rails and rspec-rails APIs to keep things simple and stable.
#
# Compared to earlier versions of this generator, there is very limited use of
# stubs and message expectations in this spec. Stubs are only used when there
# is no simpler way to get a handle on the object needed for the example.
# Message expectations are only used when there is no simpler way to specify
# that an instance is receiving a specific message.
#
# Also compared to earlier versions of this generator, there are no longer any
# expectations of assigns and templates rendered. These features have been
# removed from Rails core in Rails 5, but can be added back in via the
# `rails-controller-testing` gem.
RSpec.describe Settings::ItemSetsController, type: :controller do
# This should return the minimal set of attributes required to create a valid
# Settings::ItemSet. As you add validations to Settings::ItemSet, be sure to
# adjust the attributes here as well.
let(:valid_attributes) {
skip("Add a hash of attributes valid for your model")
}
let(:invalid_attributes) {
skip("Add a hash of attributes invalid for your model")
}
# This should return the minimal set of values that should be in the session
# in order to pass any filters (e.g. authentication) defined in
# Settings::ItemSetsController. Be sure to keep this updated too.
let(:valid_session) { {} }
describe "GET #index" do
it "returns a success response" do
item_set = Settings::ItemSet.create! valid_attributes
get :index, params: {}, session: valid_session
expect(response).to be_success
end
end
describe "GET #show" do
it "returns a success response" do
item_set = Settings::ItemSet.create! valid_attributes
get :show, params: {id: item_set.to_param}, session: valid_session
expect(response).to be_success
end
end
describe "GET #new" do
it "returns a success response" do
get :new, params: {}, session: valid_session
expect(response).to be_success
end
end
describe "GET #edit" do
it "returns a success response" do
item_set = Settings::ItemSet.create! valid_attributes
get :edit, params: {id: item_set.to_param}, session: valid_session
expect(response).to be_success
end
end
describe "POST #create" do
context "with valid params" do
it "creates a new Settings::ItemSet" do
expect {
post :create, params: {settings_item_set: valid_attributes}, session: valid_session
}.to change(Settings::ItemSet, :count).by(1)
end
it "redirects to the created settings_item_set" do
post :create, params: {settings_item_set: valid_attributes}, session: valid_session
expect(response).to redirect_to(Settings::ItemSet.last)
end
end
context "with invalid params" do
it "returns a success response (i.e. to display the 'new' template)" do
post :create, params: {settings_item_set: invalid_attributes}, session: valid_session
expect(response).to be_success
end
end
end
describe "PUT #update" do
context "with valid params" do
let(:new_attributes) {
skip("Add a hash of attributes valid for your model")
}
it "updates the requested settings_item_set" do
item_set = Settings::ItemSet.create! valid_attributes
put :update, params: {id: item_set.to_param, settings_item_set: new_attributes}, session: valid_session
item_set.reload
skip("Add assertions for updated state")
end
it "redirects to the settings_item_set" do
item_set = Settings::ItemSet.create! valid_attributes
put :update, params: {id: item_set.to_param, settings_item_set: valid_attributes}, session: valid_session
expect(response).to redirect_to(item_set)
end
end
context "with invalid params" do
it "returns a success response (i.e. to display the 'edit' template)" do
item_set = Settings::ItemSet.create! valid_attributes
put :update, params: {id: item_set.to_param, settings_item_set: invalid_attributes}, session: valid_session
expect(response).to be_success
end
end
end
describe "DELETE #destroy" do
it "destroys the requested settings_item_set" do
item_set = Settings::ItemSet.create! valid_attributes
expect {
delete :destroy, params: {id: item_set.to_param}, session: valid_session
}.to change(Settings::ItemSet, :count).by(-1)
end
it "redirects to the settings_item_sets list" do
item_set = Settings::ItemSet.create! valid_attributes
delete :destroy, params: {id: item_set.to_param}, session: valid_session
expect(response).to redirect_to(settings_item_sets_url)
end
end
end

View File

@@ -0,0 +1,15 @@
require 'rails_helper'
# Specs in this file have access to a helper object that includes
# the Settings::ItemSetsHelper. For example:
#
# describe Settings::ItemSetsHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
RSpec.describe Settings::ItemSetsHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end

View File

@@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe Settings::ItemSet, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end

View File

@@ -0,0 +1,10 @@
require 'rails_helper'
RSpec.describe "Settings::ItemSets", type: :request do
describe "GET /settings_item_sets" do
it "works! (now write some real specs)" do
get settings_item_sets_path
expect(response).to have_http_status(200)
end
end
end

View File

@@ -0,0 +1,39 @@
require "rails_helper"
RSpec.describe Settings::ItemSetsController, type: :routing do
describe "routing" do
it "routes to #index" do
expect(:get => "/settings/item_sets").to route_to("settings/item_sets#index")
end
it "routes to #new" do
expect(:get => "/settings/item_sets/new").to route_to("settings/item_sets#new")
end
it "routes to #show" do
expect(:get => "/settings/item_sets/1").to route_to("settings/item_sets#show", :id => "1")
end
it "routes to #edit" do
expect(:get => "/settings/item_sets/1/edit").to route_to("settings/item_sets#edit", :id => "1")
end
it "routes to #create" do
expect(:post => "/settings/item_sets").to route_to("settings/item_sets#create")
end
it "routes to #update via PUT" do
expect(:put => "/settings/item_sets/1").to route_to("settings/item_sets#update", :id => "1")
end
it "routes to #update via PATCH" do
expect(:patch => "/settings/item_sets/1").to route_to("settings/item_sets#update", :id => "1")
end
it "routes to #destroy" do
expect(:delete => "/settings/item_sets/1").to route_to("settings/item_sets#destroy", :id => "1")
end
end
end

View File

@@ -0,0 +1,24 @@
require 'rails_helper'
RSpec.describe "settings/item_sets/edit", type: :view do
before(:each) do
@settings_item_set = assign(:settings_item_set, Settings::ItemSet.create!(
:name => "MyString",
:min_selectable_qty => 1,
:max_selectable_qty => 1
))
end
it "renders the edit settings_item_set form" do
render
assert_select "form[action=?][method=?]", settings_item_set_path(@settings_item_set), "post" do
assert_select "input[name=?]", "settings_item_set[name]"
assert_select "input[name=?]", "settings_item_set[min_selectable_qty]"
assert_select "input[name=?]", "settings_item_set[max_selectable_qty]"
end
end
end

View File

@@ -0,0 +1,25 @@
require 'rails_helper'
RSpec.describe "settings/item_sets/index", type: :view do
before(:each) do
assign(:settings_item_sets, [
Settings::ItemSet.create!(
:name => "Name",
:min_selectable_qty => 2,
:max_selectable_qty => 3
),
Settings::ItemSet.create!(
:name => "Name",
:min_selectable_qty => 2,
:max_selectable_qty => 3
)
])
end
it "renders a list of settings/item_sets" do
render
assert_select "tr>td", :text => "Name".to_s, :count => 2
assert_select "tr>td", :text => 2.to_s, :count => 2
assert_select "tr>td", :text => 3.to_s, :count => 2
end
end

View File

@@ -0,0 +1,24 @@
require 'rails_helper'
RSpec.describe "settings/item_sets/new", type: :view do
before(:each) do
assign(:settings_item_set, Settings::ItemSet.new(
:name => "MyString",
:min_selectable_qty => 1,
:max_selectable_qty => 1
))
end
it "renders new settings_item_set form" do
render
assert_select "form[action=?][method=?]", settings_item_sets_path, "post" do
assert_select "input[name=?]", "settings_item_set[name]"
assert_select "input[name=?]", "settings_item_set[min_selectable_qty]"
assert_select "input[name=?]", "settings_item_set[max_selectable_qty]"
end
end
end

View File

@@ -0,0 +1,18 @@
require 'rails_helper'
RSpec.describe "settings/item_sets/show", type: :view do
before(:each) do
@settings_item_set = assign(:settings_item_set, Settings::ItemSet.create!(
:name => "Name",
:min_selectable_qty => 2,
:max_selectable_qty => 3
))
end
it "renders attributes in <p>" do
render
expect(rendered).to match(/Name/)
expect(rendered).to match(/2/)
expect(rendered).to match(/3/)
end
end

View File

@@ -0,0 +1,9 @@
require "application_system_test_case"
class Settings::ItemSetsTest < ApplicationSystemTestCase
# test "visiting the index" do
# visit settings_item_sets_url
#
# assert_selector "h1", text: "Settings::ItemSet"
# end
end