Skip to content

Commit

Permalink
WIP: Minor adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Jan 31, 2025
1 parent 4b536f9 commit c927cca
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 37 deletions.
4 changes: 2 additions & 2 deletions gem/apps/demo.ru
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,8 @@ class PagyDemo < Sinatra::Base
<h2>pagy<%= prefix %>_nav_js <span class="notes">Responsive nav <code>steps: {...}</code> (Resize the window to see)</span></h2>
<%= html = send(:"pagy#{prefix}_nav_js", @pagy, id: 'nav-js-responsive',
aria_label: 'Pages nav_js_responsive',
steps: { 0 => 5, 500 => 7, 750 => 9, 1000 => 11 }) %>
aria_label: 'Pages nav_js_responsive',
steps: { 0 => 5, 500 => 7, 750 => 9, 1000 => 11 }) %>
<%= highlight(html) %>
<h2>pagy<%= prefix %>_combo_nav_js</h2>
Expand Down
4 changes: 2 additions & 2 deletions gem/apps/keynav.ru
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ class PagyKeynav < Sinatra::Base
<h3>pagy_nav_js (responsive)</h3>
<p>
<%= pagy_nav_js(@pagy, id: 'nav-js-responsive',
aria_label: 'Pages (nav_js_responsive)',
steps: { 0 => 5, 500 => 7, 750 => 9, 1000 => 11 }) %>
aria_label: 'Pages (nav_js_responsive)',
steps: { 0 => 5, 500 => 7, 750 => 9, 1000 => 11 }) %>
</p>
<h3>pagy_combo_nav_js</h3>
<p>
Expand Down
14 changes: 7 additions & 7 deletions gem/lib/pagy/calendar/unit.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# frozen_string_literal: true

require_relative '../core/assignable'
require_relative '../core/shiftable'
require_relative '../core/rangeable'
require_relative '../core/navable'
require_relative '../core/seriable'

class Pagy
class Calendar
# Base class for time units subclasses (Year, Quarter, Month, Week, Day)
class Unit < Pagy
include Core::Assignable
include Core::Navable
include Core::Rangeable
include Core::Seriable
include Core::Shiftable

DEFAULT = { page: 1 }.freeze

Expand Down Expand Up @@ -74,12 +74,12 @@ def page_at(time, **opts)
def assign_unit_vars
raise OptionError.new(self, :format, 'to be a strftime format', @opts[:format]) unless @opts[:format].is_a?(String)
raise OptionError.new(self, :order, 'to be in [:asc, :desc]', @order) \
unless %i[asc desc].include?(@order = @opts[:order])
unless %i[asc desc].include?(@order = @opts[:order])

@starting, @ending = @opts[:period]
raise OptionError.new(self, :period, 'to be a an Array of min and max TimeWithZone instances', @opts[:period]) \
unless @starting.is_a?(ActiveSupport::TimeWithZone) \
&& @ending.is_a?(ActiveSupport::TimeWithZone) && @starting <= @ending
unless @starting.is_a?(ActiveSupport::TimeWithZone) \
&& @ending.is_a?(ActiveSupport::TimeWithZone) && @starting <= @ending
end

# Apply the strftime format to the time.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class Pagy
module Core
# Add methods enabling frontend navigation
module Navable
module Seriable
# Label for any page. Allow the customization of the output (overridden by the calendar)
def label(page: @page, **) = page.to_s

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class Pagy
module Core
# Add a few assign_* methods
module Assignable
module Shiftable
# Assign @prev and @next
def assign_prev_and_next
@prev = (@page - 1 unless @page == 1)
Expand Down
27 changes: 15 additions & 12 deletions gem/lib/pagy/keyset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'json'
require_relative 'modules/b64'

puts 'I am a keyset file'

class Pagy
# Implement wicked-fast keyset pagination for big data
class Keyset < Pagy
Expand All @@ -13,18 +15,19 @@ class Keyset < Pagy
class TypeError < ::TypeError; end

# Allow to run Keyset.new or Keyset::ActiveRecord.new
def self.new(set, **opts)
# Run the initializer if is an adapter instance (e.g. *::ActiveRecord instance)
return allocate.tap { |instance| instance.send(:initialize, set, **opts) } if /::(?:ActiveRecord|Sequel)$/.match?(name)

