Skip to content

Commit cf13e26

Browse files
DEV: Introduce syntax_tree for ruby formatting (#46)
1 parent e2fd30c commit cf13e26

18 files changed

+156
-123
lines changed

.github/workflows/plugin-linting.yml

+9
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,12 @@ jobs:
5555
- name: Rubocop
5656
if: ${{ !cancelled() }}
5757
run: bundle exec rubocop .
58+
59+
- name: Syntax Tree
60+
if: ${{ !cancelled() }}
61+
run: |
62+
if test -f .streerc; then
63+
bundle exec stree check Gemfile $(git ls-files '*.rb') $(git ls-files '*.rake')
64+
else
65+
echo "Stree config not detected for this repository. Skipping."
66+
fi

.github/workflows/plugin-tests.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ jobs:
8080

8181
- name: Get yarn cache directory
8282
id: yarn-cache-dir
83-
run: echo "::set-output name=dir::$(yarn cache dir)"
83+
run: echo "dir=$(yarn cache dir)" >> $GITHUB_OUTPUT
8484

8585
- name: Yarn cache
8686
uses: actions/cache@v3
@@ -130,7 +130,7 @@ jobs:
130130
shell: bash
131131
run: |
132132
if [ 0 -lt $(find plugins/${{ github.event.repository.name }}/spec -type f -name "*.rb" 2> /dev/null | wc -l) ]; then
133-
echo "::set-output name=files_exist::true"
133+
echo "files_exist=true" >> $GITHUB_OUTPUT
134134
fi
135135
136136
- name: Plugin RSpec
@@ -142,7 +142,7 @@ jobs:
142142
shell: bash
143143
run: |
144144
if [ 0 -lt $(find plugins/${{ github.event.repository.name }}/test/javascripts -type f \( -name "*.js" -or -name "*.es6" \) 2> /dev/null | wc -l) ]; then
145-
echo "::set-output name=files_exist::true"
145+
echo "files_exist=true" >> $GITHUB_OUTPUT
146146
fi
147147
148148
- name: Plugin QUnit

.rubocop.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
inherit_gem:
2-
rubocop-discourse: default.yml
2+
rubocop-discourse: stree-compat.yml

.streerc

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--print-width=100
2+
--plugins=plugin/trailing_comma

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ source "https://rubygems.org"
44

55
group :development do
66
gem "rubocop-discourse"
7+
gem "syntax_tree"
78
end

Gemfile.lock

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ GEM
66
parallel (1.22.1)
77
parser (3.1.2.1)
88
ast (~> 2.4.1)
9+
prettier_print (1.2.0)
910
rainbow (3.1.1)
1011
regexp_parser (2.6.0)
1112
rexml (3.2.5)
@@ -27,6 +28,8 @@ GEM
2728
rubocop-rspec (2.13.2)
2829
rubocop (~> 1.33)
2930
ruby-progressbar (1.11.0)
31+
syntax_tree (5.1.0)
32+
prettier_print (>= 1.2.0)
3033
unicode-display_width (2.3.0)
3134

3235
PLATFORMS
@@ -39,6 +42,7 @@ PLATFORMS
3942

4043
DEPENDENCIES
4144
rubocop-discourse
45+
syntax_tree
4246

4347
BUNDLED WITH
4448
2.3.10

app/controllers/discourse_teambuild/targets_controller.rb

+17-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
# frozen_string_literal: true
22

3-
require_dependency 'teambuild_target'
4-
require_dependency 'teambuild_target_serializer'
3+
require_dependency "teambuild_target"
4+
require_dependency "teambuild_target_serializer"
55

66
module DiscourseTeambuild
77
class TargetsController < ApplicationController
8-
98
requires_login
109
before_action :ensure_enabled
1110

@@ -15,13 +14,16 @@ def index
1514
targets,
1615
TeambuildTargetSerializer,
1716
rest_serializer: true,
18-
root: 'teambuild_targets',
19-
extras: { groups: Group.all }
17+
root: "teambuild_targets",
18+
extras: {
19+
groups: Group.all,
20+
},
2021
)
2122
end
2223

2324
def create
24-
target = TeambuildTarget.create!(params[:teambuild_target].permit(:name, :target_type_id, :group_id))
25+
target =
26+
TeambuildTarget.create!(params[:teambuild_target].permit(:name, :target_type_id, :group_id))
2527
render_serialized(target, TeambuildTargetSerializer, rest_serializer: true)
2628
end
2729

@@ -44,9 +46,15 @@ def swap_position
4446
raise Discourse::NotFound if target_position.nil? || other_position.nil?
4547

