Skip to content

Commit

Permalink
Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan committed Nov 9, 2024
1 parent c441425 commit f1ee161
Show file tree
Hide file tree
Showing 40 changed files with 323 additions and 313 deletions.
15 changes: 15 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@ AllCops:
NewCops: enable
Style/StringLiterals:
EnforcedStyle: double_quotes
Style/TrailingCommaInArguments:
EnforcedStyleForMultiline: consistent_comma
Style/TrailingCommaInArrayLiteral:
EnforcedStyleForMultiline: consistent_comma
Style/TrailingCommaInHashLiteral:
EnforcedStyleForMultiline: consistent_comma
Style/RegexpLiteral:
EnforcedStyle: percent_r
Layout/FirstHashElementIndentation:
EnforcedStyle: consistent
Layout/FirstArrayElementIndentation:
EnforcedStyle: consistent
Layout/ArgumentAlignment:
EnforcedStyle: with_fixed_indentation

Metrics/BlockLength:
Enabled: false
Metrics/MethodLength:
Expand Down
6 changes: 3 additions & 3 deletions datagrid.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ Gem::Specification.new do |s|
s.email = "[email protected]"
s.extra_rdoc_files = [
"LICENSE.txt",
"Readme.markdown"
"Readme.markdown",
]
s.files = [
"LICENSE.txt",
"CHANGELOG.md",
"Readme.markdown",
"datagrid.gemspec"
"datagrid.gemspec",
]
s.files += `git ls-files | grep -E '^(app|lib|templates)'`.split("\n")
s.homepage = "https://github.com/bogdan/datagrid"
Expand All @@ -30,7 +30,7 @@ Gem::Specification.new do |s|
"documentation_uri" => "#{s.homepage}/wiki",
"changelog_uri" => "#{s.homepage}/blob/master/CHANGELOG.md",
"source_code_uri" => s.homepage,
"rubygems_mfa_required" => "true"
"rubygems_mfa_required" => "true",
}

s.add_dependency "railties", ">= 6.1"
Expand Down
3 changes: 1 addition & 2 deletions lib/datagrid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

# @main README.md
module Datagrid

# @!visibility private
def self.included(base)
Utils.warn_once("Including Datagrid is deprecated. Inherit Datagrid::Base instead.")
Expand All @@ -25,6 +24,6 @@ class ArgumentError < ::ArgumentError; end
class ColumnUnavailableError < StandardError; end
end

require 'datagrid/base'
require "datagrid/base"
require "datagrid/scaffold"
I18n.load_path << File.expand_path("datagrid/locale/en.yml", __dir__)
2 changes: 2 additions & 0 deletions lib/datagrid/base.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Datagrid
extend ActiveSupport::Autoload

Expand Down
2 changes: 1 addition & 1 deletion lib/datagrid/column_names_attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def column_names_filter(**options)
select: :optional_columns_select,
multiple: true,
dummy: true,
**options
**options,
)
end
end
Expand Down
14 changes: 7 additions & 7 deletions lib/datagrid/columns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ def find_column_by_name(columns, name)
def assets
append_column_preload(
driver.append_column_queries(
super, columns.select(&:query)
)
super, columns.select(&:query),
),
)
end

Expand Down Expand Up @@ -277,7 +277,7 @@ def to_csv(*column_names, **options)
CSV.generate(
headers: header(*column_names),
write_headers: true,
**options
**options,
) do |csv|
each_with_batches do |asset|
csv << row_for(asset, *column_names)
Expand All @@ -294,7 +294,7 @@ def to_csv(*column_names, **options)
# grid.columns(:id, :category) # => id and category column
def columns(*column_names, data: false, html: false)
self.class.filter_columns(
columns_array, *column_names, data: data, html: html
columns_array, *column_names, data: data, html: html,
).select do |column|
column.enabled?(self)
end
Expand Down Expand Up @@ -480,9 +480,9 @@ def cache_key(asset)
end
rescue NotImplementedError
raise Datagrid::ConfigurationError,
<<~MSG
#{self} is setup to use cache. But there was appropriate cache key found for #{asset.inspect}.
MSG
<<~MSG
#{self} is setup to use cache. But there was appropriate cache key found for #{asset.inspect}.
MSG
end

def map_with_batches(&block)
Expand Down
2 changes: 1 addition & 1 deletion lib/datagrid/drivers/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def normalized_column_type(scope, field)
%i[float decimal] => :float,
[:date] => :date,
%i[datetime timestamp] => :timestamp,
[:boolean] => :boolean
[:boolean] => :boolean,
}.each do |keys, value|
return value if keys.include?(builtin_type)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/datagrid/drivers/mongoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def normalized_column_type(scope, field)

