Add basic functionality
This commit is contained in:
parent
4def3ac97f
commit
8630218c41
3
Gemfile
3
Gemfile
|
|
@ -48,6 +48,9 @@ gem "bootsnap", require: false
|
|||
# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
|
||||
# gem "image_processing", "~> 1.2"
|
||||
|
||||
gem "slim-rails"
|
||||
gem "active_interaction"
|
||||
|
||||
group :development, :test do
|
||||
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
|
||||
gem "debug", platforms: %i[ mri mingw x64_mingw ]
|
||||
|
|
|
|||
14
Gemfile.lock
14
Gemfile.lock
|
|
@ -46,6 +46,9 @@ GEM
|
|||
erubi (~> 1.4)
|
||||
rails-dom-testing (~> 2.0)
|
||||
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
||||
active_interaction (5.3.0)
|
||||
activemodel (>= 5.2, < 8)
|
||||
activesupport (>= 5.2, < 8)
|
||||
activejob (7.0.8)
|
||||
activesupport (= 7.0.8)
|
||||
globalid (>= 0.3.6)
|
||||
|
|
@ -157,6 +160,13 @@ GEM
|
|||
psych (>= 4.0.0)
|
||||
reline (0.3.8)
|
||||
io-console (~> 0.5)
|
||||
slim (5.1.1)
|
||||
temple (~> 0.10.0)
|
||||
tilt (>= 2.1.0)
|
||||
slim-rails (3.6.2)
|
||||
actionpack (>= 3.1)
|
||||
railties (>= 3.1)
|
||||
slim (>= 3.0, < 6.0, != 5.0.0)
|
||||
sprockets (4.2.1)
|
||||
concurrent-ruby (~> 1.0)
|
||||
rack (>= 2.2.4, < 4)
|
||||
|
|
@ -168,7 +178,9 @@ GEM
|
|||
stimulus-rails (1.2.2)
|
||||
railties (>= 6.0.0)
|
||||
stringio (3.0.8)
|
||||
temple (0.10.2)
|
||||
thor (1.2.2)
|
||||
tilt (2.3.0)
|
||||
timeout (0.4.0)
|
||||
turbo-rails (1.4.0)
|
||||
actionpack (>= 6.0.0)
|
||||
|
|
@ -190,12 +202,14 @@ PLATFORMS
|
|||
x86_64-linux
|
||||
|
||||
DEPENDENCIES
|
||||
active_interaction
|
||||
bootsnap
|
||||
debug
|
||||
importmap-rails
|
||||
jbuilder
|
||||
puma (~> 5.0)
|
||||
rails (~> 7.0.8)
|
||||
slim-rails
|
||||
sprockets-rails
|
||||
sqlite3 (~> 1.4)
|
||||
stimulus-rails
|
||||
|
|
|
|||
|
|
@ -1,2 +1,5 @@
|
|||
class ApplicationController < ActionController::Base
|
||||
def resource_class
|
||||
controller_path.classify.constantize
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -0,0 +1,13 @@
|
|||
class CountriesController < ApplicationController
|
||||
helper_method %i[countries country]
|
||||
|
||||
def countries
|
||||
resource_class.all
|
||||
end
|
||||
|
||||
def country
|
||||
return resource_class.find(params[:id]) if params[:id].is_a?(Integer)
|
||||
return resource_class.find_by(iso2: params[:id]) if params[:id].length == 2
|
||||
resource_class.find_by(iso3: params[:id])
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
class PeopleController < ApplicationController
|
||||
helper_method :person
|
||||
|
||||
def person
|
||||
resource_class.find(params[:id])
|
||||
end
|
||||
end
|
||||
|
|
@ -1,4 +1,24 @@
|
|||
class Admission < ApplicationRecord
|
||||
belongs_to :country
|
||||
belongs_to :person
|
||||
belongs_to :country, inverse_of: :admissions
|
||||
belongs_to :person, inverse_of: :admissions
|
||||
belongs_to :previous, class_name: name, inverse_of: :next, optional: true
|
||||
has_one :next, class_name: name, foreign_key: :previous_id, inverse_of: :previous
|
||||
|
||||
scope :ordered, -> { order(:arrived_on, :previous_id) }
|
||||
scope :before, -> (date) { where("#{table_name}.arrived_on <= ?", date) }
|
||||
scope :after, -> (date) { where("#{table_name}.arrived_on >= ?", date) }
|
||||
|
||||
around_create :set_previous
|
||||
before_destroy :update_nexts_previous
|
||||
|
||||
def set_previous
|
||||
self.previous ||= person.admissions.ordered.before(arrived_on).last
|
||||
future_next = previous&.reload_next
|
||||
yield
|
||||
future_next&.update(previous: self)
|
||||
end
|
||||
|
||||
def update_nexts_previous
|
||||
self.next&.update(previous: previous)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,2 +1,4 @@
|
|||
class Country < ApplicationRecord
|
||||
has_many :admissions, inverse_of: :country
|
||||
has_many :people, foreign_key: :birth_country_id, inverse_of: :birth_country
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,2 +1,18 @@
|
|||
class Person < ApplicationRecord
|
||||
belongs_to :birth_country, class_name: Country.name
|
||||
has_many :admissions
|
||||
|
||||
def stays
|
||||
stays = []
|
||||
admissions.ordered.includes(:country).all.each_cons(2) do |a, b|
|
||||
stays.push(
|
||||
country: a.country,
|
||||
arrival: a.arrived_on,
|
||||
departure: b.arrived_on,
|
||||
duration: (b.arrived_on - a.arrived_on).to_i + 1
|
||||
)
|
||||
end
|
||||
|
||||
stays
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -0,0 +1,11 @@
|
|||
h1 Countries
|
||||
table
|
||||
tr
|
||||
th Name
|
||||
th ISO 3166-1 alpha-2 code
|
||||
th ISO 3166-1 alpha-3 code
|
||||
- for country in countries
|
||||
tr
|
||||
td = country.name
|
||||
td = country.iso2
|
||||
td = country.iso3
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
h1 = country.name
|
||||
b ID  
|
||||
= country.id
|
||||
br
|
||||
b ISO 3166-1 alpha-2 code  
|
||||
= country.iso2
|
||||
br
|
||||
b ISO 3166-1 alpha-2 code  
|
||||
= country.iso3
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
h1= person.name
|
||||
table
|
||||
thead
|
||||
tr
|
||||
th Country
|
||||
th Arrival
|
||||
th Departure
|
||||
th Duration (days)
|
||||
tbody
|
||||
- for stay in person.stays
|
||||
tr
|
||||
td = stay[:country].name + " (" + stay[:country].iso2 + ")"
|
||||
td = stay[:arrival]
|
||||
td = stay[:departure]
|
||||
td = stay[:duration]
|
||||
|
|
@ -1,4 +1,7 @@
|
|||
Rails.application.routes.draw do
|
||||
resources :people, only: :show
|
||||
resources :countries, only: %i[index show]
|
||||
|
||||
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
|
||||
|
||||
# Defines the root path route ("/")
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@ class CreatePeople < ActiveRecord::Migration[7.0]
|
|||
def change
|
||||
create_table :people do |t|
|
||||
t.string :name, null: false
|
||||
t.date :birth_day, null: false
|
||||
t.references :birth_country, null: false, foreign_key: { to_table: :countries }
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
|
|
|
|||
|
|
@ -3,6 +3,8 @@ class CreateAdmissions < ActiveRecord::Migration[7.0]
|
|||
create_table :admissions do |t|
|
||||
t.references :country, null: false, foreign_key: true
|
||||
t.references :person, null: false, foreign_key: true
|
||||
t.references :previous, null: true, foreign_key: { to_table: :admissions }
|
||||
|
||||
t.date :arrived_on, null: false
|
||||
|
||||
t.timestamps
|
||||
|
|
|
|||
|
|
@ -14,11 +14,13 @@ ActiveRecord::Schema[7.0].define(version: 2023_09_24_015803) do
|
|||
create_table "admissions", force: :cascade do |t|
|
||||
t.integer "country_id", null: false
|
||||
t.integer "person_id", null: false
|
||||
t.integer "previous_id"
|
||||
t.date "arrived_on", null: false
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.index ["country_id"], name: "index_admissions_on_country_id"
|
||||
t.index ["person_id"], name: "index_admissions_on_person_id"
|
||||
t.index ["previous_id"], name: "index_admissions_on_previous_id"
|
||||
end
|
||||
|
||||
create_table "countries", force: :cascade do |t|
|
||||
|
|
@ -34,10 +36,15 @@ ActiveRecord::Schema[7.0].define(version: 2023_09_24_015803) do
|
|||
|
||||
create_table "people", force: :cascade do |t|
|
||||
t.string "name", null: false
|
||||
t.date "birth_day", null: false
|
||||
t.integer "birth_country_id", null: false
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.index ["birth_country_id"], name: "index_people_on_birth_country_id"
|
||||
end
|
||||
|
||||
add_foreign_key "admissions", "admissions", column: "previous_id"
|
||||
add_foreign_key "admissions", "countries"
|
||||
add_foreign_key "admissions", "people"
|
||||
add_foreign_key "people", "countries", column: "birth_country_id"
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in New Issue