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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@

# Ignore master key for decrypting credentials and more.
/config/master.key

.env
26 changes: 26 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
AllCops:
TargetRubyVersion: 2.6.1
Exclude:
- "config/**/*"
- "db/**/*"
- "bin/**"
- "*Gemfile*"
- "tmp/**/*"

Metrics/AbcSize:
Enabled: false

Metrics/LineLength:
Max: 120

Metrics/MethodLength:
Max: 15

Style/ClassAndModuleChildren:
Enabled: false

Style/Documentation:
Enabled: false

Style/FrozenStringLiteralComment:
Enabled: false
9 changes: 9 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,18 @@ gem 'pg', '>= 0.18', '< 2.0'
gem 'puma', '~> 3.11'
gem 'bootsnap', '>= 1.1.0', require: false

gem 'activerecord-import'
gem 'dotenv-rails'
gem 'newrelic_rpm'
gem 'oj'
gem 'pghero'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

gem 'strong_migrations'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍



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 'rubocop'
end

group :development do
Expand Down
36 changes: 36 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 @@ -43,19 +45,25 @@ GEM
minitest (~> 5.1)
tzinfo (~> 1.1)
arel (9.0.0)
ast (2.4.0)
bindex (0.6.0)
bootsnap (1.4.2)
msgpack (~> 1.0)
builder (3.2.3)
byebug (11.0.1)
concurrent-ruby (1.1.5)
crass (1.0.4)
dotenv (2.7.2)
dotenv-rails (2.7.2)
dotenv (= 2.7.2)
railties (>= 3.2, < 6.1)
erubi (1.8.0)
ffi (1.10.0)
globalid (0.4.2)
activesupport (>= 4.2.0)
i18n (1.6.0)
concurrent-ruby (~> 1.0)
jaro_winkler (1.5.2)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand All @@ -73,10 +81,18 @@ GEM
mini_portile2 (2.4.0)
minitest (5.11.3)
msgpack (1.2.9)
newrelic_rpm (6.2.0.354)
nio4r (2.3.1)
nokogiri (1.10.2)
mini_portile2 (~> 2.4.0)
oj (3.7.11)
parallel (1.14.0)
parser (2.6.0.0)
ast (~> 2.4.0)
pg (1.1.4)
pghero (2.2.0)
activerecord
psych (3.1.0)
puma (3.12.1)
rack (2.0.6)
rack-test (1.1.0)
Expand Down Expand Up @@ -105,10 +121,20 @@ GEM
method_source
rake (>= 0.8.7)
thor (>= 0.19.0, < 2.0)
rainbow (3.0.0)
rake (12.3.2)
rb-fsevent (0.10.3)
rb-inotify (0.10.0)
ffi (~> 1.0)
rubocop (0.66.0)
jaro_winkler (~> 1.5.1)
parallel (~> 1.10)
parser (>= 2.5, != 2.5.1.1)
psych (>= 3.1.0)
rainbow (>= 2.2.2, < 4.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 1.6)
ruby-progressbar (1.10.0)
ruby_dep (1.5.0)
sprockets (3.7.2)
concurrent-ruby (~> 1.0)
Expand All @@ -117,10 +143,13 @@ GEM
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
strong_migrations (0.3.1)
activerecord (>= 3.2.0)
thor (0.20.3)
thread_safe (0.3.6)
tzinfo (1.2.5)
thread_safe (~> 0.1)
unicode-display_width (1.5.0)
web-console (3.7.0)
actionview (>= 5.0)
activemodel (>= 5.0)
Expand All @@ -134,12 +163,19 @@ PLATFORMS
ruby

DEPENDENCIES
activerecord-import
bootsnap (>= 1.1.0)
byebug
dotenv-rails
listen (>= 3.0.5, < 3.2)
newrelic_rpm
oj
pg (>= 0.18, < 2.0)
pghero
puma (~> 3.11)
rails (~> 5.2.3)
rubocop
strong_migrations
tzinfo-data
web-console (>= 3.3.0)

Expand Down
4 changes: 3 additions & 1 deletion app/controllers/trips_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ 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.eager_load(bus: :services)
.where(from: @from, to: @to)
.order(:start_time)
end
end
3 changes: 3 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
module ApplicationHelper
def trips_delimiter
'===================================================='.freeze
end
end
25 changes: 13 additions & 12 deletions app/models/bus.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
class Bus < ApplicationRecord
MODELS = [
'Икарус',
'Мерседес',
'Сканиа',
'Буханка',
'УАЗ',
'Спринтер',
'ГАЗ',
'ПАЗ',
'Вольво',
'Газель',
MODELS = %w[
Икарус
Мерседес
Сканиа
Буханка
УАЗ
Спринтер
ГАЗ
ПАЗ
Вольво
Газель
].freeze

has_many :trips
has_and_belongs_to_many :services, join_table: :buses_services
has_many :buses_services
has_many :services, through: :buses_services

validates :number, presence: true, uniqueness: true
validates :model, inclusion: { in: MODELS }
Expand Down
4 changes: 4 additions & 0 deletions app/models/buses_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class BusesService < ApplicationRecord
belongs_to :bus
belongs_to :service
end
2 changes: 1 addition & 1 deletion app/models/city.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ class City < ApplicationRecord
validate :name_has_no_spaces

def name_has_no_spaces
errors.add(:name, "has spaces") if name.include?(' ')
errors.add(:name, 'has spaces') if name.include?(' ')
end
end
5 changes: 3 additions & 2 deletions app/models/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ class Service < ApplicationRecord
'Телевизор общий',
'Телевизор индивидуальный',
'Стюардесса',
'Можно не печатать билет',
'Можно не печатать билет'
].freeze