[Float] => :float,

[Integer] => :integer
[Integer] => :integer,
}.each do |keys, value|
return value if keys.include?(type)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/datagrid/drivers/sequel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def normalized_column_type(scope, field)
%i[float decimal] => :float,
[:date] => :date,
[:datetime] => :timestamp,
[:boolean] => :boolean
[:boolean] => :boolean,
}.each do |keys, value|
return value if keys.include?(type)
end
Expand Down
6 changes: 3 additions & 3 deletions lib/datagrid/filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module Filters
integer: Filters::IntegerFilter,
enum: Filters::EnumFilter,
float: Filters::FloatFilter,
dynamic: Filters::DynamicFilter
dynamic: Filters::DynamicFilter,
}.freeze

# @!visibility private
Expand Down Expand Up @@ -144,7 +144,7 @@ def filters_inspection
def initialize(...)
self.filters_array = self.class.filters_array.clone
filters_array.each do |filter|
if value = filter.default(self)
if (value = filter.default(self))
self[filter.name] = value
end
end
Expand Down Expand Up @@ -219,7 +219,7 @@ def find_select_filter(filter)
type = FILTER_TYPES.invert[filter.class].inspect
raise(
::Datagrid::ArgumentError,
"#{self.class.name}##{filter.name} with type #{type} can not have select options"
"#{self.class.name}##{filter.name} with type #{type} can not have select options",
)
end
filter
Expand Down
6 changes: 3 additions & 3 deletions lib/datagrid/filters/base_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class FilteringError < StandardError
module Datagrid
module Filters
class BaseFilter
class_attribute :default_input_options, instance_writer: false, default: {type: 'text'}
class_attribute :default_input_options, instance_writer: false, default: { type: "text" }

attr_accessor :grid_class, :options, :block, :name

Expand Down Expand Up @@ -41,7 +41,7 @@ def apply(grid_object, scope, value)
unless grid_object.driver.match?(result)
raise(
Datagrid::FilteringError,
"Filter #{name.inspect} unapplicable: result no longer match #{grid_object.driver.class}."
"Filter #{name.inspect} unapplicable: result no longer match #{grid_object.driver.class}.",
)
end

Expand All @@ -57,7 +57,7 @@ def parse_values(value)
end
elsif value.is_a?(Array)
raise Datagrid::ArgumentError,
"#{grid_class}##{name} filter can not accept Array argument. Use :multiple option."
"#{grid_class}##{name} filter can not accept Array argument. Use :multiple option."
else
parse(value)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/datagrid/filters/boolean_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
module Datagrid
module Filters
class BooleanFilter < Datagrid::Filters::BaseFilter
self.default_input_options = {type: 'checkbox' }
self.default_input_options = { type: "checkbox" }

def parse(value)
Datagrid::Utils.booleanize(value)
Expand Down
2 changes: 1 addition & 1 deletion lib/datagrid/filters/date_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Filters
class DateFilter < Datagrid::Filters::BaseFilter
include Datagrid::Filters::RangedFilter

self.default_input_options = { type: 'date' }
self.default_input_options = { type: "date" }

def apply(grid_object, scope, value)
value = value.begin&.beginning_of_day..value.end&.end_of_day if value.is_a?(Range)
Expand Down
2 changes: 1 addition & 1 deletion lib/datagrid/filters/date_time_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Filters
class DateTimeFilter < Datagrid::Filters::BaseFilter
include Datagrid::Filters::RangedFilter

self.default_input_options = {type: 'datetime-local' }
self.default_input_options = { type: "datetime-local" }

def parse(value)
Datagrid::Utils.parse_datetime(value)
Expand Down
28 changes: 14 additions & 14 deletions lib/datagrid/filters/dynamic_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class DynamicFilter < Datagrid::Filters::BaseFilter
EQUAL_OPERATION,
LIKE_OPERATION,
MORE_EQUAL_OPERATION,
LESS_EQUAL_OPERATION
LESS_EQUAL_OPERATION,
].freeze
AVAILABLE_OPERATIONS = %w[= =~ >= <=].freeze

Expand Down Expand Up @@ -48,7 +48,7 @@ def default_filter_where(scope, filter)

unless operations.include?(operation)
raise Datagrid::FilteringError,
"Unknown operation: #{operation.inspect}. Available operations: #{operations.join(' ')}"
"Unknown operation: #{operation.inspect}. Available operations: #{operations.join(' ')}"
end

case operation
Expand All @@ -70,7 +70,7 @@ def default_filter_where(scope, filter)
driver.less_equal(scope, field, value)
else
raise Datagrid::FilteringError,
"Unknown operation: #{operation.inspect}. Use filter block argument to implement operation"
"Unknown operation: #{operation.inspect}. Use filter block argument to implement operation"
end
end

Expand All @@ -91,7 +91,7 @@ def default_select
grid.driver.column_names(grid.scope).map do |name|
# Mongodb/Rails problem:
# '_id'.humanize returns ''
[name.gsub(/^_/, "").humanize.strip, name]
[name.gsub(%r{^_}, "").humanize.strip, name]
end
}
end
Expand Down Expand Up @@ -119,16 +119,16 @@ def initialize(grid_class, object = nil)
else
raise ArgumentError, object.inspect
end
if grid_class
type = grid_class.driver.normalized_column_type(
grid_class.scope, field
)
self.value = type_cast(type, value)
end
return unless grid_class

type = grid_class.driver.normalized_column_type(
grid_class.scope, field,
)
self.value = type_cast(type, value)
end

def inspect
{field: field, operation: operation, value: value}
{ field: field, operation: operation, value: value }
end

def to_ary
Expand All @@ -140,7 +140,7 @@ def to_a
end

def to_h
{field: field, operation: operation, value: value}
{ field: field, operation: operation, value: value }
end

protected
Expand All @@ -152,9 +152,9 @@ def type_cast(type, value)
when :string
value.to_s
when :integer
value.is_a?(Numeric) || value =~ /^\d/ ? value.to_i : nil
value.is_a?(Numeric) || value =~ %r{^\d} ? value.to_i : nil
when :float
value.is_a?(Numeric) || value =~ /^\d/ ? value.to_f : nil
value.is_a?(Numeric) || value =~ %r{^\d} ? value.to_f : nil
when :date, :timestamp
Datagrid::Utils.parse_date(value)
when :boolean
Expand Down
2 changes: 1 addition & 1 deletion lib/datagrid/filters/enum_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Filters
class EnumFilter < Datagrid::Filters::BaseFilter
include Datagrid::Filters::SelectOptions

self.default_input_options = {type: 'select' }
self.default_input_options = { type: "select" }

def initialize(*args)
super
Expand Down
2 changes: 1 addition & 1 deletion lib/datagrid/filters/extended_boolean_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ExtendedBooleanFilter < Datagrid::Filters::EnumFilter
TRUTH_VALUES = [true, "true", "y", "yes"].freeze
FALSE_VALUES = [false, "false", "n", "no"].freeze

self.default_input_options = {type: 'select' }
self.default_input_options = { type: "select" }

def initialize(report, attribute, options = {}, &block)
options[:select] = -> { boolean_select }
Expand Down
2 changes: 1 addition & 1 deletion lib/datagrid/filters/float_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Filters
class FloatFilter < Datagrid::Filters::BaseFilter
include Datagrid::Filters::RangedFilter

self.default_input_options = {type: 'number', step: 'any' }
self.default_input_options = { type: "number", step: "any" }

def parse(value)
return nil if value.blank?
Expand Down
2 changes: 1 addition & 1 deletion lib/datagrid/filters/integer_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Filters
class IntegerFilter < Datagrid::Filters::BaseFilter
include Datagrid::Filters::RangedFilter

self.default_input_options = {type: 'number', step: '1' }
self.default_input_options = { type: "number", step: "1" }

def parse(value)
return nil if value.blank?
Expand Down
19 changes: 8 additions & 11 deletions lib/datagrid/filters/ranged_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
module Datagrid
module Filters
module RangedFilter
SERIALIZED_RANGE = /\A(.*)\.{2,3}(.*)\z/
SERIALIZED_RANGE = %r{\A(.*)\.{2,3}(.*)\z}.freeze

def parse_values(value)
unless range?
return super
end
return super unless range?

case value
when String
if value == '..' || value == '...'
if ["..", "..."].include?(value)
nil
elsif match = value.match(SERIALIZED_RANGE)
to_range(match.captures[0], match.captures[1], value.include?('...'))
elsif (match = value.match(SERIALIZED_RANGE))
to_range(match.captures[0], match.captures[1], value.include?("..."))
else
super
end
Expand Down Expand Up @@ -56,9 +55,7 @@ def to_range(from, to, exclusive = false)
return nil unless to || from

# If wrong range is given - reverse it to be always valid
if from && to && from > to
from, to = to, from
end
from, to = to, from if from && to && from > to
exclusive ? from...to : from..to
end

Expand All @@ -69,7 +66,7 @@ def parse_array(result)
case result.size
when 0
nil
when 1,2
when 1, 2
to_range(first, last)
else
raise ArgumentError, "Can not create a range from array of more than two elements"
Expand Down
Loading

0 comments on commit f1ee161

Please sign in to comment.