Skip to content

Commit

Permalink
Linting
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan committed Nov 17, 2024
1 parent 76b9792 commit e82f48b
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 64 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ Metrics/ModuleLength:
Enabled: false
Style/Documentation:
Enabled: false
Style/OptionalBooleanParameter:
Enabled: false
Style/MultilineBlockChain:
Enabled: false
6 changes: 4 additions & 2 deletions lib/datagrid/deprecated_object.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Datagrid
# @!visibility private
class DeprecatedObject < BasicObject
Expand All @@ -6,9 +8,9 @@ def initialize(real_object, &block)
@block = block
end

def method_missing(method_name, *args, &block)
def method_missing(method_name, ...)
@block.call
@real_object.public_send(method_name, *args, &block)
@real_object.public_send(method_name, ...)
end

def respond_to_missing?(method_name, include_private = false)
Expand Down
4 changes: 1 addition & 3 deletions lib/datagrid/filters/date_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ def default_input_options
end

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

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 @@ -34,7 +34,7 @@ def label_options
# Each checkbox has its own label
# The main label has no specific input to focus
# See app/views/datagrid/_enum_checkboxes.html.erb
{for: nil, **super}
{ for: nil, **super }
else
super
end
Expand Down
7 changes: 4 additions & 3 deletions lib/datagrid/form_builder.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require "action_view"
require 'datagrid/deprecated_object'
require "datagrid/deprecated_object"

module Datagrid
module FormBuilder
Expand Down Expand Up @@ -98,10 +98,11 @@ def datagrid_enum_checkboxes_filter(filter, options = {})
form: self,
elements: Datagrid::DeprecatedObject.new(
elements,

) do
Datagrid::Utils.warn_once(
"Using `elements` variable in app/views/datagrid/enum_checkboxes is deprecated, use `choices` instead."
<<~MSG,
Using `elements` variable in enum_checkboxes view is deprecated, use `choices` instead.
MSG
)
end,
choices: choices,
Expand Down
3 changes: 1 addition & 2 deletions spec/datagrid/filters/date_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ def entry_dated(date)

it "supports search by timestamp column" do
report = test_report(created_at: Date.today) do
scope {Entry}
scope { Entry }
filter(:created_at, :date)
end
e1 = Entry.create!(created_at: Date.yesterday + 23.hours)
Expand All @@ -276,7 +276,6 @@ def entry_dated(date)
expect(report.assets).to_not include(e5)
end


it "allows filter to be defined before scope" do
class ParentGrid < Datagrid::Base
filter(:created_at, :date, range: true)
Expand Down
74 changes: 38 additions & 36 deletions spec/datagrid/form_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,23 +213,23 @@ class MyTemplate
context "with only right bound" do
let(:_range) { [nil, 10] }
it {
should equal_to_dom(
'<input class="datagrid-range-from" type="number" step="1" name="report[group_id][from]" id="report_group_id"/>' \
'<span class="datagrid-range-separator"> - </span>' \
'<input value="10" class="datagrid-range-to" type="number" step="1" name="report[group_id][to]"/>',
)
should equal_to_dom(<<~HTML)
<input class="datagrid-range-from" type="number" step="1" name="report[group_id][from]" id="report_group_id"/>
<span class="datagrid-range-separator"> - </span>
<input value="10" class="datagrid-range-to" type="number" step="1" name="report[group_id][to]"/>
HTML
}
it { should be_html_safe }
end

context "with invalid range value" do
let(:_range) { 2..1 }
it {
should equal_to_dom(
'<input value="1" class="datagrid-range-from" type="number" step="1" name="report[group_id][from]" id="report_group_id"/>' \
'<span class="datagrid-range-separator"> - </span>' \
'<input value="2" class="datagrid-range-to" type="number" step="1" name="report[group_id][to]"/>',
)
should equal_to_dom(<<~HTML)
<input value="1" class="datagrid-range-from" type="number" step="1" name="report[group_id][from]" id="report_group_id"/>
<span class="datagrid-range-separator"> - </span>
<input value="2" class="datagrid-range-to" type="number" step="1" name="report[group_id][to]"/>
HTML
}
end

