Skip to content

Instantly share code, notes, and snippets.

@mumoc
Last active May 9, 2016 12:59
Show Gist options
  • Save mumoc/55dce225244fb02363e2 to your computer and use it in GitHub Desktop.
Save mumoc/55dce225244fb02363e2 to your computer and use it in GitHub Desktop.
Spree Site Wide Preferences
# ***config/application.rb***
module SpreeDemo
class Application < Rails::Application
# more awesome code here...
config.to_prepare do |config|
# more code here...
config.autoload_paths += %W(#{config.root}/lib/spree/)
#...
end
end
end
# ***lib/spree/bucket_configuration.rb***
module Spree
class BucketConfiguration < Spree::Preferences::Configuration
preference :bucket_name, :string, default: "waka"
preference :bucket_url, :string, default: ""
end
end
# ***config/initializers/bucket_configuration.rb***
module Spree
SpreeDemo::Config = Spree::BucketConfiguration.new
end
SpreeDemo::Config[:bucket_name] = "blue_theme"
SpreeDemo::Config[:bucket_url] = "waka" + SpreeDemo::Config.bucket_name
@dfreerksen
Copy link

Another option is to create app/models/spree/app_configuration_decorator.rb. Inside of it add

Spree::AppConfiguration.class_eval do
  preference :bucket_name, :string, default: "waka"
  preference :bucket_url, :string, default: ""
end

No other changes or additions should be required. Once you restart the app, you can access the new preferences with Spree::Config.bucket_name and they can be set with Spree::Config.bucket_name = "blue_theme". You can also set these new preference in config/initializers/spree.rb

Depending on what you are wanting to do, messing with Spree::Config instead of creating a custom preferences configuration class isn't always the best of ideas.

@vishes-shell
Copy link

@dfreerksen sorry for bothering, but i was trying to write decorator for app configuration app_configuration_decorator.rb to add some preference and everything works ok, i guess. But if i am trying to set the new preference in config/initializers/spree.rb with Spree::Config[:new_preference] = "new preference value" i am getting
has_preference!': new_preference preference not defined (NoMethodError)
Have you successfuly set the newly created prefence yourself? And if you have, maybe you have any thoughts what am i missing?
Thanks!

@Infigic
Copy link

Infigic commented May 9, 2016

@vishelshell spring might be issue. stop spring and than start up.

bin/spring status
bin/spring stop
bin/rails c

and than check

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment