diff --git a/app/assets/javascripts/api/restaurant/zone.coffee b/app/assets/javascripts/api/restaurant/zone.coffee new file mode 100644 index 00000000..24f83d18 --- /dev/null +++ b/app/assets/javascripts/api/restaurant/zone.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/api/restaurant/zone.scss b/app/assets/stylesheets/api/restaurant/zone.scss new file mode 100644 index 00000000..a9c71ee3 --- /dev/null +++ b/app/assets/stylesheets/api/restaurant/zone.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the api/restaurant/zone controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/api/restaurant/zone_controller.rb b/app/controllers/api/restaurant/zone_controller.rb new file mode 100644 index 00000000..b2428713 --- /dev/null +++ b/app/controllers/api/restaurant/zone_controller.rb @@ -0,0 +1,4 @@ +class Api::Restaurant::ZoneController < ApplicationController + def index + end +end diff --git a/app/helpers/api/restaurant/zone_helper.rb b/app/helpers/api/restaurant/zone_helper.rb new file mode 100644 index 00000000..922e87ba --- /dev/null +++ b/app/helpers/api/restaurant/zone_helper.rb @@ -0,0 +1,2 @@ +module Api::Restaurant::ZoneHelper +end diff --git a/app/models/seat_table.rb b/app/models/seat_table.rb index 99bcf879..758545fd 100644 --- a/app/models/seat_table.rb +++ b/app/models/seat_table.rb @@ -1,3 +1,6 @@ class SeatTable < ApplicationRecord belongs_to :zone + + # validations + validates_presence_of :name,:order_by, :created_by end diff --git a/app/models/zone.rb b/app/models/zone.rb index f4c6e236..6ce3dd0a 100644 --- a/app/models/zone.rb +++ b/app/models/zone.rb @@ -1,2 +1,7 @@ class Zone < ApplicationRecord + # model association + has_many :seat_tables, dependent: :destroy + + # validations + validates_presence_of :name, :created_by end diff --git a/app/views/api/restaurant/zone/index.html.erb b/app/views/api/restaurant/zone/index.html.erb new file mode 100644 index 00000000..32946096 --- /dev/null +++ b/app/views/api/restaurant/zone/index.html.erb @@ -0,0 +1,2 @@ +

Api::Restaurant::Zone#index

+

Find me in app/views/api/restaurant/zone/index.html.erb

diff --git a/config/routes.rb b/config/routes.rb index 787824f8..4205712a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,3 +1,9 @@ Rails.application.routes.draw do + namespace :api do + namespace :restaurant do + get 'zone/index' + end + end + # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end diff --git a/db/migrate/20170324135138_create_zones.rb b/db/migrate/20170324135138_create_zones.rb index 7dfa1938..30a92048 100644 --- a/db/migrate/20170324135138_create_zones.rb +++ b/db/migrate/20170324135138_create_zones.rb @@ -3,6 +3,7 @@ class CreateZones < ActiveRecord::Migration[5.0] create_table :zones do |t| t.string :name, :null => false t.boolean :is_active, :default => true + t.string :created_by t.timestamps end end diff --git a/db/migrate/20170324135335_create_seat_tables.rb b/db/migrate/20170324135335_create_seat_tables.rb index 81522697..9b7da5b7 100644 --- a/db/migrate/20170324135335_create_seat_tables.rb +++ b/db/migrate/20170324135335_create_seat_tables.rb @@ -8,7 +8,7 @@ class CreateSeatTables < ActiveRecord::Migration[5.0] t.string :table_type, :null => false, :default => "square" t.float :position_x, :null => false, :default => 0.0 t.float :position_y, :null => false, :default => 0.0 - + t.string :created_by t.timestamps end end diff --git a/spec/controllers/api/restaurant/zone_controller_spec.rb b/spec/controllers/api/restaurant/zone_controller_spec.rb new file mode 100644 index 00000000..b81a8d14 --- /dev/null +++ b/spec/controllers/api/restaurant/zone_controller_spec.rb @@ -0,0 +1,12 @@ +require 'rails_helper' + +RSpec.describe Api::Restaurant::ZoneController, type: :controller do + + describe "GET #index" do + it "returns http success" do + get :index + expect(response).to have_http_status(:success) + end + end + +end diff --git a/spec/factories/seat_tables.rb b/spec/factories/seat_tables.rb new file mode 100644 index 00000000..5f2fdea2 --- /dev/null +++ b/spec/factories/seat_tables.rb @@ -0,0 +1,8 @@ +FactoryGirl.define do + factory :seat_tables do + name { Faker::StarWars.character } + order_by { Faker::Number.number(5)} + no_of_seater {Faker::Number.number(5)} + zone_id nil + end +end diff --git a/spec/factories/zones.rb b/spec/factories/zones.rb new file mode 100644 index 00000000..ed8a024d --- /dev/null +++ b/spec/factories/zones.rb @@ -0,0 +1,6 @@ +FactoryGirl.define do + factory :zone do + name { Faker::Lorem.word } + created_by { Faker::StarWars.character } + end +end diff --git a/spec/models/seat_table_spec.rb b/spec/models/seat_table_spec.rb index fc5bbbae..500b4679 100644 --- a/spec/models/seat_table_spec.rb +++ b/spec/models/seat_table_spec.rb @@ -1,5 +1,13 @@ require 'rails_helper' RSpec.describe SeatTable, type: :model do - pending "add some examples to (or delete) #{__FILE__}" + # Association test + # ensure an item record belongs to a single todo record + it { should belong_to(:zone) } + # Validation test + # ensure column name is present before saving + it { should validate_presence_of(:name) } + it { should validate_presence_of(:order_by) } + it { should validate_presence_of(:created_by) } + end diff --git a/spec/models/zone_spec.rb b/spec/models/zone_spec.rb index 3f5ad259..7ec51333 100644 --- a/spec/models/zone_spec.rb +++ b/spec/models/zone_spec.rb @@ -1,5 +1,11 @@ require 'rails_helper' RSpec.describe Zone, type: :model do - pending "add some examples to (or delete) #{__FILE__}" + # Association test + # ensure Todo model has a 1:m relationship with the Item model + it { should have_many(:seat_tables).dependent(:destroy) } + # Validation tests + # ensure columns title and created_by are present before saving + it { should validate_presence_of(:name) } + it { should validate_presence_of(:created_by) } end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index d791e5d4..05871e71 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -22,7 +22,7 @@ require 'database_cleaner' # directory. Alternatively, in the individual `*_spec.rb` files, manually # require only the support files necessary. # -# Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } +Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } # Checks for pending migration and applies them before tests are run. # If you are not using ActiveRecord, you can remove this line. @@ -43,6 +43,9 @@ RSpec.configure do |config| # add `FactoryGirl` methods config.include FactoryGirl::Syntax::Methods + config.include RequestSpecHelper, type: :request + + # start by truncating all the tables but then use the faster transaction strategy the rest of the time. config.before(:suite) do DatabaseCleaner.clean_with(:truncation) diff --git a/spec/requests/api/restaurant/seat_tables.rb b/spec/requests/api/restaurant/seat_tables.rb new file mode 100644 index 00000000..e69de29b diff --git a/spec/requests/api/restaurant/zones.rb b/spec/requests/api/restaurant/zones.rb new file mode 100644 index 00000000..f34b1435 --- /dev/null +++ b/spec/requests/api/restaurant/zones.rb @@ -0,0 +1,25 @@ +require 'rails_helper' + +RSpec.describe 'Zone API', type: :request do + # initialize test data + let!(:zone) { create_list(:zone, 10) } + let(:zone_id) { zones.first.id } + + # Test suite for GET /todos + describe 'GET /api/restaurant/zones' do + # make HTTP get request before each example + before { get '/api/restaurant/zones' } + + it 'returns zones' do + # Note `json` is a custom helper to parse JSON responses + expect(json).not_to be_empty + expect(json.size).to eq(10) + end + + it 'returns status code 200' do + expect(response).to have_http_status(200) + end + end + + +end diff --git a/spec/support/request_spec_helper.rb b/spec/support/request_spec_helper.rb new file mode 100644 index 00000000..d7dfbc21 --- /dev/null +++ b/spec/support/request_spec_helper.rb @@ -0,0 +1,7 @@ +# spec/support/request_spec_helper +module RequestSpecHelper + # Parse JSON response to ruby hash + def json + JSON.parse(response.body) + end +end