Skip to content

Commit 5e98241

Browse files
committed
Add StandardRB and set up a linting workflow
1 parent 62ea0b8 commit 5e98241

25 files changed

+443
-415
lines changed

.github/workflows/linting.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Linting
2+
3+
on:
4+
push:
5+
branches: [master]
6+
pull_request:
7+
branches: [master]
8+
9+
jobs:
10+
lint:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v4
16+
17+
- name: Setup Ruby and install gems
18+
uses: ruby/setup-ruby@v1
19+
with:
20+
ruby-version: 3.4
21+
bundler-cache: true
22+
23+
- name: Lint Ruby code
24+
run: |
25+
bundle exec standardrb
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: CI
1+
name: Tests
22

33
on:
44
push:
@@ -17,7 +17,8 @@ jobs:
1717
env:
1818
PAGINATOR: ${{ matrix.paginator }}
1919
steps:
20-
- uses: actions/checkout@v4
20+
- name: Checkout code
21+
uses: actions/checkout@v4
2122

2223
- name: Set up Ruby
2324
uses: ruby/setup-ruby@v1

Gemfile

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
source 'https://rubygems.org'
1+
source "https://rubygems.org"
22

33
# Specify your gem's dependencies in api_pagination.gemspec
44
gemspec
55

66
# Explicitly specify version constraints to match gemspec requirements
77
# Without these, Bundler may override gemspec constraints and install incompatible versions
8-
gem 'kaminari', '~> 1.2', '>= 1.2.1', require: false
9-
gem 'will_paginate', '~> 3.3', '>= 3.3.1', require: false
10-
gem 'pagy', '>= 9.4.0', '< 10.0.0', require: false
8+
gem "kaminari", "~> 1.2", ">= 1.2.1", require: false
9+
gem "will_paginate", "~> 3.3", ">= 3.3.1", require: false
10+
gem "pagy", ">= 9.4.0", "< 10.0.0", require: false
1111

12-
gem 'sqlite3', require: false
13-
gem 'sequel', '~> 5.49', require: false
12+
gem "sqlite3", require: false
13+
gem "sequel", "~> 5.49", require: false
14+
15+
gem "standardrb"

api-pagination.gemspec

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
1-
# encoding: utf-8
2-
$:.unshift(File.expand_path('../lib', __FILE__))
3-
require 'api-pagination/version'
1+
$:.unshift(File.expand_path("../lib", __FILE__))
2+
require "api-pagination/version"
43

54
Gem::Specification.new do |s|
6-
s.name = 'api-pagination'
7-
s.version = ApiPagination::VERSION
8-
s.authors = ['David Celis']
9-
s.email = ['me@davidcel.is']
10-
s.description = 'Link header pagination for Rails and Grape APIs'
11-
s.summary = "Link header pagination for Rails and Grape APIs. Don't use the request body."
12-
s.homepage = 'https://github.com/davidcelis/api-pagination'
13-
s.license = 'MIT'
5+
s.name = "api-pagination"
6+
s.version = ApiPagination::VERSION
7+
s.authors = ["David Celis"]
8+
s.email = ["me@davidcel.is"]
9+
s.description = "Link header pagination for Rails and Grape APIs"
10+
s.summary = "Link header pagination for Rails and Grape APIs. Don't use the request body."
11+
s.homepage = "https://github.com/davidcelis/api-pagination"
12+
s.license = "MIT"
1413

15-
s.files = Dir['lib/**/*']
16-
s.test_files = Dir['spec/**/*']
17-
s.require_paths = ['lib']
14+
s.files = Dir["lib/**/*"]
15+
s.require_paths = ["lib"]
1816

19-
s.required_ruby_version = '> 2.7'
17+
s.required_ruby_version = "> 2.7"
2018

21-
s.add_development_dependency 'kaminari', '~> 1.2', '>= 1.2.1'
22-
s.add_development_dependency 'pagy', '>= 9.4.0', '< 10.0.0'
23-
s.add_development_dependency 'will_paginate', '~> 3.3', '>= 3.3.1'
19+
s.add_development_dependency "kaminari", "~> 1.2", ">= 1.2.1"
20+
s.add_development_dependency "pagy", ">= 9.4.0", "< 10.0.0"
21+
s.add_development_dependency "will_paginate", "~> 3.3", ">= 3.3.1"
2422

25-
s.add_development_dependency 'rspec', '~> 3.10'
26-
s.add_development_dependency 'grape', '~> 1.6'
27-
s.add_development_dependency 'railties', '~> 7.0'
28-
s.add_development_dependency 'actionpack', '~> 7.0'
29-
s.add_development_dependency 'sequel', '~> 5.49'
30-
s.add_development_dependency 'activerecord-nulldb-adapter', '~> 0.9.0'
23+
s.add_development_dependency "rspec", "~> 3.10"
24+
s.add_development_dependency "grape", "~> 1.6"
25+
s.add_development_dependency "railties", "~> 7.0"
26+
s.add_development_dependency "actionpack", "~> 7.0"
27+
s.add_development_dependency "sequel", "~> 5.49"
28+
s.add_development_dependency "activerecord-nulldb-adapter", "~> 0.9.0"
3129
end

