Allow non-unique PassportMap#passport
This commit is contained in:
parent
15b7a8e3fd
commit
9d75e9b01b
|
@ -16,7 +16,7 @@ class PassportsController < ApplicationController
|
|||
# GET /passports/new
|
||||
def new
|
||||
@passport = Passport.new
|
||||
@passport.build_passport_map
|
||||
@passport.passport_maps.build
|
||||
|
||||
authorize @passport
|
||||
end
|
||||
|
|
|
@ -5,17 +5,21 @@ class Passport < ApplicationRecord
|
|||
|
||||
has_many_attached :images
|
||||
|
||||
has_one :passport_map, dependent: :restrict_with_exception
|
||||
has_many :passport_maps, dependent: :restrict_with_exception
|
||||
|
||||
has_many :passport_confirmations, dependent: :restrict_with_exception
|
||||
|
||||
accepts_nested_attributes_for :passport_map
|
||||
accepts_nested_attributes_for :passport_maps
|
||||
|
||||
validates :confirmed,
|
||||
inclusion: { in: [false], unless: :enough_confirmations? }
|
||||
|
||||
def passport_map
|
||||
passport_maps.order(created_at: :asc).last
|
||||
end
|
||||
|
||||
def image
|
||||
images.order(created_at: :desc).last
|
||||
images.order(created_at: :asc).last
|
||||
end
|
||||
|
||||
def enough_confirmations?
|
||||
|
|
|
@ -5,8 +5,6 @@ class PassportMap < ApplicationRecord
|
|||
|
||||
belongs_to :passport
|
||||
|
||||
validates :passport_id, uniqueness: true
|
||||
|
||||
validates :surname, presence: true
|
||||
validates :given_name, presence: true
|
||||
validates :sex, presence: true
|
||||
|
|
|
@ -12,7 +12,7 @@ class PassportPolicy < ApplicationPolicy
|
|||
def permitted_attributes_for_create
|
||||
[
|
||||
:images,
|
||||
passport_map_attributes: %i[
|
||||
passport_maps_attributes: %i[
|
||||
surname given_name patronymic sex date_of_birth place_of_birth series
|
||||
number issued_by unit_code date_of_issue
|
||||
],
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
<%= simple_form_for @passport, wrapper: :horizontal_form do |f| %>
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
<%= f.simple_fields_for :passport_map do |ff| %>
|
||||
<%= f.simple_fields_for :passport_maps do |ff| %>
|
||||
<%= ff.input :surname %>
|
||||
<%= ff.input :given_name %>
|
||||
<%= ff.input :patronymic %>
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class NonUniquePassportForPassportMaps < ActiveRecord::Migration[5.2]
|
||||
def change
|
||||
remove_reference :passport_maps, :passport,
|
||||
null: false,
|
||||
index: { unique: true },
|
||||
foreign_key: true
|
||||
|
||||
add_reference :passport_maps, :passport,
|
||||
null: false, # rubocop:disable Rails/NotNullColumn
|
||||
foreign_key: true
|
||||
end
|
||||
end
|
|
@ -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_142918) do
|
||||
ActiveRecord::Schema.define(version: 2018_12_02_201924) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
|
@ -90,7 +90,6 @@ ActiveRecord::Schema.define(version: 2018_12_02_142918) do
|
|||
create_table "passport_maps", force: :cascade do |t|
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.bigint "passport_id", null: false
|
||||
t.string "surname", null: false
|
||||
t.string "given_name", null: false
|
||||
t.string "patronymic"
|
||||
|
@ -102,7 +101,8 @@ ActiveRecord::Schema.define(version: 2018_12_02_142918) do
|
|||
t.text "issued_by", null: false
|
||||
t.string "unit_code", null: false
|
||||
t.date "date_of_issue", null: false
|
||||
t.index ["passport_id"], name: "index_passport_maps_on_passport_id", unique: true
|
||||
t.bigint "passport_id", null: false
|
||||
t.index ["passport_id"], name: "index_passport_maps_on_passport_id"
|
||||
end
|
||||
|
||||
create_table "passports", force: :cascade do |t|
|
||||
|
|
|
@ -5,7 +5,9 @@ FactoryBot.define do
|
|||
confirmed { false }
|
||||
|
||||
trait :with_passport_map do
|
||||
association :passport_map, strategy: :build
|
||||
after :create do |passport, _evaluator|
|
||||
create :passport_map, passport: passport
|
||||
end
|
||||
end
|
||||
|
||||
trait :with_image do
|
||||
|
|
|
@ -21,8 +21,6 @@ RSpec.describe PassportMap do
|
|||
it { is_expected.to validate_presence_of :unit_code }
|
||||
it { is_expected.to validate_presence_of :date_of_issue }
|
||||
|
||||
it { is_expected.to validate_uniqueness_of :passport_id }
|
||||
|
||||
describe '#patronymic' do
|
||||
context 'when it is empty' do
|
||||
subject { create :passport_map, patronymic: '' }
|
||||
|
|
|
@ -7,7 +7,7 @@ RSpec.describe Passport do
|
|||
|
||||
it do
|
||||
is_expected.to \
|
||||
have_one(:passport_map)
|
||||
have_many(:passport_maps)
|
||||
.dependent(:restrict_with_exception)
|
||||
end
|
||||
|
||||
|
@ -17,6 +17,7 @@ RSpec.describe Passport do
|
|||
.dependent(:restrict_with_exception)
|
||||
end
|
||||
|
||||
pending '#passport_map'
|
||||
pending '#image'
|
||||
pending '#enough_confirmations?'
|
||||
|
||||
|
|
Reference in New Issue