From e2321a2d49c6739dbaea283c264e12b5fd2f123a Mon Sep 17 00:00:00 2001 From: mb00t Date: Mon, 14 Mar 2016 02:01:01 +0300 Subject: [PATCH] 14 full --- railway/app/assets/javascripts/tickets.js | 2 + railway/app/assets/stylesheets/tickets.scss | 3 + railway/app/controllers/tickets_controller.rb | 70 +++++++++++++++++++ railway/app/helpers/tickets_helper.rb | 2 + railway/app/models/railway_station.rb | 1 + railway/app/models/ticket.rb | 6 +- railway/app/models/user.rb | 2 +- railway/app/views/tickets/_form.html.slim | 22 ++++++ railway/app/views/tickets/_ticket.html.slim | 5 ++ railway/app/views/tickets/edit.html.slim | 7 ++ railway/app/views/tickets/index.html.slim | 15 ++++ railway/app/views/tickets/new.html.slim | 5 ++ railway/app/views/tickets/show.html.slim | 27 +++++++ railway/app/views/welcome/index.html.slim | 2 + railway/config/routes.rb | 1 + .../20160313164948_add_tikets_field.rb | 8 +++ ...0160313165722_remove_user_id_to_tickets.rb | 5 ++ .../20160313170114_add_route_id_to_tickets.rb | 7 ++ ...160313170445_remove_train_id_to_tickets.rb | 5 ++ railway/db/schema.rb | 13 ++-- .../controllers/tickets_controller_test.rb | 7 ++ 21 files changed, 207 insertions(+), 8 deletions(-) create mode 100644 railway/app/assets/javascripts/tickets.js create mode 100644 railway/app/assets/stylesheets/tickets.scss create mode 100644 railway/app/controllers/tickets_controller.rb create mode 100644 railway/app/helpers/tickets_helper.rb create mode 100644 railway/app/views/tickets/_form.html.slim create mode 100644 railway/app/views/tickets/_ticket.html.slim create mode 100644 railway/app/views/tickets/edit.html.slim create mode 100644 railway/app/views/tickets/index.html.slim create mode 100644 railway/app/views/tickets/new.html.slim create mode 100644 railway/app/views/tickets/show.html.slim create mode 100644 railway/db/migrate/20160313164948_add_tikets_field.rb create mode 100644 railway/db/migrate/20160313165722_remove_user_id_to_tickets.rb create mode 100644 railway/db/migrate/20160313170114_add_route_id_to_tickets.rb create mode 100644 railway/db/migrate/20160313170445_remove_train_id_to_tickets.rb create mode 100644 railway/test/controllers/tickets_controller_test.rb diff --git a/railway/app/assets/javascripts/tickets.js b/railway/app/assets/javascripts/tickets.js new file mode 100644 index 0000000..dee720f --- /dev/null +++ b/railway/app/assets/javascripts/tickets.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/railway/app/assets/stylesheets/tickets.scss b/railway/app/assets/stylesheets/tickets.scss new file mode 100644 index 0000000..92b729b --- /dev/null +++ b/railway/app/assets/stylesheets/tickets.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Tickets controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/railway/app/controllers/tickets_controller.rb b/railway/app/controllers/tickets_controller.rb new file mode 100644 index 0000000..7fe8bc1 --- /dev/null +++ b/railway/app/controllers/tickets_controller.rb @@ -0,0 +1,70 @@ +class TicketsController < ApplicationController + before_action :set_ticket, only: [:show, :edit, :update, :destroy] + + # GET /tickets + # GET /tickets.json + def index + @tickets = Ticket.all + end + + # GET /tickets/1 + # GET /tickets/1.json + def show + end + + # GET /tickets/new + def new + @ticket = Ticket.new + end + + # GET /tickets/1/edit + def edit + end + + # POST /tickets + # POST /tickets.json + def create + @ticket = Ticket.new(ticket_params) + + respond_to do |format| + if @ticket.save + format.html { redirect_to @ticket, notice: 'Ticket was successfully created.' } + format.json { render :show, status: :created, location: @ticket } + else + format.html { render :new } + format.json { render json: @ticket.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /tickets/1 + # PATCH/PUT /tickets/1.json + def update + respond_to do |format| + if @ticket.update(ticket_params) + format.html { redirect_to @ticket, notice: 'Ticket was successfully updated.' } + else + format.html { render :edit } + end + end + end + + # DELETE /tickets/1 + # DELETE /tickets/1.json + + def destroy + @ticket.destroy + redirect_to tickets_path + end + + private + # Use callbacks to share common setup or consticketts between actions. + def set_ticket + @ticket = Ticket.find(params[:id]) + end + + # Never trust parameters from the scary internet, only allow the white list through. + def ticket_params + params.require(:ticket).permit(:f_name, :l_name, :route_id, :f_station_id, :l_station_id) + end +end diff --git a/railway/app/helpers/tickets_helper.rb b/railway/app/helpers/tickets_helper.rb new file mode 100644 index 0000000..4722254 --- /dev/null +++ b/railway/app/helpers/tickets_helper.rb @@ -0,0 +1,2 @@ +module TicketsHelper +end diff --git a/railway/app/models/railway_station.rb b/railway/app/models/railway_station.rb index 1587ee5..4df5803 100644 --- a/railway/app/models/railway_station.rb +++ b/railway/app/models/railway_station.rb @@ -4,5 +4,6 @@ class RailwayStation < ActiveRecord::Base has_many :trains has_many :railway_stations_routes has_many :routes, through: :railway_stations_routes + has_many :tickets end diff --git a/railway/app/models/ticket.rb b/railway/app/models/ticket.rb index a7ebfcf..cd33535 100644 --- a/railway/app/models/ticket.rb +++ b/railway/app/models/ticket.rb @@ -1,4 +1,6 @@ class Ticket < ActiveRecord::Base - belongs_to :train - belongs_to :user + belongs_to :route + belongs_to :f_station, class_name: 'RailwayStation', foreign_key: :f_station_id + belongs_to :l_station, class_name: 'RailwayStation', foreign_key: :l_station_id + #belongs_to :user end diff --git a/railway/app/models/user.rb b/railway/app/models/user.rb index c11d76e..009ac6e 100644 --- a/railway/app/models/user.rb +++ b/railway/app/models/user.rb @@ -1,3 +1,3 @@ class User < ActiveRecord::Base - has_many :tickets + #has_many :tickets end diff --git a/railway/app/views/tickets/_form.html.slim b/railway/app/views/tickets/_form.html.slim new file mode 100644 index 0000000..2b659eb --- /dev/null +++ b/railway/app/views/tickets/_form.html.slim @@ -0,0 +1,22 @@ += form_for(@ticket) do |f| + = render 'common/errors', resource: @ticket + + .field + = f.label :f_name + = f.text_field :f_name + .field + = f.label :l_name + = f.text_field :l_name + .field + = f.label :route, "Route" + = f.collection_select :route_id, Route.all, :id, :title + + .field + = f.label :f_station, "first station" + = f.collection_select :f_station_id, RailwayStation.all, :id, :title + .field + = f.label :l_station, "last station" + = f.collection_select :l_station_id, RailwayStation.all, :id, :title + + .actions + = f.submit "#{@ticket.new_record? ? 'Create' : 'Change'}" diff --git a/railway/app/views/tickets/_ticket.html.slim b/railway/app/views/tickets/_ticket.html.slim new file mode 100644 index 0000000..ee2fb9a --- /dev/null +++ b/railway/app/views/tickets/_ticket.html.slim @@ -0,0 +1,5 @@ +tr + td = "#{ticket.f_name} #{ticket.l_name} " + td = link_to 'Show', ticket + td = link_to 'Edit', edit_ticket_path(ticket) + td = link_to 'Destroy', ticket, data: { :confirm => 'Are you sure?' }, :method => :delete \ No newline at end of file diff --git a/railway/app/views/tickets/edit.html.slim b/railway/app/views/tickets/edit.html.slim new file mode 100644 index 0000000..fa61957 --- /dev/null +++ b/railway/app/views/tickets/edit.html.slim @@ -0,0 +1,7 @@ +h1 Editing Train + +== render 'form' + += link_to 'Show', @ticket +'| += link_to 'Back', tickets_path diff --git a/railway/app/views/tickets/index.html.slim b/railway/app/views/tickets/index.html.slim new file mode 100644 index 0000000..ead71a5 --- /dev/null +++ b/railway/app/views/tickets/index.html.slim @@ -0,0 +1,15 @@ +p#notice = notice + +h1 Listing Tickets + +table + thead + tr + th Title + th colspan="3" + + tbody + = render @tickets +br + += link_to 'New Ticket', new_ticket_path diff --git a/railway/app/views/tickets/new.html.slim b/railway/app/views/tickets/new.html.slim new file mode 100644 index 0000000..c547d11 --- /dev/null +++ b/railway/app/views/tickets/new.html.slim @@ -0,0 +1,5 @@ +h1 New Train + +== render 'form' + += link_to 'Back', tickets_path diff --git a/railway/app/views/tickets/show.html.slim b/railway/app/views/tickets/show.html.slim new file mode 100644 index 0000000..c828861 --- /dev/null +++ b/railway/app/views/tickets/show.html.slim @@ -0,0 +1,27 @@ +p#notice = notice + +p + strong Name: + = @ticket.f_name + = @ticket.l_name + +p + strong Route: + - if @ticket.route.present? + = @ticket.route.title + - else + .not Not point route + +p + strong Stations: + - if @ticket.f_station.present? && @ticket.l_station.present? + = @ticket.f_station.title + '| + = @ticket.l_station.title + - else + .not Not point stations + + += link_to 'Edit', edit_ticket_path(@ticket) +'| += link_to 'Back', tickets_path diff --git a/railway/app/views/welcome/index.html.slim b/railway/app/views/welcome/index.html.slim index 16e1fae..ff518b2 100644 --- a/railway/app/views/welcome/index.html.slim +++ b/railway/app/views/welcome/index.html.slim @@ -7,3 +7,5 @@ h1 Welcome Rails! = link_to 'Route', routes_path '| = link_to 'Wagon', wagons_path +'| += link_to 'Ticket', tickets_path diff --git a/railway/config/routes.rb b/railway/config/routes.rb index 3c4c791..6eedffc 100644 --- a/railway/config/routes.rb +++ b/railway/config/routes.rb @@ -4,6 +4,7 @@ resources :routes resources :wagons resources :utypes + resources :tickets get 'welcome/index' diff --git a/railway/db/migrate/20160313164948_add_tikets_field.rb b/railway/db/migrate/20160313164948_add_tikets_field.rb new file mode 100644 index 0000000..2986965 --- /dev/null +++ b/railway/db/migrate/20160313164948_add_tikets_field.rb @@ -0,0 +1,8 @@ +class AddTiketsField < ActiveRecord::Migration + def change + change_table :tickets do |t| + t.string :f_name + t.string :l_name + end + end +end diff --git a/railway/db/migrate/20160313165722_remove_user_id_to_tickets.rb b/railway/db/migrate/20160313165722_remove_user_id_to_tickets.rb new file mode 100644 index 0000000..93ed087 --- /dev/null +++ b/railway/db/migrate/20160313165722_remove_user_id_to_tickets.rb @@ -0,0 +1,5 @@ +class RemoveUserIdToTickets < ActiveRecord::Migration + def change + remove_belongs_to :tickets, :user + end +end diff --git a/railway/db/migrate/20160313170114_add_route_id_to_tickets.rb b/railway/db/migrate/20160313170114_add_route_id_to_tickets.rb new file mode 100644 index 0000000..d9acdf1 --- /dev/null +++ b/railway/db/migrate/20160313170114_add_route_id_to_tickets.rb @@ -0,0 +1,7 @@ +class AddRouteIdToTickets < ActiveRecord::Migration + def change + add_belongs_to :tickets, :route + add_belongs_to :tickets, :f_station + add_belongs_to :tickets, :l_station + end +end diff --git a/railway/db/migrate/20160313170445_remove_train_id_to_tickets.rb b/railway/db/migrate/20160313170445_remove_train_id_to_tickets.rb new file mode 100644 index 0000000..66b00a9 --- /dev/null +++ b/railway/db/migrate/20160313170445_remove_train_id_to_tickets.rb @@ -0,0 +1,5 @@ +class RemoveTrainIdToTickets < ActiveRecord::Migration + def change + remove_belongs_to :tickets, :train + end +end diff --git a/railway/db/schema.rb b/railway/db/schema.rb index ca01d1d..306c891 100644 --- a/railway/db/schema.rb +++ b/railway/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160311152424) do +ActiveRecord::Schema.define(version: 20160313170445) do create_table "railway_stations", force: :cascade do |t| t.string "title" @@ -31,10 +31,13 @@ end create_table "tickets", force: :cascade do |t| - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.integer "user_id" - t.integer "train_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.string "f_name" + t.string "l_name" + t.integer "route_id" + t.integer "f_station_id" + t.integer "l_station_id" end create_table "trains", force: :cascade do |t| diff --git a/railway/test/controllers/tickets_controller_test.rb b/railway/test/controllers/tickets_controller_test.rb new file mode 100644 index 0000000..f1458fc --- /dev/null +++ b/railway/test/controllers/tickets_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class TicketsControllerTest < ActionController::TestCase + # test "the truth" do + # assert true + # end +end