lib/api-pagination.rb

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
require 'api-pagination/configuration'
2-
require 'api-pagination/version'
1+
require "api-pagination/configuration"
2+
require "api-pagination/version"
33

44
module ApiPagination
55
class << self
66
def paginate(collection, options = {})
7-
options[:page] = options[:page].to_i
8-
options[:page] = 1 if options[:page] <= 0
7+
options[:page] = options[:page].to_i
8+
options[:page] = 1 if options[:page] <= 0
99
options[:per_page] = options[:per_page].to_i
1010

1111
case ApiPagination.config.paginator
@@ -26,10 +26,10 @@ def pages_from(collection, options = {})
2626
{}.tap do |pages|
2727
unless collection.first_page?
2828
pages[:first] = 1
29-
pages[:prev] = collection.current_page - 1
29+
pages[:prev] = collection.current_page - 1
3030
end
3131

32-
unless collection.last_page? || (ApiPagination.config.paginator == :kaminari && collection.out_of_range?)
32+
if !(collection.last_page? || (ApiPagination.config.paginator == :kaminari && collection.out_of_range?))
3333
pages[:last] = collection.total_pages if ApiPagination.config.include_total
3434
pages[:next] = collection.current_page + 1
3535
end
@@ -38,9 +38,9 @@ def pages_from(collection, options = {})
3838

3939
def total_from(collection)
4040
case ApiPagination.config.paginator
41-
when :pagy then collection.count.to_s
42-
when :kaminari then collection.total_count.to_s
43-
when :will_paginate then collection.total_entries.to_s
41+
when :pagy then collection.count.to_s
42+
when :kaminari then collection.total_count.to_s
43+
when :will_paginate then collection.total_entries.to_s
4444
end
4545
end
4646

@@ -60,14 +60,14 @@ def paginate_with_pagy(collection, options)
6060
collection[pagy.offset, pagy.limit]
6161
end
6262

63-
return [collection, pagy]
63+
[collection, pagy]
6464
end
6565

6666
def pagy_from(collection, options)
67-
if options[:count]
68-
count = options[:count]
67+
count = if options[:count]
68+
options[:count]
6969
else
70-
count = collection.is_a?(Array) ? collection.count : collection.count(:all)
70+
collection.is_a?(Array) ? collection.count : collection.count(:all)
7171
end
7272

7373
# Pagy 9.x requires keyword arguments
@@ -80,7 +80,7 @@ def pagy_pages_from(pagy)
8080
{}.tap do |pages|
8181
unless pagy.page == 1
8282
pages[:first] = 1
83-
pages[:prev] = pagy.prev
83+
pages[:prev] = pagy.prev
8484
end
8585

8686
unless pagy.page == pagy.pages
@@ -108,11 +108,11 @@ def paginate_with_will_paginate(collection, options)
108108
options[:per_page] = default_per_page_for_will_paginate(collection)
109109
end
110110

111-
collection = if defined?(Sequel::Dataset) && collection.kind_of?(Sequel::Dataset)
111+
collection = if defined?(Sequel::Dataset) && collection.is_a?(Sequel::Dataset)
112112
collection.paginate(options[:page], options[:per_page])
113113
else
114114
supported_options = [:page, :per_page, :total_entries]
115-
options = options.dup.keep_if { |k,v| supported_options.include?(k.to_sym) }
115+
options = options.dup.keep_if { |k, v| supported_options.include?(k.to_sym) }
116116
collection.paginate(options)
117117
end
118118

@@ -142,4 +142,4 @@ def extract_per_page_from_model(collection, accessor)
142142
end
143143
end
144144

145-
require 'api-pagination/hooks'
145+
require "api-pagination/hooks"

lib/api-pagination/configuration.rb

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ def configure(&block)
1717
end
1818

1919
def initialize
20-
@total_header = 'Total'
21-
@per_page_header = 'Per-Page'
22-
@page_header = nil
23-
@include_total = true
24-
@base_url = nil
20+
@total_header = "Total"
21+
@per_page_header = "Per-Page"
22+
@page_header = nil
23+
@include_total = true
24+
@base_url = nil
2525
@response_formats = [:json, :xml]
2626
end
2727

28-
['page', 'per_page'].each do |param_name|
28+
["page", "per_page"].each do |param_name|
2929
method_name = "#{param_name}_param"
3030
instance_variable_name = "@#{method_name}"
3131

@@ -37,15 +37,15 @@ def initialize
3737

3838
if instance_variable_get(instance_variable_name).nil?
3939
# use :page & :per_page by default
40-
instance_variable_set(instance_variable_name, (lambda { |p| p[param_name.to_sym] }))
40+
instance_variable_set(instance_variable_name, lambda { |p| p[param_name.to_sym] })
4141
end
4242

4343
instance_variable_get(instance_variable_name).call(params)
4444
end
4545

4646
define_method "#{method_name}=" do |param|
4747
if param.is_a?(Symbol) || param.is_a?(String)
48-
instance_variable_set(instance_variable_name, (lambda { |params| params[param] }))
48+
instance_variable_set(instance_variable_name, lambda { |params| params[param] })
4949
else
5050
raise ArgumentError, "Cannot set page_param option"
5151
end
@@ -78,24 +78,24 @@ def paginator=(paginator)
7878
def set_paginator
7979
conditions = [defined?(Pagy), defined?(Kaminari), defined?(WillPaginate::CollectionMethods)]
8080
if conditions.compact.size > 1
81-
Kernel.warn <<-WARNING
82-
Warning: api-pagination relies on Pagy, Kaminari, or WillPaginate, but more than
83-
one are currently active. If possible, you should remove one or the other. If
84-
you can't, you _must_ configure api-pagination on your own. For example:
85-
86-
ApiPagination.configure do |config|
87-
config.paginator = :kaminari
88-
end
89-
90-
You should also configure Kaminari to use a different `per_page` method name as
91-
using these gems together causes a conflict; some information can be found at
92-
https://github.com/activeadmin/activeadmin/wiki/How-to-work-with-will_paginate
93-
94-
Kaminari.configure do |config|
95-
config.page_method_name = :per_page_kaminari
96-
end
97-
98-
WARNING
81+
Kernel.warn <<~WARNING
82+
Warning: api-pagination relies on Pagy, Kaminari, or WillPaginate, but more than
83+
one are currently active. If possible, you should remove one or the other. If
84+
you can't, you _must_ configure api-pagination on your own. For example:
85+
86+
ApiPagination.configure do |config|
87+
config.paginator = :kaminari
88+
end
89+
90+
You should also configure Kaminari to use a different `per_page` method name as
91+
using these gems together causes a conflict; some information can be found at
92+
https://github.com/activeadmin/activeadmin/wiki/How-to-work-with-will_paginate
93+
94+
Kaminari.configure do |config|
95+
config.page_method_name = :per_page_kaminari
96+
end
97+
98+
WARNING
9999
elsif defined?(Pagy)
100100
use_pagy
101101
elsif defined?(Kaminari)
@@ -110,14 +110,19 @@ def use_pagy
110110
end
111111

112112
def use_kaminari
113-
require 'kaminari/models/array_extension'
113+
require "kaminari/models/array_extension"
114114
@paginator = :kaminari
115115
end
116116

117117
def use_will_paginate
118118
WillPaginate::CollectionMethods.module_eval do
119-
def first_page?() !previous_page end
120-
def last_page?() !next_page end
119+
def first_page?
120+
!previous_page
121+
end
122+
123+
def last_page?
124+
!next_page
125+
end
121126
end
122127

123128
@paginator = :will_paginate
@@ -132,6 +137,6 @@ def configure
132137
def config
133138
@config ||= Configuration.new
134139
end
135-
alias :configuration :config
140+
alias_method :configuration, :config
136141
end
137142
end

lib/api-pagination/hooks.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
begin; require 'grape'; rescue LoadError; end
1+
begin; require "grape"; rescue LoadError; end
22
if defined?(Grape::API)
3-
require 'grape/pagination'
3+
require "grape/pagination"
44

5-
klass = if Grape::VERSION >= '1.2.0' || defined?(Grape::API::Instance)
5+
klass = if Grape::VERSION >= "1.2.0" || defined?(Grape::API::Instance)
66
Grape::API::Instance
77
else
88
Grape::API
@@ -11,12 +11,12 @@
1111
klass.send(:include, Grape::Pagination)
1212
end
1313

14-
begin; require 'pagy'; rescue LoadError; end
15-
begin; require 'kaminari'; rescue LoadError; end
16-
begin; require 'will_paginate'; rescue LoadError; end
14+
begin; require "pagy"; rescue LoadError; end
15+
begin; require "kaminari"; rescue LoadError; end
16+
begin; require "will_paginate"; rescue LoadError; end
1717

1818
unless defined?(Pagy) || defined?(Kaminari) || defined?(WillPaginate::CollectionMethods)
19-
Kernel.warn <<-WARNING.gsub(/^\s{4}/, '')
19+
Kernel.warn <<-WARNING.gsub(/^\s{4}/, "")
2020
Warning: api-pagination relies on either Pagy, Kaminari, or WillPaginate.
2121
Please install a paginator by adding one of the following to your Gemfile:
2222
@@ -39,5 +39,5 @@ def self.rails_parent_controller
3939
end
4040
end
4141

42-
require 'api-pagination/railtie'
42+
require "api-pagination/railtie"
4343
end

lib/api-pagination/railtie.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
require 'rails/railtie'
1+
require "rails/railtie"
22

33
module ApiPagination
44
class Railtie < ::Rails::Railtie
55
initializer :api_pagination do
66
ActiveSupport.on_load(:action_controller) do
7-
require 'rails/pagination'
7+
require "rails/pagination"
88

99
klass = if Rails::VERSION::MAJOR >= 5 || defined?(ActionController::API)
1010
ActionController::API

lib/api-pagination/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Version
55
PATCH = 0
66

77
def self.to_s
8-
[MAJOR, MINOR, PATCH].join('.')
8+
[MAJOR, MINOR, PATCH].join(".")
99
end
1010
end
1111

0 commit comments

Comments
 (0)