Skip to content

Commit

Permalink
DEV: Update rubocop-discourse to latest version
Browse files Browse the repository at this point in the history
  • Loading branch information
Flink committed May 17, 2024
1 parent 826daf3 commit 5e1855a
Show file tree
Hide file tree
Showing 20 changed files with 191 additions and 116 deletions.
45 changes: 36 additions & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,47 +1,74 @@
GEM
remote: https://rubygems.org/
specs:
activesupport (7.1.3.3)
base64
bigdecimal
concurrent-ruby (~> 1.0, >= 1.0.2)
connection_pool (>= 2.2.5)
drb
i18n (>= 1.6, < 2)
minitest (>= 5.1)
mutex_m
tzinfo (~> 2.0)
ast (2.4.2)
json (2.7.1)
base64 (0.2.0)
bigdecimal (3.1.8)
concurrent-ruby (1.2.3)
connection_pool (2.4.1)
drb (2.2.1)
i18n (1.14.5)
concurrent-ruby (~> 1.0)
json (2.7.2)
language_server-protocol (3.17.0.3)
minitest (5.23.0)
mutex_m (0.2.0)
parallel (1.24.0)
parser (3.3.0.4)
parser (3.3.1.0)
ast (~> 2.4.1)
racc
prettier_print (1.2.1)
racc (1.7.3)
rainbow (3.1.1)
regexp_parser (2.9.0)
regexp_parser (2.9.2)
rexml (3.2.8)
strscan (>= 3.0.9)
rubocop (1.60.0)
rubocop (1.63.5)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.3.0.2)
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)
rubocop-ast (>= 1.31.1, < 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-ast (1.31.3)
parser (>= 3.3.1.0)
rubocop-capybara (2.20.0)
rubocop (~> 1.41)
rubocop-discourse (3.6.0)
rubocop-discourse (3.7.1)
activesupport (>= 6.1)
rubocop (>= 1.59.0)
rubocop-capybara (>= 2.0.0)
rubocop-factory_bot (>= 2.0.0)
rubocop-rspec (>= 2.25.0)
rubocop-factory_bot (2.25.1)
rubocop (~> 1.41)
rubocop-rspec (2.26.1)
rubocop-rspec (2.29.2)
rubocop (~> 1.40)
rubocop-capybara (~> 2.17)
rubocop-factory_bot (~> 2.22)
rubocop-rspec_rails (~> 2.28)
rubocop-rspec_rails (2.28.3)
rubocop (~> 1.40)
ruby-progressbar (1.13.0)
strscan (3.1.0)
syntax_tree (6.2.0)
prettier_print (>= 1.2.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.5.0)

PLATFORMS
Expand Down
File renamed without changes.
13 changes: 13 additions & 0 deletions lib/discourse_staff_alias/post_extension.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module DiscourseStaffAlias
module PostExtension
extend ActiveSupport::Concern

prepended do
has_many :users_posts_links,
class_name: "DiscourseStaffAlias::UsersPostsLink",
dependent: :delete_all
end
end
end
13 changes: 13 additions & 0 deletions lib/discourse_staff_alias/post_revision_extension.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module DiscourseStaffAlias
module PostRevisionExtension
extend ActiveSupport::Concern

prepended do
has_many :users_post_revisions_links,
class_name: "DiscourseStaffAlias::UsersPostRevisionsLink",
dependent: :delete_all
end
end
end
31 changes: 31 additions & 0 deletions lib/discourse_staff_alias/posts_controller_extension.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

module DiscourseStaffAlias
module PostsControllerExtension
extend ActiveSupport::Concern

prepended do
include WithCurrentUser

around_action do |controller, action|
if DiscourseStaffAlias::CONTROLLER_ACTIONS.include?(action_name = controller.action_name) &&
(params = controller.params).dig(
*DiscourseStaffAlias::CONTROLLER_PARAMS[action_name],
) == "true"
existing_user = controller.current_user

if !DiscourseStaffAlias.user_allowed?(existing_user) || params[:whisper]
raise Discourse::InvalidAccess
end

alias_user = DiscourseStaffAlias.alias_user
alias_user.aliased_user = existing_user

controller.with_current_user(alias_user) { action.call }
else
action.call
end
end
end
end
end
27 changes: 27 additions & 0 deletions lib/discourse_staff_alias/topics_controller_extension.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

module DiscourseStaffAlias
module TopicsControllerExtension
extend ActiveSupport::Concern

prepended do
include WithCurrentUser

around_action do |controller, action|
if (action_name = controller.action_name) == "update" &&
(params = controller.params)["as_staff_alias"]
existing_user = controller.current_user

raise Discourse::InvalidAccess if !DiscourseStaffAlias.user_allowed?(existing_user)

alias_user = DiscourseStaffAlias.alias_user
alias_user.aliased_user = existing_user

controller.with_current_user(alias_user) { action.call }
else
action.call
end
end
end
end
end
23 changes: 23 additions & 0 deletions lib/discourse_staff_alias/user_extension.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module DiscourseStaffAlias
module UserExtension
extend ActiveSupport::Concern

prepended do
attr_accessor :aliased_user

has_many :users_posts_links, class_name: "DiscourseStaffAlias::UsersPostsLink"
has_many :users_post_revisions_links,
class_name: "DiscourseStaffAlias::UsersPostRevisionsLink"
end

def can_post_as_staff_alias
@can_post_as_staff_alias ||=
begin
allowed_group_ids = SiteSetting.staff_alias_allowed_groups.split("|")
GroupUser.exists?(user_id: self.id, group_id: allowed_group_ids)
end
end
end
end
16 changes: 16 additions & 0 deletions lib/discourse_staff_alias/with_current_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module DiscourseStaffAlias
module WithCurrentUser
def with_current_user(user)
@current_user = user
yield if block_given?
ensure
@current_user = nil
end

def current_user
@current_user || current_user_provider.current_user
end
end
end
107 changes: 16 additions & 91 deletions plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@

enabled_site_setting :staff_alias_enabled

PLUGIN_NAME ||= "DiscourseStaffAlias"

%w[
lib/discourse-staff-alias/engine.rb
lib/discourse-staff-alias/validators/username_validator.rb
lib/discourse-staff-alias/validators/enabled_validator.rb
].each { |path| load File.expand_path(path, __dir__) }
require_relative "lib/discourse_staff_alias/engine"
require_relative "lib/discourse_staff_alias/validators/username_validator"
require_relative "lib/discourse_staff_alias/validators/enabled_validator"
require_relative "lib/discourse_staff_alias/with_current_user"
require_relative "lib/discourse_staff_alias/user_extension"
require_relative "lib/discourse_staff_alias/post_extension"
require_relative "lib/discourse_staff_alias/post_revision_extension"
require_relative "lib/discourse_staff_alias/topics_controller_extension"
require_relative "lib/discourse_staff_alias/posts_controller_extension"

register_asset "stylesheets/common/discourse-staff-alias.scss"

after_initialize do
# rubocop:disable Discourse/Plugins/UsePluginInstanceOn
DiscourseEvent.on(:site_setting_changed) do |name, _old_value, new_value|
if name.to_s == "staff_alias_username" && new_value.present?
DistributedMutex.synchronize("discourse_staff_alias") do
Expand All @@ -44,92 +47,14 @@
end
end
end
# rubocop:enable Discourse/Plugins/UsePluginInstanceOn

reloadable_patch do
User.class_eval do
attr_accessor :aliased_user

has_many :users_posts_links, class_name: "DiscourseStaffAlias::UsersPostsLink"
has_many :users_post_revisions_links,
class_name: "DiscourseStaffAlias::UsersPostRevisionsLink"

def can_post_as_staff_alias
@can_post_as_staff_alias ||=
begin
allowed_group_ids = SiteSetting.staff_alias_allowed_groups.split("|")
GroupUser.exists?(user_id: self.id, group_id: allowed_group_ids)
end
end
end

module WithCurrentUser
def with_current_user(user)
@current_user = user
yield if block_given?
ensure
@current_user = nil
end

def current_user
@current_user || current_user_provider.current_user
end
end

Post.class_eval do
has_many :users_posts_links,
class_name: "DiscourseStaffAlias::UsersPostsLink",
dependent: :delete_all
end

PostRevision.class_eval do
has_many :users_post_revisions_links,
class_name: "DiscourseStaffAlias::UsersPostRevisionsLink",
dependent: :delete_all
end

TopicsController.class_eval do
include WithCurrentUser

around_action do |controller, action|
if (action_name = controller.action_name) == "update" &&
(params = controller.params)["as_staff_alias"]
existing_user = controller.current_user

raise Discourse::InvalidAccess if !DiscourseStaffAlias.user_allowed?(existing_user)

alias_user = DiscourseStaffAlias.alias_user
alias_user.aliased_user = existing_user

controller.with_current_user(alias_user) { action.call }
else
action.call
end
end
end

PostsController.class_eval do
include WithCurrentUser

around_action do |controller, action|
if DiscourseStaffAlias::CONTROLLER_ACTIONS.include?(action_name = controller.action_name) &&
(params = controller.params).dig(
*DiscourseStaffAlias::CONTROLLER_PARAMS[action_name],
) == "true"
existing_user = controller.current_user

if !DiscourseStaffAlias.user_allowed?(existing_user) || params[:whisper]
raise Discourse::InvalidAccess
end

alias_user = DiscourseStaffAlias.alias_user
alias_user.aliased_user = existing_user

controller.with_current_user(alias_user) { action.call }
else
action.call
end
end
end
User.prepend(DiscourseStaffAlias::UserExtension)
Post.prepend(DiscourseStaffAlias::PostExtension)
PostRevision.prepend(DiscourseStaffAlias::PostRevisionExtension)
TopicsController.prepend(DiscourseStaffAlias::TopicsControllerExtension)
PostsController.prepend(DiscourseStaffAlias::PostsControllerExtension)
end

on(:post_created) do |post, opts, user|
Expand Down
4 changes: 2 additions & 2 deletions spec/models/post_revision_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
require "rails_helper"

describe PostRevision do
fab!(:user) { Fabricate(:user) }
fab!(:post_revision) { Fabricate(:post_revision) }
fab!(:user)
fab!(:post_revision)

fab!(:admin) do
user = Fabricate(:admin)
Expand Down
4 changes: 2 additions & 2 deletions spec/models/post_revisor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
require "rails_helper"

describe PostRevisor do
fab!(:user) { Fabricate(:user) }
fab!(:user)
fab!(:post) { Fabricate(:post, user: user) }

context "when post_edited" do
fab!(:moderator) { Fabricate(:moderator) }
fab!(:moderator)
let(:post_revisor) { PostRevisor.new(post) }

before do
Expand Down
4 changes: 2 additions & 2 deletions spec/models/post_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
require "rails_helper"

describe Post do
fab!(:user) { Fabricate(:user) }
fab!(:post) { Fabricate(:post) }
fab!(:user)
fab!(:post)

it "cleans up users_posts_links association on destroy" do
link = ::DiscourseStaffAlias::UsersPostsLink.create!(user: user, post: post)
Expand Down
8 changes: 4 additions & 4 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
require "rails_helper"

describe User do
fab!(:user) { Fabricate(:user) }
fab!(:post) { Fabricate(:post) }
fab!(:group) { Fabricate(:group) }
fab!(:user)
fab!(:post)
fab!(:group)

fab!(:admin) do
user = Fabricate(:admin)
Expand All @@ -14,7 +14,7 @@
user
end

fab!(:post_revision) { Fabricate(:post_revision) }
fab!(:post_revision)

it "does not clean up users_posts_links association on destroy" do
link = ::DiscourseStaffAlias::UsersPostsLink.create!(user: user, post: post)
Expand Down
Loading

0 comments on commit 5e1855a

Please sign in to comment.