From e58b9be5a8a639c923c194175eec9bce922fae67 Mon Sep 17 00:00:00 2001 From: Damir Sultanbekov Date: Fri, 3 May 2024 18:47:18 +0500 Subject: [PATCH] Updated authors#create action to work with intellectual_property. Added spec for it --- Gemfile | 1 + app/controllers/authors_controller.rb | 21 +++---- app/models/person.rb | 5 +- app/views/authors/_form.html.haml | 2 +- spec/controllers/authors_controller_spec.rb | 65 +++++++++++++++++++++ 5 files changed, 78 insertions(+), 16 deletions(-) diff --git a/Gemfile b/Gemfile index bee6309e..ac402f60 100644 --- a/Gemfile +++ b/Gemfile @@ -149,6 +149,7 @@ group :test, :development do gem 'rspec-rails', '~> 5.0.2' gem 'spring' gem 'spring-commands-rspec' + gem 'debug' end gem "sidekiq", "~> 7.2" diff --git a/app/controllers/authors_controller.rb b/app/controllers/authors_controller.rb index e12cffb8..c59ce139 100644 --- a/app/controllers/authors_controller.rb +++ b/app/controllers/authors_controller.rb @@ -379,7 +379,7 @@ def to_manual_toc # end def new - @person = Person.new + @person = Person.new(intellectual_property: :unknown) @page_title = t(:new_author) respond_to do |format| format.html # new.html.erb @@ -391,18 +391,15 @@ def create params[:person][:wikidata_id] = params[:person][:wikidata_id].strip[1..-1] if params[:person] and params[:person][:wikidata_id] and params[:person][:wikidata_id][0] and params[:person][:wikidata_id].strip[0] == 'Q' # tolerate pasting the Wikidata number with the Q Chewy.strategy(:atomic) { @person = Person.new(person_params) - unless @person.status.present? - @person.status = @person.public_domain ? :awaiting_first : :unpublished # default to unpublished. Publishing happens automatically upon first works uploaded if public domain, or by button in status column in authors#list if copyrighted + if @person.status.blank? + @person.status = @person.intellectual_property_public_domain? ? :awaiting_first : :unpublished end - respond_to do |format| - if @person.save - format.html { redirect_to url_for(action: :show, id: @person.id), notice: t(:updated_successfully) } - format.json { render json: @person, status: :created, location: @person } - else - format.html { render action: "new" } - format.json { render json: @person.errors, status: :unprocessable_entity } - end + if @person.save + flash.notice = t(:created_successfully) + redirect_to action: :show, params: { id: @person.id } + else + render action: :new, status: :unprocessable_entity end } end @@ -606,7 +603,7 @@ def edit_toc protected def person_params - params[:person].permit( + params.require(:person).permit( :affiliation, :comment, :country, diff --git a/app/models/person.rb b/app/models/person.rb index b6b8a337..250c9939 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -30,8 +30,6 @@ class Person < ApplicationRecord unknown: 100 }, _prefix: true - validates :intellectual_property, presence: true - # scopes scope :has_toc, -> { where.not(toc_id: nil) } scope :no_toc, -> { where(toc_id: nil) } @@ -53,7 +51,8 @@ class Person < ApplicationRecord include CompactedImpressions # validations - validates :name, presence: true + validates :name, :intellectual_property, presence: true + validates_attachment_content_type :profile_image, content_type: /\Aimage\/.*\z/ update_index('people'){self} # update PeopleIndex when entity is updated diff --git a/app/views/authors/_form.html.haml b/app/views/authors/_form.html.haml index 013c891e..987d41d5 100644 --- a/app/views/authors/_form.html.haml +++ b/app/views/authors/_form.html.haml @@ -36,7 +36,7 @@ .backend-field = f.label Person.human_attribute_name(:intellectual_property) - options = Person.intellectual_properties.keys.map { |ip| [textify_intellectual_property(ip), ip] } - = f.select :intellectual_property, options_for_select(options, @author.intellectual_property) + = f.select :intellectual_property, options_for_select(options, person.intellectual_property) .backend-field = f.label t(:bib_done) %br diff --git a/spec/controllers/authors_controller_spec.rb b/spec/controllers/authors_controller_spec.rb index de1ad361..c119a1a9 100644 --- a/spec/controllers/authors_controller_spec.rb +++ b/spec/controllers/authors_controller_spec.rb @@ -119,6 +119,71 @@ session[:user_id] = user.id end + describe '#new' do + subject { get :new } + + it { is_expected.to be_successful } + end + + describe '#create' do + subject(:call) { post :create, params: { person: person_params } } + + let(:intellectual_property) { 'permission_for_selected' } + let(:status) { 'published' } + + let(:person_params) do + { + name: 'New name', + intellectual_property: intellectual_property, + status: status + } + end + + let(:created_person) { Person.order(id: :desc).first } + + context 'when save successful' do + it 'creates record' do + expect { call }.to change(Person, :count).by(1) + expect(created_person).to have_attributes(person_params) + + expect(call).to redirect_to authors_show_path(id: created_person.id) + expect(flash.notice).to eq I18n.t(:created_successfully) + end + + context 'when status is not set' do + let(:status) { nil } + + context 'when intellectual_property is public_domain' do + let(:intellectual_property) { 'public_domain' } + + it 'sets status to awaiting_first' do + expect { call }.to change(Person, :count).by(1) + expect(created_person.status).to eq 'awaiting_first' + end + end + + context 'when intellectual_property is not public_domain' do + let(:intellectual_property) { 'permission_for_selected' } + + it 'sets status to unpublished' do + expect { call }.to change(Person, :count).by(1) + expect(created_person.status).to eq 'unpublished' + end + end + end + end + + context 'when save fails' do + let(:status) { :unpublished } + let(:intellectual_property) { nil } + + it 're-renders new form' do + expect(call).to render_template(:new) + expect(call).to have_http_status(:unprocessable_entity) + end + end + end + describe 'member actions' do let(:period) { 'revival' } let(:author) { create(:person, intellectual_property: :public_domain, period: period) }