Search code examples
ruby-on-railssettingslogic

Dynamic settingslogic settings


I'm using the settingslogic gem in my Rails app (Ruby 2.0.0p247 / Rails 3.2.13).

I can dynamically change a setting when it is not nested e.g.

#config/settings.yml
defaults:&defaults
  mysetting: 1
  nested:
    myothersetting: 2

Settings[:mysetting] = 10
Settings.mysetting # puts 10

But I am unable to change a nested attribute in the same way:

Settings[:nested][:myothersetting] = 20
Settings.nested.myothersetting # puts 2

What am I doing wrong?


Solution

  • I'm not sure what version of ruby & rails you're using, but I tried using a similar setup as your yaml file and was presented with an error about bad parsing. I think that a key which has something nested underneath it cannot also have a value.

    That said, I experienced similar behavior - the hash-based accessor for nested values set/returned the expected values while the method-based accessor returned the value from the file and did not get the updates:

    # config/settings.yml
    defaults: &defaults
      thing1: 1
      thing2: 2
      nest:
        thing3: 3
    
    development:
      <<: *defaults
    
    2.0.0p247 :001 > Settings[:nest][:thing3] = 30
     => 30
    2.0.0p247 :002 > Settings[:nest][:thing3]
     => 30
    2.0.0p247 :003 > Settings.nest.thing3
     => 3
    

    It looks like this could be a bug in the gem, which based on the issue list on github seems to be one of many with nested attributes.