26747-where_first-to-find_by.patch

Yuichi HARADA, 2018-10-03 03:13

Download (11.2 KB)

View differences:

app/helpers/application_helper.rb
964 964
                link = link_to_project(p, {:only_path => only_path}, :class => 'project')
965 965
              end
966 966
            when 'user'
967
              u = User.visible.where(:id => oid, :type => 'User').first
967
              u = User.visible.find_by(:id => oid, :type => 'User')
968 968
              link = link_to_user(u, :only_path => only_path) if u
969 969
            end
970 970
          elsif sep == ':'
......
1025 1025
                link = link_to_project(p, {:only_path => only_path}, :class => 'project')
1026 1026
              end
1027 1027
            when 'user'
1028
              u = User.visible.where("LOWER(login) = :s AND type = 'User'", :s => name.downcase).first
1028
              u = User.visible.find_by("LOWER(login) = :s AND type = 'User'", :s => name.downcase)
1029 1029
              link = link_to_user(u, :only_path => only_path) if u
1030 1030
            end
1031 1031
          elsif sep == "@"
1032 1032
            name = remove_double_quotes(identifier)
1033
            u = User.visible.where("LOWER(login) = :s AND type = 'User'", :s => name.downcase).first
1033
            u = User.visible.find_by("LOWER(login) = :s AND type = 'User'", :s => name.downcase)
1034 1034
            link = link_to_user(u, :only_path => only_path) if u
1035 1035
          end
1036 1036
        end
app/models/attachment.rb
276 276
  def self.find_by_token(token)
277 277
    if token.to_s =~ /^(\d+)\.([0-9a-f]+)$/
278 278
      attachment_id, attachment_digest = $1, $2
279
      attachment = Attachment.where(:id => attachment_id, :digest => attachment_digest).first
279
      attachment = Attachment.find_by(:id => attachment_id, :digest => attachment_digest)
280 280
      if attachment && attachment.container.nil?
281 281
        attachment
282 282
      end
app/models/principal.rb
132 132
  end
133 133

  
134 134
  def visible?(user=User.current)
135
    Principal.visible(user).where(:id => id).first == self
135
    Principal.visible(user).find_by(:id => id) == self
136 136
  end
137 137

  
138 138
  # Returns true if the principal is a member of project
app/models/repository.rb
247 247
    return nil if name.blank?
248 248
    s = name.to_s
249 249
    if s.match(/^\d*$/)
250
      changesets.where("revision = ?", s).first
250
      changesets.find_by(:revision => s)
251 251
    else
252 252
      changesets.where("revision LIKE ?", s + '%').first
253 253
    end
app/models/repository/git.rb
45 45
    return false if v.nil?
46 46
    v.to_s != '0'
47 47
  end
48
 
48

  
49 49
  def report_last_commit=(arg)
50 50
    merge_extra_info "extra_report_last_commit" => arg
51 51
  end
......
89 89

  
90 90
  def find_changeset_by_name(name)
91 91
    if name.present?
92
      changesets.where(:revision => name.to_s).first ||
92
      changesets.find_by(:revision => name.to_s) ||
93 93
        changesets.where('scmid LIKE ?', "#{name}%").first
94 94
    end
95 95
  end
app/models/repository/mercurial.rb
99 99
    if /[^\d]/ =~ s or s.size > 8
100 100
      cs = changesets.where(:scmid => s).first
101 101
    else
102
      cs = changesets.where(:revision => s).first
102
      cs = changesets.find_by(:revision => s)
103 103
    end
104 104
    return cs if cs
105 105
    changesets.where('scmid LIKE ?', "#{s}%").first
app/models/role.rb
293 293
  end
294 294

  
295 295
  def self.find_or_create_system_role(builtin, name)
296
    role = unscoped.where(:builtin => builtin).first
296
    role = unscoped.find_by(:builtin => builtin)
297 297
    if role.nil?
298 298
      role = unscoped.create(:name => name) do |r|
299 299
        r.builtin = builtin
app/models/token.rb
112 112
    key = key.to_s
113 113
    return nil unless action.present? && key =~ /\A[a-z0-9]+\z/i
114 114

  
115
    token = Token.where(:action => action, :value => key).first
115
    token = Token.find_by(:action => action, :value => key)
116 116
    if token && (token.action == action) && (token.value == key) && token.user
117 117
      if validity_days.nil? || (token.created_on > validity_days.days.ago)
118 118
        token
app/models/user.rb
492 492
      user = where(:login => login).detect {|u| u.login == login}
493 493
      unless user
494 494
        # Fail over to case-insensitive if none was found
495
        user = where("LOWER(login) = ?", login.downcase).first
495
        user = find_by("LOWER(login) = ?", login.downcase)
496 496
      end
497 497
      user
498 498
    end
......
610 610
    # eg. project.children.visible(user)
611 611
    Project.unscoped do
612 612
      return @project_ids_by_role if @project_ids_by_role
613
  
613

  
614 614
      group_class = anonymous? ? GroupAnonymous : GroupNonMember
615 615
      group_id = group_class.pluck(:id).first
616
  
616

  
617 617
      members = Member.joins(:project, :member_roles).
