Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ActiveSupport::Inflector default for Zeitwerk #797

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

- Zeitwerk inflector is configured to use ActiveSupport::Inflector by default
**Possible breaking change**

## [1.3.0] - 2023-06-18

- esbuild and bundled configurations fixes
Expand Down Expand Up @@ -680,7 +683,7 @@ Final release of 0.21.0! See below for full changelog.
## 0.11.1 - 2020-04-24

* Add a git init step to `bridgetown new` command [#18](https://github.com/bridgetownrb/bridgetown/pull/18)
* Update sass-loader webpack config to support .sass [#14](https://github.com/bridgetownrb/bridgetown/pull/14) ([jaredmoody](https://github.com/jaredmoody))
* Update sass-loader webpack config to support .sass [#14](https://github.com/bridgetownrb/bridgetown/pull/14) ([jaredmoody](https://github.com/jaredmoody))
* Add customizable permalinks to Prototype Pages (aka `/path/to/:term/and/beyond`). Use hooks and in-memory caching to speed up Pagination. _Inspired by [use cases like this](https://annualbeta.com/blog/dynamic-social-sharing-images-with-eleventy/)…_ [#12](https://github.com/bridgetownrb/bridgetown/pull/12)

## 0.11.0 - 2020-04-21
Expand Down
1 change: 1 addition & 0 deletions bridgetown-core/lib/bridgetown-core/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def initialize(*)
category: { key: "categories", title: "Category" }, tag: { key: "tags", title: "Tag" },
},
"autoload_paths" => [],
"inflector" => ActiveSupport::Inflector,
"eager_load_paths" => [],
"autoloader_collapsed_paths" => [],
"additional_watch_paths" => [],
Expand Down
3 changes: 2 additions & 1 deletion bridgetown-core/lib/bridgetown-core/utils/loaders_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ def clear_descendants_for_reload(_cpath, value, _abspath)
end

def setup_loaders(autoload_paths = []) # rubocop:todo Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
(autoload_paths.presence || config.autoload_paths).each do |load_path|
(autoload_paths.presence || config.autoload_paths).each do |load_path| # rubocop:todo Metrics/BlockLength
if @loaders.key?(load_path)
raise "Zeitwerk loader already added for `#{load_path}'. Please check your config"
end

next unless Dir.exist? load_path

loader = Zeitwerk::Loader.new
loader.inflector = config.inflector if config.inflector
begin
loader.push_dir(load_path)
rescue Zeitwerk::Error
Expand Down
20 changes: 20 additions & 0 deletions bridgetown-core/lib/site_template/config/initializers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,26 @@
# config.autoload_paths << "models"
#

# You can configure the inflector used by Zeitwerk. By default it is
# configured to use ActiveSupport::Inflector.
#
# config.inflector = ActiveSupport::Inflector
#
# Add new inflection rules using the following format. Inflections
# are locale specific, and you may define rules for as many different
# locales as you wish. All of these examples are active by default:
# ActiveSupport::Inflector.inflections(:en) do |inflect|
# inflect.plural /^(ox)$/i, "\\1en"
# inflect.singular /^(ox)en/i, "\\1"
# inflect.irregular "person", "people"
# inflect.uncountable %w( fish sheep )
# end
#
# These inflection rules are supported but not enabled by default:
# ActiveSupport::Inflector.inflections(:en) do |inflect|
# inflect.acronym "RESTful"
# end

# You can use `init` to initialize various Bridgetown features or plugin gems.
# For example, you can use the Dotenv gem to load environment variables from
# `.env`. Just `bundle add dotenv` and then uncomment this:
Expand Down
16 changes: 16 additions & 0 deletions bridgetown-website/src/_docs/configuration/initializers.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,22 @@ init :dotenv

Now anywhere in your Ruby plugins, templates, etc., you can access environment variables via `ENV` once you've defined your `.env` file. Our integration also supports specially-named files such as `.env.development`, `.env.test`, etc.

### Inflector

Zeitwerk's inflector is configured to use ActiveSupport::Inflector by default.

```ruby
config.inflector = ActiveSupport::Inflector
```

To add new inflection rules, use the following format.

```ruby
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.acronym "RESTful"
end
```

### Parse Roda Routes

Because of how Roda works via its dynamic routing tree, there's no straightforward way to programmatically list out all the routes in your application.
Expand Down
9 changes: 1 addition & 8 deletions bridgetown-website/src/_docs/resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,14 +245,7 @@ The three types of relations you can configure are:
* **has_one**: a single resource you want to reference will define the slug of the current resource in _its_ front matter
* **has_many**: multiple resources you want to reference will define the slug of the current resource in their front matter

The "inflector" loaded in from Rails' ActiveSupport is used to convert between singular and plural collection names automatically. If you need to customize the inflector with words it doesn't specifically recognize, create a plugin and add your own:

```ruby
ActiveSuport::Inflector.inflections(:en) do |inflect|
inflect.plural /^(ox)$/i, '\1\2en'
inflect.singular /^(ox)en/i, '\1'
end
```
The "inflector" is loaded from Rails' ActiveSupport and is used to convert between singular and plural collection names automatically. If you need to customize the inflector with words it doesn't specifically recognize, see configuring ActiveSupport::Inflector in the [`config/initializers.rb`](/docs/configuration/initializers#inflector) file.

## Configuring Permalinks

Expand Down