has_settings 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.4
1
+ 0.0.5
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{has_settings}
8
- s.version = "0.0.4"
8
+ s.version = "0.0.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Morten Primdahl"]
12
- s.date = %q{2010-11-04}
12
+ s.date = %q{2010-11-11}
13
13
  s.description = %q{This gem is an ActiveRecord extension which provides a convenient interface for managing per row settings}
14
14
  s.email = %q{morten@zendesk.com}
15
15
  s.extra_rdoc_files = [
@@ -9,7 +9,7 @@ module HasSettings
9
9
 
10
10
  def build(*setting_names)
11
11
  setting_names.each do |setting_name|
12
- self << proxy_reflection.klass.new(:name => setting_name)
12
+ self << proxy_reflection.klass.new(:name => setting_name.to_s)
13
13
  end
14
14
  end
15
15
 
@@ -2,7 +2,8 @@ module ActionView
2
2
  module Helpers
3
3
  def setting_check_box(model_name, method, options = {}, checked_value = "1", unchecked_value = "0")
4
4
  the_model = @template.instance_variable_get("@#{model_name}")
5
- throw "setting_check_box only work on models with settings" unless the_model.respond_to?(:settings)
5
+ throw "No @#{model_name} in scope" if the_model.nil?
6
+ throw "The setting_check_box only works on models with settings" unless the_model.respond_to?(:settings)
6
7
  options[:checked] = the_model.settings.send("#{method}?")
7
8
  options[:id] ||= "#{model_name}_settings_#{method}"
8
9
  options[:name] = "#{model_name}[settings][#{method}]"
@@ -2,8 +2,9 @@ module HasSettings
2
2
  class Setting < ActiveRecord::Base
3
3
  abstract_class = true
4
4
 
5
- after_create :reset_owner_association
6
- after_destroy :reset_owner_association
5
+ after_create :reset_owner_association
6
+ after_destroy :reset_owner_association
7
+ validate :validate_format_of_name
7
8
 
8
9
  def create
9
10
  if new_record?
@@ -35,6 +36,14 @@ module HasSettings
35
36
 
36
37
  private
37
38
 
39
+ def validate_format_of_name
40
+ if name.blank?
41
+ errors.add(:name, :blank)
42
+ elsif !name.is_a?(String) || name !~ /^([a-z0-9]+_?)+$/
43
+ errors.add(:name, :invalid)
44
+ end
45
+ end
46
+
38
47
  def owner_class_instance
39
48
  send(self.class.owner_class_sym)
40
49
  end
@@ -12,7 +12,7 @@ end
12
12
  class TestHasSettings < ActiveSupport::TestCase
13
13
  context "settings" do
14
14
  fixtures :accounts, :account_settings
15
-
15
+
16
16
  should "be accessable from the owner association" do
17
17
  a = Account.create(:name => 'name')
18
18
  assert a.settings.empty?
@@ -22,7 +22,7 @@ class TestHasSettings < ActiveSupport::TestCase
22
22
  assert a.settings.archive?
23
23
  assert a.settings.archive.id == a.settings.archive.create.id
24
24
  end
25
-
25
+
26
26
  should "be destroyable via the association" do
27
27
  a = accounts(:account1)
28
28
  assert a.settings.archive?
@@ -30,14 +30,14 @@ class TestHasSettings < ActiveSupport::TestCase
30
30
  a.settings.reload
31
31
  assert !a.settings.archive?
32
32
  end
33
-
33
+
34
34
  should "have convenience methods for checking presence" do
35
35
  a = accounts(:account1)
36
36
  assert a.settings.archive?
37
37
  assert a.settings.ssl?
38
38
  assert !a.settings.reports?
39
39
  end
40
-
40
+
41
41
  should "support mass updates" do
42
42
  a = Account.create(:name => 'name')
43
43
  assert a.settings.empty?
@@ -52,7 +52,7 @@ class TestHasSettings < ActiveSupport::TestCase
52
52
  assert !a.settings.archive?
53
53
  assert !a.settings.reports?
54
54
  end
55
-
55
+
56
56
  should "support protecting certain settings from mass updates" do
57
57
  a = Account.create(:name => 'name')
58
58
  assert a.settings.empty?
@@ -69,14 +69,41 @@ class TestHasSettings < ActiveSupport::TestCase
69
69
  assert !a.settings.archive?
70
70
  assert a.settings.ssl?
71
71
  end
72
-
72
+
73
73
  should "be able to build multiple settings in one go" do
74
74
  a = Account.create(:name => 'name')
75
75
  assert a.settings.empty?
76
76
  a.settings.build('archive', 'ssl')
77
77
  assert a.settings.size == 2
78
78
  end
79
-
79
+
80
+ should "convert symbols to strings when building multiple settings" do
81
+ a = Account.create(:name => 'name')
82
+ assert a.settings.empty?
83
+ a.settings.build(:archive, :ssl)
84
+ a.save!
85
+ assert a.settings.size == 2
86
+ assert a.settings.archive?
87
+ assert a.settings.ssl?
88
+ assert a.settings.ssl.name == 'ssl'
89
+ assert a.settings.archive.name == 'archive'
90
+ end
91
+
92
+ should "reject settings with an invalid name" do
93
+ a = Account.create(:name => 'name')
94
+ s = AccountSetting.new(:account => a)
95
+ s.name = 'hello'
96
+ assert s.valid?
97
+ s.name = :hello
98
+ assert !s.valid?
99
+ s.name = "_foo_bar"
100
+ assert !s.valid?
101
+ s.name = "foo_bar"
102
+ assert s.valid?
103
+ s.name = "horse4"
104
+ assert s.valid?
105
+ end
106
+
80
107
  should "validate uniqueness of settings" do
81
108
  a = Account.create(:name => 'name')
82
109
  AccountSetting.create!(:account => a, :name => 'fish')
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_settings
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 4
10
- version: 0.0.4
9
+ - 5
10
+ version: 0.0.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Morten Primdahl
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-04 00:00:00 -07:00
18
+ date: 2010-11-11 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency