lib/rails-settings/settings.rb in rails-settings-cached-0.2.4 vs lib/rails-settings/settings.rb in rails-settings-cached-0.3.0
- old
+ new
@@ -1,10 +1,10 @@
module RailsSettings
class Settings < ActiveRecord::Base
self.table_name = 'settings'
- attr_accessible :var
+ # attr_accessible :var
class SettingNotFound < RuntimeError; end
cattr_accessor :defaults
@@defaults = {}.with_indifferent_access
@@ -16,22 +16,19 @@
#get or set a variable with the variable as the called method
def self.method_missing(method, *args)
method_name = method.to_s
super(method, *args)
-
rescue NoMethodError
#set a value for a variable
if method_name =~ /=$/
var_name = method_name.gsub('=', '')
value = args.first
self[var_name] = value
-
#retrieve a value
else
self[method_name]
-
end
end
#destroy the specified settings record
def self.destroy(var_name)
@@ -43,21 +40,30 @@
raise SettingNotFound, "Setting variable \"#{var_name}\" not found"
end
end
#retrieve all settings as a hash (optionally starting with a given namespace)
- def self.all(starting_with=nil)
- options = starting_with ? { :conditions => "var LIKE '#{starting_with}%'"} : {}
- vars = thing_scoped.find(:all, {:select => 'var, value'}.merge(options))
+ def self.all(starting_with = nil)
+ vars = thing_scoped.select("var,value")
+ if starting_with
+ vars = vars.where("var LIKE '#{starting_with}%'")
+ end
result = {}
vars.each do |record|
result[record.var] = record.value
end
result.with_indifferent_access
end
-
+
+ def self.where(sql = nil)
+ if sql
+ vars = thing_scoped.where(sql)
+ end
+ vars
+ end
+
#get a setting value by [] notation
def self.[](var_name)
if var = object(var_name)
var.value
elsif @@defaults[var_name.to_s]
@@ -103,11 +109,10 @@
def value=(new_value)
self[:value] = new_value.to_yaml
end
def self.thing_scoped
- self.scoped_by_thing_type_and_thing_id(nil, nil)
+ unscoped.where("thing_type is NULL and thing_id is NULL")
end
-
end
end