huacnlee / rails-settings-cached

Global settings for your Rails application.
Other
1.06k stars 202 forks source link

Use Setting model in application.rb and other environment configs #193

Closed cweilemann closed 4 years ago

cweilemann commented 4 years ago

I've tried everything I can think of (including using the documented readonly: true methodology) to get the Setting model to work in my config/application.rb and other initializers, but for whatever reason, I cannot past the error of:

NameError: uninitialized constant AppName::Application::Setting

Is there some trick to initialize the Setting model and all the available configurations so they are accessible to the configuration files?

huacnlee commented 4 years ago

Put it in initializers/some.rb

cweilemann commented 4 years ago

@huacnlee I did this - moved app/models/setting.rb to config/initializers/setting.rb (as is), and now when starting rails s, I receive:

Traceback (most recent call last):
    64: from bin/rails:3:in `<main>'
    63: from bin/rails:3:in `load'
    62: from /Users/user/Projects/appname/bin/spring:16:in `<top (required)>'
    61: from /Users/user/.asdf/installs/ruby/2.6.1/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    60: from /Users/user/.asdf/installs/ruby/2.6.1/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    59: from /Users/user/.asdf/installs/ruby/2.6.1/lib/ruby/gems/2.6.0/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `<top (required)>'
    58: from /Users/user/.asdf/installs/ruby/2.6.1/lib/ruby/gems/2.6.0/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `load'
    57: from /Users/user/.asdf/installs/ruby/2.6.1/lib/ruby/gems/2.6.0/gems/spring-2.1.0/bin/spring:49:in `<top (required)>'
    56: from /Users/user/.asdf/installs/ruby/2.6.1/lib/ruby/gems/2.6.0/gems/spring-2.1.0/lib/spring/client.rb:30:in `run'
        ...
        ...
        ...
        /Users/user/.asdf/installs/ruby/2.6.1/lib/ruby/gems/2.6.0/gems/rails-settings-cached-2.2.0/lib/rails-settings/base.rb:129:in `_all_settings': You cannot use settings before Rails initialize. (RuntimeError)
huacnlee commented 4 years ago

@cweilemann Sorry I have a mistake of last reply.

In rails-settings-cached 2.x, I have added this limit to disallow use Setting before Rails initialize, to avoid load issue.

If you wants use some key in this case, you must be set the key as readonly mode.

For example:

class Setting
  field :asset_host, type: :string, default: ENV["ASSET_HOST"], readonly: true
end
cweilemann commented 4 years ago

