Require Passport#image
This commit is contained in:
parent
139770b4ce
commit
8ffd8b27bb
8 changed files with 29 additions and 54 deletions
|
@ -31,15 +31,6 @@ 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
|
||||
|
|
|
@ -16,6 +16,10 @@ class Passport < ApplicationRecord
|
|||
validates :unit_code, presence: true
|
||||
validates :date_of_issue, presence: true
|
||||
|
||||
validate do
|
||||
errors.add :image, :blank unless image.attached?
|
||||
end
|
||||
|
||||
before_validation do
|
||||
self.patronymic = nil if patronymic.blank?
|
||||
end
|
||||
|
|
|
@ -9,21 +9,13 @@ 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
|
||||
number issued_by unit_code date_of_issue
|
||||
number issued_by unit_code date_of_issue image
|
||||
]
|
||||
end
|
||||
|
||||
def permitted_attributes_for_update
|
||||
%i[image]
|
||||
end
|
||||
|
||||
class Scope < Scope
|
||||
def resolve
|
||||
scope.all
|
||||
|
|
|
@ -9,14 +9,7 @@
|
|||
</div>
|
||||
|
||||
<div class="col-md-6">
|
||||
<% 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 %>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -28,7 +28,7 @@ Rails.application.routes.draw do
|
|||
|
||||
resources :membership_applications, only: %i[new create]
|
||||
|
||||
resources :passports, only: %i[index show new create update]
|
||||
resources :passports, only: %i[index show new create]
|
||||
|
||||
resources :telegram_bot_updates, only: :create
|
||||
end
|
||||
|
|
|
@ -23,7 +23,7 @@ FactoryBot.define do
|
|||
end
|
||||
date_of_issue { Faker::Date.backward }
|
||||
|
||||
after :create do |passport, evaluator|
|
||||
after :build do |passport, evaluator|
|
||||
passport.image.attach(
|
||||
filename: evaluator.image_filename,
|
||||
io: File.open(evaluator.image_path),
|
||||
|
|
|
@ -3,10 +3,20 @@
|
|||
require 'rails_helper'
|
||||
|
||||
RSpec.describe 'POST /passports' do
|
||||
let(:passport_attributes) { attributes_for :passport }
|
||||
let(:passport_plain_attributes) { attributes_for :passport }
|
||||
|
||||
let :passport_all_attributes do
|
||||
passport_plain_attributes.merge(
|
||||
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
|
||||
|
||||
def make_request
|
||||
post '/passports', params: { passport: passport_attributes }
|
||||
post '/passports', params: { passport: passport_all_attributes }
|
||||
end
|
||||
|
||||
specify do
|
||||
|
@ -17,7 +27,15 @@ RSpec.describe 'POST /passports' do
|
|||
before { make_request }
|
||||
|
||||
specify do
|
||||
expect(Passport.last).to have_attributes passport_attributes
|
||||
expect(response).to redirect_to passport_url Passport.last
|
||||
end
|
||||
|
||||
specify do
|
||||
expect(Passport.last).to have_attributes passport_plain_attributes
|
||||
end
|
||||
|
||||
specify do
|
||||
expect(Passport.last.image).to be_attached
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,23 +0,0 @@
|
|||
# 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
|
Reference in a new issue