Patch #44169 » 0002-Refactor-query-preloads-and-controller-finding-logic.patch
| app/controllers/application_controller.rb | ||
|---|---|---|
|
# Find issues with a single :id param or :ids array param
|
||
|
# Raises a Unauthorized exception if one of the issues is not visible
|
||
|
def find_issues
|
||
|
@issues = Issue.
|
||
|
where(:id => (params[:id] || params[:ids])).
|
||
|
preload(:project, :status, :tracker, :priority,
|
||
|
:author, :assigned_to, :relations_to,
|
||
|
{:custom_values => :custom_field}).
|
||
|
to_a
|
||
|
@issues = Issue.find_with_preloads(params[:id] || params[:ids])
|
||
|
raise ActiveRecord::RecordNotFound if @issues.empty?
|
||
|
raise Unauthorized unless @issues.all?(&:visible?)
|
||
|
@projects = @issues.filter_map(&:project).uniq
|
||
|
@project = @projects.first if @projects.size == 1
|
||
|
find_project_from_items(@issues)
|
||
|
rescue ActiveRecord::RecordNotFound
|
||
|
render_404
|
||
|
end
|
||
|
# Helper method to find unique projects and single project from items list
|
||
|
def find_project_from_items(items)
|
||
|
@projects = items.filter_map(&:project).uniq
|
||
|
@project = @projects.first if @projects.size == 1
|
||
|
end
|
||
|
def find_attachments
|
||
|
if (attachments = params[:attachments]).present?
|
||
|
att = attachments.values.collect do |attachment|
|
||
| app/controllers/context_menus/time_entries_controller.rb | ||
|---|---|---|
|
private
|
||
|
def find_time_entries
|
||
|
@time_entries = TimeEntry.where(:id => params[:ids]).
|
||
|
preload(:project => :time_entry_activities).
|
||
|
preload(:user).to_a
|
||
|
@time_entries = TimeEntry.find_with_preloads(params[:ids])
|
||
|
if @time_entries.blank? || !@time_entries.all?(&:visible?)
|
||
|
render_404;
|
||
|
render_404
|
||
|
return
|
||
|
end
|
||
| ... | ... | |
|
@time_entry = @time_entries.first
|
||
|
end
|
||
|
@projects = @time_entries.filter_map(&:project).uniq
|
||
|
@project = @projects.first if @projects.size == 1
|
||
|
find_project_from_items(@time_entries)
|
||
|
end
|
||
|
end
|
||
|
end
|
||
| app/controllers/timelog_controller.rb | ||
|---|---|---|
|
end
|
||
|
def find_time_entries
|
||
|
@time_entries = TimeEntry.where(:id => params[:id] || params[:ids]).
|
||
|
preload(:project => :time_entry_activities).
|
||
|
preload(:user).to_a
|
||
|
@time_entries = TimeEntry.find_with_preloads(params[:id] || params[:ids])
|
||
|
raise ActiveRecord::RecordNotFound if @time_entries.empty?
|
||
|
raise Unauthorized unless @time_entries.all? {|t| t.editable_by?(User.current)}
|
||
|
@projects = @time_entries.filter_map(&:project).uniq
|
||
|
@project = @projects.first if @projects.size == 1
|
||
|
find_project_from_items(@time_entries)
|
||
|
rescue ActiveRecord::RecordNotFound
|
||
|
render_404
|
||
|
end
|
||
|
def find_optional_issue
|
||
|
if params[:issue_id].present?
|
||
|
@issue = Issue.find(params[:issue_id])
|
||
| app/models/issue.rb | ||
|---|---|---|
|
end
|
||
|
end
|
||
|
# Returns issues found by IDs with preloaded associations
|
||
|
def self.find_with_preloads(ids)
|
||
|
where(:id => ids).
|
||
|
preload(:project, :status, :tracker, :priority,
|
||
|
:author, :assigned_to, :relations_to,
|
||
|
{:custom_values => :custom_field}).
|
||
|
to_a
|
||
|
end
|
||
|
# Returns true if usr or current user is allowed to view the issue
|
||
|
def visible?(usr=nil)
|
||
|
(usr || User.current).allowed_to?(:view_issues, self.project) do |role, user|
|
||
| app/models/time_entry.rb | ||
|---|---|---|
|
end
|
||
|
end
|
||
|
# Returns time entries found by IDs with preloaded associations
|
||
|
def self.find_with_preloads(ids)
|
||
|
where(:id => ids).
|
||
|
preload(:project => :time_entry_activities).
|
||
|
preload(:user).to_a
|
||
|
end
|
||
|
# Returns true if user or current user is allowed to view the time entry
|
||
|
def visible?(user=nil)
|
||
|
(user || User.current).allowed_to?(:view_time_entries, self.project) do |role, user|
|
||