diff options
author | Go MAEDA <maeda@farend.jp> | 2018-10-06 16:03:26 +0000 |
---|---|---|
committer | Go MAEDA <maeda@farend.jp> | 2018-10-06 16:03:26 +0000 |
commit | e159928e6b81c02001411e7e886208533178c0ba (patch) | |
tree | 41c78911f17078c798e5fdc1362b7c18aff138d2 /app | |
parent | 323ef3182bc45184c7a14e3a8dc7244850baabed (diff) | |
download | redmine-e159928e6b81c02001411e7e886208533178c0ba.tar.gz redmine-e159928e6b81c02001411e7e886208533178c0ba.zip |
Use find_by instead of where.first to remove unnecessary sorting (#26747).
Patch by Yuichi HARADA.
git-svn-id: http://svn.redmine.org/redmine/trunk@17586 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app')
-rw-r--r-- | app/helpers/application_helper.rb | 6 | ||||
-rw-r--r-- | app/models/attachment.rb | 2 | ||||
-rw-r--r-- | app/models/principal.rb | 2 | ||||
-rw-r--r-- | app/models/repository.rb | 2 | ||||
-rw-r--r-- | app/models/repository/git.rb | 4 | ||||
-rw-r--r-- | app/models/repository/mercurial.rb | 2 | ||||
-rw-r--r-- | app/models/role.rb | 2 | ||||
-rw-r--r-- | app/models/token.rb | 2 | ||||
-rw-r--r-- | app/models/user.rb | 14 | ||||
-rw-r--r-- | app/models/wiki.rb | 2 |
10 files changed, 19 insertions, 19 deletions
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index d34a9c0ae..864a9d094 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -964,7 +964,7 @@ module ApplicationHelper link = link_to_project(p, {:only_path => only_path}, :class => 'project') end when 'user' - u = User.visible.where(:id => oid, :type => 'User').first + u = User.visible.find_by(:id => oid, :type => 'User') link = link_to_user(u, :only_path => only_path) if u end elsif sep == ':' @@ -1025,12 +1025,12 @@ module ApplicationHelper link = link_to_project(p, {:only_path => only_path}, :class => 'project') end when 'user' - u = User.visible.where("LOWER(login) = :s AND type = 'User'", :s => name.downcase).first + u = User.visible.find_by("LOWER(login) = :s AND type = 'User'", :s => name.downcase) link = link_to_user(u, :only_path => only_path) if u end elsif sep == "@" name = remove_double_quotes(identifier) - u = User.visible.where("LOWER(login) = :s AND type = 'User'", :s => name.downcase).first + u = User.visible.find_by("LOWER(login) = :s AND type = 'User'", :s => name.downcase) link = link_to_user(u, :only_path => only_path) if u end end diff --git a/app/models/attachment.rb b/app/models/attachment.rb index 6795f602e..cc9b5f930 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -276,7 +276,7 @@ class Attachment < ActiveRecord::Base def self.find_by_token(token) if token.to_s =~ /^(\d+)\.([0-9a-f]+)$/ attachment_id, attachment_digest = $1, $2 - attachment = Attachment.where(:id => attachment_id, :digest => attachment_digest).first + attachment = Attachment.find_by(:id => attachment_id, :digest => attachment_digest) if attachment && attachment.container.nil? attachment end diff --git a/app/models/principal.rb b/app/models/principal.rb index aac4e6b0d..08926d3a1 100644 --- a/app/models/principal.rb +++ b/app/models/principal.rb @@ -132,7 +132,7 @@ class Principal < ActiveRecord::Base end def visible?(user=User.current) - Principal.visible(user).where(:id => id).first == self + Principal.visible(user).find_by(:id => id) == self end # Returns true if the principal is a member of project diff --git a/app/models/repository.rb b/app/models/repository.rb index 235739057..256222f50 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -247,7 +247,7 @@ class Repository < ActiveRecord::Base return nil if name.blank? s = name.to_s if s.match(/^\d*$/) - changesets.where("revision = ?", s).first + changesets.find_by(:revision => s) else changesets.where("revision LIKE ?", s + '%').first end diff --git a/app/models/repository/git.rb b/app/models/repository/git.rb index 4cb16e524..e48d36490 100644 --- a/app/models/repository/git.rb +++ b/app/models/repository/git.rb @@ -45,7 +45,7 @@ class Repository::Git < Repository return false if v.nil? v.to_s != '0' end - + def report_last_commit=(arg) merge_extra_info "extra_report_last_commit" => arg end @@ -89,7 +89,7 @@ class Repository::Git < Repository def find_changeset_by_name(name) if name.present? - changesets.where(:revision => name.to_s).first || + changesets.find_by(:revision => name.to_s) || changesets.where('scmid LIKE ?', "#{name}%").first end end diff --git a/app/models/repository/mercurial.rb b/app/models/repository/mercurial.rb index 922893a6a..2a8079ca0 100644 --- a/app/models/repository/mercurial.rb +++ b/app/models/repository/mercurial.rb @@ -99,7 +99,7 @@ class Repository::Mercurial < Repository if /[^\d]/ =~ s or s.size > 8 cs = changesets.where(:scmid => s).first else - cs = changesets.where(:revision => s).first + cs = changesets.find_by(:revision => s) end return cs if cs changesets.where('scmid LIKE ?', "#{s}%").first diff --git a/app/models/role.rb b/app/models/role.rb index bf987e210..36ef4bc8b 100644 --- a/app/models/role.rb +++ b/app/models/role.rb @@ -293,7 +293,7 @@ private end def self.find_or_create_system_role(builtin, name) - role = unscoped.where(:builtin => builtin).first + role = unscoped.find_by(:builtin => builtin) if role.nil? role = unscoped.create(:name => name) do |r| r.builtin = builtin diff --git a/app/models/token.rb b/app/models/token.rb index 5990056f5..85d6abb9a 100644 --- a/app/models/token.rb +++ b/app/models/token.rb @@ -112,7 +112,7 @@ class Token < ActiveRecord::Base key = key.to_s return nil unless action.present? && key =~ /\A[a-z0-9]+\z/i - token = Token.where(:action => action, :value => key).first + token = Token.find_by(:action => action, :value => key) if token && (token.action == action) && (token.value == key) && token.user if validity_days.nil? || (token.created_on > validity_days.days.ago) token diff --git a/app/models/user.rb b/app/models/user.rb index 23d897649..c5061225c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -492,7 +492,7 @@ class User < Principal user = where(:login => login).detect {|u| u.login == login} unless user # Fail over to case-insensitive if none was found - user = where("LOWER(login) = ?", login.downcase).first + user = find_by("LOWER(login) = ?", login.downcase) end user end @@ -610,24 +610,24 @@ class User < Principal # eg. project.children.visible(user) Project.unscoped do return @project_ids_by_role if @project_ids_by_role - + group_class = anonymous? ? GroupAnonymous : GroupNonMember group_id = group_class.pluck(:id).first - + members = Member.joins(:project, :member_roles). where("#{Project.table_name}.status <> 9"). where("#{Member.table_name}.user_id = ? OR (#{Project.table_name}.is_public = ? AND #{Member.table_name}.user_id = ?)", self.id, true, group_id). pluck(:user_id, :role_id, :project_id) - + hash = {} members.each do |user_id, role_id, project_id| # Ignore the roles of the builtin group if the user is a member of the project next if user_id != id && project_ids.include?(project_id) - + hash[role_id] ||= [] hash[role_id] << project_id end - + result = Hash.new([]) if hash.present? roles = Role.where(:id => hash.keys).to_a @@ -798,7 +798,7 @@ class User < Principal # Returns the anonymous user. If the anonymous user does not exist, it is created. There can be only # one anonymous user per database. def self.anonymous - anonymous_user = AnonymousUser.unscoped.first + anonymous_user = AnonymousUser.unscoped.find_by(:lastname => 'Anonymous') if anonymous_user.nil? anonymous_user = AnonymousUser.unscoped.create(:lastname => 'Anonymous', :firstname => '', :login => '', :status => 0) raise 'Unable to create the anonymous user.' if anonymous_user.new_record? diff --git a/app/models/wiki.rb b/app/models/wiki.rb index 683fa24f3..186abdc74 100644 --- a/app/models/wiki.rb +++ b/app/models/wiki.rb @@ -53,7 +53,7 @@ class Wiki < ActiveRecord::Base @page_found_with_redirect = false title = start_page if title.blank? title = Wiki.titleize(title) - page = pages.where("LOWER(title) = LOWER(?)", title).first + page = pages.find_by("LOWER(title) = LOWER(?)", title) if page.nil? && options[:with_redirect] != false # search for a redirect redirect = redirects.where("LOWER(title) = LOWER(?)", title).first |