Skip to content

Commit

Permalink
chore: move const authorization_adapters to class method
Browse files Browse the repository at this point in the history
  • Loading branch information
galetahub committed Jan 23, 2025
1 parent 24fcda5 commit 476401e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 28 deletions.
38 changes: 19 additions & 19 deletions lib/ckeditor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ module Backend

DEFAULT_AUTHORIZE = -> {}

AUTHORIZATION_ADAPTERS = {}

DEFAULT_CURRENT_USER = lambda do
request.env['warden'].try(:user) || respond_to?(:current_user) && current_user
end
Expand Down Expand Up @@ -127,11 +125,11 @@ def self.setup
end

def self.root_path
@root_path ||= Pathname.new(File.dirname(File.expand_path('../', __FILE__)))
@root_path ||= Pathname.new(File.dirname(File.expand_path(__dir__)))
end

def self.base_path
@base_path ||= (asset_path || File.join([Rails.application.config.assets.prefix, '/ckeditor/']))
@base_path ||= asset_path || File.join([Rails.application.config.assets.prefix, '/ckeditor/'])
end

# All css and js files from ckeditor folder
Expand Down Expand Up @@ -163,13 +161,12 @@ def self.picture_model(&block)
if block_given?
self.picture_model = block
else
@@picture_model_class ||= begin
if @@picture_model.respond_to? :call
@@picture_model.call
else
@@picture_model || Ckeditor::Picture
end
end
@@picture_model_class ||= if @@picture_model.respond_to? :call
@@picture_model.call
else
@@picture_model || Ckeditor::Picture
end

end
end

Expand All @@ -186,13 +183,12 @@ def self.attachment_file_model(&block)
if block_given?
self.attachment_file_model = block
else
@@attachment_file_model_class ||= begin
if @@attachment_file_model.respond_to? :call
@@attachment_file_model.call
else
@@attachment_file_model || Ckeditor::AttachmentFile
end
end
@@attachment_file_model_class ||= if @@attachment_file_model.respond_to? :call
@@attachment_file_model.call
else
@@attachment_file_model || Ckeditor::AttachmentFile
end

end
end

Expand Down Expand Up @@ -230,7 +226,7 @@ def self.authorize_with(*args, &block)

if extension
@authorize = lambda do
@authorization_adapter = Ckeditor::AUTHORIZATION_ADAPTERS[extension].new(*([self] + args).compact)
@authorization_adapter = Ckeditor.authorization_adapters[extension].new(*([self] + args).compact)
end
elsif block_given?
@authorize = block
Expand Down Expand Up @@ -260,6 +256,10 @@ def self.assets_pipeline_enabled?
@@assets_pipeline_enabled = Utils.assets_pipeline_enabled? if @@assets_pipeline_enabled.nil?
@@assets_pipeline_enabled
end

def self.authorization_adapters
@authorization_adapters ||= {}
end
end

require 'ckeditor/rails'
Expand Down
2 changes: 1 addition & 1 deletion lib/ckeditor/hooks/action_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ def authorized?(action, model_object = nil)
end
end

Ckeditor::AUTHORIZATION_ADAPTERS[:action_policy] = Ckeditor::Hooks::ActionPolicyAuthorization
Ckeditor.authorization_adapters[:action_policy] = Ckeditor::Hooks::ActionPolicyAuthorization
4 changes: 2 additions & 2 deletions lib/ckeditor/hooks/cancan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ def current_ability
end
end

Ckeditor::AUTHORIZATION_ADAPTERS[:cancan] = Ckeditor::Hooks::CanCanAuthorization
Ckeditor::AUTHORIZATION_ADAPTERS[:cancancan] = Ckeditor::Hooks::CanCanAuthorization
Ckeditor.authorization_adapters[:cancan] = Ckeditor::Hooks::CanCanAuthorization
Ckeditor.authorization_adapters[:cancancan] = Ckeditor::Hooks::CanCanAuthorization
2 changes: 1 addition & 1 deletion lib/ckeditor/hooks/pundit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ def current_user_for_pundit
end
end

Ckeditor::AUTHORIZATION_ADAPTERS[:pundit] = Ckeditor::Hooks::PunditAuthorization
Ckeditor.authorization_adapters[:pundit] = Ckeditor::Hooks::PunditAuthorization
10 changes: 5 additions & 5 deletions lib/generators/ckeditor/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class InstallGenerator < Rails::Generators::Base
class_option :orm, type: :string, default: 'active_record',
desc: 'Backend processor for upload support'

class_option :backend, type: :string, default: 'paperclip',
desc: 'paperclip (default), active_storage, carrierwave or dragonfly'
class_option :backend, type: :string, default: 'active_storage',
desc: 'active_storage (default), paperclip, carrierwave or dragonfly'

def self.source_root
@source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
Expand Down Expand Up @@ -43,7 +43,7 @@ def mount_engine
end

def create_models
[:asset, :picture, :attachment_file].each do |filename|
%w[asset picture attachment_file].each do |filename|
template "#{generator_dir}/ckeditor/#{filename}.rb",
File.join('app/models', ckeditor_dir, "#{filename}.rb")
end
Expand All @@ -67,7 +67,7 @@ def create_uploaders
def create_ckeditor_migration
return unless ['active_record'].include?(orm)

migration_template "#{generator_dir}/migration.rb",
migration_template "#{generator_dir}/create_ckeditor_assets.rb",
File.join('db/migrate', 'create_ckeditor_assets.rb')
end

Expand Down Expand Up @@ -102,7 +102,7 @@ def orm
end

def backend
(options[:backend] || 'paperclip').to_s
(options[:backend] || 'active_storage').to_s
end
end
end
Expand Down

0 comments on commit 476401e

Please sign in to comment.