-
Notifications
You must be signed in to change notification settings - Fork 112
ДЗ №3 #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
ДЗ №3 #48
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| class BusesService < ApplicationRecord | ||
| belongs_to :bus | ||
| belongs_to :service | ||
|
|
||
| validates :bus, presence: true | ||
| validates :service, presence: true | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| class ReloadJson | ||
| def initialize(file_name) | ||
| @file_name = file_name | ||
| end | ||
|
|
||
| def call | ||
| json = JSON.parse(File.read(@file_name)) | ||
| trips = [] | ||
| buses = {} | ||
| services = {} | ||
| cities = {} | ||
| bus_services = {} | ||
|
|
||
| ActiveRecord::Base.transaction do | ||
| City.delete_all | ||
| Bus.delete_all | ||
| Service.delete_all | ||
| Trip.delete_all | ||
| BusesService.delete_all | ||
|
|
||
| json.each do |trip| | ||
| from = find_or_new_city!(cities, trip['from']) | ||
| to = find_or_new_city!(cities, trip['to']) | ||
|
|
||
| bus = buses[trip['bus']['number']] | ||
| unless bus | ||
| bus = Bus.new(number: trip['bus']['number']) | ||
| bus.model = trip['bus']['model'] | ||
| trip['bus']['services'].each do |service| | ||
| s = find_or_new_service!(services, service) | ||
| find_or_new_bus_service!(bus_services, bus, s) | ||
| end | ||
| buses[trip['bus']['number']] = bus | ||
| end | ||
|
|
||
| trips << Trip.new( | ||
| from: from, | ||
| to: to, | ||
| bus: bus, | ||
| start_time: trip['start_time'], | ||
| duration_minutes: trip['duration_minutes'], | ||
| price_cents: trip['price_cents'] | ||
| ) | ||
| end | ||
|
|
||
| City.import! cities.values | ||
| Service.import! services.values | ||
| Bus.import! buses.values | ||
| BusesService.import! bus_services.values.map(&:values).flatten | ||
| Trip.import! trips | ||
| end | ||
| end | ||
|
|
||
| def find_or_new_city!(cities, name) | ||
| found = cities[name] | ||
| unless found | ||
| found = City.new(name: name) | ||
| cities[name] = found | ||
| end | ||
| found | ||
| end | ||
|
|
||
| def find_or_new_service!(services, name) | ||
| found = services[name] | ||
| unless found | ||
| found = Service.new(name: name) | ||
| services[name] = found | ||
| end | ||
| found | ||
| end | ||
|
|
||
| def find_or_new_bus_service!(bus_services, bus, service) | ||
| found = bus_services.dig(bus, service) | ||
| unless found | ||
| found = BusesService.new(bus: bus, service: service) | ||
| bus_services[bus] = {} unless bus_services[bus] | ||
| bus_services[bus][service] = found | ||
| end | ||
| found | ||
| end | ||
| end |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,15 +2,24 @@ | |
| <%= "Автобусы #{@from.name} – #{@to.name}" %> | ||
| </h1> | ||
| <h2> | ||
| <%= "В расписании #{@trips.count} рейсов" %> | ||
| <%= "В расписании #{@trips.load.size} рейсов" %> | ||
| </h2> | ||
|
|
||
| <% @trips.each do |trip| %> | ||
| <ul> | ||
| <%= render "trip", trip: trip %> | ||
| <li><%= "Отправление: #{trip.start_time}" %></li> | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. С инлайном работает быстрее, но с partial может быть более читаемо |
||
| <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? %> | ||
| <%= render "services", services: trip.bus.services %> | ||
| <% end %> | ||
| <li>Сервисы в автобусе:</li> | ||
| <ul> | ||
| <% trip.bus.services.each do |service| %> | ||
| <li><%= "#{service.name}" %></li> | ||
| <% end %> | ||
| </ul> | ||
| <% end %> | ||
| </ul> | ||
| <%= render "delimiter" %> | ||
| ==================================================== | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| <% end %> | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