Skip to content

Commit

Permalink
WIP: Super refactoring 6
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Jan 12, 2025
1 parent 68c7643 commit adf6653
Show file tree
Hide file tree
Showing 60 changed files with 283 additions and 299 deletions.
20 changes: 10 additions & 10 deletions docs/api/javascript/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ Your app should [serve or bundle](#2-configure) a small [javascript file](#1-pic
[!file](/gem/javascripts/pagy.mjs)

```ruby
module_path = Pagy.root.join('javascripts', 'pagy.mjs')
module_path = Pagy::ROOT.join('javascripts', 'pagy.mjs')
```

[!file](/gem/javascripts/pagy.d.ts)

```ruby
types_path = Pagy.root.join('javascripts', 'pagy.d.ts')
types_path = Pagy::ROOT.join('javascripts', 'pagy.d.ts')
```

+++ `pagy.min.js`
Expand All @@ -59,7 +59,7 @@ types_path = Pagy.root.join('javascripts', 'pagy.d.ts')
[!file](/gem/javascripts/pagy.min.js)

```ruby
script_path = Pagy.root.join('javascripts', 'pagy.min.js')
script_path = Pagy::ROOT.join('javascripts', 'pagy.min.js')
```

+++ `pagy.js`
Expand All @@ -73,7 +73,7 @@ script_path = Pagy.root.join('javascripts', 'pagy.min.js')
[!file](/gem/javascripts/pagy.js)

```ruby
script_path = Pagy.root.join('javascripts', 'pagy.js')
script_path = Pagy::ROOT.join('javascripts', 'pagy.js')
```

+++ `pagy.js.map`
Expand All @@ -84,7 +84,7 @@ script_path = Pagy.root.join('javascripts', 'pagy.js')
[!file](/gem/javascripts/pagy.js.map)

```ruby
script_path = Pagy.root.join('javascripts', 'pagy.js.map')
script_path = Pagy::ROOT.join('javascripts', 'pagy.js.map')
```

+++
Expand All @@ -100,7 +100,7 @@ You can configure the app to look into the installed pagy gem javascript files.
+++ Propshaft

```ruby pagy.rb (initializer)
Rails.application.config.assets.paths << Pagy.root.join('javascripts')
Rails.application.config.assets.paths << Pagy::ROOT.join('javascripts')
```

```erb application.html.erb
Expand All @@ -110,7 +110,7 @@ Rails.application.config.assets.paths << Pagy.root.join('javascripts')
+++ Importmaps

```ruby pagy.rb (initializer)
Rails.application.config.assets.paths << Pagy.root.join('javascripts') #uncomment
Rails.application.config.assets.paths << Pagy::ROOT.join('javascripts') #uncomment
```

```js app/assets/config/manifest.js
Expand All @@ -124,7 +124,7 @@ pin 'pagy'
+++ Sprockets

```ruby pagy.rb (initializer)
Rails.application.config.assets.paths << Pagy.root.join('javascripts') # uncomment.
Rails.application.config.assets.paths << Pagy::ROOT.join('javascripts') # uncomment.
```

```js manifest.js (or "application.js" for old sprocket sprockets):
Expand Down Expand Up @@ -152,7 +152,7 @@ a regular app file. That way any builder will pick it up. For example:
# Create/refresh the `app/javascript/pagy.mjs` symlink/copy every time
# the app restarts (unless in production), ensuring syncing when pagy is updated.
# Replace the FileUtils.ln_sf with FileUtils.cp if your OS doesn't support file linking.
FileUtils.ln_sf(Pagy.root.join('javascripts', 'pagy.mjs'), Rails.root.join('app', 'javascript')) \
FileUtils.ln_sf(Pagy::ROOT.join('javascripts', 'pagy.mjs'), Rails.root.join('app', 'javascript')) \
unless Rails.env.production?
```

Expand Down Expand Up @@ -190,7 +190,7 @@ module.exports = {

#### Non-Rails apps

* Just ensure `Pagy.root.join('javascripts', 'pagy.js')` is served with the page.
* Just ensure `Pagy::ROOT.join('javascripts', 'pagy.js')` is served with the page.

### 3. Initialize Pagy

Expand Down
11 changes: 6 additions & 5 deletions docs/api/pagy.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,17 @@ You will typically use it in a `pagy.rb` initializer file to pass defaults value
Pagy::DEFAULT[:limit] = 25
Pagy::DEFAULT[:my_option] = 'my option'
...
Pagy::DEFAULT.freeze
```

## Constants

## Methods

==- `Pagy.root`
==- `Pagy::ROOT`

This method returns the `pathname` of the `pagy/gem` root dir. It is useful to get the absolute path of locale and javascript
This constant returns the `pathname` of the `pagy/gem` root dir. It is useful to get the absolute path of locale and javascript
files installed with the gem.

## Methods

==- `Pagy.new(**vars)`

_Notice_: If you use the `Pagy::Backend` its `pagy` method will instantiate and return the Pagy object for you.
Expand Down
6 changes: 3 additions & 3 deletions docs/api/stylesheets.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ the rest untouched.
[!file](/gem/stylesheets/pagy.scss)

```ruby
stylesheet_path = Pagy.root.join('stylesheets', 'pagy.scss')
stylesheet_path = Pagy::ROOT.join('stylesheets', 'pagy.scss')
```

:::code source="/gem/stylesheets/pagy.scss" :::
Expand All @@ -57,7 +57,7 @@ stylesheet_path = Pagy.root.join('stylesheets', 'pagy.scss')
[!file](/gem/stylesheets/pagy.css)

```ruby
stylesheet_path = Pagy.root.join('stylesheets', 'pagy.css')
stylesheet_path = Pagy::ROOT.join('stylesheets', 'pagy.css')
```

:::code source="/gem/stylesheets/pagy.css" :::
Expand All @@ -67,7 +67,7 @@ stylesheet_path = Pagy.root.join('stylesheets', 'pagy.css')
[!file](/gem/stylesheets/pagy.tailwind.css)

```ruby
stylesheet_path = Pagy.root.join('stylesheets', 'pagy.tailwind.css')
stylesheet_path = Pagy::ROOT.join('stylesheets', 'pagy.tailwind.css')
```

:::code source="/gem/stylesheets/pagy.tailwind.css" :::
Expand Down
1 change: 0 additions & 1 deletion gem/apps/calendar.ru
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ gemfile(ENV['PAGY_INSTALL_BUNDLE'] == 'true') do
end

# Pagy initializer
Pagy::DEFAULT.freeze

# Sinatra setup
require 'sinatra/base'
Expand Down
6 changes: 3 additions & 3 deletions gem/apps/demo.ru
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ class PagyDemo < Sinatra::Base
elsif format == 'map'
content_type 'application/json'
end
send_file Pagy.root.join('javascripts', params[:file])
send_file Pagy::ROOT.join('javascripts', params[:file])
end

get('/stylesheets/:file') do
content_type 'text/css'
send_file Pagy.root.join('stylesheets', params[:file])
send_file Pagy::ROOT.join('stylesheets', params[:file])
end

# One route/action per style
Expand Down Expand Up @@ -251,7 +251,7 @@ class PagyDemo < Sinatra::Base
<script src="https://cdn.tailwindcss.com?plugins=forms,typography,aspect-ratio"></script>
<!-- copy and paste the pagy.tailwind style in order to edit it -->
<style type="text/tailwindcss">
<%= Pagy.root.join('stylesheets', 'pagy.tailwind.css').read %>
<%= Pagy::ROOT.join('stylesheets', 'pagy.tailwind.css').read %>
</style>
ERB
end
Expand Down
3 changes: 1 addition & 2 deletions gem/apps/keyset.ru
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ end
# Pagy initializer
require 'pagy/extras/limit'
Pagy::DEFAULT[:limit] = 10
Pagy::DEFAULT.freeze

# Sinatra setup
require 'sinatra/base'
Expand Down Expand Up @@ -82,7 +81,7 @@ class PagyKeyset < Sinatra::Base
padding: 1rem 1.5rem 2rem !important;
}
<%= Pagy.root.join('stylesheets', 'pagy.css').read %>
<%= Pagy::ROOT.join('stylesheets', 'pagy.css').read %>
</style>
</head>
<body>
Expand Down
5 changes: 2 additions & 3 deletions gem/apps/keyset_augmented.ru
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ end
# Pagy initializer
require 'pagy/extras/limit'
Pagy::DEFAULT[:limit] = 4
Pagy::DEFAULT.freeze

# Sinatra setup
require 'sinatra/base'
Expand All @@ -48,7 +47,7 @@ class PagyKeysetAugmented < Sinatra::Base
elsif format == 'map'
content_type 'application/json'
end
send_file Pagy.root.join('javascripts', params[:file])
send_file Pagy::ROOT.join('javascripts', params[:file])
end

# Root route/action
Expand Down Expand Up @@ -98,7 +97,7 @@ class PagyKeysetAugmented < Sinatra::Base
padding: 1rem 1.5rem 2rem !important;
}
<%= Pagy.root.join('stylesheets', 'pagy.css').read %>
<%= Pagy::ROOT.join('stylesheets', 'pagy.css').read %>
</style>
</head>
<body>
Expand Down
3 changes: 1 addition & 2 deletions gem/apps/keyset_sequel.ru
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ end
# Pagy initializer
require 'pagy/extras/limit'
Pagy::DEFAULT[:limit] = 10
Pagy::DEFAULT.freeze

# Sinatra setup
require 'sinatra/base'
Expand Down Expand Up @@ -81,7 +80,7 @@ class PagyKeysetSequel < Sinatra::Base
padding: 1rem 1.5rem 2rem !important;
}
<%= Pagy.root.join('stylesheets', 'pagy.css').read %>
<%= Pagy::ROOT.join('stylesheets', 'pagy.css').read %>
</style>
</head>
<body>
Expand Down
6 changes: 3 additions & 3 deletions gem/apps/rails.ru
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ class PagyController < ActionController::Base
def javascripts
format = params[:file].split('.').last
if format == 'js'
render js: Pagy.root.join('javascripts', params[:file]).read
render js: Pagy::ROOT.join('javascripts', params[:file]).read
elsif format == 'map'
render json: Pagy.root.join('javascripts', params[:file]).read
render json: Pagy::ROOT.join('javascripts', params[:file]).read
end
end
end
Expand Down Expand Up @@ -166,7 +166,7 @@ TEMPLATE = <<~ERB
If you want to customize the style,
please replace the line below with the actual file content
*/
<%== Pagy.root.join('stylesheets', 'pagy.css').read %>
<%== Pagy::ROOT.join('stylesheets', 'pagy.css').read %>
</style>
</head>
Expand Down
5 changes: 2 additions & 3 deletions gem/apps/repro.ru
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ end
# pagy initializer
require 'pagy/extras/limit'
Pagy::Offset::DEFAULT[:overflow] = :empty_page
Pagy::DEFAULT.freeze

# Sinatra setup
require 'sinatra/base'
Expand All @@ -48,7 +47,7 @@ class PagyRepro < Sinatra::Base
elsif format == 'map'
content_type 'application/json'
end
send_file Pagy.root.join('javascripts', params[:file])
send_file Pagy::ROOT.join('javascripts', params[:file])
end

# Edit this action as needed
Expand Down Expand Up @@ -102,7 +101,7 @@ class PagyRepro < Sinatra::Base
If you want to customize the style,
please replace the line below with the actual file content
*/
<%= Pagy.root.join('stylesheets', 'pagy.css').read %>
<%= Pagy::ROOT.join('stylesheets', 'pagy.css').read %>
</style>
</head>
<body>
Expand Down
16 changes: 16 additions & 0 deletions gem/config/pagy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,19 @@
# { locale: 'xyz', # not built-in
# filepath: 'path/to/pagy-xyz.yml',
# pluralize: lambda{ |count| ... } )

# Changing DEFAULT for a class will also load the class
# DEFAULTs get inherited from the super classes and merged with the instance argument variables
#
# Pagy::DEFAULT[...] = ...
# Pagy::Offset::DEFAULT[...] = ...
# Pagy::Offset::Calendar::Day::DEFAULT[...] = ...
# Pagy::Offset::Calendar::Month::DEFAULT[...] = ...
# Pagy::Offset::Calendar::Quarter::DEFAULT[...] = ...
# Pagy::Offset::Calendar::Week::DEFAULT[...] = ...
# Pagy::Offset::Calendar::Year::DEFAULT[...] = ...
# Pagy::Offset::Countless::DEFAULT[...] = ...
# Pagy::Offset::ElasticsearchRails::DEFAULT[...] = ...
# Pagy::Offset::Meilisearch::DEFAULT[...] = ...
# Pagy::Offset::Searchkick::DEFAULT[...] = ...
# Pagy::Keyset::Augmented::DEFAULT[...] = ...
38 changes: 19 additions & 19 deletions gem/lib/pagy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@

# Top superclass: it defines only what's common to all the subclasses
class Pagy
autoload :Backend, 'pagy/backend'
autoload :Frontend, 'pagy/frontend'
autoload :I18n, 'pagy/i18n'
autoload :UISupport, 'pagy/ui_support'
autoload :Offset, 'pagy/offset'
autoload :Keyset, 'pagy/keyset'
autoload :ElasticsearchRails, 'pagy/mixins/elasticsearch_rails'
autoload :Meilisearch, 'pagy/mixins/meilisearch'
autoload :Searchkick, 'pagy/mixins/searchkick'
autoload :Console, 'pagy/console'

VERSION = '9.3.3'
VERSION = '9.3.3'
ROOT = Pathname.new(__dir__).parent.freeze
PAGY_PATH = ROOT.join('lib/pagy').freeze

autoload :Backend, PAGY_PATH.join('backend').to_s
autoload :Frontend, PAGY_PATH.join('frontend').to_s
autoload :I18n, PAGY_PATH.join('helpers/i18n').to_s
autoload :UISupport, PAGY_PATH.join('helpers/ui_support').to_s
autoload :Offset, PAGY_PATH.join('offset').to_s
autoload :Keyset, PAGY_PATH.join('keyset').to_s
autoload :Search, PAGY_PATH.join('helpers/search').to_s
autoload :ElasticsearchRails, PAGY_PATH.join('mixins/elasticsearch_rails').to_s
autoload :Meilisearch, PAGY_PATH.join('mixins/meilisearch').to_s
autoload :Searchkick, PAGY_PATH.join('mixins/searchkick').to_s
autoload :Console, PAGY_PATH.join('console').to_s

DEFAULT = { limit: 20, page_sym: :page } # rubocop:disable Style/MutableConstant
PAGE_TOKEN = 'P '
LABEL_TOKEN = 'L'
LIMIT_TOKEN = 'L '
A_TAG = '<a href="#" style="display: none;">#</a>'
DEFAULT = { limit: 20, # rubocop:disable Style/MutableConstant
page_sym: :page }

# Gem root pathname to get the path of Pagy files stylesheets, javascripts, apps, locales, etc.
def self.root = @root ||= Pathname.new(__dir__).parent.freeze

# Used by the pagy links
def self.predict_last? = true
Expand All @@ -50,10 +50,10 @@ def assign_limit
# Assign @vars
def assign_vars(vars)
cclass = self.class
default = cclass::DEFAULT
default = { **cclass::DEFAULT }
while cclass != Pagy
cclass = cclass.superclass
default = cclass::DEFAULT.merge(default)
default = { **cclass::DEFAULT, **default }
end
@vars = { **default, **vars.delete_if { |k, v| default.key?(k) && (v.nil? || v == '') } }
end
Expand Down
2 changes: 1 addition & 1 deletion gem/lib/pagy/extras/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ def pagy_t(key, **)
Frontend.prepend I18nExtra::FrontendOverride
end
# Add the pagy locales to the I18n.load_path
::I18n.load_path += Dir[Pagy.root.join('locales', '*.yml')]
::I18n.load_path += Dir[Pagy::ROOT.join('locales', '*.yml')]
end
Loading

0 comments on commit adf6653

Please sign in to comment.