diff --git a/app/controllers/passports_controller.rb b/app/controllers/passports_controller.rb
index 14026ea..3fccd71 100644
--- a/app/controllers/passports_controller.rb
+++ b/app/controllers/passports_controller.rb
@@ -16,6 +16,7 @@ class PassportsController < ApplicationController
# GET /passports/new
def new
@passport = Passport.new
+ @passport.build_passport_map
authorize @passport
end
diff --git a/app/models/passport.rb b/app/models/passport.rb
index b6911c7..ede5442 100644
--- a/app/models/passport.rb
+++ b/app/models/passport.rb
@@ -3,32 +3,17 @@
class Passport < ApplicationRecord
REQUIRED_CONFIRMATIONS = 3
- enum sex: %i[male female]
-
has_many_attached :images
has_one :passport_map, dependent: :restrict_with_exception
has_many :passport_confirmations, dependent: :restrict_with_exception
+ accepts_nested_attributes_for :passport_map
+
validates :confirmed,
inclusion: { in: [false], unless: :enough_confirmations? }
- validates :surname, presence: true
- validates :given_name, presence: true
- validates :sex, presence: true
- validates :date_of_birth, presence: true
- validates :place_of_birth, presence: true
- validates :series, presence: true
- validates :number, presence: true
- validates :issued_by, presence: true
- validates :unit_code, presence: true
- validates :date_of_issue, presence: true
-
- before_validation do
- self.patronymic = nil if patronymic.blank?
- end
-
def image
images.order(created_at: :desc).last
end
diff --git a/app/policies/passport_policy.rb b/app/policies/passport_policy.rb
index d592ef7..6563910 100644
--- a/app/policies/passport_policy.rb
+++ b/app/policies/passport_policy.rb
@@ -10,11 +10,12 @@ class PassportPolicy < ApplicationPolicy
end
def permitted_attributes_for_create
- %i[
- surname given_name patronymic sex date_of_birth place_of_birth series
- number issued_by unit_code date_of_issue
- ] + [
+ [
images: [],
+ passport_map_attributes: %i[
+ surname given_name patronymic sex date_of_birth place_of_birth series
+ number issued_by unit_code date_of_issue
+ ],
]
end
diff --git a/app/views/passports/new.html.erb b/app/views/passports/new.html.erb
index 513de8d..c776f3f 100644
--- a/app/views/passports/new.html.erb
+++ b/app/views/passports/new.html.erb
@@ -1,22 +1,25 @@
<%= simple_form_for @passport do |f| %>
- <%= f.input :surname %>
- <%= f.input :given_name %>
- <%= f.input :patronymic %>
- <%= f.input :sex,
- as: :radio_buttons,
- collection: Passport.sexes.keys.map { |sex|
- [Passport.human_attribute_name("sex.#{sex}"), sex]
- } %>
- <%= f.input :date_of_birth, start_year: Date.today.year - 150,
- end_year: Date.today.year %>
- <%= f.input :place_of_birth %>
- <%= f.input :series %>
- <%= f.input :number %>
- <%= f.input :issued_by %>
- <%= f.input :unit_code %>
- <%= f.input :date_of_issue, start_year: Date.today.year - 150,
- end_year: Date.today.year %>
+ <%= f.simple_fields_for :passport_map do |ff| %>
+ <%= ff.input :surname %>
+ <%= ff.input :given_name %>
+ <%= ff.input :patronymic %>
+ <%= ff.input :sex,
+ as: :radio_buttons,
+ collection: PassportMap.sexes.keys.map { |sex|
+ [Passport.human_attribute_name("sex.#{sex}"), sex]
+ } %>
+ <%= ff.input :date_of_birth, start_year: Date.today.year - 150,
+ end_year: Date.today.year %>
+ <%= ff.input :place_of_birth %>
+ <%= ff.input :series %>
+ <%= ff.input :number %>
+ <%= ff.input :issued_by %>
+ <%= ff.input :unit_code %>
+ <%= ff.input :date_of_issue, start_year: Date.today.year - 150,
+ end_year: Date.today.year %>
+ <% end %>
+
<%= f.input :images %>
<%= f.button :submit %>
diff --git a/app/views/passports/show.html.erb b/app/views/passports/show.html.erb
index 7ed81c2..48b6568 100644
--- a/app/views/passports/show.html.erb
+++ b/app/views/passports/show.html.erb
@@ -1,8 +1,8 @@
- <%= @passport.surname %>
- <%= @passport.given_name %>
- <%= @passport.patronymic %>
+ <%= @passport.passport_map.surname %>
+ <%= @passport.passport_map.given_name %>
+ <%= @passport.passport_map.patronymic %>
@@ -62,77 +62,77 @@
<%= Passport.human_attribute_name :surname %>
- <%= @passport.surname %>
+ <%= @passport.passport_map.surname %>
<%= Passport.human_attribute_name :given_name %>
- <%= @passport.given_name %>
+ <%= @passport.passport_map.given_name %>
<%= Passport.human_attribute_name :patronymic %>
- <%= @passport.patronymic %>
+ <%= @passport.passport_map.patronymic %>
<%= Passport.human_attribute_name :sex %>
- <%= Passport.human_attribute_name "sex.#{@passport.sex}" %>
+ <%= Passport.human_attribute_name "sex.#{@passport.passport_map.sex}" %>
<%= Passport.human_attribute_name :date_of_birth %>
- <%= localize @passport.date_of_birth %>
+ <%= localize @passport.passport_map.date_of_birth %>
<%= Passport.human_attribute_name :place_of_birth %>
- <%= @passport.place_of_birth %>
+ <%= @passport.passport_map.place_of_birth %>
<%= Passport.human_attribute_name :series %>
- <%= passport_series @passport.series %>
+ <%= passport_series @passport.passport_map.series %>
<%= Passport.human_attribute_name :number %>
- <%= passport_number @passport.number %>
+ <%= passport_number @passport.passport_map.number %>
<%= Passport.human_attribute_name :issued_by %>
- <%= @passport.issued_by %>
+ <%= @passport.passport_map.issued_by %>
<%= Passport.human_attribute_name :unit_code %>
- <%= @passport.unit_code %>
+ <%= @passport.passport_map.unit_code %>
<%= Passport.human_attribute_name :date_of_issue %>
- <%= localize @passport.date_of_issue %>
+ <%= localize @passport.passport_map.date_of_issue %>
diff --git a/config/locales/activerecord/en.yml b/config/locales/activerecord/en.yml
index a872a48..e26cc4a 100644
--- a/config/locales/activerecord/en.yml
+++ b/config/locales/activerecord/en.yml
@@ -35,17 +35,6 @@ en:
confirmed: Confirmed?
passport_confirmations_count: Confirmations count
images: Images
- surname: Surname
- given_name: Given names
- patronymic: Patronymic
- sex: Sex
- date_of_birth: Date of birth
- place_of_birth: Place of birth
- series: Series
- number: Number
- issued_by: Issued by
- unit_code: Unit code
- date_of_issue: Date of issue
passport_map:
id: ID
surname: Surname
diff --git a/config/locales/activerecord/ru.yml b/config/locales/activerecord/ru.yml
index 014954d..328daab 100644
--- a/config/locales/activerecord/ru.yml
+++ b/config/locales/activerecord/ru.yml
@@ -35,17 +35,6 @@ ru:
confirmed: Подтверждён?
passport_confirmations_count: Количество подтверждений
images: Изображения
- surname: Фамилия
- given_name: Имя
- patronymic: Отчество
- sex: Пол
- date_of_birth: Дата рождения
- place_of_birth: Место рождения
- series: Серия
- number: Номер
- issued_by: Кем выдан
- unit_code: Код подразделения
- date_of_issue: Дата выдачи
passport_map:
id: ID
surname: Фамилия
diff --git a/db/migrate/20181202142918_remove_mapping_from_passports.rb b/db/migrate/20181202142918_remove_mapping_from_passports.rb
new file mode 100644
index 0000000..bbf7ff2
--- /dev/null
+++ b/db/migrate/20181202142918_remove_mapping_from_passports.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+class RemoveMappingFromPassports < ActiveRecord::Migration[5.2]
+ def change
+ remove_column :passports, :surname, :string, null: false
+ remove_column :passports, :given_name, :string, null: false
+ remove_column :passports, :patronymic, :string
+ remove_column :passports, :sex, :integer, null: false
+ remove_column :passports, :date_of_birth, :date, null: false
+ remove_column :passports, :place_of_birth, :string, null: false
+ remove_column :passports, :series, :integer, null: false
+ remove_column :passports, :number, :integer, null: false
+ remove_column :passports, :issued_by, :text, null: false
+ remove_column :passports, :unit_code, :string, null: false
+ remove_column :passports, :date_of_issue, :date, null: false
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index be1a49c..7dd819f 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 2018_12_02_140310) do
+ActiveRecord::Schema.define(version: 2018_12_02_142918) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -108,17 +108,6 @@ ActiveRecord::Schema.define(version: 2018_12_02_140310) do
create_table "passports", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
- t.string "surname", null: false
- t.string "given_name", null: false
- t.string "patronymic"
- t.integer "sex", null: false
- t.date "date_of_birth", null: false
- t.string "place_of_birth", null: false
- t.integer "series", null: false
- t.integer "number", null: false
- t.string "unit_code", null: false
- t.date "date_of_issue", null: false
- t.text "issued_by", null: false
t.boolean "confirmed", default: false, null: false
end
diff --git a/factories/passport_maps.rb b/factories/passport_maps.rb
index 0a85674..3ba01af 100644
--- a/factories/passport_maps.rb
+++ b/factories/passport_maps.rb
@@ -7,7 +7,7 @@ FactoryBot.define do
surname { Faker::Name.last_name }
given_name { Faker::Name.first_name }
patronymic { Faker::Name.first_name }
- sex { Passport.sexes.keys.sample }
+ sex { PassportMap.sexes.keys.sample }
date_of_birth { Faker::Date.backward }
place_of_birth { Faker::Address.city }
series { rand 0..9999 }
diff --git a/factories/passports.rb b/factories/passports.rb
index 9996223..d9ac6b0 100644
--- a/factories/passports.rb
+++ b/factories/passports.rb
@@ -1,21 +1,7 @@
# frozen_string_literal: true
FactoryBot.define do
- factory :empty_passport, class: Passport do
- surname { Faker::Name.last_name }
- given_name { Faker::Name.first_name }
- patronymic { Faker::Name.first_name }
- sex { Passport.sexes.keys.sample }
- date_of_birth { Faker::Date.backward }
- place_of_birth { Faker::Address.city }
- series { rand 0..9999 }
- number { rand 0..999_999 }
- issued_by { Faker::Lorem.sentence }
- unit_code do
- "#{rand(0..999).to_s.rjust(3, '0')}-#{rand(0..999).to_s.rjust(3, '0')}"
- end
- date_of_issue { Faker::Date.backward }
- end
+ factory :empty_passport, class: Passport
factory :passport_without_image, parent: :empty_passport do
confirmed { false }
diff --git a/spec/models/passport_map_spec.rb b/spec/models/passport_map_spec.rb
index 20d190e..4fee52a 100644
--- a/spec/models/passport_map_spec.rb
+++ b/spec/models/passport_map_spec.rb
@@ -25,7 +25,7 @@ RSpec.describe PassportMap do
describe '#patronymic' do
context 'when it is empty' do
- subject { create :passport_without_image, patronymic: '' }
+ subject { create :passport_map, patronymic: '' }
specify do
expect(subject.patronymic).to eq nil
@@ -33,7 +33,7 @@ RSpec.describe PassportMap do
end
context 'when it is blank' do
- subject { create :passport_without_image, patronymic: ' ' }
+ subject { create :passport_map, patronymic: ' ' }
specify do
expect(subject.patronymic).to eq nil
diff --git a/spec/models/passport_spec.rb b/spec/models/passport_spec.rb
index d70b8f8..8993d93 100644
--- a/spec/models/passport_spec.rb
+++ b/spec/models/passport_spec.rb
@@ -17,18 +17,6 @@ RSpec.describe Passport do
.dependent(:restrict_with_exception)
end
- it { is_expected.to validate_presence_of :surname }
- it { is_expected.to validate_presence_of :given_name }
- it { is_expected.not_to validate_presence_of :patronymic }
- it { is_expected.to validate_presence_of :sex }
- it { is_expected.to validate_presence_of :date_of_birth }
- it { is_expected.to validate_presence_of :place_of_birth }
- it { is_expected.to validate_presence_of :series }
- it { is_expected.to validate_presence_of :number }
- it { is_expected.to validate_presence_of :issued_by }
- it { is_expected.to validate_presence_of :unit_code }
- it { is_expected.to validate_presence_of :date_of_issue }
-
pending '#image'
pending '#enough_confirmations?'
@@ -72,22 +60,4 @@ RSpec.describe Passport do
it { is_expected.to allow_value true }
end
end
-
- describe '#patronymic' do
- context 'when it is empty' do
- subject { create :passport_without_image, patronymic: '' }
-
- specify do
- expect(subject.patronymic).to eq nil
- end
- end
-
- context 'when it is blank' do
- subject { create :passport_without_image, patronymic: ' ' }
-
- specify do
- expect(subject.patronymic).to eq nil
- end
- end
- end
end
diff --git a/spec/requests/passports/show_spec.rb b/spec/requests/passports/show_spec.rb
index a975398..0ee93fc 100644
--- a/spec/requests/passports/show_spec.rb
+++ b/spec/requests/passports/show_spec.rb
@@ -4,6 +4,7 @@ require 'rails_helper'
RSpec.describe 'GET /passports/:id' do
before do
+ create :passport_map, passport: passport
get "/passports/#{passport.id}"
end