618 618
        where("#{Project.table_name}.status <> 9").
619 619
        where("#{Member.table_name}.user_id = ? OR (#{Project.table_name}.is_public = ? AND #{Member.table_name}.user_id = ?)", self.id, true, group_id).
620 620
        pluck(:user_id, :role_id, :project_id)
621
  
621

  
622 622
      hash = {}
623 623
      members.each do |user_id, role_id, project_id|
624 624
        # Ignore the roles of the builtin group if the user is a member of the project
625 625
        next if user_id != id && project_ids.include?(project_id)
626
  
626

  
627 627
        hash[role_id] ||= []
628 628
        hash[role_id] << project_id
629 629
      end
630
  
630

  
631 631
      result = Hash.new([])
632 632
      if hash.present?
633 633
        roles = Role.where(:id => hash.keys).to_a
......
798 798
  # Returns the anonymous user.  If the anonymous user does not exist, it is created.  There can be only
799 799
  # one anonymous user per database.
800 800
  def self.anonymous
801
    anonymous_user = AnonymousUser.unscoped.first
801
    anonymous_user = AnonymousUser.unscoped.find_by(:lastname => 'Anonymous')
802 802
    if anonymous_user.nil?
803 803
      anonymous_user = AnonymousUser.unscoped.create(:lastname => 'Anonymous', :firstname => '', :login => '', :status => 0)
804 804
      raise 'Unable to create the anonymous user.' if anonymous_user.new_record?
app/models/wiki.rb
53 53
    @page_found_with_redirect = false
54 54
    title = start_page if title.blank?
55 55
    title = Wiki.titleize(title)
56
    page = pages.where("LOWER(title) = LOWER(?)", title).first
56
    page = pages.find_by("LOWER(title) = LOWER(?)", title)
57 57
    if page.nil? && options[:with_redirect] != false
58 58
      # search for a redirect
59 59
      redirect = redirects.where("LOWER(title) = LOWER(?)", title).first
lib/redmine/field_format.rb
155 155
      def target_class
156 156
        nil
157 157
      end
158
 
158

  
159 159
      def possible_custom_value_options(custom_value)
160 160
        possible_values_options(custom_value.custom_field, custom_value.customized)
161 161
      end
......
625 625
          value ||= label
626 626
          checked = (custom_value.value.is_a?(Array) && custom_value.value.include?(value)) || custom_value.value.to_s == value
627 627
          tag = view.send(tag_method, tag_name, value, checked, :id => nil)
628
          s << view.content_tag('label', tag + ' ' + label) 
628
          s << view.content_tag('label', tag + ' ' + label)
629 629
        end
630 630
        if custom_value.custom_field.multiple?
631 631
          s << view.hidden_field_tag(tag_name, '', :id => nil)
......
730 730
      def reset_target_class
731 731
        @target_class = nil
732 732
      end
733
 
733

  
734 734
      def possible_custom_value_options(custom_value)
735 735
        options = possible_values_options(custom_value.custom_field, custom_value.customized)
736 736
        missing = [custom_value.value_was].flatten.reject(&:blank?) - options.map(&:last)
......
776 776
    class EnumerationFormat < RecordList
777 777
      add 'enumeration'
778 778
      self.form_partial = 'custom_fields/formats/enumeration'
779
 
779

  
780 780
      def label
781 781
        "label_field_format_enumeration"
782 782
      end
......
964 964
          end
965 965
        else
966 966
          if custom_value.value.present?
967
            attachment = Attachment.where(:id => custom_value.value.to_s).first
967
            attachment = Attachment.find_by(:id => custom_value.value.to_s)
968 968
            extensions = custom_value.custom_field.extensions_allowed
969 969
            if attachment && extensions.present? && !attachment.extension_in?(extensions)
970 970
              errors << "#{::I18n.t('activerecord.errors.messages.invalid')} (#{l(:setting_attachment_extensions_allowed)}: #{extensions})"
......
978 978
      def after_save_custom_value(custom_field, custom_value)
979 979
        if custom_value.saved_change_to_value?
980 980
          if custom_value.value.present?
981
            attachment = Attachment.where(:id => custom_value.value.to_s).first
981
            attachment = Attachment.find_by(:id => custom_value.value.to_s)
982 982
            if attachment
983 983
              attachment.container = custom_value
984 984
              attachment.save!
985 985
            end
986 986
          end
987 987
          if custom_value.value_before_last_save.present?
988
            attachment = Attachment.where(:id => custom_value.value_before_last_save.to_s).first
988
            attachment = Attachment.find_by(:id => custom_value.value_before_last_save.to_s)
989 989
            if attachment
990 990
              attachment.destroy
991 991
            end
lib/tasks/migrate_from_trac.rake
453 453
        puts
454 454

  
455 455
        # Trac 'resolution' field as a Redmine custom field
456
        r = IssueCustomField.where(:name => "Resolution").first
456
        r = IssueCustomField.find_by(:name => "Resolution")
457 457
        r = IssueCustomField.new(:name => 'Resolution',
458 458
                                 :field_format => 'list',
459 459
                                 :is_filter => true) if r.nil?