Skip to content

Commit

Permalink
Lint Ruby with standardrb (#2508)
Browse files Browse the repository at this point in the history
Some time ago, we at thoughtbot stopped using our custom Rubocop rules
and replaced it with standard. This adds Standard, sets up support for
it in GitHub Actions and runs it against the code base.

We ignore `Lint/ConstantDefinitionInBlock` in a few specs, as this is
related to the Pundit integration. It'd be good to come back to this in
future and adjust the specs to avoid defining/overriding classes in
specs, but honestly it was too difficult to make it work in a reasonable
time.

Finally, we need to use `main` of the standard Action as, currently,
that's the only one which is working (`0.0.5` fails silently).

https://github.com/standardrb/standard
thoughtbot/guides#606
  • Loading branch information
nickcharlton authored Feb 5, 2024
1 parent cc9b13b commit 3ff3b7d
Show file tree
Hide file tree
Showing 148 changed files with 1,142 additions and 1,141 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ on:
types: [opened, synchronize, reopened]

jobs:
standardrb:
runs-on: ubuntu-latest
permissions:
contents: read
checks: write
steps:
- uses: actions/checkout@v4
- name: StandardRB Linter
uses: testdouble/standard-ruby-action@main
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

stylelint:
runs-on: ubuntu-latest
steps:
Expand Down
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source 'https://rubygems.org'
source "https://rubygems.org"
ruby "3.2.2" unless ENV["CI"]

gemspec
Expand Down Expand Up @@ -27,6 +27,7 @@ group :development, :test do
gem "factory_bot_rails"
gem "i18n-tasks", "1.0.13"
gem "pry"
gem "standard"
gem "yard"
end

Expand Down
34 changes: 34 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ GEM
terminal-table (>= 1.5.1)
jsbundling-rails (1.3.0)
railties (>= 6.0.0)
json (2.7.1)
kaminari (1.2.2)
activesupport (>= 4.1.0)
kaminari-actionview (= 1.2.2)
Expand All @@ -176,8 +177,10 @@ GEM
kaminari
rails
kgio (2.11.4)
language_server-protocol (3.17.0.3)
launchy (2.5.2)
addressable (~> 2.8)
lint_roller (1.1.0)
loofah (2.22.0)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
Expand Down Expand Up @@ -205,6 +208,7 @@ GEM
nokogiri (1.16.0)
mini_portile2 (~> 2.8.2)
racc (~> 1.4)
parallel (1.24.0)
parser (3.2.2.4)
ast (~> 2.4.1)
racc
Expand Down Expand Up @@ -274,6 +278,23 @@ GEM
rspec-mocks (~> 3.12)
rspec-support (~> 3.12)
rspec-support (3.12.1)
rubocop (1.59.0)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.2.2.4)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.30.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.30.0)
parser (>= 3.2.1.0)
rubocop-performance (1.20.2)
rubocop (>= 1.48.1, < 2.0)
rubocop-ast (>= 1.30.0, < 2.0)
ruby-progressbar (1.13.0)
rubyzip (2.3.2)
selenium-webdriver (4.17.0)
base64 (~> 0.2)
Expand All @@ -295,6 +316,18 @@ GEM
actionpack (>= 5.2)
activesupport (>= 5.2)
sprockets (>= 3.0.0)
standard (1.33.0)
language_server-protocol (~> 3.17.0.2)
lint_roller (~> 1.0)
rubocop (~> 1.59.0)
standard-custom (~> 1.0.0)
standard-performance (~> 1.3)
standard-custom (1.0.2)
lint_roller (~> 1.0)
rubocop (~> 1.50)
standard-performance (1.3.1)
lint_roller (~> 1.1)
rubocop-performance (~> 1.20.2)
terminal-table (3.0.2)
unicode-display_width (>= 1.1.1, < 3)
thor (1.3.0)
Expand Down Expand Up @@ -355,6 +388,7 @@ DEPENDENCIES
sentry-ruby
shoulda-matchers
sprockets-rails (~> 3.4)
standard
timecop
uglifier
unicorn
Expand Down
18 changes: 9 additions & 9 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
begin
require 'bundler/setup'
require "bundler/setup"
rescue LoadError
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
puts "You must `gem install bundler` and `bundle install` to run rake tasks"
end

require 'rdoc/task'
require "rdoc/task"

require File.expand_path('../spec/example_app/config/application', __FILE__)
require File.expand_path("../spec/example_app/config/application", __FILE__)

RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'Administrate'
rdoc.options << '--line-numbers'
rdoc.rdoc_files.include('README.rdoc')
rdoc.rdoc_files.include('lib/**/*.rb')
rdoc.rdoc_dir = "rdoc"
rdoc.title = "Administrate"
rdoc.options << "--line-numbers"
rdoc.rdoc_files.include("README.rdoc")
rdoc.rdoc_files.include("lib/**/*.rb")
end

Bundler::GemHelper.install_tasks
Expand Down
26 changes: 13 additions & 13 deletions administrate.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ Gem::Specification.new do |s|
s.add_dependency "activerecord", ">= 6.0", "< 8.0"
s.add_dependency "kaminari", "~> 1.2.2"

s.description = <<-DESCRIPTION
Administrate is heavily inspired by projects like Rails Admin and ActiveAdmin,
but aims to provide a better user experience for site admins,
and to be easier for developers to customize.
To do that, we're following a few simple rules:
- No DSLs (domain-specific languages)
- Support the simplest use cases,
and let the user override defaults with standard tools
such as plain Rails controllers and views.
- Break up the library into core components and plugins,
so each component stays small and easy to maintain.
s.description = <<~DESCRIPTION
Administrate is heavily inspired by projects like Rails Admin and ActiveAdmin,
but aims to provide a better user experience for site admins,
and to be easier for developers to customize.
To do that, we're following a few simple rules:
- No DSLs (domain-specific languages)
- Support the simplest use cases,
and let the user override defaults with standard tools
such as plain Rails controllers and views.
- Break up the library into core components and plugins,
so each component stays small and easy to maintain.
DESCRIPTION
end
38 changes: 19 additions & 19 deletions app/controllers/administrate/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@ def index
resources: resources,
search_term: search_term,
page: page,
show_search_bar: show_search_bar?,
show_search_bar: show_search_bar?
}
end

def show
render locals: {
page: Administrate::Page::Show.new(dashboard, requested_resource),
page: Administrate::Page::Show.new(dashboard, requested_resource)
}
end

def new
resource = new_resource
authorize_resource(resource)
render locals: {
page: Administrate::Page::Form.new(dashboard, resource),
page: Administrate::Page::Form.new(dashboard, resource)
}
end

def edit
render locals: {
page: Administrate::Page::Form.new(dashboard, requested_resource),
page: Administrate::Page::Form.new(dashboard, requested_resource)
}
end

Expand All @@ -47,11 +47,11 @@ def create
yield(resource) if block_given?
redirect_to(
after_resource_created_path(resource),
notice: translate_with_resource("create.success"),
notice: translate_with_resource("create.success")
)
else
render :new, locals: {
page: Administrate::Page::Form.new(dashboard, resource),
page: Administrate::Page::Form.new(dashboard, resource)
}, status: :unprocessable_entity
end
end
Expand All @@ -60,11 +60,11 @@ def update
if requested_resource.update(resource_params)
redirect_to(
after_resource_updated_path(requested_resource),
notice: translate_with_resource("update.success"),
notice: translate_with_resource("update.success")
)
else
render :edit, locals: {
page: Administrate::Page::Form.new(dashboard, requested_resource),
page: Administrate::Page::Form.new(dashboard, requested_resource)
}, status: :unprocessable_entity
end
end
Expand All @@ -84,12 +84,12 @@ def filter_resources(resources, search_term:)
Administrate::Search.new(
resources,
dashboard,
search_term,
search_term
).run
end

def after_resource_destroyed_path(_requested_resource)
{ action: :index }
{action: :index}
end

def after_resource_created_path(requested_resource)
Expand All @@ -103,7 +103,7 @@ def after_resource_updated_path(requested_resource)
helper_method :nav_link_state
def nav_link_state(resource)
underscore_resource = resource.to_s.split("/").join("__")
resource_name.to_s.pluralize == underscore_resource ? :active : :inactive
(resource_name.to_s.pluralize == underscore_resource) ? :active : :inactive
end

# Whether the named action route exists for the resource class.
Expand Down Expand Up @@ -141,8 +141,8 @@ def order
sorting_attribute,
sorting_direction,
association_attribute: order_by_field(
dashboard_attribute(sorting_attribute),
),
dashboard_attribute(sorting_attribute)
)
)
end

Expand Down Expand Up @@ -201,9 +201,9 @@ def apply_collection_includes(relation)
end

