Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

285 - Refactor PublicationsController#index #286

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 57 additions & 72 deletions app/controllers/publications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,81 +32,19 @@ class PublicationsController < ApplicationController
before_action :signed_in, only: %i[index show edit update destroy]
before_action :check_max_submissions, only: %i[new]

RESOURCE_NAMES = %i[
artworks books book_chapters digital_projects editings
films journal_articles musical_scores photographies
physical_media public_performances plays other_publications
].freeze

def index
redirect_to publications_path unless request.path.include? publications_path
if session[:admin] && params[:id].nil?
@pagy_submitters, @submitters = pagy(Submitter.all, page_param: :page_submitters)
@pagy_artworks, @artworks = pagy(Artwork.all, page_param: :page_artworks)
@pagy_books, @books = pagy(Book.all, page_param: :page_books)
@pagy_book_chapters, @book_chapters = pagy(BookChapter.all, page_param: :page_book_chapters)
@pagy_digital_projects, @digital_projects = pagy(DigitalProject.all, page_param: :page_digital_projectss)
@pagy_editings, @editings = pagy(Editing.all, page_param: :page_editings)
@pagy_films, @films = pagy(Film.all, page_param: :page_films)
@pagy_journal_articles, @journal_articles = pagy(JournalArticle.all, page_param: :page_journal_articles)
@pagy_musical_scores, @musical_scores = pagy(MusicalScore.all, page_param: :page_musical_scores)
@pagy_photographies, @photographies = pagy(Photography.all, page_param: :page_photographies)
@pagy_physical_media, @physical_media = pagy(PhysicalMedium.all, page_param: :page_physical_media)
@pagy_plays, @plays = pagy(Play.all, page_param: :page_plays)
@pagy_public_performances, @public_performances = pagy(PublicPerformance.all, page_param: :page_public_performances)
@pagy_other_publications, @other_publications = pagy(OtherPublication.all, page_param: :page_other_publications)
@submitter_count = Submitter.all.count
@artwork_count = Artwork.all.count
@book_count = Book.all.count
@book_chapter_count = BookChapter.all.count
@digital_project_count = DigitalProject.all.count
@editing_count = Editing.all.count
@film_count = Film.all.count
@journal_article_count = JournalArticle.all.count
@musical_score_count = MusicalScore.all.count
@photography_count = Photography.all.count
@physical_medium_count = PhysicalMedium.all.count
@play_count = Play.all.count
@public_performance_count = PublicPerformance.all.count
@other_publication_count = OtherPublication.all.count
elsif session[:admin] && params[:id]
@submitter = helpers.find_submitter(params[:id])
@artworks = helpers.find_artworks(params[:id])
@books = helpers.find_books(params[:id])
@book_chapters = helpers.find_book_chapters(params[:id])
@digital_projects = helpers.find_digital_projects(params[:id])
@editings = helpers.find_editings(params[:id])
@films = helpers.find_films(params[:id])
@journal_articles = helpers.find_journal_articles(params[:id])
@musical_scores = helpers.find_musical_scores(params[:id])
@photographies = helpers.find_photographies(params[:id])
@physical_media = helpers.find_physical_media(params[:id])
@plays = helpers.find_plays(params[:id])
@public_performances = helpers.find_public_performances(params[:id])
@other_publications = helpers.find_other_publications(params[:id])
@artwork_count = helpers.find_artworks(params[:id]).count
@book_count = helpers.find_books(params[:id]).count
@book_chapter_count = helpers.find_book_chapters(params[:id]).count
@digital_project_count = helpers.find_digital_projects(params[:id]).count
@editing_count = helpers.find_editings(params[:id]).count
@film_count = helpers.find_films(params[:id]).count
@journal_article_count = helpers.find_journal_articles(params[:id]).count
@musical_score_count = helpers.find_musical_scores(params[:id]).count
@photography_count = helpers.find_photographies(params[:id]).count
@physical_medium_count = helpers.find_physical_media(params[:id]).count
@play_count = helpers.find_plays(params[:id]).count
@public_performance_count = helpers.find_public_performances(params[:id]).count
@other_publication_count = helpers.find_other_publications(params[:id]).count

