Skip to content
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
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
7 changes: 7 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,26 @@ gem 'rails', '~> 5.2.3'
gem 'pg', '>= 0.18', '< 2.0'
gem 'puma', '~> 3.11'
gem 'bootsnap', '>= 1.1.0', require: false
gem 'activerecord-import'
gem "rack-mini-profiler"

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
gem 'rspec-rails', '~> 3.8'
gem 'pry'
end

group :development do
# Access an interactive console on exception pages or by calling 'console' anywhere in the code.
gem 'web-console', '>= 3.3.0'
gem 'listen', '>= 3.0.5', '< 3.2'
gem 'stackprof'
gem "flamegraph"
end

group :test do
gem 'rspec-sqlimit'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
Expand Down
42 changes: 42 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ GEM
activemodel (= 5.2.3)
activesupport (= 5.2.3)
arel (>= 9.0)
activerecord-import (1.0.1)
activerecord (>= 3.2)
activestorage (5.2.3)
actionpack (= 5.2.3)
activerecord (= 5.2.3)
Expand All @@ -48,10 +50,13 @@ GEM
msgpack (~> 1.0)
builder (3.2.3)
byebug (11.0.1)
coderay (1.1.2)
concurrent-ruby (1.1.5)
crass (1.0.4)
diff-lcs (1.3)
erubi (1.8.0)
ffi (1.10.0)
flamegraph (0.9.5)
globalid (0.4.2)
activesupport (>= 4.2.0)
i18n (1.6.0)
Expand All @@ -77,8 +82,13 @@ GEM
nokogiri (1.10.2)
mini_portile2 (~> 2.4.0)
pg (1.1.4)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
puma (3.12.1)
rack (2.0.6)
rack-mini-profiler (1.0.2)
rack (>= 1.2.0)
rack-test (1.1.0)
rack (>= 1.0, < 3)
rails (5.2.3)
Expand Down Expand Up @@ -109,6 +119,30 @@ GEM
rb-fsevent (0.10.3)
rb-inotify (0.10.0)
ffi (~> 1.0)
rspec (3.8.0)
rspec-core (~> 3.8.0)
rspec-expectations (~> 3.8.0)
rspec-mocks (~> 3.8.0)
rspec-core (3.8.0)
rspec-support (~> 3.8.0)
rspec-expectations (3.8.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.8.0)
rspec-mocks (3.8.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.8.0)
rspec-rails (3.8.2)
actionpack (>= 3.0)
activesupport (>= 3.0)
railties (>= 3.0)
rspec-core (~> 3.8.0)
rspec-expectations (~> 3.8.0)
rspec-mocks (~> 3.8.0)
rspec-support (~> 3.8.0)
rspec-sqlimit (0.0.2)
rails (> 4.0, < 6.0)
rspec (~> 3.0)
rspec-support (3.8.0)
ruby_dep (1.5.0)
sprockets (3.7.2)
concurrent-ruby (~> 1.0)
Expand All @@ -117,6 +151,7 @@ GEM
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
stackprof (0.2.12)
thor (0.20.3)
thread_safe (0.3.6)
tzinfo (1.2.5)
Expand All @@ -134,12 +169,19 @@ PLATFORMS
ruby

DEPENDENCIES
activerecord-import
bootsnap (>= 1.1.0)
byebug
flamegraph
listen (>= 3.0.5, < 3.2)
pg (>= 0.18, < 2.0)
pry
puma (~> 3.11)
rack-mini-profiler
rails (~> 5.2.3)
rspec-rails (~> 3.8)
rspec-sqlimit
stackprof
tzinfo-data
web-console (>= 3.3.0)

Expand Down
5 changes: 4 additions & 1 deletion app/controllers/trips_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ class TripsController < ApplicationController
def index
@from = City.find_by_name!(params[:from])
@to = City.find_by_name!(params[:to])
@trips = Trip.where(from: @from, to: @to).order(:start_time)
@trips = Trip.preload(:bus, :services)
.where(from: @from, to: @to)
.select_finish_time
.order(:start_time)#.load
end
end
2 changes: 1 addition & 1 deletion app/models/bus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ class Bus < ApplicationRecord
has_many :trips
has_and_belongs_to_many :services, join_table: :buses_services

validates :number, presence: true, uniqueness: true
validates :number, presence: true
validates :model, inclusion: { in: MODELS }
end
2 changes: 1 addition & 1 deletion app/models/city.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class City < ApplicationRecord
validates :name, presence: true, uniqueness: true
validates :name, presence: true
validate :name_has_no_spaces

