Skip to content

Commit

Permalink
fix rubocop, use custom config
Browse files Browse the repository at this point in the history
  • Loading branch information
matthutchinson committed Dec 31, 2024
1 parent 5b8356c commit f9c80e5
Show file tree
Hide file tree
Showing 7 changed files with 261 additions and 22 deletions.
243 changes: 241 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,241 @@
inherit_gem:
rubocop-rails-omakase: rubocop.yml
require:
- rubocop-performance

inherit_mode:
merge:
- Exclude

AllCops:
SuggestExtensions: false
Exclude:
- "data/**/*"

# All cops except your using extensions are disabled by default.
Bundler:
Enabled: false
Gemspec:
Enabled: false
Layout:
Enabled: false
Lint:
Enabled: false
Metrics:
Enabled: false
Naming:
Enabled: false
Performance:
Enabled: false
Exclude:
- "test/**/*"
Security:
Enabled: false
Style:
Enabled: false

# Align `when` with `end`.
Layout/CaseIndentation:
Enabled: true
EnforcedStyle: end

# Align comments with method definitions.
Layout/CommentIndentation:
Enabled: true

Layout/ElseAlignment:
Enabled: true

Layout/EmptyLineAfterMagicComment:
Enabled: true

Layout/EmptyLinesAroundBlockBody:
Enabled: true

# In a regular class definition, no empty lines around the body.
Layout/EmptyLinesAroundClassBody:
Enabled: true

# In a regular method definition, no empty lines around the body.
Layout/EmptyLinesAroundMethodBody:
Enabled: true

# In a regular module definition, no empty lines around the body.
Layout/EmptyLinesAroundModuleBody:
Enabled: true

# Align `end` with the matching keyword or starting expression except for
# assignments, where it should be aligned with the LHS.
Layout/EndAlignment:
Enabled: true
EnforcedStyleAlignWith: variable

# Method definitions after `private` or `protected` isolated calls need one
# extra level of indentation.
#
# We break this rule in context, though, e.g. for private-only concerns,
# so we leave it disabled.
Layout/IndentationConsistency:
Enabled: false
EnforcedStyle: indented_internal_methods

# Detect hard tabs, no hard tabs.
Layout/IndentationStyle:
Enabled: true

# Two spaces, no tabs (for indentation).
#
# Doesn't behave properly with private-only concerns, so it's disabled.
Layout/IndentationWidth:
Enabled: false

Layout/LeadingCommentSpace:
Enabled: true

Layout/SpaceAfterColon:
Enabled: true

Layout/SpaceAfterComma:
Enabled: true

Layout/SpaceAroundEqualsInParameterDefault:
Enabled: true

Layout/SpaceAroundKeyword:
Enabled: true

# Use `foo {}` not `foo{}`.
Layout/SpaceBeforeBlockBraces:
Enabled: true

Layout/SpaceBeforeComma:
Enabled: true

Layout/SpaceBeforeFirstArg:
Enabled: true

# Use `->(x, y) { x + y }` not `-> (x, y) { x + y }`
Layout/SpaceInLambdaLiteral:
Enabled: true

# Use `[ a, [ b, c ] ]` not `[a, [b, c]]`
# Use `[]` not `[ ]`
Layout/SpaceInsideArrayLiteralBrackets:
Enabled: true
EnforcedStyle: space
EnforcedStyleForEmptyBrackets: no_space

# Use `%w[ a b ]` not `%w[ a b ]`.
Layout/SpaceInsideArrayPercentLiteral:
Enabled: true

# Use `foo { bar }` not `foo {bar}`.
# Use `foo { }` not `foo {}`.
Layout/SpaceInsideBlockBraces:
Enabled: true
EnforcedStyleForEmptyBraces: space

# Use `{ a: 1 }` not `{a:1}`.
# Use `{}` not `{ }`.
Layout/SpaceInsideHashLiteralBraces:
Enabled: true
EnforcedStyle: space
EnforcedStyleForEmptyBraces: no_space

# Use `foo(bar)` not `foo( bar )`
Layout/SpaceInsideParens:
Enabled: true

# Requiring a space is not yet supported as of 0.59.2
# Use `%w[ foo ]` not `%w[foo]`
Layout/SpaceInsidePercentLiteralDelimiters:
Enabled: false
#EnforcedStyle: space

# Use `hash[:key]` not `hash[ :key ]`
Layout/SpaceInsideReferenceBrackets:
Enabled: true

# Blank lines should not have any spaces.
Layout/TrailingEmptyLines:
Enabled: true

