Skip to content

Compability with Rails engine #323

Open
@alienxp03

Description

I'm trying to use the gem with Rails engine, and noticed there might be some issue.

# Assume ENGINE_ROOT = /home/user/new_engine
# in engine.gemspec
s.add_dependency 'config'

# Common step for Rails engine. Add this code to ENGINE_ROOT/lib/new_engine/engine.rb
require 'config'

# Run command from the engine's root path
 > bundle install
> rails g config:install # all files are generated correctly in ENGINE_ROOT/config

Pretty straightforward setup so far. However adding any new settings doesn't load by default, which is odd. For example:

# ENGINE_ROOT/config/settings/settings.development.local
total: 10

# In Rails console
> Settings.total 
nil

> Settings.add_source!('config/settings/test.local.yml')
=> [#<Config::Sources::YAMLSource:0x0000563cbe0bbde0 @evaluate_erb=true, @path="/home/user/new_engine/spec/dummy/config/settings.yml">,
 #<Config::Sources::YAMLSource:0x0000563cbe0bbd68 @evaluate_erb=true, @path="/home/user/new_engine/spec/dummy/config/settings/test.yml">,
 #<Config::Sources::YAMLSource:0x0000563cbe0bbd18 @evaluate_erb=true, @path="/home/user/new_engine/spec/dummy/config/environments/test.yml">,
 #<Config::Sources::YAMLSource:0x0000563cbe0bbcf0 @evaluate_erb=true, @path="/home/user/new_engine/spec/dummy/config/settings/test.local.yml">,
 #<Config::Sources::YAMLSource:0x0000563cbe0bbcc8 @evaluate_erb=true, @path="/home/user/new_engine/spec/dummy/config/environments/test.local.yml">,
 #<Config::Sources::YAMLSource:0x0000563cbe2db6c0 @evaluate_erb=true, @path="config/settings/test.local.yml">]

Adding the source manually works, but what I noticed here is that Settings is trying to load the default settings files from the wrong location.

Correct path: /home/user/new_engine/config
Current path being used: /home/user/new_engine/spec/dummy/config.

Some background. In Rails engine, we can create a dummy application so that it's easier for us to test it out. Usually the path is ENGINE_ROOT/spec/folder_name. From what I noticed so far, it looks like Settings is trying to load the config from the dummy path not the engine's root path.

I'm just guessing at the moment, but it could be because Settings is using Rails.root to find the right default path. This should work for normal Rails app. However for Rails engine, Rails.root is referring to the dummy path hence the issue. In Rails engine, typically we want to use NewEngine::Engine.root for the right engine's path.

Hope all is clear so far. Based on this, I'm wondering if we could make the gem work by default with Rails engine. So far I can see two options that we can do with minimal changes to the gem.

  1. I'm not sure how, but instead of using Rails.root as the path, we can use NewEngine::Engine.root. instead. However, since every engine have different name, I'm not so sure on how can we accomplish this.
  2. I'm looking the default template. I'm thinking whether we could add another option for the developer to set/override the default root path. So this way, we can use Rails.root by default. And for Rails engine, I can manually override this path to use NewEngine::Engine.root instead.

Do let me know if I'm missing anything, or if there's anything that's not clear.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions