Skip to content

Commit

Permalink
Linting
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan committed Nov 13, 2024
1 parent 92b3717 commit e7a8bb8
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lib/datagrid/drivers/abstract_driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def reverse_order(scope)
raise NotImplementedError
end

def is_timestamp?(scope, field)
def timestamp_column?(scope, field)
normalized_column_type(scope, field) == :timestamp
end

Expand Down
2 changes: 1 addition & 1 deletion lib/datagrid/drivers/mongo_mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def scope_has_column?(scope, column_name)
scope.key?(column_name)
end

def is_timestamp?(_scope, _column_name)
def timestamp_column?(_scope, _column_name)
# TODO: implement the support
false
end
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 @@ -25,7 +25,7 @@ def format(value)
end

def default_filter_where(scope, value)
value = Datagrid::Utils.format_date_as_timestamp(value) if driver.is_timestamp?(scope, name)
value = Datagrid::Utils.format_date_as_timestamp(value) if driver.timestamp_column?(scope, name)
super
end

Expand Down
2 changes: 1 addition & 1 deletion lib/datagrid/filters/dynamic_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def unapplicable_value?(filter)

def default_filter_where(scope, filter)
field, operation, value = filter
date_conversion = value.is_a?(Date) && driver.is_timestamp?(scope, field)
date_conversion = value.is_a?(Date) && driver.timestamp_column?(scope, field)

return scope if field.blank? || operation.blank?

Expand Down
2 changes: 2 additions & 0 deletions lib/datagrid/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def parse_date(value)
Array(Datagrid.configuration.date_formats).each do |format|
return Date.strptime(value, format)
rescue ::ArgumentError
nil
end
end
return Date.parse(value) if value.is_a?(String)
Expand All @@ -97,6 +98,7 @@ def parse_datetime(value)
Array(Datagrid.configuration.datetime_formats).each do |format|
return Time.strptime(value, format)
rescue ::ArgumentError
nil
end
end
return Time.parse(value) if value.is_a?(String)
Expand Down
4 changes: 3 additions & 1 deletion spec/datagrid/filters/boolean_filter_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require 'spec_helper'
# frozen_string_literal: true

require "spec_helper"

describe Datagrid::Filters::BooleanFilter do
it "applies default filtering" do
Expand Down
6 changes: 3 additions & 3 deletions spec/datagrid/filters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -304,16 +304,16 @@ class TestGrid8728
end

context "with delegation to attribute" do
let(:role) { OpenStruct.new("admin?" => admin) }
let(:role) { Struct.new(:admin).new(admin) }
let(:klass) do
test_report_class do
attr_accessor :role

delegate :admin?, to: :role
delegate :admin, to: :role

scope { Entry }

filter(:id, :integer, if: :admin?)
filter(:id, :integer, if: :admin)
end
end

Expand Down
18 changes: 9 additions & 9 deletions spec/datagrid/helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -483,15 +483,15 @@ class FormForGrid
end
object = FormForGrid.new(category: "hello")
expect(subject.datagrid_form_for(object, url: "/grid")).to equal_to_dom(<<~HTML)
<form class="datagrid-form form_for_grid" id="new_form_for_grid" action="/grid" accept-charset="UTF-8" method="get"> <div class="datagrid-filter filter">
<label for="form_for_grid_category">Category</label>
<input value="hello" class="category default_filter" type="text" name="form_for_grid[category]" id="form_for_grid_category" />
</div>
<div class="datagrid-actions">
<input type="submit" name="commit" value="Search" class="datagrid-submit" data-disable-with="Search" />
<a class="datagrid-reset" href="/location">Reset</a>
</div>
</form>
<form class="datagrid-form form_for_grid" id="new_form_for_grid" action="/grid" accept-charset="UTF-8" method="get"> <div class="datagrid-filter filter">
<label for="form_for_grid_category">Category</label>
<input value="hello" class="category default_filter" type="text" name="form_for_grid[category]" id="form_for_grid_category" />
</div>
<div class="datagrid-actions">
<input type="submit" name="commit" value="Search" class="datagrid-submit" data-disable-with="Search" />
<a class="datagrid-reset" href="/location">Reset</a>
</div>
</form>
HTML
end
it "should support html classes for grid class with namespace" do
Expand Down
5 changes: 1 addition & 4 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,10 @@
begin
require "mongo_mapper"
rescue LoadError
nil
end

require "datagrid"
begin
require "ruby-debug"
rescue LoadError
end
require "rspec"
require "logger"

Expand Down

2 comments on commit e7a8bb8

@zhuravel
Copy link
Contributor

@zhuravel zhuravel commented on e7a8bb8 Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bogdan Breaking change.

class ExampleGrid < ApplicationGrid
  scope { [] }
  filter(:period, :date, range: true)
end

ExampleGrid.new(period: ["2024-01-01", "2024-12-31"]).assets

# Before this commit: returns []
# After this commit: raises NotImplementedError
# Backtrace:
#  lib/datagrid/drivers/abstract_driver.rb:79:in `normalized_column_type'
#  lib/datagrid/drivers/abstract_driver.rb:67:in `timestamp_column?'
#  lib/datagrid/filters/date_filter.rb:28:in `default_filter_where'
#  lib/datagrid/filters/base_filter.rb:185:in `default_filter'
#  lib/datagrid/filters/base_filter.rb:117:in `block in default_filter_block'
#  lib/datagrid/utils.rb:71:in `apply_args'
#  lib/datagrid/filters/base_filter.rb:154:in `execute'
#  lib/datagrid/filters/base_filter.rb:35:in `apply'
#  lib/datagrid/filters/date_filter.rb:12:in `apply'
#  lib/datagrid/filters.rb:227:in `block in apply_filters'
#  lib/datagrid/filters.rb:226:in `each'
#  lib/datagrid/filters.rb:226:in `inject'
#  lib/datagrid/filters.rb:226:in `apply_filters'
#  lib/datagrid/filters.rb:156:in `assets'
#  lib/datagrid/columns.rb:204:in `assets'
#  lib/datagrid/ordering.rb:38:in `assets'

It’s because is_timestamp? was defined for Array scope while timestamp_column? is not:

def is_timestamp?(scope, column_name)
scope_has_column?(scope, column_name) &&
timestamp_class?(get(scope.first, column_name).class)
end

@bogdan
Copy link
Owner Author

@bogdan bogdan commented on e7a8bb8 Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.