Migrate model scopes to use lambda/block

Signed-off-by: Alex Coles <alex@alexbcoles.com>
pull/3194/head
Alex Coles 9 years ago
parent 51512f1522
commit 5557f58e80
  1. 6
      app/models/changeset.rb
  2. 2
      app/models/enumeration.rb
  3. 2
      app/models/journal.rb
  4. 4
      app/models/legacy_journal.rb
  5. 8
      app/models/menu_items/wiki_menu_item.rb
  6. 6
      app/models/message.rb
  7. 13
      app/models/news.rb
  8. 2
      app/models/planning_element_type_color.rb
  9. 6
      app/models/principal.rb
  10. 14
      app/models/project.rb
  11. 4
      app/models/project_association.rb
  12. 2
      app/models/project_type.rb
  13. 9
      app/models/role.rb
  14. 2
      app/models/status.rb
  15. 9
      app/models/time_entry.rb
  16. 2
      app/models/timeline.rb
  17. 8
      app/models/type.rb
  18. 19
      app/models/user.rb
  19. 11
      app/models/wiki_page.rb
  20. 2
      app/models/work_package_custom_field.rb

@ -52,9 +52,9 @@ class Changeset < ActiveRecord::Base
validates_uniqueness_of :revision, scope: :repository_id
validates_uniqueness_of :scmid, scope: :repository_id, allow_nil: true
scope :visible, lambda {|*args|
{ include: { repository: :project },
conditions: Project.allowed_to_condition(args.first || User.current, :view_changesets) }
scope :visible, -> (*args) {
includes(repository: :project)
.where(Project.allowed_to_condition(args.first || User.current, :view_changesets))
}
def revision=(r)

@ -30,7 +30,7 @@
class Enumeration < ActiveRecord::Base
include ActiveModel::ForbiddenAttributesProtection
default_scope order: "#{Enumeration.table_name}.position ASC"
default_scope { order("#{Enumeration.table_name}.position ASC") }
belongs_to :project

@ -53,7 +53,7 @@ class Journal < ActiveRecord::Base
# Scopes to all journals excluding the initial journal - useful for change
# logs like the history on issue#show
scope 'changing', conditions: ['version > 1']
scope :changing, -> { where(['version > 1']) }
def changed_data=(changed_attributes)
attributes = changed_attributes

@ -63,7 +63,9 @@ class LegacyJournal < ActiveRecord::Base
# Scopes to all journals excluding the initial journal - useful for change
# logs like the history on issue#show
scope 'changing', conditions: ['version > 1']
scope :changing, -> {
where(['version > 1'])
}
# let all child classes have Journal as it's model name
# used to not having to create another route for every subclass of Journal

@ -30,10 +30,10 @@
class MenuItems::WikiMenuItem < MenuItem
belongs_to :wiki, foreign_key: 'navigatable_id'
scope :main_items, lambda { |wiki_id|
{ conditions: { navigatable_id: wiki_id, parent_id: nil },
include: :children,
order: 'id ASC' }
scope :main_items, -> (wiki_id) {
where(navigatable_id: wiki_id, parent_id: nil)
.includes(:children)
.order('id ASC')
}
def item_class

@ -70,9 +70,9 @@ class Message < ActiveRecord::Base
after_update :update_ancestors
after_destroy :reset_counters
scope :visible, lambda {|*args|
{ include: { board: :project },
conditions: Project.allowed_to_condition(args.first || User.current, :view_messages) }
scope :visible, -> (*args) {
includes(board: :project)
.where(Project.allowed_to_condition(args.first || User.current, :view_messages))
}
safe_attributes 'subject', 'content', 'board_id'

@ -31,7 +31,9 @@ class News < ActiveRecord::Base
include Redmine::SafeAttributes
belongs_to :project
belongs_to :author, class_name: 'User', foreign_key: 'author_id'
has_many :comments, as: :commented, dependent: :delete_all, order: 'created_on'
has_many :comments, -> {
order('created_on')
}, as: :commented, dependent: :delete_all
attr_protected :project_id, :author_id
@ -50,11 +52,10 @@ class News < ActiveRecord::Base
after_create :add_author_as_watcher
scope :visible, lambda {|*args|
{
include: :project,
conditions: Project.allowed_to_condition(args.first || User.current, :view_news)
}}
scope :visible, -> (*args) {
includes(:project)
.where(Project.allowed_to_condition(args.first || User.current, :view_news))
}
safe_attributes 'title', 'summary', 'description'

@ -33,7 +33,7 @@ class PlanningElementTypeColor < ActiveRecord::Base
self.table_name = 'planning_element_type_colors'
acts_as_list
default_scope order: 'position ASC'
default_scope { order('position ASC') }
has_many :planning_element_types, class_name: 'Type',
foreign_key: 'color_id',

@ -67,7 +67,7 @@ class Principal < ActiveRecord::Base
where("id NOT IN (select m.user_id FROM members as m where m.project_id = #{project.id})")
}
scope :like, lambda { |q|
scope :like, -> (q) {
firstnamelastname = "((firstname || ' ') || lastname)"
lastnamefirstname = "((lastname || ' ') || firstname)"
@ -87,7 +87,9 @@ class Principal < ActiveRecord::Base
.order(:type, :login, :lastname, :firstname, :mail)
}
scope :visible_by, lambda { |principal| Principal.visible_by_condition(principal) }
scope :visible_by, -> (principal) {
Principal.visible_by_condition(principal)
}
before_create :set_default_empty_values

@ -119,12 +119,14 @@ class Project < ActiveRecord::Base
# ORDER BY "projects"."id" ASC LIMIT 1
# this results in the following genre of errors on PostgreSQL:
# …it is not contained in either an aggregate function or the GROUP BY clause
default_scope order('')
scope :has_module, lambda { |mod| { conditions: ["#{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name=?)", mod.to_s] } }
scope :active, lambda { |*_args| where(status: STATUS_ACTIVE) }
scope :public, lambda { |*_args| where(is_public: true) }
scope :visible, ->(user = User.current) { { conditions: Project.visible_by(user) } }
default_scope { order('') }
scope :has_module, ->(mod) {
where(["#{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name=?)", mod.to_s])
}
scope :active, -> { where(status: STATUS_ACTIVE) }
scope :public, -> { where(is_public: true) }
scope :visible, ->(user = User.current) { where(Project.visible_by(user)) }
# timelines stuff

@ -44,11 +44,11 @@ class ProjectAssociation < ActiveRecord::Base
validate :validate,
:validate_projects_not_identical
scope :with_projects, lambda { |projects|
scope :with_projects, -> (projects) {
projects = [projects] unless projects.is_a? Array
project_ids = projects.first.respond_to?(:id) ? projects.map(&:id).join(',') : projects
{ conditions: ["#{table_name}.project_a_id in (?) or #{table_name}.project_b_id in (?)", project_ids, project_ids] }
where(["#{table_name}.project_a_id in (?) or #{table_name}.project_b_id in (?)", project_ids, project_ids])
}
def projects

@ -35,7 +35,7 @@ class ProjectType < ActiveRecord::Base
self.table_name = 'project_types'
acts_as_list
default_scope order: 'position ASC'
default_scope { order('position ASC') }
has_many :projects, class_name: 'Project',
foreign_key: 'project_type_id'

@ -35,10 +35,13 @@ class Role < ActiveRecord::Base
BUILTIN_NON_MEMBER = 1
BUILTIN_ANONYMOUS = 2
scope :givable, conditions: 'builtin = 0', order: 'position'
scope :builtin, lambda { |*args|
scope :givable, -> {
where('builtin = 0')
.order('position')
}
scope :builtin, -> (*args) {
compare = 'not' if args.first == true
{ conditions: "#{compare} builtin = 0" }
where("#{compare} builtin = 0")
}
before_destroy :check_deletable

@ -31,7 +31,7 @@ class Status < ActiveRecord::Base
include ActiveModel::ForbiddenAttributesProtection
extend Pagination::Model
default_scope order('position ASC')
default_scope { order('position ASC') }
before_destroy :check_integrity
has_many :workflows, foreign_key: 'old_status_id'
acts_as_list

@ -56,11 +56,10 @@ class TimeEntry < ActiveRecord::Base
validate :validate_project_is_set
validate :validate_consistency_of_work_package_id
scope :visible, lambda {|*args|
{
include: :project,
conditions: Project.allowed_to_condition(args.first || User.current, :view_time_entries)
}}
scope :visible, -> (*args) {
includes(:project)
.where(Project.allowed_to_condition(args.first || User.current, :view_time_entries))
}
scope :on_work_packages, ->(work_packages) { where(work_package_id: work_packages) }

@ -46,7 +46,7 @@ class Timeline < ActiveRecord::Base
self.table_name = 'timelines'
default_scope order: 'name ASC'
default_scope { order('name ASC') }
belongs_to :project, class_name: 'Project'

@ -61,10 +61,12 @@ class ::Type < ActiveRecord::Base
validates_inclusion_of :in_aggregation, :is_default, :is_milestone, in: [true, false]
default_scope order: 'position ASC'
default_scope { order('position ASC') }
scope :without_standard, conditions: { is_standard: false },
order: :position
scope :without_standard, -> {
where(is_standard: false)
.order(:position)
}
def to_s; name end

@ -98,13 +98,14 @@ class User < Principal
belongs_to :auth_source
# Active non-anonymous users scope
scope :not_builtin,
conditions: "#{User.table_name}.status <> #{STATUSES[:builtin]}"
scope :not_builtin, -> {
where("#{User.table_name}.status <> #{STATUSES[:builtin]}")
}
# Users blocked via brute force prevention
# use lambda here, so time is evaluated on each query
scope :blocked, lambda { create_blocked_scope(true) }
scope :not_blocked, lambda { create_blocked_scope(false) }
scope :blocked, -> { create_blocked_scope(true) }
scope :not_blocked, -> { create_blocked_scope(false) }
def self.create_blocked_scope(blocked)
block_duration = Setting.brute_force_block_minutes.to_i.minutes
@ -145,15 +146,15 @@ class User < Principal
before_destroy :reassign_associated
before_destroy :remove_from_filter
scope :in_group, lambda {|group|
scope :in_group, -> (group) {
group_id = group.is_a?(Group) ? group.id : group.to_i
{ conditions: ["#{User.table_name}.id IN (SELECT gu.user_id FROM #{table_name_prefix}group_users#{table_name_suffix} gu WHERE gu.group_id = ?)", group_id] }
where(["#{User.table_name}.id IN (SELECT gu.user_id FROM #{table_name_prefix}group_users#{table_name_suffix} gu WHERE gu.group_id = ?)", group_id])
}
scope :not_in_group, lambda {|group|
scope :not_in_group, -> (group) {
group_id = group.is_a?(Group) ? group.id : group.to_i
{ conditions: ["#{User.table_name}.id NOT IN (SELECT gu.user_id FROM #{table_name_prefix}group_users#{table_name_suffix} gu WHERE gu.group_id = ?)", group_id] }
where(["#{User.table_name}.id NOT IN (SELECT gu.user_id FROM #{table_name_prefix}group_users#{table_name_suffix} gu WHERE gu.group_id = ?)", group_id])
}
scope :admin, conditions: { admin: true }
scope :admin, -> { where(admin: true) }
def sanitize_mail_notification_setting
self.mail_notification = Setting.default_notification_option if mail_notification.blank?

@ -64,12 +64,13 @@ class WikiPage < ActiveRecord::Base
before_destroy :remove_redirects
# eager load information about last updates, without loading text
scope :with_updated_on,
select: "#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on",
joins: "LEFT JOIN #{WikiContent.table_name} ON #{WikiContent.table_name}.page_id = #{WikiPage.table_name}.id"
scope :with_updated_on, -> {
select("#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on")
.joins("LEFT JOIN #{WikiContent.table_name} ON #{WikiContent.table_name}.page_id = #{WikiPage.table_name}.id")
}
scope :main_pages, lambda {|wiki_id|
{ conditions: { wiki_id: wiki_id, parent_id: nil } }
scope :main_pages, -> (wiki_id) {
where(wiki_id: wiki_id, parent_id: nil)
}
# Wiki pages that are protected by default

@ -32,7 +32,7 @@ class WorkPackageCustomField < CustomField
has_and_belongs_to_many :types, join_table: "#{table_name_prefix}custom_fields_types#{table_name_suffix}", foreign_key: 'custom_field_id'
has_many :work_packages, through: :work_package_custom_values
scope :visible_by_user, lambda { |user|
scope :visible_by_user, -> (user) {
unless user.admin?
joins('LEFT OUTER JOIN custom_fields_projects AS cfp ON (custom_fields.id = cfp.custom_field_id) ' \
'LEFT OUTER JOIN projects AS p ON (cfp.project_id = p.id) ' \

Loading…
Cancel
Save