Skip to content

Commit 0a93b4b

Browse files
authored
Rubocop rails omakase (#602)
* add rubocop-rails-omakase * rubocop autocorrect * add ci
1 parent 53c0c3a commit 0a93b4b

File tree

342 files changed

+4245
-4274
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

342 files changed

+4245
-4274
lines changed

.github/workflows/rubocop.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# .github/workflows/rubocop.yml
2+
# https://lovro-bikic.github.io/github-actions-rubocop-workflow/
3+
#
4+
name: RuboCop
5+
6+
concurrency:
7+
group: ${{ github.workflow }}-${{ github.ref }}
8+
cancel-in-progress: true
9+
10+
on:
11+
pull_request:
12+
push:
13+
branches:
14+
- main
15+
16+
jobs:
17+
rubocop:
18+
runs-on: ubuntu-latest
19+
timeout-minutes: 10
20+
env:
21+
RUBOCOP_CACHE_ROOT: tmp/rubocop
22+
steps:
23+
- name: Git checkout
24+
uses: actions/checkout@v4
25+
- name: Set up Ruby
26+
uses: ruby/setup-ruby@v1
27+
with:
28+
bundler-cache: true
29+
- name: Prepare RuboCop cache
30+
uses: actions/cache@v4
31+
env:
32+
DEPENDENCIES_HASH: ${{ hashFiles('.ruby-version', '**/.rubocop.yml', '**/.rubocop_todo.yml', 'Gemfile.lock') }}
33+
with:
34+
path: ${{ env.RUBOCOP_CACHE_ROOT }}
35+
key: rubocop-cache-${{ runner.os }}-${{ env.DEPENDENCIES_HASH }}-${{ github.ref_name == github.event.repository.default_branch && github.run_id || 'default' }}
36+
restore-keys: |
37+
rubocop-cache-${{ runner.os }}-${{ env.DEPENDENCIES_HASH }}-
38+
- name: Run RuboCop
39+
run: bundle exec rubocop --format github --format clang

.rubocop.yml

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,5 @@
1-
Encoding:
2-
Enabled: false
1+
# Omakase Ruby styling for Rails
2+
inherit_gem:
3+
rubocop-rails-omakase: rubocop.yml
34

4-
Documentation:
5-
Enabled: false
6-
7-
ClassAndModuleChildren:
8-
Enabled: false
9-
10-
ClassLength:
11-
Enabled: false
12-
13-
MethodLength:
14-
Enabled: false
15-
16-
AbcSize:
17-
Max: 30
18-
19-
DoubleNegation:
20-
Enabled: false
21-
22-
AllCops:
23-
Exclude:
24-
- "bin/**/*"
25-
- "db/**/*"
26-
- "config/**/*"
5+
# Your own specialized rules go here

Gemfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ gem "stimulus-rails", "~> 1.3"
4747

4848
gem "active_storage_validations", "~> 3.0"
4949

50+
group :development do
51+
gem "rubocop-rails-omakase", require: false
52+
end
53+
5054
group :development, :test do
5155
gem "better_errors"
5256
gem "brakeman", "~> 7.1.2", require: false

Gemfile.lock

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ GEM
9090
apipie-rails (1.5.0)
9191
actionpack (>= 5.0)
9292
activesupport (>= 5.0)
93+
ast (2.4.3)
9394
aws-eventstream (1.4.0)
9495
aws-partitions (1.1198.0)
9596
aws-sdk-core (3.240.0)
@@ -223,6 +224,8 @@ GEM
223224
marcel (~> 1.0.1)
224225
mime-types
225226
terrapin (~> 0.6.0)
227+
language_server-protocol (3.17.0.5)
228+
lint_roller (1.1.0)
226229
listen (3.9.0)
227230
rb-fsevent (~> 0.10, >= 0.10.3)
228231
rb-inotify (~> 0.9, >= 0.9.10)
@@ -266,6 +269,10 @@ GEM
266269
mini_portile2 (~> 2.8.2)
267270
racc (~> 1.4)
268271
orm_adapter (0.5.0)
272+
parallel (1.27.0)
273+
parser (3.3.10.0)
274+
ast (~> 2.4.1)
275+
racc
269276
polyglot (0.3.5)
270277
positioning (0.4.7)
271278
activerecord (>= 6.1)
@@ -281,6 +288,7 @@ GEM
281288
net-smtp
282289
premailer (~> 1.7, >= 1.7.9)
283290
prettyprint (0.2.0)
291+
prism (1.7.0)
284292
pry (0.15.2)
285293
coderay (~> 1.1)
286294
method_source (~> 1.0)
@@ -335,6 +343,7 @@ GEM
335343
thor (~> 1.0, >= 1.2.2)
336344
tsort (>= 0.2)
337345
zeitwerk (~> 2.6)
346+
rainbow (3.1.1)
338347
rake (13.3.0)
339348
rb-fsevent (0.11.2)
340349
rb-inotify (0.11.1)
@@ -370,6 +379,35 @@ GEM
370379
rspec-mocks (~> 3.13)
371380
rspec-support (~> 3.13)
372381
rspec-support (3.13.5)
382+
rubocop (1.82.1)
383+
json (~> 2.3)
384+
language_server-protocol (~> 3.17.0.2)
385+
lint_roller (~> 1.1.0)
386+
parallel (~> 1.10)
387+
parser (>= 3.3.0.2)
388+
rainbow (>= 2.2.2, < 4.0)
389+
regexp_parser (>= 2.9.3, < 3.0)
390+
rubocop-ast (>= 1.48.0, < 2.0)
391+
ruby-progressbar (~> 1.7)
392+
unicode-display_width (>= 2.4.0, < 4.0)
393+
rubocop-ast (1.49.0)
394+
parser (>= 3.3.7.2)
395+
prism (~> 1.7)
396+
rubocop-performance (1.26.1)
397+
lint_roller (~> 1.1)
398+
rubocop (>= 1.75.0, < 2.0)
399+
rubocop-ast (>= 1.47.1, < 2.0)
400+
rubocop-rails (2.34.2)
401+
activesupport (>= 4.2.0)
402+
lint_roller (~> 1.1)
403+
rack (>= 1.1)
404+
rubocop (>= 1.75.0, < 2.0)
405+
rubocop-ast (>= 1.44.0, < 2.0)
406+
rubocop-rails-omakase (1.1.0)
407+
rubocop (>= 1.72)
408+
rubocop-performance (>= 1.24)
409+
rubocop-rails (>= 2.30)
410+
ruby-progressbar (1.13.0)
373411
ruby-vips (2.2.5)
374412
ffi (~> 1.12)
375413
logger
@@ -422,6 +460,9 @@ GEM
422460
uglifier (4.2.1)
423461
execjs (>= 0.3.0, < 3)
424462
unaccent (0.4.0)
463+
unicode-display_width (3.2.0)
464+
unicode-emoji (~> 4.1)
465+
unicode-emoji (4.2.0)
425466
uri (1.0.4)
426467
useragent (0.16.11)
427468
vite_rails (3.0.19)
@@ -486,6 +527,7 @@ DEPENDENCIES
486527
rack-cors
487528
rails (~> 8.1.0)
488529
rspec-rails
530+
rubocop-rails-omakase
489531
search_cop (~> 1.0.6)
490532
selenium-webdriver
491533
shoulda-matchers

Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ Rails.application.load_tasks
66
desc "Build local docker image"
77
task "build:docker" do |t, args|
88
sh "docker build --no-cache -t awbw-portal ."
9-
end
9+
end
Lines changed: 56 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,58 @@
11
module Admin
2-
class AnalyticsController < Admin::BaseController
3-
protect_from_forgery with: :null_session
4-
5-
def index
6-
@most_viewed_workshops = Workshop.published.order(view_count: :desc, created_at: :desc).limit(10).decorate
7-
@most_viewed_workshop_variations = WorkshopVariation.published.order(view_count: :desc, created_at: :desc).limit(10).decorate
8-
@most_viewed_resources = Resource.published.order(view_count: :desc, created_at: :desc).limit(10).decorate
9-
@most_viewed_community_news = CommunityNews.published.order(view_count: :desc, created_at: :desc).limit(10).decorate
10-
@most_viewed_stories = Story.published.order(view_count: :desc, created_at: :desc).limit(10).decorate
11-
@most_viewed_quotes = Quote.published.order(view_count: :desc, created_at: :desc).limit(10).decorate
12-
@most_viewed_tutorials = Tutorial.published.order(view_count: :desc, created_at: :desc).limit(10).decorate
13-
@most_viewed_projects = Project.published.order(view_count: :desc, created_at: :desc).limit(10).decorate
14-
@most_viewed_events = Event.published.order(view_count: :desc, created_at: :desc).limit(10).decorate
15-
@most_viewed_facilitators = Facilitator.published.order(view_count: :desc, created_at: :desc).limit(10).decorate
16-
17-
@most_printed_workshops = Workshop.published.order(print_count: :desc, created_at: :desc).limit(10).decorate
18-
@most_downloaded_resources = Resource.published.order(download_count: :desc, created_at: :desc).limit(10).decorate
19-
20-
@zero_engagement_workshops = Workshop.published.where(view_count: 0).order(created_at: :desc).limit(10).decorate
21-
@zero_engagement_resources = Resource.published.where(view_count: 0).order(created_at: :desc).limit(10).decorate
22-
23-
@summary = {
24-
workshops: Workshop.sum(:view_count),
25-
workshop_prints: Workshop.sum(:print_count),
26-
resources: Resource.sum(:view_count),
27-
resource_downloads: Resource.sum(:download_count),
28-
community_news: CommunityNews.sum(:view_count),
29-
stories: Story.sum(:view_count),
30-
events: Event.sum(:view_count),
31-
workshop_variations: WorkshopVariation.sum(:view_count),
32-
quotes: Quote.sum(:view_count),
33-
tutorials: Tutorial.sum(:view_count),
34-
projects: Project.sum(:view_count),
35-
facilitators: Facilitator.sum(:view_count)
36-
}
37-
end
38-
39-
def print
40-
printable_models = {
41-
"Resource" => Resource,
42-
"Story" => Story,
43-
"Workshop" => Workshop,
44-
"CommunityNews" => CommunityNews
45-
}.freeze
46-
47-
klass = printable_models[params[:printable_type]]
48-
return head :bad_request unless klass
49-
50-
record = klass.find_by(id: params[:printable_id])
51-
return head :not_found unless record
52-
53-
record.increment_print_count!
54-
55-
head :ok
56-
end
57-
end
58-
2+
class AnalyticsController < Admin::BaseController
3+
protect_from_forgery with: :null_session
4+
5+
def index
6+
@most_viewed_workshops = Workshop.published.order(view_count: :desc, created_at: :desc).limit(10).decorate
7+
@most_viewed_workshop_variations = WorkshopVariation.published.order(view_count: :desc, created_at: :desc).limit(10).decorate
8+
@most_viewed_resources = Resource.published.order(view_count: :desc, created_at: :desc).limit(10).decorate
9+
@most_viewed_community_news = CommunityNews.published.order(view_count: :desc, created_at: :desc).limit(10).decorate
10+
@most_viewed_stories = Story.published.order(view_count: :desc, created_at: :desc).limit(10).decorate
11+
@most_viewed_quotes = Quote.published.order(view_count: :desc, created_at: :desc).limit(10).decorate
12+
@most_viewed_tutorials = Tutorial.published.order(view_count: :desc, created_at: :desc).limit(10).decorate
13+
@most_viewed_projects = Project.published.order(view_count: :desc, created_at: :desc).limit(10).decorate
14+
@most_viewed_events = Event.published.order(view_count: :desc, created_at: :desc).limit(10).decorate
15+
@most_viewed_facilitators = Facilitator.published.order(view_count: :desc, created_at: :desc).limit(10).decorate
16+
17+
@most_printed_workshops = Workshop.published.order(print_count: :desc, created_at: :desc).limit(10).decorate
18+
@most_downloaded_resources = Resource.published.order(download_count: :desc, created_at: :desc).limit(10).decorate
19+
20+
@zero_engagement_workshops = Workshop.published.where(view_count: 0).order(created_at: :desc).limit(10).decorate
21+
@zero_engagement_resources = Resource.published.where(view_count: 0).order(created_at: :desc).limit(10).decorate
22+
23+
@summary = {
24+
workshops: Workshop.sum(:view_count),
25+
workshop_prints: Workshop.sum(:print_count),
26+
resources: Resource.sum(:view_count),
27+
resource_downloads: Resource.sum(:download_count),
28+
community_news: CommunityNews.sum(:view_count),
29+
stories: Story.sum(:view_count),
30+
events: Event.sum(:view_count),
31+
workshop_variations: WorkshopVariation.sum(:view_count),
32+
quotes: Quote.sum(:view_count),
33+
tutorials: Tutorial.sum(:view_count),
34+
projects: Project.sum(:view_count),
35+
facilitators: Facilitator.sum(:view_count)
36+
}
37+
end
38+
39+
def print
40+
printable_models = {
41+
"Resource" => Resource,
42+
"Story" => Story,
43+
"Workshop" => Workshop,
44+
"CommunityNews" => CommunityNews
45+
}.freeze
46+
47+
klass = printable_models[params[:printable_type]]
48+
return head :bad_request unless klass
49+
50+
record = klass.find_by(id: params[:printable_id])
51+
return head :not_found unless record
52+
53+
record.increment_print_count!
54+
55+
head :ok
56+
end
57+
end
5958
end
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
module Admin
2-
class BaseController < ApplicationController
3-
before_action :require_super_user
2+
class BaseController < ApplicationController
3+
before_action :require_super_user
44

5-
private
5+
private
66

7-
def require_super_user
8-
redirect_to authenticated_root_path, alert: "Not authorized" unless current_user&.super_user?
9-
end
10-
end
7+
def require_super_user
8+
redirect_to authenticated_root_path, alert: "Not authorized" unless current_user&.super_user?
9+
end
10+
end
1111
end

app/controllers/api/v1/api_controller.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Api::V1::ApiController < ActionController::API
22
include ActionController::RequestForgeryProtection
3-
protect_from_forgery with: :null_session, :if => Proc.new { |c| c.request.format.json? }
3+
protect_from_forgery with: :null_session, if: Proc.new { |c| c.request.format.json? }
44

55
rescue_from AuthenticationFailed, with: :authentication_failed
66
before_action :authenticate_api_user!
@@ -18,17 +18,17 @@ def current_api_user
1818
end
1919

2020
def authentication_failed(exception)
21-
render json: {error: exception.message}, status: :unauthorized
21+
render json: { error: exception.message }, status: :unauthorized
2222
end
2323

2424
private
2525

2626
def http_auth_header_content
27-
if request.headers['Authorization'].present?
27+
if request.headers["Authorization"].present?
2828
@http_auth_header_content ||=
29-
request.headers['Authorization'].split(' ').last
29+
request.headers["Authorization"].split(" ").last
3030
else
31-
@http_auth_header_content ||= params['Authorization']
31+
@http_auth_header_content ||= params["Authorization"]
3232
end
3333
end
3434
end
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
class Api::V1::AuthenticationsController < Api::V1::ApiController
22
skip_before_action :authenticate_api_user!
3-
api :POST, '/v1/authentications'
4-
param :email, String, 'User\'s email', required: true
5-
param :password, String, 'User\'s password', required: true
6-
desc 'With valid credentials, return\'s an API Authorization token'
3+
api :POST, "/v1/authentications"
4+
param :email, String, "User's email", required: true
5+
param :password, String, "User's password", required: true
6+
desc "With valid credentials, return's an API Authorization token"
77
def create
88
user = User.find_by_email(params[:email])
99
if user.present? && user.valid_password?(params[:password])
@@ -12,7 +12,7 @@ def create
1212
token = AuthenticationToken.generate_token(user.id).to_s
1313
render json: { token: token, user: user, url: "#{request.base_url}/?Authorization=#{token}" }, status: :created
1414
else
15-
render json: { error: 'Not Authorized', status: :unauthorized }
15+
render json: { error: "Not Authorized", status: :unauthorized }
1616
end
1717
end
1818
end

app/controllers/api/v1/quotes_controller.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
class Api::V1::QuotesController < Api::V1::ApiController
2-
api :GET, '/v1/quotes'
3-
param :Authorization, String, desc: 'API Authorization token returned by successful'\
4-
' user authentication', required: true
2+
api :GET, "/v1/quotes"
3+
param :Authorization, String, desc: "API Authorization token returned by successful"\
4+
" user authentication", required: true
55
param :sector, String, desc: "One of #{Sector.all.pluck(:name).join(', ')}"
6-
desc 'Without a Sector parameter, gives a list of all quotes grouped by Sector.'
6+
desc "Without a Sector parameter, gives a list of all quotes grouped by Sector."
77
def index
88
sector = Sector.find_by(name: params[:sector]) || Sector.all
9-
quotes = sector.to_json(only: :name, :include => {
10-
:quotes => { only: :quote }
9+
quotes = sector.to_json(only: :name, include: {
10+
quotes: { only: :quote }
1111
})
1212
render json: { quotes: quotes }, status: :ok
1313
end

0 commit comments

Comments
 (0)