@huacnlee I have readonly: true on every field that is used in config/application.rb and specific config/environment/*.rb files, and have the Setting model in app/models/setting.rb -- and with this setup, I get the error described in my original post.

I'm not sure if it matters, but I'm attempting to use a different set of field values for development, test, and production, by splitting them with an if / else statement:

if !Rails.env.production?
  # dev & test fields
else
  # production fields
end

I'm starting to wonder if this isn't supported, so I removed that but I still get the same error as in my original post.

huacnlee commented 4 years ago

I just changes documents for how to use Setting in Rails initializing.

And I have improved in recently, I suggest you upgrade to newest version.

https://github.com/huacnlee/rails-settings-cached#use-setting-in-rails-initializing

cweilemann commented 4 years ago

@huacnlee The documentation is a bit unclear around the field :mail_provider Setting - I don't see it being used.

Regardless, I tried building my project with the updated gem and using the updated, corresponding documentation. It seems I'm not even getting to the initializers as I'm still seeing this error:

/Users/cweilemann/Projects/<application>/config/application.rb:48:in `<class:Application>': uninitialized constant ApplicationName::Application::Setting (NameError)
    from /Users/cweilemann/Projects/<application>/config/application.rb:10:in `<module:ApplicationName>'
    from /Users/cweilemann/Projects/<application>/config/application.rb:9:in `<top (required)>'
    from /Users/cweilemann/.asdf/installs/ruby/2.6.1/lib/ruby/gems/2.6.0/gems/spring-2.1.0/lib/spring/application.rb:92:in `require'
    from /Users/cweilemann/.asdf/installs/ruby/2.6.1/lib/ruby/gems/2.6.0/gems/spring-2.1.0/lib/spring/application.rb:92:in `preload'
    from /Users/cweilemann/.asdf/installs/ruby/2.6.1/lib/ruby/gems/2.6.0/gems/spring-2.1.0/lib/spring/application.rb:157:in `serve'
    from /Users/cweilemann/.asdf/installs/ruby/2.6.1/lib/ruby/gems/2.6.0/gems/spring-2.1.0/lib/spring/application.rb:145:in `block in run'
    from /Users/cweilemann/.asdf/installs/ruby/2.6.1/lib/ruby/gems/2.6.0/gems/spring-2.1.0/lib/spring/application.rb:139:in `loop'
    from /Users/cweilemann/.asdf/installs/ruby/2.6.1/lib/ruby/gems/2.6.0/gems/spring-2.1.0/lib/spring/application.rb:139:in `run'
    from /Users/cweilemann/.asdf/installs/ruby/2.6.1/lib/ruby/gems/2.6.0/gems/spring-2.1.0/lib/spring/application/boot.rb:19:in `<top (required)>'
    from /Users/cweilemann/.asdf/installs/ruby/2.6.1/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Users/cweilemann/.asdf/installs/ruby/2.6.1/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from -e:1:in `<main>'

Line 47-48 in my config/application.rb is:

require File.expand_path('../boot', __FILE__)

require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module ApplicationName
  class Application < Rails::Application
    ...
    config.dynamic_assets = ActiveSupport::OrderedOptions.new # Line 47
    config.dynamic_assets.bucket_prefix = Setting.aws_bucket_prefix # Line 48 - defaults to "dev"
    ...
  end
end

Thus in my app/models/setting.rb, I have:

class Setting < RailsSettings::Base
  cache_prefix { "v1" }

  field :aws_bucket_prefix, type: :string, default: (ENV["AWS_BUCKET_PREFIX"] || "dev")   # , readonly: true - commented out per the new documentation
end

I'm not sure it'll help, but here's my Gemfile:

ruby "2.6.1"

gem 'rails', '~> 5.2' # Full version is Rails 5.2.4.2

gem 'pg'
gem 'pghero'
gem 'pg_query'

gem 'puma'

gem 'rack-cors', :require => 'rack/cors'

gem 'devise'
gem 'fog-aws'
gem 'pundit'

gem 'acts-as-taggable-on'
gem 'ancestry'
gem 'asset_sync'
gem 'awesome_print', :require => 'awesome_print'
gem 'aws-sdk-s3'
gem 'bootstrap3-datetimepicker-rails'
gem 'bootstrap-sass'
gem 'canonical-rails'
gem 'ckeditor'
gem 'cocoon'
gem 'coffee-rails'
gem 'connection_pool'
gem 'dalli'
gem 'draper'
gem 'execjs'
gem 'figaro', :git => "https://github.com/laserlemon/figaro.git"
gem 'gretel'
gem 'google-api-client'
gem 'haml-rails'
gem 'jbuilder'
gem 'jquery-datatables-rails', git: "https://github.com/rweng/jquery-datatables-rails.git"
gem 'jquery-rails'
gem 'jquery-ui-sass-rails', :git => 'https://github.com/cweilemann/jquery-ui-sass-rails.git'
gem 'momentjs-rails'
gem 'non-stupid-digest-assets'
gem 'paperclip'
gem 'paper_trail'
gem 'paranoia'
gem 'plyr-rails'
gem 'rails-settings-cached'
gem 'recaptcha', :require => 'recaptcha/rails'
gem 'redis'
gem 'rest-client'
gem 'rolify'
gem 'sass-rails'
gem 'sentry-raven'
gem 'sidekiq'
gem 'sidekiq-scheduler'
gem 'sinatra', :require => nil
gem 'sitemap_generator'
gem 'skylight'
gem 'sprockets', '~> 3'
gem 'state_machines-activerecord'
gem 'staccato'
gem 'therubyracer', :platforms => :ruby
gem 'to_bool'
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw]
gem 'uglifier'
gem 'will_paginate-bootstrap'

group :development do
  gem 'annotate'
  gem 'better_errors'
  gem 'binding_of_caller', :platforms=>[:mri_21]
  gem 'bullet'
  gem 'html2haml'
  gem 'rails-erd'
  gem 'rails_layout'
end

group :test do
  gem 'capybara'
  gem 'database_cleaner'
  gem 'faker'
  gem 'launchy'
  gem 'selenium-webdriver'
  gem 'shoulda-matchers'
  gem 'simplecov', :require => false
end

group :development, :test do
  gem 'factory_bot_rails', '~> 4'
  gem 'pry-byebug'
  gem 'rspec-activemodel-mocks'
  gem 'rspec-rails'
  gem 'spring'
end

group :doc do
  gem 'sdoc'
end

Full Ruby version is ruby 2.6.1p33 (2019-01-30 revision 66950) [x86_64-darwin19].

I'm also running macOS Big Sur with the latest public beta (I believe it's public beta 10) which I would think is relatively stable.

If you need more specific details, and you don't mind me emailing you, I can send you some private gists of more specific code.

Thanks again for all your help.

huacnlee commented 4 years ago

You can’t use Setting in config/application.rb, In that runtime, Rails model was not ready.

Chris Weilemann notifications@github.com于2020年10月20日 周二上午1:16写道:

@huacnlee https://github.com/huacnlee The documentation is a bit unclear around the field :mail_provider Setting - I don't see it being used.

Regardless, I tried building my project with the updated gem and using the updated, corresponding documentation. It seems I'm not even getting to the initializers as I'm still seeing this error:

/Users/cweilemann/Projects//config/application.rb:48:in <class:Application>': uninitialized constant ApplicationName::Application::Setting (NameError) from /Users/cweilemann/Projects/<application>/config/application.rb:10:in' from /Users/cweilemann/Projects//config/application.rb:9:in <top (required)>' from /Users/cweilemann/.asdf/installs/ruby/2.6.1/lib/ruby/gems/2.6.0/gems/spring-2.1.0/lib/spring/application.rb:92:inrequire' from /Users/cweilemann/.asdf/installs/ruby/2.6.1/lib/ruby/gems/2.6.0/gems/spring-2.1.0/lib/spring/application.rb:92:in preload' from /Users/cweilemann/.asdf/installs/ruby/2.6.1/lib/ruby/gems/2.6.0/gems/spring-2.1.0/lib/spring/application.rb:157:inserve' from /Users/cweilemann/.asdf/installs/ruby/2.6.1/lib/ruby/gems/2.6.0/gems/spring-2.1.0/lib/spring/application.rb:145:in block in run' from /Users/cweilemann/.asdf/installs/ruby/2.6.1/lib/ruby/gems/2.6.0/gems/spring-2.1.0/lib/spring/application.rb:139:inloop' from /Users/cweilemann/.asdf/installs/ruby/2.6.1/lib/ruby/gems/2.6.0/gems/spring-2.1.0/lib/spring/application.rb:139:in run' from /Users/cweilemann/.asdf/installs/ruby/2.6.1/lib/ruby/gems/2.6.0/gems/spring-2.1.0/lib/spring/application/boot.rb:19:in<top (required)>' from /Users/cweilemann/.asdf/installs/ruby/2.6.1/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in require' from /Users/cweilemann/.asdf/installs/ruby/2.6.1/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:inrequire' from -e:1:in `

'

Line 47-48 in my config/application.rb is:

require File.expand_path('../boot', FILE) require 'rails/all'

Require the gems listed in Gemfile, including any gems# you've limited to :test, :development, or :production.Bundler.require(*Rails.groups)

module ApplicationName class Application < Rails::Application ... config.dynamic_assets = ActiveSupport::OrderedOptions.new # Line 47 config.dynamic_assets.bucket_prefix = Setting.aws_bucket_prefix # Line 48 - defaults to "dev" ... endend

Thus in my app/models/setting.rb, I have:

class Setting < RailsSettings::Base cache_prefix { "v1" }

field :aws_bucket_prefix, type: :string, default: (ENV["AWS_BUCKET_PREFIX"] || "dev") # , readonly: true - commented out per the new documentationend

I'm not sure it'll help, but here's my Gemfile:

ruby "2.6.1" gem 'rails', '~> 5.2' # Full version is Rails 5.2.4.2 gem 'pg'gem 'pghero'gem 'pg_query' gem 'puma' gem 'rack-cors', :require => 'rack/cors' gem 'devise'gem 'fog-aws'gem 'pundit' gem 'acts-as-taggable-on'gem 'ancestry'gem 'asset_sync'gem 'awesome_print', :require => 'awesome_print'gem 'aws-sdk-s3'gem 'bootstrap3-datetimepicker-rails'gem 'bootstrap-sass'gem 'canonical-rails'gem 'ckeditor'gem 'cocoon'gem 'coffee-rails'gem 'connection_pool'gem 'dalli'gem 'draper'gem 'execjs'gem 'figaro', :git => "https://github.com/laserlemon/figaro.git"gem 'gretel'gem 'google-api-client'gem 'haml-rails'gem 'jbuilder'gem 'jquery-datatables-rails', git: "https://github.com/rweng/jquery-datatables-rails.git"gem 'jquery-rails'gem 'jquery-ui-sass-rails', :git => 'https://github.com/cweilemann/jquery-ui-sass-rails.git'gem 'momentjs-rails'gem 'non-stupid-digest-assets'gem 'paperclip'gem 'paper_trail'gem 'paranoia'gem 'plyr-rails'gem 'rails-settings-cached'gem 'recaptcha', :require => 'recaptcha/rails'gem 'redis'gem 'rest-client'gem 'rolify'gem 'sass-rails'gem 'sentry-raven'gem 'sidekiq'gem 'sidekiq-scheduler'gem 'sinatra', :require => nilgem 'sitemap_generator'gem 'skylight'gem 'sprockets', '~> 3'gem 'state_machines-activerecord'gem 'staccato'gem 'therubyracer', :platforms => :rubygem 'to_bool'gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw]gem 'uglifier'gem 'will_paginate-bootstrap' group :development do gem 'annotate' gem 'better_errors' gem 'binding_of_caller', :platforms=>[:mri_21] gem 'bullet' gem 'html2haml' gem 'rails-erd' gem 'rails_layout'end group :test do gem 'capybara' gem 'database_cleaner' gem 'faker' gem 'launchy' gem 'selenium-webdriver' gem 'shoulda-matchers' gem 'simplecov', :require => falseend group :development, :test do gem 'factory_bot_rails', '~> 4' gem 'pry-byebug' gem 'rspec-activemodel-mocks' gem 'rspec-rails' gem 'spring'end group :doc do gem 'sdoc'end

Full Ruby version is ruby 2.6.1p33 (2019-01-30 revision 66950) [x86_64-darwin19].

I'm also running macOS Big Sur with the latest public beta (I believe it's public beta 10) which I would think is relatively stable.

If you need more specific details, and you don't mind me emailing you, I can send you some private gists of more specific code.

Thanks again for all your help.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/huacnlee/rails-settings-cached/issues/193#issuecomment-712309580, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAABLDQHJWWHH77HVDPDAU3SLRX6ZANCNFSM4SLEZN5Q .

cweilemann commented 4 years ago

@huacnlee Ah ha! That makes sense! Sorry, brain glitch. Let me remove all that and see how things go. :)

cweilemann commented 4 years ago

Removing it from application.rb solved it! Thank you!

cweilemann commented 4 years ago

FYI - the docs say readonly: true say it can be used in application.rb - that may want to be changed :)