4648
TeambuildTarget.transaction do
47-
TeambuildTarget.where(id: params[:target_id], position: target_position).update_all(position: other_position * -1)
48-
TeambuildTarget.where(id: params[:other_id], position: other_position).update_all(position: target_position)
49-
TeambuildTarget.where(id: params[:target_id], position: other_position * -1).update_all(position: other_position)
49+
TeambuildTarget.where(id: params[:target_id], position: target_position).update_all(
50+
position: other_position * -1,
51+
)
52+
TeambuildTarget.where(id: params[:other_id], position: other_position).update_all(
53+
position: target_position,
54+
)
55+
TeambuildTarget.where(id: params[:target_id], position: other_position * -1).update_all(
56+
position: other_position,
57+
)
5058
end
5159
render json: success_json
5260
end

app/controllers/discourse_teambuild/teambuild_controller.rb

+25-25
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
# frozen_string_literal: true
22

3-
require_dependency 'teambuild_target'
4-
require_dependency 'teambuild_progress_serializer'
3+
require_dependency "teambuild_target"
4+
require_dependency "teambuild_progress_serializer"
55

66
module DiscourseTeambuild
77
class TeambuildController < ApplicationController
8-
98
requires_login
109
before_action :ensure_can_access
1110

@@ -19,21 +18,17 @@ def progress
1918

2019
completed = []
2120

22-
progress = {
23-
user: user,
24-
teambuild_targets: TeambuildTarget.all,
25-
completed: completed
26-
}
21+
progress = { user: user, teambuild_targets: TeambuildTarget.all, completed: completed }
2722

28-
TeambuildTargetUser.where(user_id: user.id).each do |t|
29-
completed << "#{t.teambuild_target_id}:#{t.target_user_id}"
30-
end
23+
TeambuildTargetUser
24+
.where(user_id: user.id)
25+
.each { |t| completed << "#{t.teambuild_target_id}:#{t.target_user_id}" }
3126

3227
render_serialized(
3328
progress,
3429
TeambuildProgressSerializer,
3530
rest_serializer: true,
36-
include_users: true
31+
include_users: true,
3732
)
3833
end
3934

@@ -54,32 +49,37 @@ def scores
5449
ORDER BY score DESC, u.username
5550
SQL
5651

57-
scores = results.map do |r|
58-
r.as_json.tap do |result|
59-
result['trophy'] = true if r.rank == 1
60-
result['me'] = r.id == current_user.id
61-
result['avatar_template'] = User.avatar_template(r.username_lower, r.uploaded_avatar_id)
62-
result.delete('uploaded_avatar_id')
52+
scores =
53+
results.map do |r|
54+
r.as_json.tap do |result|
55+
result["trophy"] = true if r.rank == 1
56+
result["me"] = r.id == current_user.id
57+
result["avatar_template"] = User.avatar_template(r.username_lower, r.uploaded_avatar_id)
58+
result.delete("uploaded_avatar_id")
59+
end
6360
end
64-
end
6561

6662
render json: { scores: scores }
6763
end
6864

6965
def complete
70-
TeambuildTargetUser.create!(
71-
user_id: current_user.id,
72-
teambuild_target_id: params[:target_id].to_i,
73-
target_user_id: params[:user_id].to_i
74-
) rescue ActiveRecord::RecordNotUnique
66+
begin
67+
TeambuildTargetUser.create!(
68+
user_id: current_user.id,
69+
teambuild_target_id: params[:target_id].to_i,
70+
target_user_id: params[:user_id].to_i,
71+
)
72+
rescue StandardError
73+
ActiveRecord::RecordNotUnique
74+
end
7575
render json: success_json
7676
end
7777

7878
def undo
7979
TeambuildTargetUser.where(
8080
user_id: current_user.id,
8181
teambuild_target_id: params[:target_id].to_i,
82-
target_user_id: params[:user_id].to_i
82+
target_user_id: params[:user_id].to_i,
8383
).delete_all
8484

8585
render json: success_json

app/models/teambuild_target.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ class TeambuildTarget < ActiveRecord::Base
99
validates_uniqueness_of :name
1010
default_scope { order(:position) }
1111

12-
validates :group_id, presence: true, if: -> { target_type_id == TeambuildTarget.target_types[:user_group] }
12+
validates :group_id,
13+
presence: true,
14+
if: -> { target_type_id == TeambuildTarget.target_types[:user_group] }
1315

1416
def self.target_types
1517
@target_types ||= Enum.new(regular: 1, user_group: 2)
@@ -20,5 +22,4 @@ def self.target_types
2022
def default_position
2123
self.position = (TeambuildTarget.maximum(:position) || 0) + 1
2224
end
23-
2425
end

