From c6c20e173c38bf46bcde978ead35323374a24ce9 Mon Sep 17 00:00:00 2001 From: aki Date: Thu, 9 Nov 2023 08:58:40 +0900 Subject: [PATCH] refactor: Use blazer-plus gem --- Gemfile.lock | 95 +++++++++++++++++++ .../blazer_queries_controller.rb | 40 -------- lib/sg_fargate_rails/config.rb | 2 - lib/sg_fargate_rails/railtie.rb | 9 +- sg_fargate_rails.gemspec | 1 + 5 files changed, 98 insertions(+), 49 deletions(-) delete mode 100644 lib/sg_fargate_rails/blazer_queries_controller.rb diff --git a/Gemfile.lock b/Gemfile.lock index 463a58e..2ad2625 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -4,12 +4,38 @@ PATH sg_fargate_rails (0.1.11) aws-sdk-ec2 (~> 1.413) aws-sdk-scheduler (~> 1.10) + blazer-plus lograge (~> 0.12) puma GEM remote: https://rubygems.org/ specs: + actioncable (7.0.4.2) + actionpack (= 7.0.4.2) + activesupport (= 7.0.4.2) + nio4r (~> 2.0) + websocket-driver (>= 0.6.1) + actionmailbox (7.0.4.2) + actionpack (= 7.0.4.2) + activejob (= 7.0.4.2) + activerecord (= 7.0.4.2) + activestorage (= 7.0.4.2) + activesupport (= 7.0.4.2) + mail (>= 2.7.1) + net-imap + net-pop + net-smtp + actionmailer (7.0.4.2) + actionpack (= 7.0.4.2) + actionview (= 7.0.4.2) + activejob (= 7.0.4.2) + activesupport (= 7.0.4.2) + mail (~> 2.5, >= 2.5.4) + net-imap + net-pop + net-smtp + rails-dom-testing (~> 2.0) actionpack (7.0.4.2) actionview (= 7.0.4.2) activesupport (= 7.0.4.2) @@ -17,12 +43,34 @@ GEM rack-test (>= 0.6.3) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.2.0) + actiontext (7.0.4.2) + actionpack (= 7.0.4.2) + activerecord (= 7.0.4.2) + activestorage (= 7.0.4.2) + activesupport (= 7.0.4.2) + globalid (>= 0.6.0) + nokogiri (>= 1.8.5) actionview (7.0.4.2) activesupport (= 7.0.4.2) builder (~> 3.1) erubi (~> 1.4) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.1, >= 1.2.0) + activejob (7.0.4.2) + activesupport (= 7.0.4.2) + globalid (>= 0.3.6) + activemodel (7.0.4.2) + activesupport (= 7.0.4.2) + activerecord (7.0.4.2) + activemodel (= 7.0.4.2) + activesupport (= 7.0.4.2) + activestorage (7.0.4.2) + actionpack (= 7.0.4.2) + activejob (= 7.0.4.2) + activerecord (= 7.0.4.2) + activesupport (= 7.0.4.2) + marcel (~> 1.0) + mini_mime (>= 1.1.0) activesupport (7.0.4.2) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (>= 1.6, < 2) @@ -43,11 +91,23 @@ GEM aws-sigv4 (~> 1.1) aws-sigv4 (1.6.0) aws-eventstream (~> 1, >= 1.0.2) + blazer (3.0.1) + activerecord (>= 6.1) + chartkick (>= 5) + railties (>= 6.1) + safely_block (>= 0.4) + blazer-plus (0.1.0) + blazer + rails builder (3.2.4) + chartkick (5.0.4) concurrent-ruby (1.2.0) crass (1.0.6) + date (3.3.4) diff-lcs (1.5.0) erubi (1.12.0) + globalid (1.2.1) + activesupport (>= 6.1) i18n (1.12.0) concurrent-ruby (~> 1.0) jmespath (1.6.2) @@ -59,8 +119,24 @@ GEM loofah (2.19.1) crass (~> 1.0.2) nokogiri (>= 1.5.9) + mail (2.8.1) + mini_mime (>= 0.1.1) + net-imap + net-pop + net-smtp + marcel (1.0.2) method_source (1.0.0) + mini_mime (1.1.5) minitest (5.17.0) + net-imap (0.4.4) + date + net-protocol + net-pop (0.1.2) + net-protocol + net-protocol (0.2.2) + timeout + net-smtp (0.4.0) + net-protocol nio4r (2.5.9) nokogiri (1.14.2-arm64-darwin) racc (~> 1.4) @@ -72,6 +148,20 @@ GEM rack (2.2.6.2) rack-test (2.0.2) rack (>= 1.3) + rails (7.0.4.2) + actioncable (= 7.0.4.2) + actionmailbox (= 7.0.4.2) + actionmailer (= 7.0.4.2) + actionpack (= 7.0.4.2) + actiontext (= 7.0.4.2) + actionview (= 7.0.4.2) + activejob (= 7.0.4.2) + activemodel (= 7.0.4.2) + activerecord (= 7.0.4.2) + activestorage (= 7.0.4.2) + activesupport (= 7.0.4.2) + bundler (>= 1.15.0) + railties (= 7.0.4.2) rails-dom-testing (2.0.3) activesupport (>= 4.2.0) nokogiri (>= 1.6) @@ -100,9 +190,14 @@ GEM diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.12.0) rspec-support (3.12.1) + safely_block (0.4.0) thor (1.2.1) + timeout (0.4.1) tzinfo (2.0.6) concurrent-ruby (~> 1.0) + websocket-driver (0.7.6) + websocket-extensions (>= 0.1.0) + websocket-extensions (0.1.5) zeitwerk (2.6.7) PLATFORMS diff --git a/lib/sg_fargate_rails/blazer_queries_controller.rb b/lib/sg_fargate_rails/blazer_queries_controller.rb deleted file mode 100644 index d916b86..0000000 --- a/lib/sg_fargate_rails/blazer_queries_controller.rb +++ /dev/null @@ -1,40 +0,0 @@ -module SgFargateRails - module BlazerQueriesController - extend ActiveSupport::Concern - - prepended do - before_action :require_sg_admin_if_blazer_danger_action! - end - - def csv_data(columns, rows, data_source) - # UTF-8のCSVをエクセルで文字化けせずに開くために先頭にBOMをつけている - "\xEF\xBB\xBF#{super}" - end - - # NOTE: 表示件数の上限を設定可能にしている https://github.com/ankane/blazer/pull/311 - def render_run - row_limit = Blazer.settings.fetch('row_limit', 5000) - if request.format == :html && row_limit - @row_limit ||= row_limit - end - - super - end - - private - - def require_sg_admin_if_blazer_danger_action! - return unless action_name.in?(%w[new edit create update destroy]) - unless blazer_sg_admin? - Rails.logger.warn "BlazerQueriesController##{action_name} is called by non sg_admin (#{blazer_user&.class&.name}: #{blazer_user&.id})" - render plain: '403 Forbidden', status: :forbidden - end - end - - def blazer_sg_admin? - return false unless blazer_user - - blazer_user.respond_to?(:sg_admin?) ? blazer_user.sg_admin? : blazer_user.email.ends_with?('@sonicgarden.jp') - end - end -end diff --git a/lib/sg_fargate_rails/config.rb b/lib/sg_fargate_rails/config.rb index 31552b1..5739e67 100644 --- a/lib/sg_fargate_rails/config.rb +++ b/lib/sg_fargate_rails/config.rb @@ -2,7 +2,6 @@ module SgFargateRails class Config attr_reader :proxy_ip_addresses attr_accessor :middleware_enabled - attr_accessor :blazer_extension_enabled # NOTE: good_jobダッシュボードへのアクセスをproxy経由のアクセスに制限するかどうか attr_accessor :restrict_access_to_good_job_dashboard @@ -11,7 +10,6 @@ def initialize self.proxy_ip_addresses = ENV['SG_PROXY_IP_ADDRESSES'] self.restrict_access_to_good_job_dashboard = Rails.env.production? self.middleware_enabled = !Rails.env.development? && !Rails.env.test? - self.blazer_extension_enabled = defined?(Blazer) end def proxy_ip_addresses=(ip_addresses) diff --git a/lib/sg_fargate_rails/railtie.rb b/lib/sg_fargate_rails/railtie.rb index 80e8b6f..7e9c199 100644 --- a/lib/sg_fargate_rails/railtie.rb +++ b/lib/sg_fargate_rails/railtie.rb @@ -3,7 +3,6 @@ require 'sg_fargate_rails/maintenance' require 'sg_fargate_rails/remote_ip' require 'sg_fargate_rails/task_protection' -require 'sg_fargate_rails/blazer_queries_controller' module SgFargateRails class Railtie < ::Rails::Railtie @@ -19,6 +18,8 @@ class Railtie < ::Rails::Railtie app.config.middleware.insert_after SgFargateRails::RemoteIp, SgFargateRails::Maintenance end + Blazer::Plus.blazer_danger_actionable_method ||= ->(blazer_user) { blazer_user.email.ends_with?('@sonicgarden.jp') } + ActiveSupport.on_load(:good_job_application_controller) do before_action :sg_fargate_rails_proxy_access!, if: -> { SgFargateRails.config.restrict_access_to_good_job_dashboard } @@ -28,12 +29,6 @@ def sg_fargate_rails_proxy_access! end end end - - Rails.application.reloader.to_prepare do - if SgFargateRails.config.blazer_extension_enabled - Blazer::QueriesController.prepend(SgFargateRails::BlazerQueriesController) - end - end end end end diff --git a/sg_fargate_rails.gemspec b/sg_fargate_rails.gemspec index 3017e6b..33e9a9b 100644 --- a/sg_fargate_rails.gemspec +++ b/sg_fargate_rails.gemspec @@ -40,6 +40,7 @@ Gem::Specification.new do |spec| spec.add_dependency 'lograge', '~> 0.12' spec.add_dependency 'aws-sdk-ec2', '~> 1.413' spec.add_dependency 'aws-sdk-scheduler', '~> 1.10' + spec.add_dependency 'blazer-plus' spec.add_development_dependency 'rspec' end