[#345] remove deprecated dynamic finder methods

This commit is contained in:
Alexander Machehin 2014-03-18 19:58:51 +06:00
parent 62c290dd0d
commit 2ee3ea93bd
31 changed files with 58 additions and 59 deletions

View File

@ -87,6 +87,6 @@ class Admin::UsersController < Admin::BaseController
protected
def find_user
@user = User.find_by_uname!(params[:id]) if params[:id]
@user = User.find_by!(uname: params[:id]) if params[:id].present?
end
end

View File

@ -78,8 +78,8 @@ class ApplicationController < ActionController::Base
return current_user
end
else
params['user_id'] && User.find_by_id(params['user_id']) ||
params['group_id'] && Group.find_by_id(params['group_id']) || current_user
params['user_id'] && User.find(params['user_id']) ||
params['group_id'] && Group.find(params['group_id']) || current_user
end
end

View File

@ -7,7 +7,7 @@ class Groups::MembersController < Groups::BaseController
def update
params['user'].keys.each do |user_id|
role = params['user'][user_id]
if relation = @group.actors.where(actor_id: user_id, actor_type: 'User') #find_by_actor_id_and_actor_type(user_id, 'User')
if relation = @group.actors.where(actor_id: user_id, actor_type: 'User')
relation.update_all(role: role) if @group.owner.id.to_s != user_id
else
relation = @group.actors.build(actor_id: user_id, actor_type: 'User', role: role)
@ -34,7 +34,7 @@ class Groups::MembersController < Groups::BaseController
end
def add
@user = User.find_by_uname(params[:user_uname])
@user = User.find_by uname: params[:user_uname]
if !@user
flash[:error] = t("flash.collaborators.wrong_user", uname: params[:user_uname])
elsif @group.add_member(@user, params[:role])

View File

@ -47,7 +47,7 @@ class Projects::CommentsController < Projects::BaseController
protected
def find_commentable
@commentable = params[:issue_id].present? && @project.issues.find_by_serial_id(params[:issue_id]) ||
@commentable = params[:issue_id].present? && @project.issues.find_by(serial_id: params[:issue_id]) ||
params[:commit_id].present? && @project.repo.commit(params[:commit_id])
end

View File

@ -45,7 +45,7 @@ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
else
raise 'Provider #{provider} not handled'
end
user = User.find_or_initialize_by_email(auth['info']['email'])
user = User.find_or_initialize_by email: auth['info']['email']
if user.new_record?
user.name = name
user.uname = name.gsub(/\s/, '').underscore

View File

@ -420,7 +420,7 @@ class BuildList < ActiveRecord::Base
end
def set_packages(pkg_hash, project_name)
prj = Project.joins(repositories: :platform).where('platforms.id = ?', save_to_platform.id).find_by_name!(project_name)
prj = Project.joins(repositories: :platform).where('platforms.id = ?', save_to_platform.id).find_by!(name: project_name)
build_package(pkg_hash['srpm'], 'source', prj) {|p| p.save!}
pkg_hash['rpm'].each do |rpm_hash|
build_package(rpm_hash, 'binary', prj) {|p| p.save!}

View File

@ -200,7 +200,7 @@ class Comment < ActiveRecord::Base
commentable.subscribes.create(user: user) if !commentable.subscribes.exists?(user_id: user.id)
elsif commit_comment?
recipients = project.admins
recipients << user << User.find_by_email(commentable.try(:committer).try(:email)) # commentor and committer
recipients << user << User.find_by(email: commentable.try(:committer).try(:email)) # commentor and committer
recipients.compact.uniq.each do |user|
options = {project_id: project.id, subscribeable_id: commentable_id, subscribeable_type: commentable.class.name, user_id: user.id}
Subscribe.subscribe_to_commit(options) if Subscribe.subscribed_to_commit?(project, user, commentable)

View File

@ -27,7 +27,7 @@ module ActsLikeMember
module ClassMethods
def find_by_insensitive_uname(uname)
find_by_uname(uname) || by_uname(uname).first
find_by(uname: uname) || by_uname(uname).first
end
def find_by_insensitive_uname!(uname)

View File

@ -21,7 +21,7 @@ module BuildListObserver
if status == self.class::SUCCESS
# Update project average build time
begin
statistic = project.project_statistics.find_or_create_by_arch_id(arch_id)
statistic = project.project_statistics.find_or_create_by arch_id: arch_id
rescue ActiveRecord::RecordNotUnique
retry
end

View File

@ -68,7 +68,7 @@ module Feed::Comment
end
def can_notify_on_new_comment?(subscribe)
notifier = SettingsNotifier.find_by_user_id(subscribe.user_id)
notifier = SettingsNotifier.find_by user_id: subscribe.user_id
notifier && notifier.new_comment && notifier.can_notify
end
end

View File

@ -49,7 +49,7 @@ module Feed::Git
end
when 'Hash' # 'Gollum::Committer'
actor = User.find_by_uname! record[:actor_name]
actor = User.find_by! uname: record[:actor_name]
project = Project.find record[:project_id]
project.admins.each do |recipient|

View File

@ -14,7 +14,7 @@ module FileStoreClean
def destroy_files_from_file_store(args = sha1_of_file_store_files)
files = *args
token = User.find_by_uname('file_store').authentication_token
token = User.find_by(uname: 'file_store').authentication_token
uri = URI APP_CONFIG['file_store_url']
Net::HTTP.start(uri.host, uri.port) do |http|
files.each do |sha1|

View File

@ -106,7 +106,7 @@ module Git
end
def import_srpm(srpm_path = srpm.path, branch_name = 'import')
token = User.find_by_uname('rosa_system').authentication_token
token = User.find_by(uname: 'rosa_system').authentication_token
opts = [srpm_path, path, branch_name, Rails.root.join('bin', 'file-store.rb'), token, APP_CONFIG['file_store_url']].join(' ')
system("#{Rails.root.join('bin', 'import_srpm.sh')} #{opts} >> /dev/null 2>&1")
end

View File

@ -71,13 +71,13 @@ class GitHook
def find_user(user)
if user.blank?
# Local push
User.find_by_email(project.repo.commit(newrev).author.email) rescue nil
User.find_by(email: project.repo.commit(newrev).author.email) rescue nil
elsif user =~ /\Auser-\d+\Z/
# git push over http
User.find(user.gsub('user-', ''))
elsif user =~ /\Akey-\d+\Z/
# git push over ssh
SshKey.find_by_id(user.gsub('key-', '')).try(:user)
SshKey.find(user.gsub('key-', '')).try(:user)
end
end
end

View File

@ -41,7 +41,7 @@ class MassBuild < ActiveRecord::Base
next if name.blank?
name.chomp!; name.strip!
if project = Project.joins(:repositories).where('repositories.id in (?)', save_to_platform.repository_ids).find_by_name(name)
if project = Project.joins(:repositories).where('repositories.id in (?)', save_to_platform.repository_ids).find_by(name: name)
begin
return if self.reload.stop_build
increase_rt = increase_release_tag?

View File

@ -223,13 +223,13 @@ class Platform < ActiveRecord::Base
end
Rails.cache.fetch([platform_name, token, :platform_allowed], expires_in: 2.minutes) do
platform = Platform.find_by_name platform_name
platform = Platform.find_by name: platform_name
next false unless platform
next true unless platform.hidden?
next false unless token
next true if platform.tokens.by_active.where(authentication_token: token).exists?
user = User.find_by_authentication_token token
user = User.find_by authentication_token: token
current_ability = Ability.new(user)
if user && current_ability.can?(:show, platform)
true

View File

@ -21,7 +21,7 @@ class PlatformContent
bfp_name = @path.match(/\/#{@platform.name}\/repository\/[\w]+\//)
return nil unless bfp_name
bfp_name = bfp_name[0].gsub(/\/#{@platform.name}\/repository\//, '').gsub('/', '')
build_for_platform = Platform.main.find_by_name bfp_name
build_for_platform = Platform.main.find_by name: bfp_name
return nil unless build_for_platform
end

View File

@ -40,7 +40,7 @@ class ProductBuildList < ActiveRecord::Base
belongs_to :user
# see: Issue #6
before_validation -> { self.arch_id = Arch.find_by_name('x86_64').id }, on: :create
before_validation -> { self.arch_id = Arch.find_by(name: 'x86_64').id }, on: :create
# field "not_delete" can be changed only if build has been completed
before_validation -> { self.not_delete = false unless build_completed?; true }
validates :product_id,

View File

@ -165,7 +165,7 @@ class Project < ActiveRecord::Base
#path #share by NFS
end
def build_for(mass_build, repository_id, arch = Arch.find_by_name('i586'), priority = 0, increase_rt = false)
def build_for(mass_build, repository_id, arch = Arch.find_by(name: 'i586'), priority = 0, increase_rt = false)
build_for_platform = mass_build.build_for_platform
save_to_platform = mass_build.save_to_platform
user = mass_build.user
@ -245,7 +245,7 @@ class Project < ActiveRecord::Base
archive = archive_by_treeish_and_format tag.name, format
sha1 = Digest::SHA1.file(archive[:path]).hexdigest
unless FileStoreClean.file_exist_on_file_store? sha1
token = User.find_by_uname('rosa_system').authentication_token
token = User.find_by(uname: 'rosa_system').authentication_token
begin
resp = JSON `curl --user #{token}: -POST -F 'file_store[file]=@#{archive[:path]};filename=#{name}-#{tag.name}.#{tag_file_format(format)}' #{APP_CONFIG['file_store_url']}/api/v1/upload`
rescue # Dont care about it

View File

@ -35,13 +35,13 @@ class Repository < ActiveRecord::Base
def regenerate(build_for_platform_id = nil)
build_for_platform = Platform.main.find build_for_platform_id if platform.personal?
status = repository_statuses.find_or_create_by_platform_id(build_for_platform.try(:id) || platform_id)
status = repository_statuses.find_or_create_by(platform_id: build_for_platform.try(:id) || platform_id)
status.regenerate
end
def resign
if platform.main?
status = repository_statuses.find_or_create_by_platform_id(platform_id)
status = repository_statuses.find_or_create_by(platform_id: platform_id)
status.resign
end
end

View File

@ -18,7 +18,7 @@
%tr{class: cycle("odd", "even")}
%td= check_box_tag 'request_ids[]', request.id
%td= request.name
- @user = User.find_by_email(request.email) if request.approved
- @user = User.find_by(email: request.email) if request.approved
%td= link_to_if @user, request.email, @user
%td= request.interest
%td= request.more

View File

@ -18,7 +18,7 @@ namespace :add_branch do
dst_branch = ENV['DST_BRANCH']
group = ENV['GROUP']
say "START add branch #{dst_branch} from #{src_branch} in #{group} group"
Group.find_by_uname(group).projects.find_each do |p|
Group.find_by(uname: group).projects.find_each do |p|
next if p.repo.branches.map(&:name).include?(dst_branch)
next if p.repo.branches.map(&:name).exclude?(src_branch)
say "===== Process #{p.name} project"
@ -32,7 +32,7 @@ namespace :add_branch do
src_branch = ENV['SRC_BRANCH'] || 'import_mandriva2011'
dst_branch = ENV['DST_BRANCH'] || 'rosa2012lts'
say "START add branch #{dst_branch} from #{src_branch}"
Platform.find_by_name(dst_branch).repositories.each do |r|
Platform.find_by(name: dst_branch).repositories.each do |r|
say "=== Process #{r.name} repo"
r.projects.find_each do |p|
next if p.repo.branches.map(&:name).include?(dst_branch)
@ -46,9 +46,9 @@ namespace :add_branch do
desc "Add branch for owner projects by list"
task list: :environment do
source = ENV['SOURCE'] || 'https://dl.dropbox.com/u/984976/texlive.txt'
owner = User.find_by_uname(ENV['OWNER']) || Group.find_by_uname!(ENV['OWNER'] || 'import')
platform = Platform.find_by_name!(ENV['PLATFORM'] || 'rosa2012.1')
repo = platform.repositories.find_by_name!(ENV['REPO'] || 'main')
owner = User.find_by(uname: ENV['OWNER']) || Group.find_by!(uname: ENV['OWNER'] || 'import')
platform = Platform.find_by!(name: ENV['PLATFORM'] || 'rosa2012.1')
repo = platform.repositories.find_by!(name: ENV['REPO'] || 'main')
src_branch = ENV['SRC_BRANCH'] || 'import_cooker'
dst_branch = ENV['DST_BRANCH'] || 'rosa2012.1'
say "START fork from #{src_branch} to #{dst_branch} branch using #{source} for #{owner.uname}. Add to repo '#{platform.name}/#{repo.name}'."

View File

@ -5,9 +5,8 @@ namespace :import do
desc "Load projects"
task projects: :environment do
source = ENV['SOURCE'] || 'http://dl.dropbox.com/u/984976/package_list.txt'
#owner = User.find_by_uname(ENV['OWNER_UNAME']) || Group.find_by_uname(ENV['OWNER_UNAME']) || User.first
owner = Group.find_by_uname("npp_team")
platform = Platform.find_by_name("RosaNPP") # RosaNPP
owner = Group.find_by uname: "npp_team"
platform = Platform.find_by name: "RosaNPP" # RosaNPP
repo = platform.repositories.first rescue nil
say "START import projects from '#{source}' for '#{owner.uname}'.#{repo ? " To repo '#{platform.name}/#{repo.name}'." : ''}"
ask 'Press enter to continue'
@ -33,9 +32,9 @@ namespace :import do
list = ENV['LIST'] #|| 'https://dl.dropbox.com/u/984976/alt_import.txt'
mask = ENV['MASK'] || '*.src.rpm'
hidden = ENV['HIDDEN'] == 'true' ? true : false
owner = User.find_by_uname(ENV['OWNER']) || Group.find_by_uname!(ENV['OWNER'] || 'altlinux')
platform = Platform.find_by_name!(ENV['PLATFORM'] || 'altlinux5')
repo = platform.repositories.find_by_name!(ENV['REPO'] || 'main')
owner = User.find_by(uname: ENV['OWNER']) || Group.find_by!(uname: ENV['OWNER'] || 'altlinux')
platform = Platform.find_by!(name: ENV['PLATFORM'] || 'altlinux5')
repo = platform.repositories.find_by!(name: ENV['REPO'] || 'main')
clear = ENV['CLEAR'] == 'true' ? true : false
say "START import projects from '#{base}' using '#{list || mask}' for '#{owner.uname}' to repo '#{platform.name}/#{repo.name}'."
@ -47,10 +46,10 @@ namespace :import do
project = Project.find_or_create_by_name_and_owner_type_and_owner_id(name, owner.class.to_s, owner.id)
repo.projects << project rescue nil
else # check if project already added
if project = repo.projects.find_by_name(name) || repo.projects.by_name(name).first # fallback to speedup
if project = repo.projects.find_by(name: name) || repo.projects.by_name(name).first # fallback to speedup
print "Found project '#{project.name_with_owner}' in '#{platform.name}/#{repo.name}'."
elsif scoped = Project.where(owner_id: owner.id, owner_type: owner.class) and
project = scoped.find_by_name(name) || scoped.by_name(name).first
project = scoped.find_by(name: name) || scoped.by_name(name).first
begin
repo.projects << project rescue nil
rescue Exception => e
@ -109,10 +108,10 @@ namespace :import do
desc "Parse repository for changes"
task parse: :environment do
release = ENV['RELEASE'] || 'official/2011'
platform = Platform.find_by_name(ENV['PLATFORM'] || "mandriva2011")
repository = platform.repositories.find_by_name(ENV['REPOSITORY'] || 'main')
platform = Platform.find_by(name: ENV['PLATFORM'] || "mandriva2011")
repository = platform.repositories.find_by(name: ENV['REPOSITORY'] || 'main')
source = ENV['DESTINATION'] || File.join(APP_CONFIG['root_path'], 'mirror.yandex.ru', 'mandriva', release, 'SRPMS', repository.name, '{release,updates}')
owner = Group.find_or_create_by_uname(ENV['OWNER'] || 'import') {|g| g.name = g.uname; g.owner = User.first}
owner = Group.find_or_create_by(uname: ENV['OWNER'] || 'import') {|g| g.name = g.uname; g.owner = User.first}
branch = ENV['BRANCH'] || "import_#{platform.name}"
say "START (#{Time.now.utc})"
@ -127,10 +126,10 @@ namespace :import do
project = Project.find_or_create_by_name_and_owner_type_and_owner_id(name, owner.class.to_s, owner.id)
print "Use project #{project.name_with_owner}. "
else # search project through repository
if project = repository.projects.find_by_name(name) || repository.projects.by_name(name).first # fallback to speedup
if project = repository.projects.find_by(name: name) || repository.projects.by_name(name).first # fallback to speedup
print "Found project #{project.name_with_owner} in #{platform.name}/#{repository.name}. "
elsif scoped = Project.where(owner_id: owner.id, owner_type: owner.class) and
project = scoped.find_by_name(name) || scoped.by_name(name).first
project = scoped.find_by(name: name) || scoped.by_name(name).first
repository.projects << project
print "Add project #{project.name_with_owner} to #{platform.name}/#{repository.name}. "
else

View File

@ -5,15 +5,15 @@ namespace :repositories do
repo_dirs = Dir["/root/mandriva_main_git/*.git"]
total = repo_dirs.length
cooker = Platform.find_by_name!("cooker")
main = cooker.repositories.find_by_name!("main")
cooker = Platform.find_by! name: "cooker"
main = cooker.repositories.find_by! name: "main"
repo_dirs.each_with_index do |repo_dir, index|
project_name = File.basename(repo_dir, ".git")
puts "Creating project(#{index}/#{total}): #{project_name}"
if main.projects.find_by_name(project_name)
if main.projects.find_by name: project_name
puts "\t Already created. Skipping"
next
end

View File

@ -4,7 +4,7 @@ namespace :new_core do
task update_packages: :environment do
say "[#{Time.zone.now}] Starting to extract rpms..."
token = User.find_by_uname('rosa_system').authentication_token
token = User.find_by(uname: 'rosa_system').authentication_token
BuildList.where(new_core: true).
where(status: [
BuildList::SUCCESS,

View File

@ -30,10 +30,10 @@ end
namespace :projects do
desc 'Add projects from one platform repository to another'
task copy_to_repo: :environment do
source_platform = Platform.find_by_name!(ENV['SRC_PLATFORM'])
dest_platform = Platform.find_by_name!(ENV['DST_PLATFORM'])
source_repo = source_platform.repositories.find_by_name!(ENV['SRC_REPO'])
dest_repo = dest_platform.repositories.find_by_name!(ENV['DST_REPO'])
source_platform = Platform.find_by! name: ENV['SRC_PLATFORM']
dest_platform = Platform.find_by! name: ENV['DST_PLATFORM']
source_repo = source_platform.repositories.find_by! name: ENV['SRC_REPO']
dest_repo = dest_platform.repositories.find_by! name: ENV['DST_REPO']
say "Add from repo '#{source_platform.name}/#{source_repo.name}' to repo '#{dest_platform.name}/#{dest_repo.name}'."
source_repo.projects.each do |pr|

View File

@ -4,7 +4,7 @@ namespace :remove_branch do
branch = ENV['BRANCH']
group = ENV['GROUP']
say "START remove branch #{branch} from #{group} group"
Group.find_by_uname(group).projects.find_each do |p|
Group.find_by(uname: group).projects.find_each do |p|
next if p.repo.branches.map(&:name).exclude?(branch)
say "===== Process #{p.name} project"
p.repo.git.native(:branch, {}, '-D', branch)

View File

@ -156,14 +156,14 @@ shared_examples_for 'platform admin user' do
it 'should be able to perform regenerate_metadata action' do
put :regenerate_metadata, id: @repository.id, platform_id: @platform.id
response.should redirect_to(platform_repository_path(@platform, @repository))
@repository.repository_statuses.find_by_platform_id(@platform.id).
@repository.repository_statuses.find_by(platform_id: @platform.id).
waiting_for_regeneration?.should be_true
end
it 'should be able to perform regenerate_metadata action of personal repository' do
put :regenerate_metadata, id: @personal_repository.id, platform_id: @personal_repository.platform.id, build_for_platform_id: @platform.id
response.should redirect_to(platform_repository_path(@personal_repository.platform, @personal_repository))
@personal_repository.repository_statuses.find_by_platform_id(@platform.id).
@personal_repository.repository_statuses.find_by(platform_id: @platform.id).
waiting_for_regeneration?.should be_true
end

View File

@ -3,7 +3,7 @@ FactoryGirl.define do
association :save_to_platform, factory: :platform
association :user
projects_list "first"
arches { [ Arch.find_or_create_by_name('x86_64').id ] }
arches { [ Arch.find_or_create_by(name: 'x86_64').id ] }
auto_publish true
stop_build false
end

View File

@ -6,6 +6,6 @@ FactoryGirl.define do
time_living 150
# see: before_validation in ProductBuildList model
before(:create) { Arch.find_or_create_by_name('x86_64') }
before(:create) { Arch.find_or_create_by(name: 'x86_64') }
end
end

View File

@ -47,7 +47,7 @@ describe AbfWorker::BuildListsPublishTaskManager do
it "ensures that repository_status has status publish" do
build_list.save_to_repository.repository_statuses.
find_by_platform_id(build_list.build_for_platform_id).publish?.
find_by(platform_id: build_list.build_for_platform_id).publish?.
should be_true
end