ledermann-rails-settings 2.0.0 → 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Changelog.md +5 -0
- data/README.md +1 -1
- data/lib/rails-settings.rb +2 -2
- data/lib/rails-settings/base.rb +1 -1
- data/lib/rails-settings/scopes.rb +20 -24
- data/lib/rails-settings/setting_object.rb +10 -0
- data/lib/rails-settings/version.rb +1 -1
- data/spec/setting_object_spec.rb +9 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: acfe3a67b8daf8e9108c0ddf270e638c4310d2b1
|
4
|
+
data.tar.gz: ef9c5a3a455365f5817f56f5476b12d9283d2440
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5fd20605f8b1dc5474b94402140baba09d08a5d59429269ebb37bc4bd5b63742088a24ed0c2ccbf6b7cb127c631fb5522b85636735a1a6b28f94853ed97b90e5
|
7
|
+
data.tar.gz: 3c4f8c5f8f5732e727cf116a341d04f09e519e8e1aa13df8b94e72f3dfd532a6155a1cfa7c4af0de44ceebce271ca4e28a1e5b129008994a992362fb14de44ea
|
data/Changelog.md
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Settings for Rails 3
|
2
2
|
|
3
|
-
[![Build Status](https://
|
3
|
+
[![Build Status](https://travis-ci.org/ledermann/rails-settings.png?branch=master)](https://travis-ci.org/ledermann/rails-settings)
|
4
4
|
[![Code Climate](https://codeclimate.com/github/ledermann/rails-settings.png)](https://codeclimate.com/github/ledermann/rails-settings)
|
5
5
|
|
6
6
|
Ruby gem to handle settings for ActiveRecord objects by storing them as serialized Hash in a separate database table. Optional: Defaults and Namespaces.
|
data/lib/rails-settings.rb
CHANGED
@@ -7,8 +7,8 @@ ActiveRecord::Base.class_eval do
|
|
7
7
|
def self.has_settings(*args, &block)
|
8
8
|
RailsSettings::Configuration.new(*args.unshift(self), &block)
|
9
9
|
|
10
|
-
include RailsSettings::Base
|
11
|
-
|
10
|
+
include RailsSettings::Base
|
11
|
+
extend RailsSettings::Scopes
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
data/lib/rails-settings/base.rb
CHANGED
@@ -12,7 +12,7 @@ module RailsSettings
|
|
12
12
|
raise ArgumentError unless var.is_a?(Symbol)
|
13
13
|
raise ArgumentError.new("Unknown key: #{var}") unless self.class.default_settings[var]
|
14
14
|
|
15
|
-
setting_objects.detect { |s| s.var == var.to_s } || setting_objects.build(:var => var.to_s)
|
15
|
+
setting_objects.detect { |s| s.var == var.to_s } || setting_objects.build({ :var => var.to_s }, :without_protection => true)
|
16
16
|
end
|
17
17
|
|
18
18
|
def settings=(value)
|
@@ -1,33 +1,29 @@
|
|
1
1
|
module RailsSettings
|
2
2
|
module Scopes
|
3
|
-
def
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
uniq
|
8
|
-
}
|
3
|
+
def with_settings
|
4
|
+
joins("INNER JOIN settings ON #{settings_join_condition}").
|
5
|
+
uniq
|
6
|
+
end
|
9
7
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
8
|
+
def with_settings_for(var)
|
9
|
+
raise ArgumentError.new('Symbol expected!') unless var.is_a?(Symbol)
|
10
|
+
joins("INNER JOIN settings ON #{settings_join_condition} AND settings.var = '#{var}'")
|
11
|
+
end
|
14
12
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
13
|
+
def without_settings
|
14
|
+
joins("LEFT JOIN settings ON #{settings_join_condition}").
|
15
|
+
where('settings.id IS NULL')
|
16
|
+
end
|
19
17
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
18
|
+
def without_settings_for(var)
|
19
|
+
raise ArgumentError.new('Symbol expected!') unless var.is_a?(Symbol)
|
20
|
+
joins("LEFT JOIN settings ON #{settings_join_condition} AND settings.var = '#{var}'").
|
21
|
+
where('settings.id IS NULL')
|
22
|
+
end
|
25
23
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
end
|
30
|
-
end
|
24
|
+
def settings_join_condition
|
25
|
+
"settings.target_id = #{table_name}.#{primary_key} AND
|
26
|
+
settings.target_type = '#{base_class.name}'"
|
31
27
|
end
|
32
28
|
end
|
33
29
|
end
|
@@ -13,6 +13,10 @@ module RailsSettings
|
|
13
13
|
|
14
14
|
serialize :value, Hash
|
15
15
|
|
16
|
+
# attr_protected can not be here used because it touches the database which is not connected yet.
|
17
|
+
# So allow no attributes and override <tt>#sanitize_for_mass_assignment</tt>
|
18
|
+
attr_accessible
|
19
|
+
|
16
20
|
REGEX_SETTER = /\A([a-z]\w+)=\Z/i
|
17
21
|
REGEX_GETTER = /\A([a-z]\w+)\Z/i
|
18
22
|
|
@@ -34,6 +38,12 @@ module RailsSettings
|
|
34
38
|
end
|
35
39
|
end
|
36
40
|
|
41
|
+
protected
|
42
|
+
# Simulate attr_protected by removing all regular attributes
|
43
|
+
def sanitize_for_mass_assignment(attributes, role = nil)
|
44
|
+
attributes.except('id', 'var', 'value', 'target_id', 'target_type', 'created_at', 'updated_at')
|
45
|
+
end
|
46
|
+
|
37
47
|
private
|
38
48
|
def _get_value(name)
|
39
49
|
value[name] || _target_class.default_settings[var.to_sym][name]
|
data/spec/setting_object_spec.rb
CHANGED
@@ -2,8 +2,8 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe RailsSettings::SettingObject do
|
4
4
|
let(:user) { User.create! :name => 'Mr. Pink' }
|
5
|
-
let(:new_setting_object) { user.setting_objects.build :var => 'dashboard' }
|
6
|
-
let(:saved_setting_object) { user.setting_objects.create! :var => 'dashboard', :value => { 'theme' => 'pink', 'filter' => true
|
5
|
+
let(:new_setting_object) { user.setting_objects.build({ :var => 'dashboard'}, :without_protection => true) }
|
6
|
+
let(:saved_setting_object) { user.setting_objects.create!({ :var => 'dashboard', :value => { 'theme' => 'pink', 'filter' => true}}, :without_protection => true) }
|
7
7
|
|
8
8
|
describe "Getter and Setter" do
|
9
9
|
context "on unsaved settings" do
|
@@ -92,6 +92,13 @@ describe RailsSettings::SettingObject do
|
|
92
92
|
it 'should not save blank hash' do
|
93
93
|
new_setting_object.update_attributes({}).should be_false
|
94
94
|
end
|
95
|
+
|
96
|
+
it 'should not allow changing protected attributes' do
|
97
|
+
new_setting_object.update_attributes!(:var => 'calendar', :foo => 42)
|
98
|
+
|
99
|
+
new_setting_object.var.should eq('dashboard')
|
100
|
+
new_setting_object.foo.should eq(42)
|
101
|
+
end
|
95
102
|
end
|
96
103
|
|
97
104
|
describe "save" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ledermann-rails-settings
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Georg Ledermann
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-03-
|
11
|
+
date: 2013-03-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|