if session[:admin]
load_admin_resources
else
redirect_to publications_path if params[:id]
@submitter = helpers.find_submitter(session[:submitter_id])
@artworks = helpers.find_artworks(session[:submitter_id])
@books = helpers.find_books(session[:submitter_id])
@book_chapters = helpers.find_book_chapters(session[:submitter_id])
@digital_projects = helpers.find_digital_projects(session[:submitter_id])
@editings = helpers.find_editings(session[:submitter_id])
@films = helpers.find_films(session[:submitter_id])
@journal_articles = helpers.find_journal_articles(session[:submitter_id])
@musical_scores = helpers.find_musical_scores(session[:submitter_id])
@photographies = helpers.find_photographies(session[:submitter_id])
@physical_media = helpers.find_physical_media(session[:submitter_id])
@public_performances = helpers.find_public_performances(session[:submitter_id])
@plays = helpers.find_plays(session[:submitter_id])
@other_publications = helpers.find_other_publications(session[:submitter_id])
load_submitter_resources
end
end

Expand Down Expand Up @@ -179,4 +117,51 @@ def signed_in
def check_max_submissions
redirect_to publications_path if Object.const_get(controller_name.classify).where(submitter_id: session[:submitter_id]).count > 2
end

private

def load_admin_resources
if params[:id].nil?
load_all_resources_for_admin
else
load_single_submitter_resources_for_admin(params[:id])
end
end

def load_all_resources_for_admin
RESOURCE_NAMES.each do |resource_name|
load_resource_for_admin(resource_name)
end
@pagy_submitters, @submitters = pagy(Submitter.all, page_param: :page_submitters)
@submitter_count = Submitter.all.count
end

def load_resource_for_admin(resource_name)
model_class = resource_name.to_s.classify.constantize
pagy_variable, records = pagy(model_class.all, page_param: :"page_#{resource_name}")
instance_variable_set("@pagy_#{resource_name}", pagy_variable)
instance_variable_set("@#{resource_name}", records)
singular_resource_name = resource_name.to_s.singularize
instance_variable_set("@#{singular_resource_name}_count", model_class.count)
end

def load_single_submitter_resources_for_admin(submitter_id)
RESOURCE_NAMES.each do |resource_name|
records = helpers.send("find_#{resource_name}", submitter_id)
instance_variable_set("@#{resource_name}", records)
singular_resource_name = resource_name.to_s.singularize
instance_variable_set("@#{singular_resource_name}_count", records.count)
end
@submitter = helpers.find_submitter(submitter_id)
end

def load_submitter_resources
redirect_to publications_path if params[:id]
submitter_id = session[:submitter_id]

RESOURCE_NAMES.each do |resource_name|
instance_variable_set("@#{resource_name}", helpers.send("find_#{resource_name}", submitter_id))
end
@submitter = helpers.find_submitter(submitter_id)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,22 @@
end
end

context 'when there are multiple authors' do
context 'when there are two authors' do
let(:publication) { double(author_first_name: %w[John Jane], author_last_name: %w[Doe Roe]) }

it 'returns all authors in citation format' do
expect(helper.author_citation(publication)).to eq('Doe, John and Jane Roe')
end
end

context 'when there are more than two authors' do
let(:publication) { double(author_first_name: %w[John Jane Mary Jo], author_last_name: %w[Doe Roe Wallace Smith]) }

it 'returns all authors in citation format' do
expect(helper.author_citation(publication)).to eq('Doe, John, Roe, Jane, Wallace, Mary and Jo Smith')
end
end

context 'when publication is nil' do
it 'returns an empty string' do
expect(helper.author_citation(nil)).to eq('')
Expand Down