Skip to content

Commit 6da4991

Browse files
authored
Merge pull request #52 from blocknotes/chore/address-rubocop-offenses
Address Rubocop offenses
2 parents d4aa90c + 5a0d557 commit 6da4991

File tree

16 files changed

+58
-55
lines changed

16 files changed

+58
-55
lines changed

.rubocop.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ plugins:
1010
AllCops:
1111
Exclude:
1212
- bin/*
13+
- extra/**/*
1314
- spec/dummy_rails/**/*
1415
- vendor/**/*
1516
NewCops: enable

extra/sample_features_app/admin/items.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ module Admin
1212

1313
class ItemsRepo < ::TinyAdmin::Plugins::BaseRepository
1414
def fields(options: nil)
15-
COLUMNS.each_with_object({}) do |name, result|
16-
result[name] = TinyAdmin::Field.create_field(name: name)
15+
COLUMNS.to_h do |name|
16+
[name, TinyAdmin::Field.create_field(name: name)]
1717
end
1818
end
1919

@@ -28,7 +28,7 @@ def index_title
2828
def list(page: 1, limit: 10, filters: nil, sort: ['id'])
2929
page_offset = page.positive? ? (page - 1) * limit : 0
3030
[
31-
RECORDS[page_offset...page_offset + limit],
31+
RECORDS[page_offset...(page_offset + limit)],
3232
RECORDS.size
3333
]
3434
end

lib/tiny_admin/actions/index.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ def evaluate_options(options)
5151

5252
def prepare_filters(fields)
5353
filters = (options[:filters] || []).map { _1.is_a?(Hash) ? _1 : { field: _1 } }
54-
filters = filters.each_with_object({}) { |filter, result| result[filter[:field]] = filter }
55-
values = (params['q'] || {})
54+
filters = filters.to_h { |filter| [filter[:field], filter] }
55+
values = params['q'] || {}
5656
fields.each_with_object({}) do |(name, field), result|
5757
result[field] = { value: values[name], filter: filters[name] } if filters.key?(name)
5858
end

lib/tiny_admin/plugins/base_repository.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
module TinyAdmin
44
module Plugins
55
class BaseRepository
6-
RecordNotFound = Class.new(StandardError)
6+
class RecordNotFound < StandardError
7+
end
78

89
attr_reader :model
910

lib/tiny_admin/plugins/no_auth.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def configure(_app, _opts = {}); end
88
end
99

1010
module InstanceMethods
11-
def authenticate_user!
11+
def authenticate_user! # rubocop:disable Naming/PredicateMethod
1212
true
1313
end
1414

lib/tiny_admin/views/components/pagination.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def view_template
2222
pages_range((current > pages - 4 ? current - 2 : pages - 2)..pages)
2323
else
2424
pages_range(1..1, with_dots: true)
25-
pages_range(current - 2..current + 2, with_dots: true)
25+
pages_range((current - 2)..(current + 2), with_dots: true)
2626
pages_range(pages..pages)
2727
end
2828
}

spec/lib/tiny_admin/actions/basic_action_spec.rb

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,36 @@
1111
it "handles simple string field names" do
1212
result = action.attribute_options(["id", "name"])
1313
expect(result).to eq(
14-
"id" => {field: "id"},
15-
"name" => {field: "name"}
14+
"id" => { field: "id" },
15+
"name" => { field: "name" }
1616
)
1717
end
1818

1919
it "handles single-entry hash with method shorthand" do
20-
result = action.attribute_options([{title: "downcase, capitalize"}])
20+
result = action.attribute_options([{ title: "downcase, capitalize" }])
2121
expect(result).to eq(
22-
"title" => {field: "title", method: "downcase, capitalize"}
22+
"title" => { field: "title", method: "downcase, capitalize" }
2323
)
2424
end
2525

2626
it "handles multi-entry hash with explicit field key" do
27-
result = action.attribute_options([{field: "author_id", link_to: "authors"}])
27+
result = action.attribute_options([{ field: "author_id", link_to: "authors" }])
2828
expect(result).to eq(
29-
"author_id" => {field: "author_id", link_to: "authors"}
29+
"author_id" => { field: "author_id", link_to: "authors" }
3030
)
3131
end
3232

3333
it "handles mixed input types" do
34-
result = action.attribute_options([
34+
opts = [
3535
"id",
36-
{title: "upcase"},
37-
{field: "created_at", method: "strftime, %Y-%m-%d"}
38-
])
36+
{ title: "upcase" },
37+
{ field: "created_at", method: "strftime, %Y-%m-%d" }
38+
]
39+
result = action.attribute_options(opts)
3940
expect(result).to eq(
40-
"id" => {field: "id"},
41-
"title" => {field: "title", method: "upcase"},
42-
"created_at" => {field: "created_at", method: "strftime, %Y-%m-%d"}
41+
"id" => { field: "id" },
42+
"title" => { field: "title", method: "upcase" },
43+
"created_at" => { field: "created_at", method: "strftime, %Y-%m-%d" }
4344
)
4445
end
4546
end

spec/lib/tiny_admin/field_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
end
2222

2323
it "uses the provided options when given" do
24-
options = {method: "downcase"}
24+
options = { method: "downcase" }
2525
field = described_class.create_field(name: "name", options: options)
2626
expect(field.options).to eq(options)
2727
end
@@ -39,7 +39,7 @@
3939