def name_has_no_spaces
Expand Down
11 changes: 11 additions & 0 deletions app/models/trip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class Trip < ApplicationRecord
belongs_to :from, class_name: 'City'
belongs_to :to, class_name: 'City'
belongs_to :bus
has_many :services, through: :bus

validates :from, presence: true
validates :to, presence: true
Expand All @@ -15,6 +16,16 @@ class Trip < ApplicationRecord
validates :price_cents, presence: true
validates :price_cents, numericality: { greater_than: 0 }

scope :select_finish_time, -> {
select(<<-SQL.squish)
*,
to_char(
start_time::time + (duration_minutes || ' minutes')::interval,
'HH24:MI'
) AS finish_time
SQL
}

def to_h
{
from: from.name,
Expand Down
1 change: 0 additions & 1 deletion app/views/trips/_delimiter.html.erb

This file was deleted.

1 change: 0 additions & 1 deletion app/views/trips/_service.html.erb

This file was deleted.

6 changes: 0 additions & 6 deletions app/views/trips/_services.html.erb

This file was deleted.

5 changes: 0 additions & 5 deletions app/views/trips/_trip.html.erb

This file was deleted.

23 changes: 17 additions & 6 deletions app/views/trips/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,26 @@
<%= "Автобусы #{@from.name} – #{@to.name}" %>
</h1>
<h2>
<%= "В расписании #{@trips.count} рейсов" %>
<%= "В расписании #{@trips.count(:all)} рейсов" %>
</h2>

<% @trips.each do |trip| %>
<% @trips.find_each.with_index do |trip, index| %>
<% unless index.zero? %>
====================================================
<% end %>
<ul>
<%= render "trip", trip: trip %>
<% if trip.bus.services.present? %>
<%= render "services", services: trip.bus.services %>
<li><%= "Отправление: #{trip.start_time}" %></li>
<li><%= "Прибытие: #{trip.finish_time}" %></li>
<li><%= "В пути: #{trip.duration_minutes / 60}ч. #{trip.duration_minutes % 60}мин." %></li>
<li><%= "Цена: #{trip.price_cents / 100}р. #{trip.price_cents % 100}коп." %></li>
<li><%= "Автобус: #{trip.bus.model} №#{trip.bus.number}" %></li>
<% if trip.services.any? %>
<li>Сервисы в автобусе:</li>
<% end %>
<ul>
<% trip.services.each do |service| %>
<li><%= "#{service.name}" %></li>
<% end %>
</ul>
</ul>
<%= render "delimiter" %>
<% end %>
5 changes: 5 additions & 0 deletions db/migrate/20190505081329_add_uniq_index_to_buses.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddUniqIndexToBuses < ActiveRecord::Migration[5.2]
def change
add_index :buses, :number, unique: true
end
end
5 changes: 5 additions & 0 deletions db/migrate/20190505083024_add_uniq_index_to_cities.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddUniqIndexToCities < ActiveRecord::Migration[5.2]
def change
add_index :cities, :name, unique: true
end
end
5 changes: 5 additions & 0 deletions db/migrate/20190505083758_add_uniq_index_to_services.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddUniqIndexToServices < ActiveRecord::Migration[5.2]
def change
add_index :services, :name, unique: true
end
end
5 changes: 5 additions & 0 deletions db/migrate/20190509064555_add_from_to_index_to_trips.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddFromToIndexToTrips < ActiveRecord::Migration[5.2]
def change
add_index :trips, [:from_id, :to_id, :start_time], order: { start_time: :asc }
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddBusIdIndexToBusesServices < ActiveRecord::Migration[5.2]
def change
add_index :buses_services, :bus_id
end
end
7 changes: 6 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,31 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_03_30_193044) do
ActiveRecord::Schema.define(version: 2019_05_09_074749) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "buses", force: :cascade do |t|
t.string "number"
t.string "model"
t.index ["number"], name: "index_buses_on_number", unique: true
end

create_table "buses_services", force: :cascade do |t|
t.integer "bus_id"
t.integer "service_id"
t.index ["bus_id"], name: "index_buses_services_on_bus_id"
end

create_table "cities", force: :cascade do |t|
t.string "name"
t.index ["name"], name: "index_cities_on_name", unique: true
end

create_table "services", force: :cascade do |t|
t.string "name"
t.index ["name"], name: "index_services_on_name", unique: true
end

create_table "trips", force: :cascade do |t|
Expand All @@ -40,6 +44,7 @@
t.integer "duration_minutes"
t.integer "price_cents"
t.integer "bus_id"
t.index ["from_id", "to_id", "start_time"], name: "index_trips_on_from_id_and_to_id_and_start_time"
end

end
Loading