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

Refactor Person methods #350

Merged
merged 1 commit into from
May 16, 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
36 changes: 18 additions & 18 deletions app/controllers/authors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,23 @@ def to_manual_toc
end
end

def show
@author = Person.find(params[:id])
@published_works = @author.original_works.count
@published_xlats = @author.translations.count
@total_orig_works = @author.manifestations(:author).count
@total_xlats = @author.manifestations(:translator).count
@aboutnesses = @author.aboutnesses

if @author.nil?
flash[:error] = t(:no_such_item)
redirect_to '/'
else
# TODO: add other types of content
@page_title = "#{t(:author_details)}: #{@author.name}"
end
end

def new
@person = Person.new(intellectual_property: :unknown)
@page_title = t(:new_author)
Expand Down Expand Up @@ -381,31 +398,14 @@ def create

def destroy
@author = Person.find(params[:id])
Chewy.strategy(:atomic) {
Chewy.strategy(:atomic) do
if @author.nil?
flash[:error] = t(:no_such_item)
redirect_to '/'
else
@author.destroy!
redirect_to action: :list
end
}
end

def show
@author = Person.find(params[:id])
@published_works = @author.original_works.count
@published_xlats = @author.translations.count
@total_orig_works = @author.original_work_count_including_unpublished
@total_xlats = @author.translations_count_including_unpublished
@aboutnesses = @author.aboutnesses

if @author.nil?
flash[:error] = t(:no_such_item)
redirect_to '/'
else
# TODO: add other types of content
@page_title = t(:author_details)+': '[email protected]
end
end

Expand Down
4 changes: 4 additions & 0 deletions app/models/concerns/record_with_involved_authorities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
module RecordWithInvolvedAuthorities
extend ActiveSupport::Concern

# Returns list of authorities involved into given object with the given role.
# Note: some roles can be specified both on Work and Expression level and this method will fetch only part of them.
# If you need a full list use {#Manifestation.involved_authorities_by_role}
# @param role [String/Symbol] role code
def involved_authorities_by_role(role)
role = role.to_s
raise "Unknown role #{role}" unless InvolvedAuthority.roles.keys.include?(role)
Expand Down
88 changes: 28 additions & 60 deletions app/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,26 @@ def works_since(since, maxitems)
end
end

def published_manifestations(role = nil)
# @param roles [String / Symbol] optional, if provided will only return Manifestations where person has
# one of the given roles.
# @return relation representing [Manifestation] objects current person is involved into.
def manifestations(*roles)
rel = involved_authorities
rel = rel.where(role: role) if role.present?
rel = rel.where(role: roles.to_a) if roles.present?
ids = rel.pluck(:work_id, :expression_id)

work_ids = ids.map(&:first).compact.uniq
expression_ids = ids.map(&:last).compact.uniq

Manifestation.all_published
.joins(:expression)
Manifestation.joins(:expression)
.where('expressions.work_id in (?) or expressions.id in (?)', work_ids, expression_ids)
end

# Works like {#manifestaions} method, but returns only published manifestations
def published_manifestations(*roles)
manifestations(*roles).all_published
end

def cached_works_count
Rails.cache.fetch("au_#{self.id}_work_count", expires_in: 24.hours) do
published_manifestations.count
Expand Down Expand Up @@ -221,49 +228,31 @@ def has_comment?
end

def all_genres
works_genres = original_works.select('works.genre').distinct.pluck(:genre)
translation_genres = translations.select('works.genre').distinct.pluck(:genre)
return (works_genres + translation_genres).uniq.sort
published_manifestations(:author, :translator).joins(expression: :work)
.select('works.genre')
.distinct
.pluck(:genre)
.sort
end

def all_languages
work_langs = original_works.pluck('works.orig_lang')
work_langs = original_works.joins(expression: :work).pluck('works.orig_lang')
#translation_langs = translations.pluck('works.orig_lang')
#all_languages = work_langs + translation_langs
#return all_languages.uniq
return work_langs.uniq
end

def original_works
Manifestation.all_published
.joins(expression: [work: :involved_authorities])
.where(involved_authorities: { person_id: id })
published_manifestations(:author)
end

def translations
Manifestation.all_published
.joins(expression: %i(work involved_authorities))
.merge(InvolvedAuthority.role_translator)
.where(involved_authorities: { person_id: id })
published_manifestations(:translator)
end

def all_works_including_unpublished
works = Manifestation.joins(expression: [work: :involved_authorities])
.merge(InvolvedAuthority.where(person_id: id))
xlats = Manifestation.joins(expression: :involved_authorities)
.merge(InvolvedAuthority.role_translator.where(person_id: id))
(works + xlats).uniq.sort_by(&:sort_title)
end

def original_work_count_including_unpublished
Manifestation.joins(expression: { work: :involved_authorities })
.merge(InvolvedAuthority.where(person_id: id)).count
end

def translations_count_including_unpublished
Manifestation.joins(expression: :involved_authorities)
.merge(InvolvedAuthority.role_translator.where(person_id: id))
.count
manifestations(:author, :translator).sort_by(&:sort_title)
end

def all_works_title_sorted
Expand All @@ -283,27 +272,17 @@ def all_works_by_title(term)
def title # convenience method for polymorphic handling (e.g. Taggable)
name
end

def original_works_by_genre
ret = {}
get_genres.map{|g| ret[g] = []}
Manifestation.all_published
.joins(expression: [work: :involved_authorities])
.includes(:expression)
.merge(InvolvedAuthority.where(person_id: id)).each do |m|
ret[m.expression.work.genre] << m
end
return ret
hash = published_manifestations(:author).preload(expression: :work)
.group_by { |m| m.expression.work.genre }
Work::GENRES.index_with { |genre| hash[genre] || [] }
end

def translations_by_genre
ret = {}
get_genres.map{|g| ret[g] = []}
Manifestation.all_published
.joins(expression: :involved_authorities)
.merge(InvolvedAuthority.role_translator.where(person_id: id)).each do |m|
ret[m.expression.work.genre] << m
end
return ret
hash = published_manifestations(:translator).preload(expression: :work)
.group_by { |m| m.expression.work.genre }
Work::GENRES.index_with { |genre| hash[genre] || [] }
end

def featured_work
Expand All @@ -313,18 +292,7 @@ def featured_work
end

def latest_stuff
latest_original_works = Manifestation.all_published
.joins(expression: [work: :involved_authorities])
.where(involved_authorities: { person_id: id })
.order(created_at: :desc).limit(20)

latest_translations = Manifestation.all_published
.joins(expression: [:involved_authorities])
.merge(InvolvedAuthority.role_translator)
.where(involved_authorities: { person_id: id })
.order(created_at: :desc).limit(20)

(latest_original_works + latest_translations).sort_by(&:created_at).reverse.first(20)
published_manifestations(:author, :translator).order(created_at: :desc).limit(20).to_a
end

def cached_latest_stuff
Expand Down
Loading
Loading