Skip to content

Commit c7a8b99

Browse files
committed
[E] Add order + subject scopes to journal_issue
1 parent 445e147 commit c7a8b99

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

api/app/controllers/concerns/validation.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ def project_filter_params
629629
def journal_issue_filter_params
630630
params.permit(
631631
filter: [:keyword, :order, :typeahead, :journal_id, :journal_volume_id,
632-
:volume_is_nil, :with_update_ability]
632+
:volume_is_nil, :with_update_ability, :subject]
633633
)[:filter]
634634
end
635635

api/app/models/journal.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,9 @@ class Journal < ApplicationRecord
9090
scope :by_subject, ->(subject = nil) {
9191
next all unless subject.present?
9292

93-
where(id: unscoped.joins(:journal_subjects)
94-
.merge(JournalSubject.by_subject(subject)).select(:journal_id))
93+
joins(:journal_subjects)
94+
.merge(JournalSubject.by_subject(subject))
95+
.distinct
9596
}
9697

9798
multisearches! :description_plaintext

api/app/models/journal_issue.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class JournalIssue < ApplicationRecord
2020
belongs_to :journal_volume, optional: true, counter_cache: true, inverse_of: :journal_issues
2121

2222
has_one :project, required: true, inverse_of: :journal_issue, dependent: :destroy
23+
has_many :project_subjects, through: :project
2324

2425
validates :number, presence: true
2526

@@ -63,6 +64,33 @@ class JournalIssue < ApplicationRecord
6364
includes(:project).where(id: Project.select(:journal_issue_id))
6465
end
6566

67+
scope :with_order, ->(by = nil) do
68+
case by
69+
when "updated_at ASC"
70+
reorder(updated_at: :asc)
71+
when "updated_at DESC"
72+
reorder(updated_at: :desc)
73+
when "created_at ASC"
74+
reorder(created_at: :asc)
75+
when "created_at DESC"
76+
reorder(created_at: :desc)
77+
when "sort_title ASC"
78+
reorder(sort_title: :asc)
79+
when "sort_title DESC"
80+
reorder(sort_title: :desc)
81+
else
82+
in_order
83+
end
84+
end
85+
86+
scope :by_subject, ->(subject = nil) {
87+
next all unless subject.present?
88+
89+
joins(:project_subjects)
90+
.merge(ProjectSubject.by_subject(subject))
91+
.distinct
92+
}
93+
6694
delegate :avatar, to: :project
6795
delegate :avatar_color, to: :project
6896
delegate :avatar_meta, to: :project

0 commit comments

Comments
 (0)