From bb42e5069abfadab5dacf87afb5e0b3f41e70b5d Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Fri, 10 Apr 2020 18:40:56 +0500 Subject: [PATCH] Add action Settings::PassportsController#create --- .../settings/passports_controller.rb | 19 ++++++++++-- app/policies/settings/passport_policy.rb | 31 +++++++++++++++++++ app/views/settings/passports/new.html.erb | 7 +++++ config/locales/simple_form/en.yml | 4 +++ config/locales/simple_form/ru.yml | 4 +++ config/routes.rb | 2 +- .../settings/passports/create_spec.rb | 7 +++++ 7 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 spec/requests/settings/passports/create_spec.rb diff --git a/app/controllers/settings/passports_controller.rb b/app/controllers/settings/passports_controller.rb index 079c375..3cefd7a 100644 --- a/app/controllers/settings/passports_controller.rb +++ b/app/controllers/settings/passports_controller.rb @@ -6,8 +6,9 @@ class Settings::PassportsController < ApplicationController before_action :skip_policy_scope, only: :index before_action :set_account - before_action :set_passport, only: :show - before_action :new_passport, only: :new + before_action :set_passport, only: :show + before_action :new_passport, only: :new + before_action :build_passport, only: :create # GET /settings/passports def index @@ -26,6 +27,15 @@ class Settings::PassportsController < ApplicationController authorize [:settings, Passport] end + # POST /settings/passports + def create + authorize [:settings, @passport] + + return render :new unless @passport.save + + redirect_to [:settings, @passport] + end + private def set_account @@ -39,4 +49,9 @@ private def new_passport @passport = Passport.new person: @account&.person end + + def build_passport + @passport = Passport.new permitted_attributes [:settings, Passport] + @passport.person = @account&.person + end end diff --git a/app/policies/settings/passport_policy.rb b/app/policies/settings/passport_policy.rb index c635cd1..33ca0b9 100644 --- a/app/policies/settings/passport_policy.rb +++ b/app/policies/settings/passport_policy.rb @@ -12,4 +12,35 @@ class Settings::PassportPolicy < ApplicationPolicy def create? account&.person end + + def permitted_attributes_for_create + %i[ + last_name + first_name + middle_name + sex + date_of_birth + place_of_birth + series + number + issued_by + unit_code + date_of_issue + zip_code + town_type + town_name + settlement_type + settlement_name + district_type + district_name + street_type + street_name + residence_type + residence_name + building_type + building_name + apartment_type + apartment_name + ].freeze + end end diff --git a/app/views/settings/passports/new.html.erb b/app/views/settings/passports/new.html.erb index 1784944..aaddee1 100644 --- a/app/views/settings/passports/new.html.erb +++ b/app/views/settings/passports/new.html.erb @@ -8,6 +8,13 @@ <%= simple_form_for [:settings, @passport] do |f| %> <%= f.error_notification %> + <%= f.input :last_name %> + <%= f.input :first_name %> + <%= f.input :middle_name %> + <%= f.input :sex, collection: Passport.sexes.keys.map(&:to_sym) %> + <%= f.input :date_of_birth, start_year: 1900, end_year: Time.zone.now.year %> + <%= f.input :place_of_birth %> + <%= f.input :series %> <%= f.input :number %> <%= f.input :issued_by %> diff --git a/config/locales/simple_form/en.yml b/config/locales/simple_form/en.yml index 4963537..5237b13 100644 --- a/config/locales/simple_form/en.yml +++ b/config/locales/simple_form/en.yml @@ -11,6 +11,10 @@ en: error_notification: default_message: 'Please review the problems below:' options: + passport: + sex: + male: Male + female: Female person: sex: male: Male diff --git a/config/locales/simple_form/ru.yml b/config/locales/simple_form/ru.yml index 7e4ca43..7889b0c 100644 --- a/config/locales/simple_form/ru.yml +++ b/config/locales/simple_form/ru.yml @@ -11,6 +11,10 @@ ru: error_notification: default_message: 'Пожалуйста, исправьте следующие ошибки:' options: + passport: + sex: + male: Мужской + female: Женский person: sex: male: Мужской diff --git a/config/routes.rb b/config/routes.rb index 10d0a9d..9dc51b5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -35,7 +35,7 @@ Rails.application.routes.draw do resource :appearance, only: %i[edit update] resource :person, only: %i[show new] resources :sessions, only: :index - resources :passports, only: %i[index show new] + resources :passports, only: %i[index show new create] resources :contacts, only: %i[index create destroy] do resource :security_notification_switch, diff --git a/spec/requests/settings/passports/create_spec.rb b/spec/requests/settings/passports/create_spec.rb new file mode 100644 index 0000000..b8d6f7b --- /dev/null +++ b/spec/requests/settings/passports/create_spec.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'POST /settings/passports' do + pending "add some examples to (or delete) #{__FILE__}" +end