Skip to content

Commit

Permalink
[Linter] Add rubocop to the gem (#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mth0158 committed Feb 4, 2025
1 parent 518b95a commit daf15d5
Show file tree
Hide file tree
Showing 196 changed files with 2,043 additions and 1,897 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@ name: CI
on: [push, pull_request]

jobs:
linters:
name: Linters
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup Ruby and install gems
uses: ruby/setup-ruby@v1
with:
bundler-cache: true

- name: Run linters
run: |
bundle exec rubocop --parallel
tests:
name: Tests
runs-on: ubuntu-latest
Expand Down
37 changes: 37 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
inherit_from: .rubocop_todo.yml

inherit_gem:
rubocop-rails-omakase: rubocop.yml

AllCops:
TargetRubyVersion: 3.0.7 # We need to support all ruby versions in CI matrix
Exclude:
- 'bin/*'
- 'config/locales/*'
- 'gemfiles/*'
- 'lib/tasks/*'
- 'vendor/bundle/**/*'
NewCops: enable

require:
- rubocop-performance

# ==============================================================================
# Cops

# Style cops

Style/Documentation:
Enabled: false

# Metrics cops

Metrics/AbcSize:
Severity: warning

Metrics/MethodLength:
Max: 15

Metrics/BlockLength:
Exclude:
- test/**/*
13 changes: 13 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2025-02-03 15:23:42 UTC using RuboCop version 1.71.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.


# Offense count: 16
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
Metrics/AbcSize:
Max: 25
7 changes: 6 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

source 'https://rubygems.org'
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

# Declare your gem's dependencies in active_storage_validations.gemspec.
Expand All @@ -22,4 +22,9 @@ gemspec
group :development, :test do
# To use a debugger:
# gem 'byebug'

# Linters
gem "rubocop", "~> 1.71", ">= 1.71.1", require: false
gem "rubocop-performance", "~> 1.23", ">= 1.23.1", require: false
gem "rubocop-rails-omakase", "~> 1.0", ">= 1.0.0", require: false
end
46 changes: 45 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ GEM
tzinfo (~> 2.0)
addressable (2.8.7)
public_suffix (>= 2.0.2, < 7.0)
ast (2.4.2)
base64 (0.2.0)
bigdecimal (3.1.8)
builder (3.3.0)
Expand All @@ -74,7 +75,7 @@ GEM
ffi (1.17.0)
ffi (1.17.0-arm64-darwin)
ffi (1.17.0-x86_64-darwin)
ffi (1.17.0-x86_64-linux-gnu)
ffi (1.17.0-x86_64-linux)
globalid (1.2.1)
activesupport (>= 6.1)
hashdiff (1.1.1)
Expand All @@ -84,6 +85,8 @@ GEM
irb (1.14.1)
rdoc (>= 4.0.0)
reline (>= 0.4.2)
json (2.9.1)
language_server-protocol (3.17.0.4)
logger (1.6.0)
loofah (2.22.0)
crass (~> 1.0.2)
Expand All @@ -107,6 +110,10 @@ GEM
racc (~> 1.4)
nokogiri (1.16.7-x86_64-linux)
racc (~> 1.4)
parallel (1.26.3)
parser (3.3.7.0)
ast (~> 2.4.1)
racc
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
Expand Down Expand Up @@ -137,12 +144,43 @@ GEM
rake (>= 12.2)
thor (~> 1.0, >= 1.2.2)
zeitwerk (~> 2.6)
rainbow (3.1.1)
rake (13.2.1)
rdoc (6.7.0)
psych (>= 4.0.0)
regexp_parser (2.10.0)
reline (0.5.11)
io-console (~> 0.5)
rexml (3.3.7)
rubocop (1.71.1)
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 (>= 2.9.3, < 3.0)
rubocop-ast (>= 1.38.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 4.0)
rubocop-ast (1.38.0)
parser (>= 3.3.1.0)
rubocop-minitest (0.36.0)
rubocop (>= 1.61, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
rubocop-performance (1.23.1)
rubocop (>= 1.48.1, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
rubocop-rails (2.29.1)
activesupport (>= 4.2.0)
rack (>= 1.1)
rubocop (>= 1.52.0, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
rubocop-rails-omakase (1.0.0)
rubocop
rubocop-minitest
rubocop-performance
rubocop-rails
ruby-progressbar (1.13.0)
ruby-vips (2.2.2)
ffi (~> 1.12)
logger
Expand All @@ -162,6 +200,9 @@ GEM
timeout (0.4.2)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (3.1.4)
unicode-emoji (~> 4.0, >= 4.0.4)
unicode-emoji (4.0.4)
webmock (3.23.1)
addressable (>= 2.8.0)
crack (>= 0.3.2)
Expand All @@ -184,6 +225,9 @@ DEPENDENCIES
minitest-mock_expectations (~> 1.2, >= 1.2.0)
minitest-stub_any_instance (~> 1.0, >= 1.0.3)
pry
rubocop (~> 1.71, >= 1.71.1)
rubocop-performance (~> 1.23, >= 1.23.1)
rubocop-rails-omakase (~> 1.0, >= 1.0.0)
ruby-vips (>= 2.1.0)
simplecov
sqlite3
Expand Down
4 changes: 2 additions & 2 deletions active_storage_validations.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ require 'active_storage_validations/version'
Gem::Specification.new do |s|
s.name = 'active_storage_validations'
s.version = ActiveStorageValidations::VERSION
s.authors = ['Igor Kasyanchuk']
s.email = ['igorkasyanchuk@gmail.com']
s.authors = [ 'Igor Kasyanchuk' ]
s.email = [ 'igorkasyanchuk@gmail.com' ]
s.homepage = 'https://github.com/igorkasyanchuk/active_storage_validations'
s.summary = 'Validations for Active Storage'
s.description = 'Validations for Active Storage (presence)'
Expand Down
44 changes: 22 additions & 22 deletions lib/active_storage_validations.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
# frozen_string_literal: true

require 'active_model'
require 'active_support/concern'
require "active_model"
require "active_support/concern"

require 'active_storage_validations/analyzer'
require 'active_storage_validations/analyzer/image_analyzer'
require 'active_storage_validations/analyzer/image_analyzer/image_magick'
require 'active_storage_validations/analyzer/image_analyzer/vips'
require 'active_storage_validations/analyzer/null_analyzer'
require 'active_storage_validations/analyzer/video_analyzer'
require 'active_storage_validations/analyzer/audio_analyzer'
require "active_storage_validations/analyzer"
require "active_storage_validations/analyzer/image_analyzer"
require "active_storage_validations/analyzer/image_analyzer/image_magick"
require "active_storage_validations/analyzer/image_analyzer/vips"
require "active_storage_validations/analyzer/null_analyzer"
require "active_storage_validations/analyzer/video_analyzer"
require "active_storage_validations/analyzer/audio_analyzer"

require 'active_storage_validations/extensors/asv_blob_metadatable'
require 'active_storage_validations/extensors/asv_marcelable'
require "active_storage_validations/extensors/asv_blob_metadatable"
require "active_storage_validations/extensors/asv_marcelable"

require 'active_storage_validations/attached_validator'
require 'active_storage_validations/content_type_validator'
require 'active_storage_validations/limit_validator'
require 'active_storage_validations/dimension_validator'
require 'active_storage_validations/duration_validator'
require 'active_storage_validations/aspect_ratio_validator'
require 'active_storage_validations/processable_file_validator'
require 'active_storage_validations/size_validator'
require 'active_storage_validations/total_size_validator'
require "active_storage_validations/attached_validator"
require "active_storage_validations/content_type_validator"
require "active_storage_validations/limit_validator"
require "active_storage_validations/dimension_validator"
require "active_storage_validations/duration_validator"
require "active_storage_validations/aspect_ratio_validator"
require "active_storage_validations/processable_file_validator"
require "active_storage_validations/size_validator"
require "active_storage_validations/total_size_validator"

require 'active_storage_validations/engine'
require 'active_storage_validations/railtie'
require "active_storage_validations/engine"
require "active_storage_validations/railtie"
47 changes: 24 additions & 23 deletions lib/active_storage_validations/analyzer.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require_relative 'shared/asv_attachable'
require_relative 'shared/asv_loggable'
require_relative "shared/asv_attachable"
require_relative "shared/asv_loggable"

module ActiveStorageValidations
# = Active Storage Validations \Analyzer
Expand Down Expand Up @@ -37,29 +37,30 @@ def read_media
raise NotImplementedError
end

# rubocop:disable Metrics/MethodLength
def media(tempfile)
@media ||= case @attachable
when ActiveStorage::Blob, String
blob = @attachable.is_a?(String) ? ActiveStorage::Blob.find_signed!(@attachable) : @attachable
media_from_tempfile_path(tempfile, blob)
when Hash
io = @attachable[:io]
if io.is_a?(StringIO)
media_from_tempfile_path(tempfile, io)
else
File.open(io) do |file|
media_from_path(file.path)
end
end
when ActionDispatch::Http::UploadedFile, Rack::Test::UploadedFile
media_from_path(@attachable.path)
when File
supports_file_attachment? ? media_from_path(@attachable.path) : raise_rails_like_error(@attachable)
when Pathname
supports_pathname_attachment? ? media_from_path(@attachable.to_s) : raise_rails_like_error(@attachable)
else
raise_rails_like_error(@attachable)
end
when ActiveStorage::Blob, String
blob = @attachable.is_a?(String) ? ActiveStorage::Blob.find_signed!(@attachable) : @attachable
media_from_tempfile_path(tempfile, blob)
when Hash
io = @attachable[:io]
if io.is_a?(StringIO)
media_from_tempfile_path(tempfile, io)
else
File.open(io) do |file|
media_from_path(file.path)
end
end
when ActionDispatch::Http::UploadedFile, Rack::Test::UploadedFile
media_from_path(@attachable.path)
when File
supports_file_attachment? ? media_from_path(@attachable.path) : raise_rails_like_error(@attachable)
when Pathname
supports_pathname_attachment? ? media_from_path(@attachable.to_s) : raise_rails_like_error(@attachable)
else
raise_rails_like_error(@attachable)
end
end

def media_from_tempfile_path(tempfile, file_representation)
Expand Down
4 changes: 2 additions & 2 deletions lib/active_storage_validations/analyzer/audio_analyzer.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'open3'
require_relative 'shared/asv_ff_probable'
require "open3"
require_relative "shared/asv_ff_probable"

module ActiveStorageValidations
# = ActiveStorageValidations Audio \Analyzer
Expand Down
10 changes: 5 additions & 5 deletions lib/active_storage_validations/analyzer/content_type_analyzer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'open3'
require "open3"

module ActiveStorageValidations
# = ActiveStorageValidations ContentType \Analyzer
Expand Down Expand Up @@ -41,15 +41,15 @@ def read_media
end
end
rescue Errno::ENOENT
raise FileCommandLineToolNotInstalledError, 'file command-line tool is not installed'
raise FileCommandLineToolNotInstalledError, "file command-line tool is not installed"
end

def media_from_path(path)
instrument("file") do
stdout, status = Open3.capture2(
'file',
'-b',
'--mime-type',
"file",
"-b",
"--mime-type",
path
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ module ActiveStorageValidations
# MiniMagick requires the {ImageMagick}[http://www.imagemagick.org] system library.
# This is the default Rails image analyzer.
class Analyzer::ImageAnalyzer::ImageMagick < Analyzer::ImageAnalyzer

private

def read_media
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ module ActiveStorageValidations
# This analyzer relies on the third-party {ruby-vips}[https://github.com/libvips/ruby-vips] gem.
# Ruby-vips requires the {libvips}[https://libvips.github.io/libvips/] system library.
class Analyzer::ImageAnalyzer::Vips < Analyzer::ImageAnalyzer

private

def read_media
Expand Down
8 changes: 4 additions & 4 deletions lib/active_storage_validations/analyzer/video_analyzer.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'open3'
require_relative 'shared/asv_ff_probable'
require "open3"
require_relative "shared/asv_ff_probable"

module ActiveStorageValidations
# = ActiveStorageValidations Video \Analyzer
Expand Down Expand Up @@ -80,7 +80,7 @@ def display_aspect_ratio
numerator = Integer(terms[0])
denominator = Integer(terms[1])

[numerator, denominator] unless numerator == 0
[ numerator, denominator ] unless numerator == 0
end
end
end
Expand Down Expand Up @@ -127,4 +127,4 @@ def container
@container ||= @media["format"] || {}
end
end
end
end
Loading

0 comments on commit daf15d5

Please sign in to comment.