Skip to content

Commit

Permalink
Updated authors#create action to work with intellectual_property. Add…
Browse files Browse the repository at this point in the history
…ed spec for it
  • Loading branch information
damisul committed May 3, 2024
1 parent 3885b85 commit 38164a3
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 16 deletions.
21 changes: 9 additions & 12 deletions app/controllers/authors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -606,7 +603,7 @@ def edit_toc
protected

def person_params
params[:person].permit(
params.require(:person).permit(
:affiliation,
:comment,
:country,
Expand Down
5 changes: 2 additions & 3 deletions app/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/views/authors/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
65 changes: 65 additions & 0 deletions spec/controllers/authors_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down

0 comments on commit 38164a3

Please sign in to comment.