# No trailing whitespace.
Layout/TrailingWhitespace:
Enabled: true

Lint/RedundantStringCoercion:
Enabled: true

# Use my_method(my_arg) not my_method( my_arg ) or my_method my_arg.
Lint/RequireParentheses:
Enabled: true

Lint/UriEscapeUnescape:
Enabled: true

Performance/FlatMap:
Enabled: true

# We generally prefer &&/|| but like low-precedence and/or in context
Style/AndOr:
Enabled: false

# Prefer Foo.method over Foo::method
Style/ColonMethodCall:
Enabled: true

Style/DefWithParentheses:
Enabled: true

# Use Ruby >= 1.9 syntax for hashes. Prefer { a: :b } over { :a => :b }.
Style/HashSyntax:
Enabled: true
EnforcedShorthandSyntax: either

# Defining a method with parameters needs parentheses.
Style/MethodDefParentheses:
Enabled: true

Style/ParenthesesAroundCondition:
Enabled: true

Style/PercentLiteralDelimiters:
Enabled: true
PreferredDelimiters:
default: "()"
"%i": "[]"
"%I": "[]"
"%r": "{}"
"%w": "[]"
"%W": "[]"

# Use quotes for string literals when they are enough.
Style/RedundantPercentQ:
Enabled: false

Style/RedundantReturn:
Enabled: true
AllowMultipleReturnValues: true

Style/Semicolon:
Enabled: true
AllowAsExpressionSeparator: true

Style/StabbyLambdaParentheses:
Enabled: true

# Use `"foo"` not `'foo'` unless escaping is required
Style/StringLiterals:
Enabled: true
EnforcedStyle: double_quotes
Include:
- "app/**/*"
- "config/**/*"
- "lib/**/*"
- "test/**/*"
- "Gemfile"

Style/TrailingCommaInArrayLiteral:
Enabled: true

Style/TrailingCommaInHashLiteral:
Enabled: true
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ source "https://rubygems.org"
gemspec

group :rubocop do
gem "rubocop-rails-omakase", require: false
gem "rubocop-performance", require: false
end
8 changes: 4 additions & 4 deletions acts_as_textcaptcha.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ require "acts_as_textcaptcha/version"
Gem::Specification.new do |spec|
spec.name = "acts_as_textcaptcha"
spec.version = ActsAsTextcaptcha::VERSION
spec.authors = ["Matthew Hutchinson"]
spec.email = ["matt@hiddenloop.com"]
spec.authors = [ "Matthew Hutchinson" ]
spec.email = [ "matt@hiddenloop.com" ]
spec.homepage = "http://github.com/matthutchinson/acts_as_textcaptcha"
spec.license = "MIT"
spec.summary = "A text-based logic question captcha for Rails"
spec.files = Dir["lib/**/*", "config/**/*", "LICENSE", "README.md"]

spec.require_paths = ["lib"]
spec.require_paths = [ "lib" ]

spec.required_ruby_version = ">= 3.1"
spec.extra_rdoc_files = ["README.md", "LICENSE"]
spec.extra_rdoc_files = [ "README.md", "LICENSE" ]
spec.rdoc_options << "--title" << "ActAsTextcaptcha" << "--main" << "README.md" << "-ri"

spec.description = <<-DESCRIPTION
Expand Down
14 changes: 7 additions & 7 deletions test/helpers/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,24 @@ class CommentMyEndpoint < ApplicationRecord
class FastComment < ApplicationRecord
# inline options with super fast (0.006 seconds) cache expiry time
acts_as_textcaptcha cache_expiry_minutes: "0.0001",
questions: [{ question: "1+1", answers: "2,two" }]
questions: [ { question: "1+1", answers: "2,two" } ]
end

class Review < ApplicationRecord
# inline options with all possible options
acts_as_textcaptcha api_key: "api_key",
questions: [{ question: "The green hat is what color?", answers: "green" }]
questions: [ { question: "The green hat is what color?", answers: "green" } ]
end

class MovieReview < ApplicationRecord
# inline options with string keys
acts_as_textcaptcha "api_key" => "api_key",
"questions" => [{ "Question" => "The green hat is what color?", "answers" => nil }]
"questions" => [ { "Question" => "The green hat is what color?", "answers" => nil } ]
end

class Note < ApplicationRecord
# inline options (string keys) with user defined questions only (no textcaptcha service)
acts_as_textcaptcha "questions" => [{ "question" => "1+1", "answers" => "2,two" }]
acts_as_textcaptcha "questions" => [ { "question" => "1+1", "answers" => "2,two" } ]

