settler 1.1.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +11 -2
- data/VERSION +1 -1
- data/lib/setting.rb +29 -11
- data/lib/settler.rb +1 -1
- data/lib/type_casters.rb +1 -1
- data/settler.gemspec +4 -4
- data/test/helper.rb +5 -4
- data/test/test_settler.rb +22 -4
- metadata +10 -5
data/README.rdoc
CHANGED
@@ -4,7 +4,7 @@ Settler can be used for defining application wide settings in Rails. Settings ar
|
|
4
4
|
|
5
5
|
== Requirements
|
6
6
|
|
7
|
-
* Activerecord, tested with Rails 2.3.5
|
7
|
+
* Activerecord, tested with Rails 2.3.5, 2.3.8 & rails 3.
|
8
8
|
|
9
9
|
== Installation
|
10
10
|
|
@@ -133,7 +133,16 @@ When you require this class in your application, you can easily use your typecas
|
|
133
133
|
>> Settler.google_analytics_key.destroy
|
134
134
|
Setting Load (0.7ms) SELECT \* FROM "settings" WHERE ("settings"."key" = 'google_analytics_key') LIMIT 1
|
135
135
|
=> false
|
136
|
-
|
136
|
+
|
137
|
+
* As of version 1.2, settings can always be updated when using the update_attribute method. Before that version, a before_filter was used instead of a validation. Note that you can change the validation message for settings that cannot be changed by adding the key 'settler.errors.editable' to I18n. Defaults to: 'cannot be changed'.
|
138
|
+
|
139
|
+
* Settings can be reset to the values defined in the configuration file by calling reset!:
|
140
|
+
|
141
|
+
>> Settler.google_analytics_key.reset!
|
142
|
+
=> true
|
143
|
+
|
144
|
+
Note that this will always work regardless of the 'editable' attribute, so be careful when using this method!
|
145
|
+
|
137
146
|
* The Setting model performs a soft delete when it is destroyed, meaning the record is not really destroyed, but just marked as deleted. The reason for doing this is because settings are reloaded from the configuration file when your application is (re)started, unless a setting is already available in the database. Therefore, it should know about all deleted settings, otherwise it would re-insert the deleted setting. If you want to enforce this behaviour, use Setting#delete instead.
|
138
147
|
|
139
148
|
== Advanced usage
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.2.0
|
data/lib/setting.rb
CHANGED
@@ -2,19 +2,23 @@ require 'type_casters'
|
|
2
2
|
|
3
3
|
# The Setting class is an AR model that encapsulates a Settler setting. The key if the setting is the only required attribute.\
|
4
4
|
class Setting < ActiveRecord::Base
|
5
|
+
cattr_accessor :rails3
|
6
|
+
self.rails3 = defined?(ActiveRecord::VERSION) && ActiveRecord::VERSION::MAJOR >= 3
|
7
|
+
|
5
8
|
attr_readonly :key
|
6
9
|
attr_protected :editable, :deletable, :deleted
|
7
10
|
|
8
|
-
before_update :ensure_editable
|
9
|
-
|
10
11
|
validates_presence_of :key
|
11
12
|
validate :setting_validations
|
13
|
+
if rails3 then validate(:ensure_editable, :on => :update) else validate_on_update(:ensure_editable) end
|
12
14
|
|
13
15
|
serialize :value
|
14
16
|
|
15
17
|
default_scope :conditions => ['deleted = ? or deleted IS NULL', false]
|
16
|
-
|
17
|
-
|
18
|
+
|
19
|
+
scope_method = rails3 ? :scope : :named_scope
|
20
|
+
send scope_method, :editable, :conditions => { :editable => true }
|
21
|
+
send scope_method, :deletable, :conditions => { :deletable => true }
|
18
22
|
|
19
23
|
# Returns the value, typecasted if a typecaster is available.
|
20
24
|
def value
|
@@ -60,6 +64,16 @@ class Setting < ActiveRecord::Base
|
|
60
64
|
end
|
61
65
|
end
|
62
66
|
|
67
|
+
# Resets this setting to the default stored in the settler configuration
|
68
|
+
def reset!
|
69
|
+
defaults = Settler.config[self.key]
|
70
|
+
self.alt = defaults['alt']
|
71
|
+
self.value = defaults['value']
|
72
|
+
self.editable = defaults['editable']
|
73
|
+
self.deletable = defaults['deletable']
|
74
|
+
rails3 ? save(:validate => false) : save(false)
|
75
|
+
end
|
76
|
+
|
63
77
|
# Can be used to get *all* settings, including deleted settings.
|
64
78
|
def self.without_default_scope &block
|
65
79
|
Setting.with_exclusive_scope(&block)
|
@@ -68,15 +82,17 @@ class Setting < ActiveRecord::Base
|
|
68
82
|
# Deleted scope is specified as a method as it needs to be an exclusive scope
|
69
83
|
def self.deleted
|
70
84
|
Setting.without_default_scope{ Setting.all :conditions => { :deleted => true } }
|
71
|
-
end
|
85
|
+
end
|
72
86
|
|
73
|
-
|
87
|
+
private
|
74
88
|
|
75
89
|
# Performs instance validations as defined in the settler configuration.
|
76
90
|
def setting_validations
|
77
|
-
|
78
|
-
|
79
|
-
|
91
|
+
if errors.empty?
|
92
|
+
errors.add(:value, I18n.t(:blank, :scope => 'activerecord.errors.messages')) if validators['presence'] && ['1','true',true].include?(validators['presence']) && self.value.nil?
|
93
|
+
errors.add(:value, I18n.t(:inclusion, :scope => 'activerecord.errors.messages')) if valid_values && !valid_values.include?(self.value)
|
94
|
+
errors.add(:value, I18n.t(:invalid, :scope => 'activerecord.errors.messages')) if validators['format'] && !(Regexp.new(validators['format']) =~ self.value)
|
95
|
+
end
|
80
96
|
end
|
81
97
|
|
82
98
|
# Retrieves all validators defined for this setting.
|
@@ -89,7 +105,9 @@ protected
|
|
89
105
|
@typecaster ||= Typecaster.typecaster_for(typecast)
|
90
106
|
end
|
91
107
|
|
92
|
-
#
|
93
|
-
def ensure_editable
|
108
|
+
# Ensures uneditable settings cannot be updated.
|
109
|
+
def ensure_editable
|
110
|
+
errors.add(:value, I18n.t('settler.errors.editable', :default => 'cannot be changed')) if value_changed? && !editable?
|
111
|
+
end
|
94
112
|
|
95
113
|
end
|
data/lib/settler.rb
CHANGED
@@ -8,7 +8,7 @@ class Settler
|
|
8
8
|
private_class_method :new
|
9
9
|
cattr_accessor :config, :raise_missing, :report_missing
|
10
10
|
cattr_writer :namespace, :source
|
11
|
-
|
11
|
+
|
12
12
|
class << self
|
13
13
|
# Loads the settler configuration from settler.yml and defines methods for retrieving the found settings.
|
14
14
|
def load!
|
data/lib/type_casters.rb
CHANGED
@@ -13,7 +13,7 @@ class Typecaster
|
|
13
13
|
# Returns the first typecaster from the list of typecasters that is able to typecast the passed type.
|
14
14
|
def self.typecaster_for(typecast)
|
15
15
|
typecaster = typecast.constantize.instance rescue nil
|
16
|
-
typecaster.present? && typecaster.is_a?(Typecaster) ? typecaster : registered_typecasters.detect{|tc| tc.type == typecast } || self.
|
16
|
+
typecaster.present? && typecaster.is_a?(Typecaster) ? typecaster : registered_typecasters.detect{|tc| tc.type == typecast } || self.instance
|
17
17
|
end
|
18
18
|
|
19
19
|
# Subclasses should implement this method and return the type of data it can typecast.
|
data/settler.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{settler}
|
8
|
-
s.version = "1.
|
8
|
+
s.version = "1.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Reinier de Lange"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-10-27}
|
13
13
|
s.description = %q{This gem is a combination of the Squeegy's rails-settings and Binarylogic's settingslogic gem, meaning it reads its configuration from a YAML file, but stores all settings in the database as well for on the fly changes.}
|
14
14
|
s.email = %q{r.j.delange@nedforce.nl}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -42,7 +42,7 @@ Gem::Specification.new do |s|
|
|
42
42
|
s.homepage = %q{http://github.com/moiristo/settler}
|
43
43
|
s.rdoc_options = ["--charset=UTF-8"]
|
44
44
|
s.require_paths = ["lib"]
|
45
|
-
s.rubygems_version = %q{1.3.
|
45
|
+
s.rubygems_version = %q{1.3.7}
|
46
46
|
s.summary = %q{Settler manages global application settings in Rails}
|
47
47
|
s.test_files = [
|
48
48
|
"test/custom_typecaster.rb",
|
@@ -55,7 +55,7 @@ Gem::Specification.new do |s|
|
|
55
55
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
56
56
|
s.specification_version = 3
|
57
57
|
|
58
|
-
if Gem::Version.new(Gem::
|
58
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
59
59
|
else
|
60
60
|
end
|
61
61
|
else
|
data/test/helper.rb
CHANGED
@@ -5,12 +5,10 @@ require 'pp'
|
|
5
5
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
6
6
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
7
7
|
|
8
|
-
require 'FileUtils'
|
9
|
-
|
10
8
|
gem "activerecord"
|
9
|
+
require 'fileutils'
|
11
10
|
require 'active_record'
|
12
|
-
|
13
|
-
require File.dirname(__FILE__) + '/../init.rb'
|
11
|
+
require 'active_support/core_ext/class/attribute_accessors'
|
14
12
|
|
15
13
|
def load_schema
|
16
14
|
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
|
@@ -36,3 +34,6 @@ def load_schema
|
|
36
34
|
load(File.dirname(__FILE__) + "/schema.rb")
|
37
35
|
end
|
38
36
|
|
37
|
+
load_schema
|
38
|
+
require File.dirname(__FILE__) + '/../init.rb'
|
39
|
+
|
data/test/test_settler.rb
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
require 'helper'
|
2
2
|
|
3
3
|
class TestSettler < Test::Unit::TestCase
|
4
|
-
load_schema
|
5
|
-
|
6
4
|
def setup
|
7
5
|
Setting.without_default_scope{ Setting.delete_all }
|
8
6
|
Settler.source = File.dirname(__FILE__) + '/settler.yml'
|
@@ -55,10 +53,18 @@ class TestSettler < Test::Unit::TestCase
|
|
55
53
|
|
56
54
|
def test_should_not_update_uneditable_setting
|
57
55
|
uneditable_setting = Settler.search_algorithm
|
58
|
-
assert !uneditable_setting.update_attributes(:value => '
|
56
|
+
assert !uneditable_setting.update_attributes(:value => 'sphinx')
|
57
|
+
assert Setting.rails3 ? uneditable_setting.errors[:value].any? : uneditable_setting.errors.on(:value).present?
|
59
58
|
assert_equal 'ferret', Settler[:search_algorithm]
|
60
59
|
end
|
61
60
|
|
61
|
+
def test_should_manually_update_uneditable_setting
|
62
|
+
uneditable_setting = Settler.search_algorithm
|
63
|
+
assert uneditable_setting.update_attribute(:value, 'sphinx')
|
64
|
+
assert Setting.rails3 ? uneditable_setting.errors[:value].empty? : uneditable_setting.errors.on(:value).nil?
|
65
|
+
assert_equal 'sphinx', Settler[:search_algorithm]
|
66
|
+
end
|
67
|
+
|
62
68
|
def test_should_update_editable_setting
|
63
69
|
editable_setting = Settler.google_analytics_key
|
64
70
|
assert editable_setting.update_attributes(:value => 'UA-xxxxxx-1')
|
@@ -67,7 +73,7 @@ class TestSettler < Test::Unit::TestCase
|
|
67
73
|
|
68
74
|
def test_key_should_be_readonly_attribute
|
69
75
|
setting = Settler.google_analytics_key
|
70
|
-
setting.update_attribute(:key, 'new_key')
|
76
|
+
setting.update_attribute(:key, 'new_key') rescue nil
|
71
77
|
assert_equal 'google_analytics_key', setting.reload.key
|
72
78
|
end
|
73
79
|
|
@@ -125,4 +131,16 @@ class TestSettler < Test::Unit::TestCase
|
|
125
131
|
assert !setting.update_attributes(:value => 'invalid_format')
|
126
132
|
end
|
127
133
|
|
134
|
+
def test_should_reset_setting
|
135
|
+
uneditable_setting = Settler.search_algorithm
|
136
|
+
assert uneditable_setting.update_attribute(:deletable, false)
|
137
|
+
assert uneditable_setting.update_attribute(:value, 'sphinx')
|
138
|
+
assert_equal 'sphinx', Settler[:search_algorithm]
|
139
|
+
assert !uneditable_setting.deletable?
|
140
|
+
|
141
|
+
uneditable_setting.reset!
|
142
|
+
assert_equal 'ferret', Settler[:search_algorithm]
|
143
|
+
assert uneditable_setting.deletable?
|
144
|
+
end
|
145
|
+
|
128
146
|
end
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: settler
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 31
|
4
5
|
prerelease: false
|
5
6
|
segments:
|
6
7
|
- 1
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 1.
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 1.2.0
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Reinier de Lange
|
@@ -14,7 +15,7 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date: 2010-
|
18
|
+
date: 2010-10-27 00:00:00 +02:00
|
18
19
|
default_executable:
|
19
20
|
dependencies: []
|
20
21
|
|
@@ -59,23 +60,27 @@ rdoc_options:
|
|
59
60
|
require_paths:
|
60
61
|
- lib
|
61
62
|
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
62
64
|
requirements:
|
63
65
|
- - ">="
|
64
66
|
- !ruby/object:Gem::Version
|
67
|
+
hash: 3
|
65
68
|
segments:
|
66
69
|
- 0
|
67
70
|
version: "0"
|
68
71
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
69
73
|
requirements:
|
70
74
|
- - ">="
|
71
75
|
- !ruby/object:Gem::Version
|
76
|
+
hash: 3
|
72
77
|
segments:
|
73
78
|
- 0
|
74
79
|
version: "0"
|
75
80
|
requirements: []
|
76
81
|
|
77
82
|
rubyforge_project:
|
78
|
-
rubygems_version: 1.3.
|
83
|
+
rubygems_version: 1.3.7
|
79
84
|
signing_key:
|
80
85
|
specification_version: 3
|
81
86
|
summary: Settler manages global application settings in Rails
|