Expand All @@ -247,11 +247,11 @@ class MyTemplate
let(:view_options) { { partials: "not_existed" } }
let(:_range) { nil }
it {
should equal_to_dom(
'<input class="datagrid-range-from" type="number" step="1" name="report[group_id][from]" id="report_group_id">
should equal_to_dom(<<~HTML)
<input class="datagrid-range-from" type="number" step="1" name="report[group_id][from]" id="report_group_id">
<span class="datagrid-range-separator"> - </span>
<input class="datagrid-range-to" type="number" step="1" name="report[group_id][to]">',
)
<input class="datagrid-range-to" type="number" step="1" name="report[group_id][to]">
HTML
}
end
end
Expand Down Expand Up @@ -287,11 +287,11 @@ class MyTemplate
context "with only left bound" do
let(:_range) { ["2012-01-03", nil] }
it {
should equal_to_dom(
'<input value="2012-01-03" class="datagrid-range-from" type="date" name="report[created_at][from]" id="report_created_at"/>' \
'<span class="datagrid-range-separator"> - </span>' \
'<input class="datagrid-range-to" type="date" name="report[created_at][to]" value=""/>',
)
should equal_to_dom(<<~HTML)
<input value="2012-01-03" class="datagrid-range-from" type="date" name="report[created_at][from]" id="report_created_at"/>
<span class="datagrid-range-separator"> - </span>
<input class="datagrid-range-to" type="date" name="report[created_at][to]" value=""/>
HTML
}
it { should be_html_safe }
end
Expand Down Expand Up @@ -346,11 +346,11 @@ class MyTemplate
end
let(:_range) { [nil, nil] }
it {
should equal_to_dom(
'<input class="datagrid-range-from" type="date" value="" name="report[created_at][from]" id="report_created_at"/>' \
'<span class="datagrid-range-separator"> - </span>' \
'<input class="datagrid-range-to" type="date" value="" name="report[created_at][to]"/>',
)
should equal_to_dom(<<~HTML)
<input class="datagrid-range-from" type="date" value="" name="report[created_at][from]" id="report_created_at"/>
<span class="datagrid-range-separator"> - </span>
<input class="datagrid-range-to" type="date" value="" name="report[created_at][to]"/>
HTML
}
end
end
Expand Down Expand Up @@ -482,23 +482,25 @@ class MyTemplate
end
end
let(:view_options) { { partials: "deprecated_enum_checkboxes" } }
it { should equal_to_dom(
[["first", "first",false],["second","second",false]].to_json
) }
it {
should equal_to_dom(
[["first", "first", false], ["second", "second", false]].to_json,
)
}
end

context "when inline class attribute specified" do
let(:_filter_options) { {for: nil, class: 'custom-class'} }
let(:_filter_options) { { for: nil, class: "custom-class" } }

it { should equal_to_dom(<<~HTML) }
<div class="datagrid-enum-checkboxes">
<label class="custom-class">
<input id="report_category_first" value="first" type="checkbox" name="report[category][]">first
</label>
<label class="custom-class">
<input id="report_category_second" value="second" type="checkbox" name="report[category][]">second
</label>
</div>
<div class="datagrid-enum-checkboxes">
<label class="custom-class">
<input id="report_category_first" value="first" type="checkbox" name="report[category][]">first
</label>
<label class="custom-class">
<input id="report_category_second" value="second" type="checkbox" name="report[category][]">second
</label>
</div>
HTML
end
end
Expand Down
34 changes: 17 additions & 17 deletions spec/datagrid/generators/scaffold_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,18 @@ def grid_params

it "works" do
expect(subject.controller_code).to eq(<<~RUBY)
class UsersController < ApplicationController
def index
@grid = UsersGrid.new(grid_params)
@pagy, @assets = pagy(@grid.assets)
end
class UsersController < ApplicationController
def index
@grid = UsersGrid.new(grid_params)
@pagy, @assets = pagy(@grid.assets)
end
protected
protected
def grid_params
params.fetch(:users_grid, {}).permit!
def grid_params
params.fetch(:users_grid, {}).permit!
end
end
end
RUBY
end
end
Expand All @@ -70,11 +70,11 @@ def grid_params
describe "#view_code" do
it "works" do
expect(subject.view_code).to eq(<<~ERB)
<%= datagrid_form_with model: @grid, url: users_path %>
<%= datagrid_form_with model: @grid, url: users_path %>
<%= paginate(@grid.assets) %>
<%= datagrid_table @grid %>
<%= paginate(@grid.assets) %>
<%= paginate(@grid.assets) %>
<%= datagrid_table @grid %>
<%= paginate(@grid.assets) %>
ERB
end

Expand All @@ -85,11 +85,11 @@ def grid_params

it "works" do
expect(subject.view_code).to eq(<<~ERB)
<%= datagrid_form_with model: @grid, url: users_path %>
<%= datagrid_form_with model: @grid, url: users_path %>
<%= pagy_nav(@pagy) %>
<%= datagrid_table @grid, @records %>
<%= pagy_nav(@pagy) %>
<%= pagy_nav(@pagy) %>
<%= datagrid_table @grid, @records %>
<%= pagy_nav(@pagy) %>
ERB
end
end
Expand Down

0 comments on commit e82f48b

Please sign in to comment.