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
Binary file added .docker/.DS_Store
Binary file not shown.
661 changes: 661 additions & 0 deletions .docker/postgresql.conf

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
node_modules
npm-debug.log
Dockerfile*
docker-compose*
.dockerignore
.git
.gitignore
.env
*/bin
*/obj
README.md
LICENSE
.vscode
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@

# Ignore master key for decrypting credentials and more.
/config/master.key
/.docker/*.tgz
46 changes: 46 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
FROM alpine:latest

LABEL Alec Pervushin <alec.for.public@gmail.com>

ENV RUBY_INSTALL_VERSION "0.7.0"
ENV RUBY_INSTALL_URL "https://github.com/postmodern/ruby-install/archive/v${RUBY_INSTALL_VERSION}.tar.gz"
ENV CHRUBY_VERSION '0.3.9'
ENV CHRUBY_URL "https://github.com/postmodern/chruby/archive/v${CHRUBY_VERSION}.tar.gz"

RUN apk update && \
apk upgrade && \
apk add --no-cache gnupg curl bash procps musl zlib openssl \
patch make gcc g++ gnupg musl-dev linux-headers zlib-dev openssl-dev \
postgresql-dev tzdata ruby readline-dev

SHELL ["/bin/bash", "-lc"]

WORKDIR /tmp

RUN curl -L -o ruby-install.tar.gz ${RUBY_INSTALL_URL} && \
tar -xzvf ruby-install.tar.gz && \
cd ruby-install-${RUBY_INSTALL_VERSION}/ && make install

ADD .docker/current.tgz /opt/

COPY bin/install-railsexpress* /tmp/
COPY .ruby-version* /root/
COPY Gemfile* /tmp/

RUN echo 'export RUBY_VERSION=$([ -z "$RUBY_VERSION" ] && cat ~/.ruby-version || "$RUBY_VERSION")' >> /etc/profile

RUN ./install-railsexpress railsexpress $RUBY_VERSION \
--jobs=2 --cleanup --no-reinstall

RUN curl -L -o chruby.tar.gz ${CHRUBY_URL} && \
tar -xzvf chruby.tar.gz && \
cd chruby-${CHRUBY_VERSION}/ && make install && \
echo "source /usr/local/share/chruby/chruby.sh" >> /etc/profile && \
echo 'chruby $RUBY_VERSION' >> /etc/profile

RUN gem install bundler rb-readline && bundle check || bundle install

RUN apk del gnupg musl-dev linux-headers ruby && \
rm -rf /tmp/* /var/cache/apk/*

COPY docker-entrypoint.sh* /
11 changes: 11 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,21 @@ group :development, :test do
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

gem 'strong_migrations', '~> 0.3.1'
Copy link
Owner

Choose a reason for hiding this comment

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

👍

gem 'oj', '~> 3.7.11'
gem 'activerecord-import', '~> 1.0.1'

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 'rack-mini-profiler', '~> 1.0.2', require: false
gem 'memory_profiler', '~> 0.9.13', require: false
gem 'flamegraph', '~> 0.9.5', require: false
gem 'stackprof', '~> 0.2.12', require: false
gem 'ruby-prof', '~> 0.17.0', require: false
gem 'benchmark-ips', '~> 2.7.2', require: false
gem 'bullet', '~> 5.9.0'
end

group :test do
Expand Down
26 changes: 26 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,15 +45,20 @@ GEM
minitest (~> 5.1)
tzinfo (~> 1.1)
arel (9.0.0)
benchmark-ips (2.7.2)
bindex (0.6.0)
bootsnap (1.4.2)
msgpack (~> 1.0)
builder (3.2.3)
bullet (5.9.0)
activesupport (>= 3.0.0)
uniform_notifier (~> 1.11)
byebug (11.0.1)
concurrent-ruby (1.1.5)
crass (1.0.4)
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 @@ -67,6 +74,7 @@ GEM
mini_mime (>= 0.1.1)
marcel (0.3.3)
mimemagic (~> 0.3.2)
memory_profiler (0.9.13)
method_source (0.9.2)
mimemagic (0.3.3)
mini_mime (1.0.1)
Expand All @@ -76,9 +84,12 @@ GEM
nio4r (2.3.1)
nokogiri (1.10.2)
mini_portile2 (~> 2.4.0)
oj (3.7.11)
pg (1.1.4)
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 +120,7 @@ GEM
rb-fsevent (0.10.3)
rb-inotify (0.10.0)
ffi (~> 1.0)
ruby-prof (0.17.0)
ruby_dep (1.5.0)
sprockets (3.7.2)
concurrent-ruby (~> 1.0)
Expand All @@ -117,10 +129,14 @@ GEM
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
stackprof (0.2.12)
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)
uniform_notifier (1.12.1)
web-console (3.7.0)
actionview (>= 5.0)
activemodel (>= 5.0)
Expand All @@ -134,12 +150,22 @@ PLATFORMS
ruby

DEPENDENCIES
activerecord-import (~> 1.0.1)
benchmark-ips (~> 2.7.2)
bootsnap (>= 1.1.0)
bullet (~> 5.9.0)
byebug
flamegraph (~> 0.9.5)
listen (>= 3.0.5, < 3.2)
memory_profiler (~> 0.9.13)
oj (~> 3.7.11)
pg (>= 0.18, < 2.0)
puma (~> 3.11)
rack-mini-profiler (~> 1.0.2)
rails (~> 5.2.3)
ruby-prof (~> 0.17.0)
stackprof (~> 0.2.12)
strong_migrations (~> 0.3.1)
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.
joins(bus: :services).
Copy link
Owner

Choose a reason for hiding this comment

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

Смущает использование joins
joins лучше использовать для фильтрации, а тут лучше бы preload или eager_load

where(from: @from, to: @to).
order(:start_time)
end
end
2 changes: 0 additions & 2 deletions app/jobs/application_job.rb

This file was deleted.

3 changes: 2 additions & 1 deletion app/models/bus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class Bus < ApplicationRecord
].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
3 changes: 2 additions & 1 deletion app/models/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ 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
97 changes: 97 additions & 0 deletions app/services/entities_processing.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
class EntitiesProcessing
Copy link
Owner

Choose a reason for hiding this comment

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

👍 плюсик за сервис

class << self
def call(file_name:, with_gc: true)
GC.disabled unless with_gc
Copy link
Owner

Choose a reason for hiding this comment

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

Опечатка GC.disable?


json = Oj.load(File.read file_name)

ActiveRecord::Base.transaction do
City.delete_all
Bus.delete_all
Service.delete_all
Trip.delete_all
BusesService.delete_all

create_cities(json)
create_services
create_buses(json)
create_trips(json)
end

clear_entities
end

private

def create_buses(json)
@buses =
json.each_with_object({}) do |trip, memo|
bus_number = trip['bus']['number']
next if memo.has_key?(bus_number)

memo[bus_number] = Bus.new(
number: bus_number,
model: trip['bus']['model'],
services: @services.
values_at(*trip['bus']['services'])
)
end

Bus.import(@buses.values, recursive: true, raise_error: true)
end

def create_cities(json)
@cities =
json.each_with_object({}) do |trip, memo|
from = trip['from']
to = trip['to']

next if memo.has_key?(from) && memo.has_key?(to)
name = memo.has_key?(from) ? to : from

memo[name] = City.new(name: name)
end

City.import(@cities.values, recursive: true, raise_error: true)
end

def create_services
@services =
Service::SERVICES.sort.each_with_object({}) do |name, memo|
memo[name] = Service.new(name: name)
end

Service.import(@services.values, recursive: true, raise_error: true)
end

def create_trips(json)
@trips =
json.each_with_object({}) do |trip, memo|
from = @cities[trip['from']]
to = @cities[trip['to']]
bus = @buses[trip['bus']['number']]
start_time = trip['start_time']


memo[start_time] =
Trip.new(
from: from,
to: to,
bus: bus,
start_time: start_time,
duration_minutes: trip['duration_minutes'],
price_cents: trip['price_cents'],
)
end

Trip.import(@trips.values, recursive: true, raise_error: true)
end

def clear_entities
@trips.clear
@buses.clear
@cities.clear
@services.clear
end
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.

26 changes: 21 additions & 5 deletions app/views/trips/_trip.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
<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>
<% cache [trip, trip.bus] do %>
<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.exists? %>
<li>Сервисы в автобусе:</li>
<ul>
<% trip.bus.services.each do |service| %>
<% cache service do %>
<li><%= "#{service.name}" %></li>
<% end %>
<% end %>
</ul>
<% end %>
</ul>
====================================================
<% end %>
10 changes: 1 addition & 9 deletions app/views/trips/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,4 @@
<%= "В расписании #{@trips.count} рейсов" %>
</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 @trips, cached: true%>
Loading