diff --git a/app/controllers/passports_controller.rb b/app/controllers/passports_controller.rb index 14026ea..41c2629 100644 --- a/app/controllers/passports_controller.rb +++ b/app/controllers/passports_controller.rb @@ -31,6 +31,15 @@ class PassportsController < ApplicationController redirect_to @passport end + # PATCH/PUT /passports/:id + def update + authorize @passport + + return render :show unless @passport.update permitted_attributes @passport + + redirect_to @passport + end + private def set_passport diff --git a/app/policies/passport_policy.rb b/app/policies/passport_policy.rb index ebf772f..2e57a9c 100644 --- a/app/policies/passport_policy.rb +++ b/app/policies/passport_policy.rb @@ -9,6 +9,10 @@ class PassportPolicy < ApplicationPolicy true end + def update? + true + end + def permitted_attributes_for_create %i[ surname given_name patronymic sex date_of_birth place_of_birth series diff --git a/app/views/passports/show.html.erb b/app/views/passports/show.html.erb index bc42c4a..8902fbc 100644 --- a/app/views/passports/show.html.erb +++ b/app/views/passports/show.html.erb @@ -11,6 +11,11 @@
<% if @passport.image.attached? %> <%= image_tag url_for(@passport.image), class: 'img-fluid' %> + <% elsif policy(@passport).update? %> + <%= simple_form_for @passport do |f| %> + <%= f.input :image %> + <%= f.button :submit %> + <% end %> <% end %>
diff --git a/config/locales/activerecord/en.yml b/config/locales/activerecord/en.yml index 388ad6f..7bb4c8b 100644 --- a/config/locales/activerecord/en.yml +++ b/config/locales/activerecord/en.yml @@ -25,6 +25,7 @@ en: comment: Comment passport: id: ID + image: Image surname: Surname given_name: Given names patronymic: Patronymic diff --git a/config/locales/activerecord/ru.yml b/config/locales/activerecord/ru.yml index 12e27c2..63fb98d 100644 --- a/config/locales/activerecord/ru.yml +++ b/config/locales/activerecord/ru.yml @@ -25,6 +25,7 @@ ru: comment: Комментарий passport: id: ID + image: Изображение surname: Фамилия given_name: Имя patronymic: Отчество diff --git a/config/routes.rb b/config/routes.rb index 991cf25..fecb482 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -28,7 +28,7 @@ Rails.application.routes.draw do resources :membership_applications, only: %i[new create] - resources :passports, only: %i[index show new create] + resources :passports, only: %i[index show new create update] resources :telegram_bot_updates, only: :create end diff --git a/factories/passports.rb b/factories/passports.rb index ed5d1ea..2cae1ef 100644 --- a/factories/passports.rb +++ b/factories/passports.rb @@ -6,9 +6,7 @@ FactoryBot.define do image_filename { image_fixture } image_fixture { "passport_image_#{rand(1..4)}.jpg" } - image_path do - File.join File.expand_path('..', __dir__), 'fixtures', image_fixture - end + image_path { Rails.root.join 'fixtures', image_fixture } end surname { Faker::Name.last_name } diff --git a/spec/requests/passports/update_spec.rb b/spec/requests/passports/update_spec.rb new file mode 100644 index 0000000..f9dafe7 --- /dev/null +++ b/spec/requests/passports/update_spec.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'PATCH/PUT /passports/:id' do + let!(:passport) { create :passport } + + let :passport_attributes do + { image: Rack::Test::UploadedFile.new(File.open(passport_image_path)) } + end + + let :passport_image_path do + Rails.root.join 'fixtures', 'passport_image_1.jpg' + end + + before do + patch "/passports/#{passport.id}", params: { passport: passport_attributes } + end + + specify do + expect(response).to redirect_to passport_url passport + end +end