psymeons
psymeons

Reputation: 41

Spree - Creating site-wide preferences

This is probably a routine job for anyone who customizes Spree to fit their needs, but I'm having quite a hard time with it.

I want to create a preference that will store an array of products, in order to display them as "featured products" in the homepage. I went through this guide, and I already created a file under lib/spree/ in my application tree. The file looks like this:

class Spree::HomepageConfiguration < Spree::Preferences::Configuration
  preference :featured_products, :string
end

After that I added the lib/spree folder in the autoload_paths in the application.rb file:

config.autoload_paths += %W(#{config.root}/lib/spree/)

Finally I created an initializer called homepage_configuration under config/initializers/ to configure the preference:

module Spree
  Spree::MyApp::Config = Spree::HomepageConfiguration.new
end

Spree::MyApp::Config[:featured_products] = ["a", "b"]

But, when I run the server I get this error:

Expected C:/path/to/my/application/lib/spree/homepage_configuration.rb to define HomepageConfiguration (LoadError)

I'm not sure what I'm doing wrong. Any suggestions are welcome.

I'm running Rails 3.2.13 and using Spree from 1-3-stable branch.

Upvotes: 0

Views: 1478

Answers (2)

mumoc
mumoc

Reputation: 41

Even is an really old question... I've been asked before for this same issue (configuring site wide spree preferences) recently.

As for the error reported and the code supplied, the lib/spree/homepage_configuration.rb should be:

module Spree
    class HomepageConfiguration < Spree::Preferences::Configuration
      preference :featured_products, :string
    end
end

Here is a sample gist with a configuration example I give recently https://gist.github.com/mumoc/55dce225244fb02363e2

Upvotes: 1

geermc4
geermc4

Reputation: 568

Why not make it a property of the product? Doing it your way would require a server restart and change of code every time you want to change a product.

I just added a checkbox to the product description to check when the product would be featured, and added a scope to the model scope :featured, lambda { |*args| { :conditions => { :featured => true }, :limit => args.first || 6, :order => "RAND()" } } that way you can check and uncheck at will.

Upvotes: 0

Related Questions