Update Complexity linters (#3507) [ci skip]

This commit is contained in:
Mac Siri 2019-08-01 16:38:27 -04:00 committed by GitHub
parent f9da88b006
commit 9a2c856dd4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 78 additions and 37 deletions

View file

@ -1,4 +1,45 @@
version: "2"
checks:
argument-count:
enabled: true
config:
threshold: 4
complex-logic:
enabled: true
config:
threshold: 4
file-lines:
enabled: true
config:
threshold: 250
method-complexity:
enabled: true
config:
threshold: 5
method-count:
enabled: true
config:
threshold: 20
method-lines:
enabled: true
config:
threshold: 25
nested-control-flow:
enabled: true
config:
threshold: 4
return-statements:
enabled: true
config:
threshold: 4
similar-code:
enabled: true
config:
threshold: #language-specific defaults. overrides affect all languages.
identical-code:
enabled: true
config:
threshold: #language-specific defaults. overrides affect all languages.
plugins:
rubocop:
enabled: true

View file

@ -7,21 +7,15 @@ require:
# our custom config
# TODO: Uncomment it after fix rubocop-todo RSpec/MultipleExpectations
RSpec/MultipleExpectations:
# Max: 1
Exclude:
- 'spec/features/**/*'
RSpec/DescribeClass:
Exclude:
- spec/features/**/*
- spec/requests/**/*
- spec/system/**/*
RSpec/ExampleLength:
Max: 10
Exclude:
- spec/features/**/*
- spec/system/**/*
# TODO: Uncomment it after fix rubocop-todo Metrics/LineLength
# Metrics/LineLength:
@ -57,7 +51,8 @@ Metrics/AbcSize:
Description: >-
A calculated magnitude based on number of assignments,
branches, and conditions.
Enabled: false
Enabled: true
Max: 50
# TODO: Uncomment it after fix rubocop-todo Metrics/BlockLength
Metrics/BlockLength:
@ -71,7 +66,12 @@ Metrics/CyclomaticComplexity:
Description: >-
A complexity metric that is strongly correlated to the number
of test cases needed to validate a method.
Enabled: false
Enabled: true
Max: 13
Metrics/PerceivedComplexity:
Enabled: true
Max: 14
Metrics/MethodLength:
Description: 'Avoid methods longer than 10 lines of code.'

View file

@ -1,27 +1,29 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2019-07-30 17:22:03 +0200 using RuboCop version 0.73.0.
# on 2019-08-01 11:17:00 -0400 using RuboCop version 0.73.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.
# Offense count: 12
# Offense count: 9
Metrics/AbcSize:
Max: 83
# Offense count: 13
# Configuration parameters: CountComments, ExcludedMethods.
# ExcludedMethods: refine
Metrics/BlockLength:
Max: 63
# Offense count: 25
Metrics/PerceivedComplexity:
Max: 13
# Offense count: 1
RSpec/AnyInstance:
# Configuration parameters: Max.
RSpec/ExampleLength:
Exclude:
- 'spec/requests/stripe_cancellations_spec.rb'
- 'spec/system/**/*'
- 'spec/requests/display_ad_events_spec.rb'
# Offense count: 343
# Offense count: 341
# Configuration parameters: AggregateFailuresByDefault.
RSpec/MultipleExpectations:
Max: 8
@ -33,10 +35,9 @@ Rails/HelperInstanceVariable:
Exclude:
- 'app/helpers/application_helper.rb'
# Offense count: 17
# Offense count: 16
Rails/OutputSafety:
Exclude:
- 'app/controllers/stripe_subscriptions_controller.rb'
- 'app/helpers/application_helper.rb'
- 'app/helpers/comments_helper.rb'
- 'app/labor/markdown_parser.rb'
@ -48,7 +49,7 @@ Rails/OutputSafety:
- 'app/views/api/v0/articles/show.json.jbuilder'
- 'app/views/articles/feed.rss.builder'
# Offense count: 19
# Offense count: 20
# Cop supports --auto-correct.
# Configuration parameters: AutoCorrect, EnforcedStyle.
# SupportedStyles: nested, compact
@ -61,7 +62,7 @@ Style/GuardClause:
Exclude:
- 'app/models/article.rb'
# Offense count: 2642
# Offense count: 2671
# Cop supports --auto-correct.
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https

View file

@ -14,10 +14,11 @@ class DisplayAdEventsController < ApplicationController
def update_display_ads_data
return if Rails.env.production? && rand(20) != 1 # We need to do this operation only once in a while.
@display_ad = DisplayAd.find(display_ad_event_params[:display_ad_id])
num_impressions = @display_ad.display_ad_events.where(category: "impression").size
num_clicks = @display_ad.display_ad_events.where(category: "click").size
rate = num_clicks.to_f / num_impressions.to_f
rate = num_clicks.to_f / num_impressions
@display_ad.
update_columns(success_rate: rate, clicks_count: num_clicks, impressions_count: num_impressions)
@ -26,4 +27,4 @@ class DisplayAdEventsController < ApplicationController
def display_ad_event_params
params.require(:display_ad_event).permit(%i[context_type category display_ad_id])
end
end
end

View file

@ -10,7 +10,6 @@ class DisplayAd < ApplicationRecord
scope :approved_and_published, -> { where(approved: true, published: true) }
def self.for_display(area)
if rand(8) == 1
approved_and_published.where(placement_area: area).order("success_rate DESC").sample

View file

@ -1,8 +1,7 @@
class DisplayAdEvent < ApplicationRecord
belongs_to :display_ad
belongs_to :user
validates :category, inclusion: {in: %w[impression click]}
validates :context_type, inclusion: {in: %w[home]}
validates :category, inclusion: { in: %w[impression click] }
validates :context_type, inclusion: { in: %w[home] }
end

View file

@ -29,7 +29,7 @@ RSpec.describe DisplayAd, type: :model do
create(:display_ad, organization_id: organization.id, published: true, approved: true)
create(:display_ad, organization_id: organization.id, published: false, approved: true)
create(:display_ad, organization_id: organization.id, published: true, approved: false)
expect(DisplayAd.for_display(DisplayAd.last.placement_area).published).to eq(true)
expect(DisplayAd.for_display(DisplayAd.last.placement_area).approved).to eq(true)
expect(described_class.for_display(described_class.last.placement_area).published).to eq(true)
expect(described_class.for_display(described_class.last.placement_area).approved).to eq(true)
end
end

View file

@ -16,7 +16,7 @@ RSpec.describe "DisplayAdEvents", type: :request do
display_ad_event: {
display_ad_id: display_ad.id,
context_type: "home",
category: "click",
category: "click"
}
}
expect(display_ad.reload.clicks_count).to eq(1)
@ -26,18 +26,18 @@ RSpec.describe "DisplayAdEvents", type: :request do
display_ad_event: {
display_ad_id: display_ad.id,
context_type: "home",
category: "impression",
category: "impression"
}
}
expect(display_ad.reload.impressions_count).to eq(1)
end
it "creates a display ad success rate" do
4.times do
4.times do
post "/display_ad_events", params: {
display_ad_event: {
display_ad_id: display_ad.id,
context_type: "home",
category: "impression",
category: "impression"
}
}
end
@ -45,7 +45,7 @@ RSpec.describe "DisplayAdEvents", type: :request do
display_ad_event: {
display_ad_id: display_ad.id,
context_type: "home",
category: "click",
category: "click"
}
}
expect(display_ad.reload.success_rate).to eq(0.25)
@ -55,11 +55,11 @@ RSpec.describe "DisplayAdEvents", type: :request do
display_ad_event: {
display_ad_id: display_ad.id,
context_type: "home",
category: "impression",
category: "impression"
}
}
expect(DisplayAdEvent.last.user_id).to eq(user.id)
end
end
end
end
end