property_sets 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,148 @@
1
+ require 'helper'
2
+
3
+ class Account < ActiveRecord::Base
4
+ property_set :settings do
5
+ property :foo
6
+ property :bar
7
+ property :baz
8
+ property :hep, :default => 'skep'
9
+ property :bob, :protected => true
10
+ end
11
+
12
+ property_set :texts do
13
+ property :foo
14
+ property :bar
15
+ end
16
+ end
17
+
18
+ class TestPropertySets < ActiveSupport::TestCase
19
+ context "property sets" do
20
+ fixtures :accounts, :account_settings, :account_texts
21
+
22
+ setup do
23
+ @account = Account.create(:name => "Name")
24
+ end
25
+
26
+ should "construct the container class" do
27
+ assert defined?(AccountSetting)
28
+ assert defined?(AccountText)
29
+ end
30
+
31
+ context "on a new account" do
32
+ should "be empty" do
33
+ assert @account.settings.empty?
34
+ assert @account.texts.empty?
35
+ end
36
+
37
+ should "respond to defaults" do
38
+ assert_equal false, @account.settings.bar?
39
+ assert_equal nil, @account.settings.bar.value
40
+ assert_equal true, @account.settings.hep?
41
+ assert_equal 'skep', @account.settings.hep.value
42
+ end
43
+ end
44
+
45
+ context "validations" do
46
+ should "reject settings with an invalid name" do
47
+ s = AccountSetting.new(:account => @account)
48
+
49
+ [ 'hello', 'hel_lo', 'hell0' ].each do |valid|
50
+ s.name = valid
51
+ assert s.valid?
52
+ end
53
+
54
+ [ '_hello', :hello ].each do |invalid|
55
+ s.name = invalid
56
+ assert !s.valid?
57
+ end
58
+ end
59
+
60
+ should "validate uniqueness of settings" do
61
+ AccountSetting.create!(:account => @account, :name => 'unique')
62
+ assert_raise ActiveRecord::RecordInvalid do
63
+ AccountSetting.create!(:account => @account, :name => 'unique')
64
+ end
65
+ end
66
+ end
67
+
68
+ context "access" do
69
+ should "support creation" do
70
+ assert !@account.settings.foo?
71
+ assert @account.settings.foo = "1"
72
+ assert @account.settings.size == 1
73
+ assert @account.texts.size == 0
74
+ assert @account.settings.foo?
75
+ assert @account.settings.foo = "2"
76
+ assert @account.settings.size == 1
77
+ assert @account.settings.foo?
78
+ end
79
+
80
+ should "support destruction" do
81
+ assert !@account.settings.foo?
82
+ assert @account.settings.foo = "1"
83
+ assert @account.settings.foo?
84
+ assert @account.settings.foo.destroy
85
+ @account.settings.reload
86
+ assert !@account.settings.foo?
87
+ end
88
+
89
+ should "support building multiple properties in one go" do
90
+ @account.settings.build(:foo => "123", :bar => "456")
91
+ @account.save!
92
+ assert_equal @account.reload.settings.size, 2
93
+ assert_equal @account.settings.foo.value, "123"
94
+ assert_equal @account.settings.bar.value, "456"
95
+ end
96
+ end
97
+ end
98
+
99
+ # should "moo" do
100
+ # assert !a.settings.foo?
101
+ # assert !a.settings.reports?
102
+ #
103
+ # a.update_attributes(:settings => { :foo => '1', :reports => '1' })
104
+ # assert a.settings.foo?
105
+ # assert a.settings.reports?
106
+ #
107
+ # a.update_attributes(:settings => { :foo => '0', :reports => '0' })
108
+ # assert !a.settings.foo?
109
+ # assert !a.settings.reports?
110
+ # end
111
+ #
112
+ # should "support protecting certain settings from mass updates" do
113
+ # a = Account.create(:name => 'name')
114
+ # assert a.settings.empty?
115
+ # assert !a.settings.foo?
116
+ # assert !a.settings.ssl?
117
+ #
118
+ # a.update_attributes(:settings => {:foo => '1', :ssl => '1'})
119
+ # assert a.settings.foo?
120
+ # assert !a.settings.ssl?
121
+ #
122
+ # assert a.settings.ssl.create
123
+ # assert a.settings.ssl?
124
+ # a.update_attributes(:settings => {:foo => '0', :ssl => '0'})
125
+ # assert !a.settings.foo?
126
+ # assert a.settings.ssl?
127
+ # end
128
+
129
+ # should "be able to build multiple settings in one go" do
130
+ # a = Account.create(:name => 'name')
131
+ # assert a.settings.empty?
132
+ # a.settings.build('foo', 'ssl')
133
+ # assert a.settings.size == 2
134
+ # end
135
+ #
136
+ # should "convert symbols to strings when building multiple settings" do
137
+ # a = Account.create(:name => 'name')
138
+ # assert a.settings.empty?
139
+ # a.settings.build(:foo, :ssl)
140
+ # a.save!
141
+ # assert a.settings.size == 2
142
+ # assert a.settings.foo?
143
+ # assert a.settings.ssl?
144
+ # assert a.settings.ssl.name == 'ssl'
145
+ # assert a.settings.foo.name == 'foo'
146
+ # end
147
+
148
+ end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: property_sets
3
+ version: !ruby/object:Gem::Version
4
+ hash: 19
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 6
10
+ version: 0.0.6
11
+ platform: ruby
12
+ authors:
13
+ - Morten Primdahl
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-10 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ prerelease: false
23
+ type: :development
24
+ name: shoulda
25
+ version_requirements: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ hash: 3
31
+ segments:
32
+ - 0
33
+ version: "0"
34
+ requirement: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ prerelease: false
37
+ type: :development
38
+ name: bundler
39
+ version_requirements: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ hash: 23
45
+ segments:
46
+ - 1
47
+ - 0
48
+ - 0
49
+ version: 1.0.0
50
+ requirement: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ prerelease: false
53
+ type: :development
54
+ name: jeweler
55
+ version_requirements: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ hash: 1
61
+ segments:
62
+ - 1
63
+ - 5
64
+ - 1
65
+ version: 1.5.1
66
+ requirement: *id003
67
+ - !ruby/object:Gem::Dependency
68
+ prerelease: false
69
+ type: :development
70
+ name: rcov
71
+ version_requirements: &id004 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ hash: 3
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ requirement: *id004
81
+ - !ruby/object:Gem::Dependency
82
+ prerelease: false
83
+ type: :development
84
+ name: thoughtbot-shoulda
85
+ version_requirements: &id005 !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ hash: 3
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ requirement: *id005
95
+ description: This gem is an ActiveRecord extension which provides a convenient interface for managing per row properties
96
+ email: morten@zendesk.com
97
+ executables: []
98
+
99
+ extensions: []
100
+
101
+ extra_rdoc_files:
102
+ - LICENSE
103
+ - LICENSE.txt
104
+ - README.rdoc
105
+ files:
106
+ - .document
107
+ - Gemfile
108
+ - LICENSE
109
+ - LICENSE.txt
110
+ - README.rdoc
111
+ - Rakefile
112
+ - VERSION
113
+ - lib/property_sets.rb
114
+ - lib/property_sets/active_record_extension.rb
115
+ - lib/property_sets/property_set_helper.rb
116
+ - lib/property_sets/property_set_model.rb
117
+ - test/database.yml
118
+ - test/fixtures/account_settings.yml
119
+ - test/fixtures/account_texts.yml
120
+ - test/fixtures/accounts.yml
121
+ - test/helper.rb
122
+ - test/schema.rb
123
+ - test/test.log
124
+ - test/test_property_sets.rb
125
+ has_rdoc: true
126
+ homepage: http://github.com/morten/property_sets
127
+ licenses: []
128
+
129
+ post_install_message:
130
+ rdoc_options: []
131
+
132
+ require_paths:
133
+ - lib
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ none: false
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ hash: 3
140
+ segments:
141
+ - 0
142
+ version: "0"
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ none: false
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ hash: 3
149
+ segments:
150
+ - 0
151
+ version: "0"
152
+ requirements: []
153
+
154
+ rubyforge_project:
155
+ rubygems_version: 1.3.7
156
+ signing_key:
157
+ specification_version: 3
158
+ summary: Property sets for ActiveRecord
159
+ test_files:
160
+ - test/helper.rb
161
+ - test/schema.rb
162
+ - test/test_property_sets.rb