def resource_params
params.require(resource_class.model_name.param_key).
permit(dashboard.permitted_attributes(action_name)).
transform_values { |v| read_param_value(v) }
params.require(resource_class.model_name.param_key)
.permit(dashboard.permitted_attributes(action_name))
.transform_values { |v| read_param_value(v) }
end

def read_param_value(data)
Expand Down Expand Up @@ -236,13 +236,13 @@ def resource_resolver
def translate_with_resource(key)
t(
"administrate.controller.#{key}",
resource: resource_resolver.resource_title,
resource: resource_resolver.resource_title
)
end

def show_search_bar?
dashboard.attribute_types_for(
dashboard.all_attributes,
dashboard.all_attributes
).any? { |_name, attribute| attribute.searchable? }
end

Expand Down Expand Up @@ -279,7 +279,7 @@ def authorize_resource(resource)
else
raise Administrate::NotAuthorizedError.new(
action: action_name,
resource: resource,
resource: resource
)
end
end
Expand Down
10 changes: 5 additions & 5 deletions app/controllers/concerns/administrate/punditize.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Administrate
module Punditize
if Object.const_defined?("Pundit")
if Object.const_defined?(:Pundit)
extend ActiveSupport::Concern

if Pundit.const_defined?(:Authorization)
Expand Down Expand Up @@ -28,7 +28,7 @@ def authorize_resource(resource)
def authorized_action?(resource, action)
namespaced_resource = policy_namespace + [resource]
policy = Pundit.policy!(pundit_user, namespaced_resource)
policy.send("#{action}?".to_sym)
policy.send(:"#{action}?")
end

def policy_scope!(user, scope)
Expand All @@ -38,13 +38,13 @@ def policy_scope!(user, scope)
policy_scope = policy_scope_class.new(user, pundit_model(scope))
rescue ArgumentError
raise(Pundit::InvalidConstructorError,
"Invalid #<#{policy_scope_class}> constructor is called")
"Invalid #<#{policy_scope_class}> constructor is called")
end

if policy_scope.respond_to? :resolve_admin
Administrate.deprecator.warn(
"Pundit policy scope `resolve_admin` method is deprecated. " +
"Please use a namespaced pundit policy instead.",
"Pundit policy scope `resolve_admin` method is deprecated. " \
"Please use a namespaced pundit policy instead."
)
policy_scope.resolve_admin
else
Expand Down
8 changes: 4 additions & 4 deletions app/helpers/administrate/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def accessible_action?(target, action_name)
def display_resource_name(resource_name, opts = {})
dashboard_from_resource(resource_name).resource_name(
count: opts[:singular] ? SINGULAR_COUNT : PLURAL_MANY_COUNT,
default: default_resource_name(resource_name, opts),
default: default_resource_name(resource_name, opts)
)
end

Expand All @@ -65,14 +65,14 @@ def sort_order(order)
def resource_index_route(resource_name)
url_for(
action: "index",
controller: "/#{namespace}/#{resource_name}",
controller: "/#{namespace}/#{resource_name}"
)
end

def sanitized_order_params(page, current_field_name)
collection_names = page.item_associations + [current_field_name]
association_params = collection_names.map do |assoc_name|
{ assoc_name => %i[order direction page per_page] }
{assoc_name => %i[order direction page per_page]}
end
params.permit(:search, :id, :_page, :per_page, association_params)
end
Expand All @@ -87,7 +87,7 @@ def clear_search_params

def default_resource_name(name, opts = {})
resource_name = (opts[:singular] ? name.to_s : name.to_s.pluralize)
resource_name.gsub("/", "_").titleize
resource_name.tr("/", "_").titleize
end
end
end
1 change: 1 addition & 0 deletions gemfiles/pundit21.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ group :development, :test do
gem "factory_bot_rails"
gem "i18n-tasks", "1.0.13"
gem "pry"
gem "standard"
gem "yard"
end

Expand Down
1 change: 1 addition & 0 deletions gemfiles/rails60.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ group :development, :test do
gem "factory_bot_rails"
gem "i18n-tasks", "1.0.13"
gem "pry"
gem "standard"
gem "yard"
end

Expand Down
1 change: 1 addition & 0 deletions gemfiles/rails61.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ group :development, :test do
gem "factory_bot_rails"
gem "i18n-tasks", "1.0.13"
gem "pry"
gem "standard"
gem "yard"
end

Expand Down
Loading

0 comments on commit 3ff3b7d

Please sign in to comment.