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

Issue 1318 #1351

Merged
merged 1 commit into from
Apr 6, 2018
Merged
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
15 changes: 9 additions & 6 deletions app/models/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,15 @@ class Template < ActiveRecord::Base
}
# Retrieves the maximum version for the array of customization_ofs passed. If customization_ofs is missing, every maximum
# version for each different customization_of will be retrieved
scope :customization_ofs_with_max_version, -> (customization_ofs=nil) {
if customization_ofs.is_a?(Array)
select("MAX(version) AS version", :customization_of).where(customization_of: customization_ofs).group(:customization_of)
else
select("MAX(version) AS version", :customization_of).group(:customization_of)
scope :customization_ofs_with_max_version, -> (customization_ofs=nil, org_id=nil) {
chained_scope = select("MAX(version) AS version", :customization_of)
if customization_ofs.respond_to?(:each)
chained_scope = chained_scope.where(customization_of: customization_ofs)
end
if org_id.present?
chained_scope = chained_scope.where(org_id: org_id)
end
chained_scope.group(:customization_of)
}
# Retrieves the latest template version, i.e. the one with maximum version for each dmptemplate_id
scope :latest_version, -> (dmptemplate_ids=nil) {
Expand All @@ -85,7 +88,7 @@ class Template < ActiveRecord::Base
}
# Retrieves the latest customized version, i.e. the one with maximum version for each customization_of=dmptemplate_id
scope :latest_customization, -> (org_id, dmptemplate_ids=nil) {
from(customization_ofs_with_max_version(dmptemplate_ids), :current)
from(customization_ofs_with_max_version(dmptemplate_ids, org_id), :current)
.joins("INNER JOIN templates ON current.version = templates.version"\
" AND current.customization_of = templates.customization_of")
.where('templates.org_id = ?', org_id)
Expand Down