Skip to content

Commit

Permalink
Avoid assignment in condition
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Jul 19, 2015
1 parent 9552f35 commit 5533f1a
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 58 deletions.
11 changes: 0 additions & 11 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 15
# Configuration parameters: AllowSafeAssignment.
Lint/AssignmentInCondition:
Enabled: false

# Offense count: 3
Lint/DuplicateMethods:
Enabled: false
Expand Down Expand Up @@ -78,12 +73,6 @@ Style/DoubleNegation:
Style/GuardClause:
Enabled: false

# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: MaxLineLength.
Style/IfUnlessModifier:
Enabled: false

# Offense count: 2005
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles.
Expand Down
6 changes: 4 additions & 2 deletions app/controllers/api/v1/owners_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ def show
end

def create
if owner = User.find_by_name(params[:email])
owner = User.find_by_name(params[:email])
if owner
@rubygem.ownerships.create(user: owner)
render text: 'Owner added successfully.'
else
Expand All @@ -22,7 +23,8 @@ def create
end

def destroy
if owner = @rubygem.owners.find_by_name(params[:email])
owner = @rubygem.owners.find_by_name(params[:email])
if owner
if @rubygem.ownerships.find_by_user_id(owner.id).try(:destroy)
render text: "Owner removed successfully."
else
Expand Down
8 changes: 3 additions & 5 deletions app/controllers/api/v1/versions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ def show
end

def latest
rg = Rubygem.find_by_name params[:id]
if rg.present? && ver = rg.versions.latest.first
number = ver.number
end

rubygem = Rubygem.find_by_name(params[:id])
version = rubygem.versions.latest.first if rubygem
number = version.number if version
render json: { "version" => number || "unknown" }, callback: params['callback']
end

Expand Down
6 changes: 3 additions & 3 deletions app/middleware/hostess.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ def call(env)
return [302, { 'Location' => "/gems/#{Regexp.last_match(1)}.gem" }, []]
end

if (full_name = gem_download_path(path)) && name = Version.rubygem_name_for(full_name)
Download.incr(name, full_name)
end
download_path = gem_download_path(path)
name = Version.rubygem_name_for(download_path) if download_path
Download.incr(name, download_path) if name
super
end
end
15 changes: 6 additions & 9 deletions app/models/download.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ def self.counts_by_day_for_version_in_date_range(version, start, stop)

def self.copy_to_sql(version, date)
count = Redis.current.hget(history_key(version), date)

if vh = VersionHistory.for(version, date)
vh.count = count
vh.save
version_history = VersionHistory.for(version, date)
if version_history
version_history.count = count
version_history.save
else
VersionHistory.make(version, date, count)
end
Expand Down Expand Up @@ -198,11 +198,8 @@ def self.cardinality
end

def self.rank(version)
if rank = Redis.current.zrevrank(today_key, version.full_name)
rank + 1
else
0
end
rank = Redis.current.zrevrank(today_key, version.full_name)
rank ? rank + 1 : 0
end

def self.cleanup_today_keys
Expand Down
6 changes: 2 additions & 4 deletions app/models/rubygem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,8 @@ def self.by_downloads
end

def self.current_rubygems_release
if (rubygem = find_by(name: "rubygems-update")) &&
version = rubygem.versions.release.indexed.latest.first
version
end
rubygem = find_by(name: "rubygems-update")
rubygem && rubygem.versions.release.indexed.latest.first
end

def all_errors(version = nil)
Expand Down
5 changes: 2 additions & 3 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ class User < ActiveRecord::Base
validates :handle, length: { within: 2..40 }, allow_nil: true

def self.authenticate(who, password)
if user = find_by(email: who.downcase) || find_by(handle: who)
user if user.authenticated?(password)
end
user = find_by(email: who.downcase) || find_by(handle: who)
user if user && user.authenticated?(password)
end

def self.find_by_slug!(slug)
Expand Down
8 changes: 4 additions & 4 deletions app/models/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,8 @@ def sha256_hex

def recalculate_sha256
key = "gems/#{full_name}.gem"
if file = RubygemFs.instance.get(key)
Digest::SHA2.base64digest file
end
file = RubygemFs.instance.get(key)
Digest::SHA2.base64digest(file) if file
end

def recalculate_sha256!
Expand All @@ -267,7 +266,8 @@ def recalculate_sha256!

def recalculate_metadata!
key = "gems/#{full_name}.gem"
if file = RubygemFs.instance.get(key)
file = RubygemFs.instance.get(key)
if file
spec = Gem::Package.new(StringIO.new(file)).spec
metadata = spec.metadata
update(metadata: metadata || {})
Expand Down
4 changes: 1 addition & 3 deletions config/initializers/forbidden_yaml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ def self.from_yaml(input)
input = normalize_yaml_input input
spec = Psych.safe_load(input, WHITELISTED_CLASSES, WHITELISTED_SYMBOLS, true)

if spec && spec.class == FalseClass
fail Gem::EndOfYAMLException
end
fail Gem::EndOfYAMLException if spec && spec.class == FalseClass

unless Gem::Specification === spec
fail Gem::Exception, "YAML data doesn't evaluate to gem specification"
Expand Down
8 changes: 4 additions & 4 deletions lib/clearance_backdoor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ def call(env)
private

def sign_in_through_the_back_door
if user_id = params['as']
user = User.find(user_id)
@env[:clearance].sign_in(user)
end
user_id = params['as']
return if user_id.nil?
user = User.find(user_id)
@env[:clearance].sign_in(user)
end

def params
Expand Down
10 changes: 4 additions & 6 deletions lib/tasks/gemcutter.rake
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ namespace :gemcutter do
desc "Initialize missing checksums."
task init: :environment do
without_sha256 = Version.where(sha256: nil)
if mod = ENV['shard']
without_sha256.where("id % 4 = ?", mod.to_i)
end
mod = ENV['shard']
without_sha256.where("id % 4 = ?", mod.to_i) if mod

total = without_sha256.count
i = 0
Expand Down Expand Up @@ -86,9 +85,8 @@ namespace :gemcutter do
desc "Backfill old gem versions with metadata."
task backfill: :environment do
without_metadata = Version.where("metadata = ''")
if mod = ENV['shard']
without_metadata = without_metadata.where("id % 4 = ?", mod.to_i)
end
mod = ENV['shard']
without_metadata = without_metadata.where("id % 4 = ?", mod.to_i) if mod

total = without_metadata.count
i = 0
Expand Down
8 changes: 4 additions & 4 deletions test/unit/rubygem_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -612,11 +612,11 @@ class RubygemTest < ActiveSupport::TestCase
end

should "update the dependency" do
rg = Rubygem.create(name: "rake")
rubygem = Rubygem.create(name: "rake")

dep = Dependency.find_by_id(@rack_dep.id)
assert_nil dep.unresolved_name
assert_equal rg, dep.rubygem
dependency = Dependency.find_by_id(@rack_dep.id)
assert_nil dependency.unresolved_name
assert_equal rubygem, dependency.rubygem
end
end

Expand Down

0 comments on commit 5533f1a

Please sign in to comment.