# allow toggling perform_textcaptcha on/off (default on)
attr_accessor :turn_off_captcha
Expand All @@ -64,7 +64,7 @@ class Contact
include ActiveModel::Conversion
extend ActsAsTextcaptcha::Textcaptcha

acts_as_textcaptcha questions: [{ question: "one+1", answers: "2,two,апельсин" }]
acts_as_textcaptcha questions: [ { question: "one+1", answers: "2,two,апельсин" } ]
end

class StrongAccessibleWidget < ApplicationRecord
Expand All @@ -73,12 +73,12 @@ def self.accessible_attributes(role = :default); end

def self.attr_accessible(*args); end

acts_as_textcaptcha "questions" => [{ "question" => "1+1", "answers" => "2,two" }]
acts_as_textcaptcha "questions" => [ { "question" => "1+1", "answers" => "2,two" } ]
end

class StrongProtectedWidget < StrongAccessibleWidget
# stub out attr_protected interface for testing
def self.attr_protected(*args); end

acts_as_textcaptcha "questions" => [{ "question" => "1+1", "answers" => "2,two" }]
acts_as_textcaptcha "questions" => [ { "question" => "1+1", "answers" => "2,two" } ]
end
6 changes: 3 additions & 3 deletions test/textcaptcha_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ def test_fetch_and_parse_q_and_a
stub_api_with(valid_json_response)
q_and_a = textcaptcha_api.fetch
assert_equal q_and_a["q"], "What is Jennifer's name?"
assert_equal q_and_a["a"], ["1660fe5c81c4ce64a2611494c439e1ba"]
assert_equal q_and_a["a"], [ "1660fe5c81c4ce64a2611494c439e1ba" ]
end

def test_fetch_and_parse_q_and_a_from_defined_endpoint
my_api_endpoint = "http://myserver.com/my-api.json"
stub_api_with(valid_json_response, api_endpoint: my_api_endpoint)
q_and_a = textcaptcha_api(api_endpoint: my_api_endpoint).fetch
assert_equal q_and_a["q"], "What is Jennifer's name?"
assert_equal q_and_a["a"], ["1660fe5c81c4ce64a2611494c439e1ba"]
assert_equal q_and_a["a"], [ "1660fe5c81c4ce64a2611494c439e1ba" ]
end

def test_returns_nil_when_net_http_errors
Expand Down Expand Up @@ -51,7 +51,7 @@ def test_logs_error_to_rails_logger_if_raise_errors_not_set
clear_rails_log
stub_request(:get, %r{http://textcaptcha.com}).to_raise(Timeout::Error)
textcaptcha_api(raise_errors: false).fetch
assert_log_matches [/ActsAsTextcaptcha::ResponseError fetching '.*' failed - Exception from WebMock/]
assert_log_matches [ /ActsAsTextcaptcha::ResponseError fetching '.*' failed - Exception from WebMock/ ]
end

def test_returns_nil_when_empty_response_received
Expand Down
8 changes: 4 additions & 4 deletions test/textcaptcha_cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

class ActsAsTextcaptcha::TextcaptchaCacheTest < Minitest::Test
def setup
cache.write("mykey", [1, 2, 3])
cache.write("mykey", [ 1, 2, 3 ])
end

def test_reading_from_cache
assert_equal cache.read("mykey"), [1, 2, 3]
assert_equal cache.read("mykey"), [ 1, 2, 3 ]
end

def test_writing_to_cache
Expand All @@ -17,14 +17,14 @@ def test_writing_to_cache
end

def test_deleting_from_cache
assert_equal cache.read("mykey"), [1, 2, 3]
assert_equal cache.read("mykey"), [ 1, 2, 3 ]
cache.delete("mykey")

assert_nil cache.read("mykey")
end

def test_cache_keys_use_a_prefix
assert_equal Rails.cache.read("#{ActsAsTextcaptcha::TextcaptchaCache::KEY_PREFIX}mykey"), [1, 2, 3]
assert_equal Rails.cache.read("#{ActsAsTextcaptcha::TextcaptchaCache::KEY_PREFIX}mykey"), [ 1, 2, 3 ]
end

private
Expand Down
2 changes: 1 addition & 1 deletion test/textcaptcha_config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ def test_creates_yaml_config_file_and_path_to_it_from_example_config
assert File.exist?(CONFIG_PATH)

example_config = YAML.safe_load(File.read(CONFIG_PATH), aliases: true)
assert_equal example_config.keys, %w(development test production)
assert_equal example_config.keys, %w[development test production]
end
end

0 comments on commit f9c80e5

Please sign in to comment.