Skip to content

Commit

Permalink
chore: fix code style
Browse files Browse the repository at this point in the history
  • Loading branch information
galetahub committed Jan 23, 2025
1 parent f033648 commit b3aa79f
Show file tree
Hide file tree
Showing 33 changed files with 93 additions and 85 deletions.
15 changes: 15 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ Style/Documentation:
Style/ClassAndModuleChildren:
Enabled: false

Style/RedundantParentheses:
Enabled: false

Style/ZeroLengthPredicate:
Enabled: false

##################### Metrics ##################################

Layout/LineLength:
Expand All @@ -32,7 +38,16 @@ Metrics/ClassLength:
Metrics/ModuleLength:
Max: 200

Metrics/AbcSize:
Max: 25

##################### Rails ##################################

Rails:
Enabled: true

Rails/LexicallyScopedActionFilter:
Enabled: false

Rails/OutputSafety:
Enabled: false
2 changes: 1 addition & 1 deletion app/controllers/ckeditor/attachment_files_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def destroy

respond_to do |format|
format.html { redirect_to attachment_files_path }
format.json { render json: @attachment, status: 204 }
format.json { render json: @attachment, status: :no_content }
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/ckeditor/pictures_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def destroy

respond_to do |format|
format.html { redirect_to pictures_path }
format.json { render json: @picture, status: 204 }
format.json { render json: @picture, status: :no_content }
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/views/ckeditor/attachment_files/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div id="fileupload" class="gal-holder">
<div class="gal-item">
<div class="fileupload-button gal-upload-holder">
<%= link_to I18n.t(:upload, scope: [:ckeditor, :buttons]), 'javascript:void(0)', class: "add" %>
<%= link_to I18n.t('ckeditor.buttons.upload'), 'javascript:void(0)', class: "add" %>
</div>
</div>

Expand Down
2 changes: 1 addition & 1 deletion app/views/ckeditor/pictures/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div id="fileupload" class="gal-holder">
<div class="gal-item">
<div class="fileupload-button gal-upload-holder">
<%= link_to I18n.t(:upload, scope: [:ckeditor, :buttons]), 'javascript:void(0)', class: "add" %>
<%= link_to I18n.t('ckeditor.buttons.upload'), 'javascript:void(0)', class: "add" %>
</div>
</div>

Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/ckeditor/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="robots" content="noindex, nofollow">
<%= csrf_meta_tag %>
<title><%= I18n.t('page_title', scope: [:ckeditor]) %></title>
<title><%= I18n.t('ckeditor.page_title') %></title>
<script src="//code.jquery.com/jquery-1.11.3.min.js" type="text/javascript"></script>

<% if Ckeditor.assets_pipeline_enabled? -%>
Expand Down
4 changes: 2 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

Ckeditor::Engine.routes.draw do
resources :pictures, only: [:index, :create, :destroy]
resources :attachment_files, only: [:index, :create, :destroy]
resources :pictures, only: %i[index create destroy]
resources :attachment_files, only: %i[index create destroy]
end
15 changes: 3 additions & 12 deletions lib/ckeditor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,7 @@ def self.picture_model(&block)
if block_given?
self.picture_model = block
else
@picture_model_class ||= if @picture_model.respond_to? :call
@picture_model.call
else
@picture_model || Ckeditor::Picture
end

@picture_model_class ||= Ckeditor::Utils.call_or_return(@picture_model, Ckeditor::Picture)
end
end

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

@attachment_file_model_class ||= Ckeditor::Utils.call_or_return(@attachment_file_model,
Ckeditor::AttachmentFile)
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/ckeditor/asset_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def json?
end

def ckeditor?
!params[:CKEditor].blank?
params[:CKEditor].present?
end

def file
Expand Down Expand Up @@ -63,7 +63,7 @@ def success_ckeditor(relative_url_root = nil)

def success_default(_relative_url_root = nil)
{
json: asset.to_json(only: [:id, :type])
json: asset.to_json(only: %i[id type])
}
end

Expand Down
19 changes: 5 additions & 14 deletions lib/ckeditor/backend/active_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,25 @@ module ClassMethods
def self.extended(base)
base.class_eval do
before_save :apply_data

validate do
if data.nil? || file.nil?
errors.add(:data, :not_data_present, message: "data must be present")
end
errors.add(:data, :not_data_present, message: 'data must be present') if data.nil? || file.nil?
end
end
end
end

