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

Where to put ConsistencyFail::Enforcer.enforce! #9

Closed
exalted opened this issue Jul 22, 2013 · 4 comments
Closed

Where to put ConsistencyFail::Enforcer.enforce! #9

exalted opened this issue Jul 22, 2013 · 4 comments

Comments

@exalted
Copy link

exalted commented Jul 22, 2013

Hi,

perhaps similar to #2, as per the updated README, I put…

require 'consistency_fail/enforcer'
ConsistencyFail::Enforcer.enforce!

… in config/environments/development.rb at first, but then I get an error:

12:20:37 web.1  | started with pid 12792
12:20:38 web.1  | I, [2013-07-22T12:20:38.488814 #12793]  INFO -- : Refreshing Gem list
12:20:40 web.1  | /Users/username/Development/foobar/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.12/lib/active_record/dynamic_matchers.rb:55:in `method_missing': undefined method `devise' for #<Class:0x007f92763a60a0> (NoMethodError)
12:20:40 web.1  |   from /Users/username/Development/foobar/app/models/user.rb:5:in `<class:User>'
12:20:40 web.1  |   from /Users/username/Development/foobar/app/models/user.rb:1:in `<top (required)>'
12:20:40 web.1  |   from /Users/username/Development/foobar/vendor/bundle/ruby/2.0.0/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:469:in `load'
12:20:40 web.1  |   from /Users/username/Development/foobar/vendor/bundle/ruby/2.0.0/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:469:in `block in load_file'
12:20:40 web.1  |   from /Users/username/Development/foobar/vendor/bundle/ruby/2.0.0/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:639:in `new_constants_in'
12:20:40 web.1  |   from /Users/username/Development/foobar/vendor/bundle/ruby/2.0.0/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:468:in `load_file'
12:20:40 web.1  |   from /Users/username/Development/foobar/vendor/bundle/ruby/2.0.0/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:353:in `require_or_load'
12:20:40 web.1  |   from /Users/username/Development/foobar/vendor/bundle/ruby/2.0.0/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:313:in `depend_on'
12:20:40 web.1  |   from /Users/username/Development/foobar/vendor/bundle/ruby/2.0.0/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:225:in `require_dependency'
12:20:40 web.1  |   from /Users/username/Development/foobar/vendor/bundle/ruby/2.0.0/gems/consistency_fail-0.3.1/lib/consistency_fail/models.rb:21:in `block (2 levels) in preload_all'
12:20:40 web.1  |   from /Users/username/Development/foobar/vendor/bundle/ruby/2.0.0/gems/consistency_fail-0.3.1/lib/consistency_fail/models.rb:20:in `each'
12:20:40 web.1  |   from /Users/username/Development/foobar/vendor/bundle/ruby/2.0.0/gems/consistency_fail-0.3.1/lib/consistency_fail/models.rb:20:in `block in preload_all'
12:20:40 web.1  |   from /Users/username/Development/foobar/vendor/bundle/ruby/2.0.0/gems/consistency_fail-0.3.1/lib/consistency_fail/models.rb:19:in `each'
12:20:40 web.1  |   from /Users/username/Development/foobar/vendor/bundle/ruby/2.0.0/gems/consistency_fail-0.3.1/lib/consistency_fail/models.rb:19:in `preload_all'
12:20:40 web.1  |   from /Users/username/Development/foobar/vendor/bundle/ruby/2.0.0/gems/consistency_fail-0.3.1/lib/consistency_fail/enforcer.rb:15:in `enforce!'
…

… without going into specifics of the above log, we could say that ConsistencyFail::Enforcer will load prior to devise in this case (or any other gem for that matter).

So, I moved enforce! check into the config/environment.rb and suddenly things seem to work for me.

But then I am not sure if is this okay for production environment, since consistency_fail is available/installed only in development group in Gemfile.

Any ideas/comments please? Thank you.

@trptcolin
Copy link
Owner

I wouldn't turn the Enforcer on in production, personally. It's extremely invasive.

The quick-n-nasty hack option would be to wrap that in an if Rails.env.development?, of course. Have you tried putting it in an initializer already? That's a good way to isolate it to its own file (though also would need the environment check).

There are a number of other options to load code during Rails startup, many of which are outlined in the Rails guide on configuration. I'd suggest taking a look there if you're looking for something different than the above options.

I'm also happy to take a docs pull request to clarify this. Let me know if this helps.

@exalted
Copy link
Author

exalted commented Jul 23, 2013

@trptcolin thanks for you answer. Indeed, I wasn’t considering for production, but again I couldn’t make it work in config/environments/development.rb either.

Checking Rails.env.development? could be a way to do this perhaps in config/environment.rb, but it certainly isn’t the prettiest: I am not sure how good it is to pollute that file with configurations for a specific environment instead of using config/environments/development.rb in the first place.

Specifically for the original issue that I opened I cannot use an initializer, because in my case I get a NoMethodError exception: 'method_missing': undefined method 'devise' for #<Class:0x007fd8fea01570> (NoMethodError).

I would still wait for some other suggestion of yours, but meanwhile how do you feel about this?

config/initializers/consistency_fail.rb:

Rails.application.config.after_initialize do
  if Rails.env.development?
    require 'consistency_fail/enforcer'
    ConsistencyFail::Enforcer.enforce!
  end
end

@trptcolin
Copy link
Owner

That solution seems fine to me.

@exalted
Copy link
Author

exalted commented Jul 23, 2013

👍

@exalted exalted closed this as completed Jul 23, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants