diff --git a/lib/ckeditor.rb b/lib/ckeditor.rb index 98025d8d4..cd8eacbf6 100644 --- a/lib/ckeditor.rb +++ b/lib/ckeditor.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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' diff --git a/lib/ckeditor/hooks/action_policy.rb b/lib/ckeditor/hooks/action_policy.rb index 23af57709..154d8c3b2 100644 --- a/lib/ckeditor/hooks/action_policy.rb +++ b/lib/ckeditor/hooks/action_policy.rb @@ -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 diff --git a/lib/ckeditor/hooks/cancan.rb b/lib/ckeditor/hooks/cancan.rb index 7a96571d5..3337774eb 100644 --- a/lib/ckeditor/hooks/cancan.rb +++ b/lib/ckeditor/hooks/cancan.rb @@ -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 diff --git a/lib/ckeditor/hooks/pundit.rb b/lib/ckeditor/hooks/pundit.rb index f1cfb4cc9..0bdb7cbce 100644 --- a/lib/ckeditor/hooks/pundit.rb +++ b/lib/ckeditor/hooks/pundit.rb @@ -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 diff --git a/lib/generators/ckeditor/install_generator.rb b/lib/generators/ckeditor/install_generator.rb index e56ac9e22..623d826e3 100644 --- a/lib/generators/ckeditor/install_generator.rb +++ b/lib/generators/ckeditor/install_generator.rb @@ -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')) @@ -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 @@ -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 @@ -102,7 +102,7 @@ def orm end def backend - (options[:backend] || 'paperclip').to_s + (options[:backend] || 'active_storage').to_s end end end