Forum: Ruby on Rails how to provide plugin configuration?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Raphael B. (Guest)
on 2006-01-15 14:11
(Received via mailing list)
Hi,

I'm finalising a translation plugin I use in one app. I'd like the
plugin the be configurable but am not sure how to provide this
configurability. For example, there's a setting for the cookie name to
use when saving user's prefered language. I thought of having a class
TranslationConfig like this:

class TranslationsConfig
    def self.cookie_name
      :user_language
    end
end

But that implies that if you want to change this setting, you should
reopen the class somewhere (I'm not even sure that putting it in
environment.rb would work as I thought plugins are loaded last).

Also, how can write unit tests for different settings so I can check
that the right cookie name is used? Reopening a class in a method
doesn't work.


Any advice on this?

Thanks.

Raph
James A. (Guest)
on 2006-01-16 01:27
(Received via mailing list)
You could always store your configuration in a Hash..... some future
version of the Engines plugin will probably provide a class like this:

class HashWithStickyValues < Hash
  alias :__store :store
  def []=(key, value)
    store(key, value) # ensure that []= uses our new #store. This
might not be needed.
  end
  def store(key, value)
    __store(key, value) if !self.has_key?(key) # only store the value
if it's not set already
  end
  def force(key, value)
    __store(key, value) # always store the value
  end
  def method_missing(name, *args)
    val = (args.length > 1) ? args : args[0]
    if name.to_s["="]
      store(name.to_s.gsub(/=/, '').to_sym, val) # try to store the
values
    else
      fetch(name.to_sym, nil) # presume this was a method-style key
access
    end
  end
end

It mimics the behaviour of the config method I use with engines (see
the Engines plugin documentation for an explanation of this
behaviour). In tests, you can use the force method to set
configuration explicitly. This exact behaviour might not be useful
given the loading mechanism differences between plugins and engines,
but you might still consider something like this.

- james
This topic is locked and can not be replied to.