# Pick the right adapter-class and run new again on the adapter class
if defined?(::ActiveRecord) && set.is_a?(::ActiveRecord::Relation)
self::ActiveRecord
elsif defined?(::Sequel) && set.is_a?(::Sequel::Dataset)
self::Sequel
else
raise TypeError, "expected set to be an instance of ActiveRecord::Relation or Sequel::Dataset; got #{set.class}"
end.new(set, **opts)
def self.new(set, **)
# Run the initializer if it's a subclass instance (check without triggering autoload)
return allocate.tap { |instance| instance.send(:initialize, set, **) } \
if /::(?:ActiveRecord|Sequel)$/.match?(name)

subclass = if defined?(::ActiveRecord) && set.is_a?(::ActiveRecord::Relation)
self::ActiveRecord
elsif defined?(::Sequel) && set.is_a?(::Sequel::Dataset)
self::Sequel
else
raise TypeError, "expected set to be an instance of ActiveRecord::Relation or Sequel::Dataset; got #{set.class}"

Check failure on line 28 in gem/lib/pagy/keyset.rb

View workflow job for this annotation

GitHub Actions / Ruby 3.2 Test

Layout/LineLength: Line is too long. [131/130]

Check failure on line 28 in gem/lib/pagy/keyset.rb

View workflow job for this annotation

GitHub Actions / Ruby 3.3 Test

Layout/LineLength: Line is too long. [131/130]

Check failure on line 28 in gem/lib/pagy/keyset.rb

View workflow job for this annotation

GitHub Actions / Ruby 3.4 Test

Layout/LineLength: Line is too long. [131/130]
end
subclass.new(set, **)
end

def initialize(set, **opts) # rubocop:disable Lint/MissingSuper
Expand Down
4 changes: 2 additions & 2 deletions gem/lib/pagy/keyset/keynav.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require_relative '../core/navable'
require_relative '../core/seriable'

class Pagy
class Keyset
Expand All @@ -9,7 +9,7 @@ class Keynav < Keyset
autoload :ActiveRecord, PAGY_PATH.join('keyset/keynav/active_record')
autoload :Sequel, PAGY_PATH.join('keyset/keynav/sequel')

include Core::Navable
include Core::Seriable

# Avoid filter args conflicts in composite SQL fragments
CUTOFF_PREFIX = 'cutoff_'
Expand Down
12 changes: 6 additions & 6 deletions gem/lib/pagy/offset.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# frozen_string_literal: true

require_relative 'core/assignable'
require_relative 'core/navable'
require_relative 'core/shiftable'
require_relative 'core/seriable'
require_relative 'core/rangeable'

class Pagy
# Implements Offset Pagination
class Offset < Pagy
autoload :Countless, PAGY_PATH.join('offset/countless')

DEFAULT = { page: 1, size: 7 }.freeze

include Core::Assignable
include Core::Navable
include Core::Rangeable
include Core::Seriable
include Core::Shiftable

DEFAULT = { page: 1, size: 7 }.freeze

attr_reader :offset, :from, :to

Expand Down
7 changes: 3 additions & 4 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
# frozen_string_literal: true

require 'simplecov'
require_relative '../gem/lib/pagy'

require 'minitest/autorun'
unless ENV['RM_INFO'] # RubyMine safe
require "minitest/reporters"
Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new
end

require_relative '../gem/lib/pagy'
require 'minitest/autorun'

# /usr/local/lib/ruby/gems/3.4.0/gems/minitest-5.25.4/lib/minitest.rb
module Minitest
class UnexpectedError
module AvoidBacktraceMangling
def message # :nodoc:
bt = Minitest.filter_backtrace(backtrace)
.map { |p| p.sub(%r{^#{Dir.pwd}/}, "") } # <-- ^ sub inside map and not gsub on string
.map { |p| p.sub(%r{^#{Dir.pwd}/}, '') } # <-- ^ sub inside map and not gsub on string
.join("\n ")
"#{error.class}: #{error.message}\n #{bt}"
end
Expand Down

0 comments on commit c927cca

Please sign in to comment.