Archive

Posts Tagged ‘acts_as’

rails acts_as_configurable file config

March 7th, 2010 1 comment

Working on my new app I thought it is necessary to get some data from configuration file, there was already plugin that had the necessary functionality – acts_as_configurable.

Unfortunately this plugin was not exactly what I was looking for It had possibility to write configuration in the class itself – I needed configuration in file.

So I did fork of the project, and now you have possibility to use rewritten version of the plugin which uses yaml file for storing configuration.

Here is example configuration file:

default:
  :first: 1st
Settings2:
  :first: 2nd
settings:
  :first: 3rd

With this configuration all the following definitions are valid:

class Settings1
  acts_as_configurable
end
class Settings2
  acts_as_configurable
end
class Settings30
  acts_as_configurable :for => 'settings'
end
class Settings31
  acts_as_configurable :for => :settings
end
class Settings4
  acts_as_configurable :for => Settings2
end
class Settings50
  acts_as_configurable 'conf'
end
class Settings51
  acts_as_configurable :conf
end
class Settings52
  acts_as_configurable :name=>'conf', :default=>'settings'
end
class Settings53
  acts_as_configurable :name=>:conf, :default=>'settings'
end
class Settings60
  acts_as_configurable :name=>'conf', :for => Settings1, :default=>'settings'
end
class Settings61
  acts_as_configurable :name=>'conf', :for => Settings1, :default=>:settings
end
class Settings7
  acts_as_configurable :name=>'conf', :for => Settings2, :default=>'settings'
end
class Settings8
  acts_as_configurable :name=>'conf1', :for => Settings1
  acts_as_configurable :name=>'conf2', :for => Settings2
end

and using following rspec code it will pass tests:

describe "ActsAsConfigurable" do
  it "should respond to 'configuration'" do
    Settings1.configuration.should_not be_nil
  end
  it "should contain default values" do
    Settings1 .configuration[:first].should eql '1st'
  end
  it "should contain values" do
    Settings2.configuration[:first].should eql "2nd"
  end
  it "should contain values for string" do
    Settings30.configuration[:first].should eql "3rd"
  end
  it "should contain values for symbol" do
    Settings31.configuration[:first].should eql "3rd"
  end
  it "should contain values for class" do
    Settings4.configuration[:first].should eql "2nd"
  end
  it "should contain values for object" do
    Settings2.new.configuration[:first].should eql "2nd"
  end
  it "should respond to 'conf' - passed as string" do
    Settings50 .conf[:first].should eql '1st'
  end
  it "should respond to 'conf' - passed as symbol" do
    Settings51 .conf[:first].should eql '1st'
  end
  it "should respond to 'conf' - passed as :name=>" do
    Settings52 .conf[:first].should eql '3rd'
  end
  it "should respond to 'conf' - passed as :name=>symbol" do
    Settings53 .conf[:first].should eql '3rd'
  end
  it "should work with other default - passed as string" do
    Settings60 .conf[:first].should eql '3rd'
  end
  it "should work with other default - passed as symbol" do
    Settings61 .conf[:first].should eql '3rd'
  end
  it "should work with other default but also exisitng for" do
    Settings7 .conf[:first].should eql '2nd'
  end
  it "should work with multiple definitions" do
    Settings8 .conf1[:first].should eql '1st'
    Settings8 .conf2[:first].should eql '2nd'
  end
end

Sources for the plugin are here http://github.com/mpapis/acts_as_configurable

Categories: Development Tags: , , ,