module InstanceMethods
def url
rails_blob_path(self.storage_data, only_path: true)
rails_blob_path(storage_data, only_path: true)
end

def path
rails_blob_path(self.storage_data, only_path: true)
rails_blob_path(storage_data, only_path: true)
end

def styles
end
delegate :content_type, :content_type=, to: :storage_data

def content_type
self.storage_data.content_type
end
def content_type=(_content_type)
self.storage_data.content_type = _content_type
end
protected

def file
Expand All @@ -61,7 +53,6 @@ def apply_data
self.data_content_type = storage_data.blob.content_type
self.data_file_size = storage_data.blob.byte_size
end

end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ckeditor/helpers/controllers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def ckeditor_attachment_files_scope(options = {})
end

def ckeditor_filebrowser_scope(options = {})
{ order: [:id, :desc] }.merge!(options)
{ order: %i[id desc] }.merge!(options)
end
end
end
Expand Down
12 changes: 6 additions & 6 deletions lib/ckeditor/hooks/action_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ def initialize(controller)
# action as a symbol (:create, :destroy, etc.). The second argument is the actual model
# instance if it is available.
def authorize(_action, model_object = nil)
@controller.authorize!(model_object, context: {user: @controller.ckeditor_current_user})
@controller.authorize!(model_object, context: { user: @controller.ckeditor_current_user })
end

# This method is called primarily from the view to determine whether the given user
# has access to perform the action on a given model. It should return true when authorized.
# This takes the same arguments as +authorize+. The difference is that this will
# return a boolean whereas +authorize+ will raise an exception when not authorized.
def authorized?(action, model_object = nil)
if action
@controller.allowed_to?(:"#{action}?",
model_object,
context: {user: @controller.ckeditor_current_user})
end
return false unless action

@controller.allowed_to?(:"#{action}?",
model_object,
context: { user: @controller.ckeditor_current_user })
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/ckeditor/hooks/cancan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ def initialize(controller, ability = ::Ability)
# action as a symbol (:create, :destroy, etc.). The second argument is the actual model
# instance if it is available.
def authorize(action, model_object = nil)
if action
@controller.instance_variable_set(:@_authorized, true)
@controller.current_ability.authorize!(action.to_sym, model_object)
end
return unless action

@controller.instance_variable_set(:@_authorized, true)
@controller.current_ability.authorize!(action.to_sym, model_object)
end

# This method is called primarily from the view to determine whether the given user
Expand Down
2 changes: 1 addition & 1 deletion lib/ckeditor/hooks/pundit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def authorize(action, model_object = nil)
# This takes the same arguments as +authorize+. The difference is that this will
# return a boolean whereas +authorize+ will raise an exception when not authorized.
def authorized?(action, model_object = nil)
Pundit.policy(@controller.current_user_for_pundit, model_object).public_send(action + '?') if action
Pundit.policy(@controller.current_user_for_pundit, model_object).public_send("#{action}?") if action
end

module ControllerExtension
Expand Down
4 changes: 2 additions & 2 deletions lib/ckeditor/orm/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def size
data_file_size
end

def has_dimensions?
def respond_to_dimensions?
respond_to?(:width) && respond_to?(:height)
end

Expand All @@ -34,7 +34,7 @@ def url_thumb
end

def as_json_methods
[:url_content, :url_thumb, :size, :filename, :format_created_at]
%i[url_content url_thumb size filename format_created_at]
end

def as_json(options = nil)
Expand Down
2 changes: 1 addition & 1 deletion lib/ckeditor/orm/mongoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def type
end

def as_json_methods
[:id, :type] + super
%i[id type] + super
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/ckeditor/paginatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def total_count

# Current page number
def current_page
offset = (offset_value < 0 ? 0 : offset_value)
offset = (offset_value.negative? ? 0 : offset_value)
(offset / limit_value) + 1
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/ckeditor/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ class Engine < ::Rails::Engine

initializer 'ckeditor.helpers' do
ActiveSupport.on_load(:action_controller) do
ActionController::Base.send :include, Ckeditor::Helpers::Controllers
ActionController::Base.include Ckeditor::Helpers::Controllers
end