4040
describe "#apply_call_option" do
4141
it "chains method calls on the target" do
42-
field = described_class.new(name: "title", title: "Title", type: :string, options: {call: "to_s, downcase"})
42+
field = described_class.new(name: "title", title: "Title", type: :string, options: { call: "to_s, downcase" })
4343
expect(field.apply_call_option(42)).to eq("42")
4444
end
4545

@@ -49,7 +49,7 @@
4949
end
5050

5151
it "handles nil target safely via safe navigation" do
52-
field = described_class.new(name: "title", title: "Title", type: :string, options: {call: "nonexistent"})
52+
field = described_class.new(name: "title", title: "Title", type: :string, options: { call: "nonexistent" })
5353
expect(field.apply_call_option(nil)).to be_nil
5454
end
5555
end
@@ -66,7 +66,7 @@
6666
end
6767

6868
it "applies the helper method from options" do
69-
field = described_class.new(name: "name", title: "Name", type: :string, options: {method: "downcase"})
69+
field = described_class.new(name: "name", title: "Name", type: :string, options: { method: "downcase" })
7070
allow(TinyAdmin.settings).to receive(:helper_class).and_return(TinyAdmin::Support)
7171
expect(field.translate_value("HELLO")).to eq("hello")
7272
end
@@ -81,15 +81,15 @@ def self.upcase(value, options: [])
8181

8282
field = described_class.new(
8383
name: "name", title: "Name", type: :string,
84-
options: {method: "upcase", converter: "TestConverter"}
84+
options: { method: "upcase", converter: "TestConverter" }
8585
)
8686
expect(field.translate_value("hello")).to eq("HELLO")
8787
end
8888

8989
it "passes additional args to the method" do
9090
field = described_class.new(
9191
name: "value", title: "Value", type: :float,
92-
options: {method: "round, 1"}
92+
options: { method: "round, 1" }
9393
)
9494
allow(TinyAdmin.settings).to receive(:helper_class).and_return(TinyAdmin::Support)
9595
expect(field.translate_value(3.456)).to eq(3.5)

spec/lib/tiny_admin/plugins/active_record_repository_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
end
3232

3333
it "returns only specified fields when options given" do
34-
options = {"name" => {}, "email" => {}}
34+
options = { "name" => {}, "email" => {} }
3535
fields = repository.fields(options: options)
3636
expect(fields.keys).to eq(["name", "email"])
3737
end
@@ -72,7 +72,7 @@
7272
end
7373

7474
it "returns only specified fields when fields given", :aggregate_failures do
75-
attrs = repository.index_record_attrs(author, fields: {"name" => nil, "email" => nil})
75+
attrs = repository.index_record_attrs(author, fields: { "name" => nil, "email" => nil })
7676
expect(attrs.keys).to eq(["name", "email"])
7777
expect(attrs["name"]).to eq(author.name)
7878
end
@@ -93,7 +93,7 @@
9393
end
9494

9595
it "sorts when sort option given" do
96-
records, = repository.list(page: 1, limit: 10, sort: {name: :desc})
96+
records, = repository.list(page: 1, limit: 10, sort: { name: :desc })
9797
names = records.map(&:name)
9898
expect(names).to eq(names.sort.reverse)
9999
end
@@ -104,7 +104,7 @@
104104

105105
it "filters string fields with LIKE" do
106106
title_field = TinyAdmin::Field.new(name: "title", title: "Title", type: :string)
107-
filters = {title_field => {value: "post 1"}}
107+
filters = { title_field => { value: "post 1" } }
108108
results = post_repository.apply_filters(Post.all, filters)
109109
results.each do |post|
110110
expect(post.title.downcase).to include("post 1")
@@ -114,7 +114,7 @@
114114
it "filters non-string fields with equality" do
115115
author = Author.first
116116
author_field = TinyAdmin::Field.new(name: "author_id", title: "Author", type: :integer)
117-
filters = {author_field => {value: author.id}}
117+
filters = { author_field => { value: author.id } }
118118
results = post_repository.apply_filters(Post.all, filters)
119119
results.each do |post|
120120
expect(post.author_id).to eq(author.id)
@@ -123,14 +123,14 @@
123123

124124
it "skips filters with nil or empty values" do
125125
title_field = TinyAdmin::Field.new(name: "title", title: "Title", type: :string)
126-
filters = {title_field => {value: nil}}
126+
filters = { title_field => { value: nil } }
127127
results = post_repository.apply_filters(Post.all, filters)
128128
expect(results.count).to eq(Post.count)
129129
end
130130

131131
it "sanitizes SQL LIKE input" do
132132
title_field = TinyAdmin::Field.new(name: "title", title: "Title", type: :string)
133-
filters = {title_field => {value: "100%"}}
133+
filters = { title_field => { value: "100%" } }
134134
# Should not raise or cause SQL injection
135135
expect { post_repository.apply_filters(Post.all, filters).to_a }.not_to raise_error
136136
end

spec/lib/tiny_admin/route_for_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
RSpec.describe "TinyAdmin.route_for" do
3+
RSpec.describe "TinyAdmin.route_for" do # rubocop:disable RSpec/DescribeClass
44
before do
55
allow(TinyAdmin.settings).to receive(:root_path).and_return("/admin")
66
end

0 commit comments

Comments
 (0)