has_and_belongs_to_many :buses, join_table: :buses_services
has_many :buses_services
has_many :buses, through: :buses_services

validates :name, presence: true
validates :name, inclusion: { in: SERVICES }
Expand Down
6 changes: 3 additions & 3 deletions app/models/trip.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Trip < ApplicationRecord
HHMM_REGEXP = /([0-1][0-9]|[2][0-3]):[0-5][0-9]/
HHMM_REGEXP = /([0-1][0-9]|[2][0-3]):[0-5][0-9]/.freeze

belongs_to :from, class_name: 'City'
belongs_to :to, class_name: 'City'
Expand All @@ -25,8 +25,8 @@ def to_h
bus: {
number: bus.number,
model: bus.model,
services: bus.services.map(&:name),
},
services: bus.services.map(&:name)
}
}
end
end
80 changes: 80 additions & 0 deletions app/services/db_importer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
require 'oj'

class DbImporter
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

def initialize
@cities = {}
@services = {}
@buses = {}
end

def call(source:)
json = Oj.load_file(source)

ActiveRecord::Base.transaction do
clear_db!
create_from_json!(json)
end
end

private

attr_reader :cities, :services, :buses

def clear_db!
City.delete_all
Bus.delete_all
Service.delete_all
Trip.delete_all
ActiveRecord::Base.connection.execute('delete from buses_services;')
end

def create_from_json!(json)
import_cities_and_services(json)
import_buses(json)
import_trips(json)
end

def import_cities_and_services(json)
json.each do |trip|
cities[trip['from']] ||= City.new(name: trip['from'])
cities[trip['to']] ||= City.new(name: trip['to'])

trip['bus']['services'].each do |service|
services[service] ||= Service.new(name: service)
end
end

City.import %i[name], cities.values, syncronize: true, raise_error: true
Service.import %i[name], services.values, syncronize: true, raise_error: true
end

def import_buses(json)
json.each do |trip|
bus = buses[trip['bus']['number']] || Bus.new(number: trip['bus']['number'])
bus.model = trip['bus']['model']
bus.services = services.values_at(*trip['bus']['services'])
buses[trip['bus']['number']] = bus
end

Bus.import buses.values, recursive: true, syncronize: true, raise_error: true
end

def import_trips(json)
trips = []
json.each do |trip|
from = cities[trip['from']]
to = cities[trip['to']]

trips << Trip.new(
from: from,
to: to,
bus: buses[trip['bus']['number']],
start_time: trip['start_time'],
duration_minutes: trip['duration_minutes'],
price_cents: trip['price_cents']
)
end

Trip.import trips, raise_error: true
end
end
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.

21 changes: 16 additions & 5 deletions app/views/trips/_trip.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
<li><%= "Отправление: #{trip.start_time}" %></li>
<li><%= "Прибытие: #{(Time.parse(trip.start_time) + trip.duration_minutes.minutes).strftime('%H:%M')}" %></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>
<ul>
<li><%= "Отправление: #{trip.start_time}" %></li>
<li><%= "Прибытие: #{(Time.parse(trip.start_time) + trip.duration_minutes.minutes).strftime('%H:%M')}" %></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.bus.services.present? %>
<li>Сервисы в автобусе:</li>
<ul>
<% trip.bus.services.each do |service| %>
<li><%= "#{service.name}" %></li>
<% end %>
</ul>
<% end %>
</ul>
<%= trips_delimiter %>
12 changes: 2 additions & 10 deletions app/views/trips/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,7 @@
<%= "Автобусы #{@from.name} – #{@to.name}" %>
</h1>
<h2>
<%= "В расписании #{@trips.count} рейсов" %>
<%= "В расписании #{@trips.length} рейсов" %>
</h2>

<% @trips.each do |trip| %>
<ul>
<%= render "trip", trip: trip %>
<% if trip.bus.services.present? %>
<%= render "services", services: trip.bus.services %>
<% end %>
</ul>
<%= render "delimiter" %>
<% end %>
<%= render partial: "trip", collection: @trips %>
Loading