ActiveSupport.on_load :action_view do
ActionView::Base.send :include, Ckeditor::Helpers::ViewHelper
ActionView::Base.send :include, Ckeditor::Helpers::FormHelper
ActionView::Helpers::FormBuilder.send :include, Ckeditor::Helpers::FormBuilder
ActionView::Base.include Ckeditor::Helpers::ViewHelper
ActionView::Base.include Ckeditor::Helpers::FormHelper
ActionView::Helpers::FormBuilder.include Ckeditor::Helpers::FormBuilder
end
end

Expand Down
14 changes: 11 additions & 3 deletions lib/ckeditor/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Utils

class << self
def escape_single_quotes(str)
str.gsub('\\', '\0\0').gsub('</', '<\/').gsub(/\r\n|\n|\r/, "\\n").gsub(/["']/) { |m| "\\#{m}" }
str.gsub('\\', '\0\0').gsub('</', '<\/').gsub(/\r\n|\n|\r/, '\\n').gsub(/["']/) { |m| "\\#{m}" }
end

def js_replace(dom_id, options = nil)
Expand All @@ -35,10 +35,10 @@ def js_fileuploader(uploader_type, options = {})
options = { multiple: true, element: 'fileupload' }.merge!(options)

case uploader_type.to_s.downcase
when 'image' then
when 'image'
options[:action] = JavascriptCode.new('EDITOR.config.filebrowserImageUploadUrl')
options[:allowedExtensions] = Ckeditor.image_file_types
when 'flash' then
when 'flash'
options[:action] = JavascriptCode.new('EDITOR.config.filebrowserFlashUploadUrl')
options[:allowedExtensions] = Ckeditor.flash_file_types
else
Expand Down Expand Up @@ -71,6 +71,14 @@ def assets_pipeline_enabled?
false
end
end

def call_or_return(value, default)
if value.respond_to? :call
value.call
else
value || default
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/generators/ckeditor/action_policy_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def self.source_root

# copy configuration
def copy_policy_files
[:picture, :attachment_file].each do |model_name|
%w[picture attachment_file].each do |model_name|
template "action_policy/#{model_name}_policy.rb", "app/policies/ckeditor/#{model_name}_policy.rb"
end
end
Expand Down
12 changes: 5 additions & 7 deletions lib/generators/ckeditor/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ def copy_initializer
template 'base/dragonfly/initializer.rb', 'config/initializers/ckeditor_dragonfly.rb'
end

if backend_shrine?
template 'base/shrine/initializer.rb', 'config/initializers/ckeditor_shrine.rb'
end
template 'base/shrine/initializer.rb', 'config/initializers/ckeditor_shrine.rb' if backend_shrine?
end

def mount_engine
Expand All @@ -58,10 +56,10 @@ def create_uploaders
File.join('app/uploaders', 'ckeditor_picture_uploader.rb')
end

if backend_shrine?
template "#{uploaders_dir}/ckeditor_attachment_uploader.rb",
File.join('app/uploaders', 'ckeditor_attachment_uploader.rb')
end
return unless backend_shrine?

template "#{uploaders_dir}/ckeditor_attachment_uploader.rb",
File.join('app/uploaders', 'ckeditor_attachment_uploader.rb')
end

def create_ckeditor_migration
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/ckeditor/pundit_policy_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def self.source_root

# copy configuration
def copy_policy_files
[:picture, :attachment_file].each do |model_name|
%w[picture attachment_file].each do |model_name|
template "pundit_policy/#{model_name}_policy.rb", "app/policies/ckeditor/#{model_name}_policy.rb"
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class Ckeditor::Asset < ActiveRecord::Base
class Ckeditor::Asset < ApplicationRecord
include Ckeditor::Orm::ActiveRecord::AssetBase
include Ckeditor::Backend::ActiveStorage

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class Ckeditor::Asset < ActiveRecord::Base
class Ckeditor::Asset < ApplicationRecord
include Ckeditor::Orm::ActiveRecord::AssetBase

delegate :url, :current_path, :content_type, to: :data
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class Ckeditor::Asset < ActiveRecord::Base
class Ckeditor::Asset < ApplicationRecord
include Ckeditor::Orm::ActiveRecord::AssetBase
include Ckeditor::Backend::Dragonfly

Expand Down
Loading

0 comments on commit b3aa79f

Please sign in to comment.