Skip to content

Commit

Permalink
Add configurable inflector with examples for ActiveSupport::Inflector (
Browse files Browse the repository at this point in the history
…#796)

Disable RuboCop Metrics/BlockLength

Add ActiveSupport::Inflector to Zeitwerk

- Configure Zeitwerk's inflector to use ActiveSupport::Inflector
- Add commented out example to config/initializer.rb
- Update existing documentation about ActiveSupport::Inflector to reference initializer configuration
- Add documentation for initializer
  • Loading branch information
jclusso authored Aug 29, 2023
1 parent 517db85 commit ecdff83
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 9 deletions.
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" => nil,
"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. In v2.0,
# ActiveSupport::Inflector will become the default.
#
# 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
17 changes: 17 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,23 @@ 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 can be configured to use ActiveSupport::Inflector. This
will become the default in v2.0.

```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

0 comments on commit ecdff83

Please sign in to comment.