app/models/teambuild_target_user.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
class TeambuildTargetUser < ActiveRecord::Base
44
belongs_to :user
55
belongs_to :teambuild_target
6-
belongs_to :teambuild_target_user, class_name: 'User'
6+
belongs_to :teambuild_target_user, class_name: "User"
77
end

app/serializers/teambuild_progress_serializer.rb

+1-4
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ def completed
2222
end
2323

2424
def total
25-
teambuild_targets.inject(0) do |total, t|
26-
total + (t.group.present? ? t.group.users.size : 1)
27-
end
25+
teambuild_targets.inject(0) { |total, t| total + (t.group.present? ? t.group.users.size : 1) }
2826
end
29-
3027
end

app/serializers/teambuild_target_serializer.rb

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
# frozen_string_literal: true
22

33
class TeambuildTargetSerializer < ApplicationSerializer
4-
attributes(
5-
:id,
6-
:target_type_id,
7-
:name,
8-
:group_id,
9-
:group_name,
10-
:position
11-
)
4+
attributes(:id, :target_type_id, :name, :group_id, :group_name, :position)
125

136
has_many :users, serializer: BasicUserSerializer
147

config/routes.rb

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
# frozen_string_literal: true
22

33
DiscourseTeambuild::Engine.routes.draw do
4-
get '/' => 'teambuild#index'
5-
get '/scores' => 'teambuild#scores'
6-
get '/manage' => 'teambuild#index', constraints: StaffConstraint.new
7-
get "/progress" => 'teambuild#progress'
8-
get "/progress/:username" => 'teambuild#progress', constraints: { username: RouteFormat.username }
9-
put '/complete/:target_id/:user_id' => 'teambuild#complete'
10-
delete '/undo/:target_id/:user_id' => 'teambuild#undo'
4+
get "/" => "teambuild#index"
5+
get "/scores" => "teambuild#scores"
6+
get "/manage" => "teambuild#index", :constraints => StaffConstraint.new
7+
get "/progress" => "teambuild#progress"
8+
get "/progress/:username" => "teambuild#progress",
9+
:constraints => {
10+
username: RouteFormat.username,
11+
}
12+
put "/complete/:target_id/:user_id" => "teambuild#complete"
13+
delete "/undo/:target_id/:user_id" => "teambuild#undo"
1114

1215
resources :targets, constraints: StaffConstraint.new do
13-
put "/swap-position" => 'targets#swap_position'
16+
put "/swap-position" => "targets#swap_position"
1417
end
1518
end

db/migrate/20191112145431_create_teambuild_target_users.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ def change
99
t.timestamps null: false
1010
end
1111

12-
add_index :teambuild_target_users, [:user_id, :teambuild_target_id, :target_user_id], name: :teambuild_unique_choice, unique: true
12+
add_index :teambuild_target_users,
13+
%i[user_id teambuild_target_id target_user_id],
14+
name: :teambuild_unique_choice,
15+
unique: true
1316
end
1417
end

lib/discourse_teambuild/engine.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module ::DiscourseTeambuild
44
class Engine < ::Rails::Engine
5-
engine_name 'discourse_teambuild'
5+
engine_name "discourse_teambuild"
66
isolate_namespace DiscourseTeambuild
77
end
88
end

plugin.rb

+4-8
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@
77
# url: https://github.com/discourse/discourse-teambuild
88
# transpile_js: true
99

10-
load File.expand_path('../lib/discourse_teambuild/engine.rb', __FILE__)
10+
load File.expand_path("../lib/discourse_teambuild/engine.rb", __FILE__)
1111

1212
enabled_site_setting :teambuild_enabled
1313

1414
register_svg_icon "campground" if respond_to?(:register_svg_icon)
15-
register_asset 'stylesheets/team-build.scss'
15+
register_asset "stylesheets/team-build.scss"
1616

17-
Discourse::Application.routes.append do
18-
mount ::DiscourseTeambuild::Engine, at: "/team-build"
19-
end
17+
Discourse::Application.routes.append { mount ::DiscourseTeambuild::Engine, at: "/team-build" }
2018

2119
after_initialize do
2220
add_to_class(:guardian, :has_teambuild_access?) do
@@ -25,7 +23,5 @@
2523
@user.groups.where(name: SiteSetting.teambuild_access_group).exists?
2624
end
2725

28-
add_to_serializer(:current_user, :can_access_teambuild) do
29-
object.guardian.has_teambuild_access?
30-
end
26+
add_to_serializer(:current_user, :can_access_teambuild) { object.guardian.has_teambuild_access? }
3127
end

0 commit comments

Comments
 (0)