preferences 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -1,5 +1,10 @@
1
1
  == master
2
2
 
3
+ == 0.1.5 / 2008-11-16
4
+
5
+ * Add all prefers/preferred accessors for preferences to be analogous to ActiveRecord column accessors
6
+ * Fix preferences defined in STI subclasses not working [Quinn Shanahan]
7
+
3
8
  == 0.1.4 / 2008-10-26
4
9
 
5
10
  * Change how the base module is included to prevent namespacing conflicts
data/README.rdoc CHANGED
@@ -31,12 +31,12 @@ Generally, basic preferences can be accomplished through simple designs, such as
31
31
  additional columns or a bit vector described and implemented by preference_fu[http://agilewebdevelopment.com/plugins/preferencefu].
32
32
  However, as you find the need for non-binary preferences and the number of
33
33
  preferences becomes unmanageable as individual columns in the database, the next
34
- step is often to create a separate "preferences" table. This is where the +preferences+
35
- plugin comes in.
34
+ step is often to create a separate "preferences" table. This is where the
35
+ +preferences+ plugin comes in.
36
36
 
37
- +preferences+ encapsulates this design by hiding the fact that preferences are
38
- stored in a separate table and making it dead-simple to define and manage
39
- preferences.
37
+ +preferences+ encapsulates this design by exposing preferences using simple
38
+ attribute accessors on the model, hiding the fact that preferences are stored in
39
+ a separate table and making it dead-simple to define and manage preferences.
40
40
 
41
41
  == Usage
42
42
 
@@ -60,26 +60,28 @@ In the above model, 5 preferences have been defined:
60
60
  * language
61
61
 
62
62
  For each preference, a data type and default value can be specified. If no
63
- data type is given, it's considered a boolean value. If no default value is
63
+ data type is given, it's assumed to be a boolean value. If no default value is
64
64
  given, the default is assumed to be nil.
65
65
 
66
66
  === Accessing preferences
67
67
 
68
- Once preferences have been defined for a model, they can be accessed either using
69
- the shortcut methods that are generated for each preference or the generic methods
70
- that are not specific to a particular preference.
68
+ Once preferences have been defined for a model, they can be accessed either
69
+ using the accessor methods that are generated for each preference or the generic
70
+ methods that are not specific to a particular preference.
71
71
 
72
- ==== Shortcut methods
72
+ ==== Accessors
73
73
 
74
- There are several shortcut methods that are generated. They are shown below.
74
+ There are several shortcut methods that are generated for each preference
75
+ defined on a model. These reflect the same set of methods (attribute accessors)
76
+ that are generated for a model's columns. Examples of these are shown below:
75
77
 
76
78
  Query methods:
77
79
  user.prefers_hot_salsa? # => false
78
- user.prefers_dark_chocolate? # => false
80
+ user.preferred_language? # => true
79
81
 
80
82
  Reader methods:
81
- user.preferred_color # => nil
82
- user.preferred_language # => "English"
83
+ user.prefers_hot_salsa # => false
84
+ user.preferred_language # => "English"
83
85
 
84
86
  Writer methods:
85
87
  user.prefers_hot_salsa = false # => false
@@ -87,16 +89,16 @@ Writer methods:
87
89
 
88
90
  ==== Generic methods
89
91
 
90
- Each shortcut method is essentially a wrapper for the various generic methods
92
+ Each preference accessor is essentially a wrapper for the various generic methods
91
93
  shown below:
92
94
 
93
95
  Query method:
94
- user.prefers?(:hot_salsa) # => false
95
- user.prefers?(:dark_chocolate) # => false
96
+ user.prefers?(:hot_salsa) # => false
97
+ user.preferred?(:language) # => true
96
98
 
97
99
  Reader method:
98
- user.preferred(:color) # => nil
99
- user.preferred(:language) # => "English"
100
+ user.prefers(:hot_salsa) # => false
101
+ user.preferred(:language) # => "English"
100
102
 
101
103
  Write method:
102
104
  user.set_preference(:hot_salsa, false) # => false
@@ -135,7 +137,7 @@ through an example:
135
137
  user.preferred_color = 'red', car
136
138
  # user.set_preference(:color, 'red', car) # The generic way
137
139
 
138
- This will create a preference for the color "red" for the given car. In this way,
140
+ This will create a color preference of "red" for the given car. In this way,
139
141
  you can have "color" preferences for different records.
140
142
 
141
143
  To access the preference for a particular record, you can use the same accessor
@@ -160,11 +162,11 @@ preferences by name. For example,
160
162
 
161
163
  === Saving preferences
162
164
 
163
- Note that preferences are not saved until the owning record is saved. Preferences
164
- are treated in a similar fashion to attributes. For example,
165
+ Note that preferences are not saved until the owning record is saved.
166
+ Preferences are treated in a similar fashion to attributes. For example,
165
167
 
166
168
  user = user.find(:first)
167
- user.attributes = {:preferred_color => 'red'}
169
+ user.attributes = {:prefers_hot_salsa => false, :preferred_color => 'red'}
168
170
  user.save!
169
171
 
170
172
  Preferences are stored in a separate table called "preferences".
data/Rakefile CHANGED
@@ -5,7 +5,7 @@ require 'rake/contrib/sshpublisher'
5
5
 
6
6
  spec = Gem::Specification.new do |s|
7
7
  s.name = 'preferences'
8
- s.version = '0.1.4'
8
+ s.version = '0.1.5'
9
9
  s.platform = Gem::Platform::RUBY
10
10
  s.summary = 'Adds support for easily creating custom preferences for models'
11
11
 
@@ -43,7 +43,10 @@ class Preference < ActiveRecord::Base
43
43
 
44
44
  # The definition for the attribute
45
45
  def definition
46
- owner_type.constantize.preference_definitions[attribute] if owner_type
46
+ # Optimize number of queries to the database by only looking up the actual
47
+ # owner record for STI cases when the definition can't be found in the
48
+ # stored owner type class
49
+ owner_type && (find_definition(owner_type.constantize) || find_definition(owner.class))
47
50
  end
48
51
 
49
52
  # Typecasts the value depending on the preference definition's declared type
@@ -58,4 +61,11 @@ class Preference < ActiveRecord::Base
58
61
  group_id ? group_without_optional_lookup : group_type
59
62
  end
60
63
  alias_method_chain :group, :optional_lookup
64
+
65
+ private
66
+ # Finds the definition for this preference's attribute in the given owner
67
+ # class.
68
+ def find_definition(owner_class)
69
+ owner_class.respond_to?(:preference_definitions) && owner_class.preference_definitions[attribute]
70
+ end
61
71
  end
data/lib/preferences.rb CHANGED
@@ -20,11 +20,35 @@ module PluginAWeek #:nodoc:
20
20
  # u = User.find_by_login('admin')
21
21
  # u.attributes = {:prefers_notifications => true}
22
22
  # u.save!
23
+ #
24
+ # == Validations
25
+ #
26
+ # Since the generated accessors for a preference allow the preference to be
27
+ # treated just like regular ActiveRecord column attributes, they can also be
28
+ # validated against in the same way. For example,
29
+ #
30
+ # class User < ActiveRecord::Base
31
+ # preference :color, :string
32
+ #
33
+ # validates_presence_of :preferred_color
34
+ # validates_inclusion_of :preferred_color, :in => %w(red green blue)
35
+ # end
36
+ #
37
+ # u = User.new
38
+ # u.valid? # => false
39
+ # u.errors.on(:preferred_color) # => "can't be blank"
40
+ #
41
+ # u.preferred_color = 'white'
42
+ # u.valid? # => false
43
+ # u.errors.on(:preferred_color) # => "is not included in the list"
44
+ #
45
+ # u.preferred_color = 'red'
46
+ # u.valid? # => true
23
47
  module Preferences
24
48
  module MacroMethods
25
- # Defines a new preference for all records in the model. By default, preferences
26
- # are assumed to have a boolean data type, so all values will be typecasted
27
- # to true/false based on ActiveRecord rules.
49
+ # Defines a new preference for all records in the model. By default,
50
+ # preferences are assumed to have a boolean data type, so all values will
51
+ # be typecasted to true/false based on ActiveRecord rules.
28
52
  #
29
53
  # Configuration options:
30
54
  # * +default+ - The default value for the preference. Default is nil.
@@ -47,34 +71,37 @@ module PluginAWeek #:nodoc:
47
71
  #
48
72
  # After the first preference is defined, the following associations are
49
73
  # created for the model:
50
- # * +stored_preferences+ - A collection of all the custom preferences specified for a record
74
+ # * +stored_preferences+ - A collection of all the custom preferences specified for a record. This will not include default preferences unless they have been explicitly set.
51
75
  #
52
- # == Generated shortcut methods
76
+ # == Generated accessors
53
77
  #
54
78
  # In addition to calling <tt>prefers?</tt> and +preferred+ on a record, you
55
- # can also use the shortcut methods that are generated when a preference is
56
- # defined. For example,
79
+ # can also use the shortcut accessor methods that are generated when a
80
+ # preference is defined. For example,
57
81
  #
58
82
  # class User < ActiveRecord::Base
59
83
  # preference :notifications
60
84
  # end
61
85
  #
62
86
  # ...generates the following methods:
63
- # * <tt>prefers_notifications?</tt> - The same as calling <tt>record.prefers?(:notifications)</tt>
64
- # * <tt>prefers_notifications=(value)</tt> - The same as calling <tt>record.set_preference(:notifications, value)</tt>
65
- # * <tt>preferred_notifications</tt> - The same as called <tt>record.preferred(:notifications)</tt>
66
- # * <tt>preferred_notifications=(value)</tt> - The same as calling <tt>record.set_preference(:notifications, value)</tt>
87
+ # * <tt>prefers_notifications?</tt> - Whether a value has been specified, i.e. <tt>record.prefers?(:notifications)</tt>
88
+ # * <tt>prefers_notifications</tt> - The actual value stored, i.e. <tt>record.prefers(:notifications)</tt>
89
+ # * <tt>prefers_notifications=(value)</tt> - Sets a new value, i.e. <tt>record.set_preference(:notifications, value)</tt>
90
+ # * <tt>preferred_notifications?</tt> - Whether a value has been specified, i.e. <tt>record.preferred?(:notifications)</tt>
91
+ # * <tt>preferred_notifications</tt> - The actual value stored, i.e. <tt>record.preferred(:notifications)</tt>
92
+ # * <tt>preferred_notifications=(value)</tt> - Sets a new value, i.e. <tt>record.set_preference(:notifications, value)</tt>
67
93
  #
68
94
  # Notice that there are two tenses used depending on the context of the
69
95
  # preference. Conventionally, <tt>prefers_notifications?</tt> is better
70
- # for boolean preferences, while +preferred_color+ is better for non-boolean
71
- # preferences.
96
+ # for accessing boolean preferences, while +preferred_color+ is better for
97
+ # accessing non-boolean preferences.
72
98
  #
73
99
  # Example:
74
100
  #
75
101
  # user = User.find(:first)
76
102
  # user.prefers_notifications? # => false
77
- # user.prefers_color? # => true
103
+ # user.prefers_notifications # => false
104
+ # user.preferred_color? # => true
78
105
  # user.preferred_color # => 'red'
79
106
  # user.preferred_color = 'blue' # => 'blue'
80
107
  #
@@ -83,7 +110,7 @@ module PluginAWeek #:nodoc:
83
110
  # car = Car.find(:first)
84
111
  # user.preferred_color = 'red', car # => 'red'
85
112
  # user.preferred_color(car) # => 'red'
86
- # user.prefers_color?(car) # => true
113
+ # user.preferred_color?(car) # => true
87
114
  #
88
115
  # user.save! # => true
89
116
  def preference(attribute, *args)
@@ -109,25 +136,27 @@ module PluginAWeek #:nodoc:
109
136
  self.preference_definitions[attribute] = definition
110
137
  self.default_preferences[attribute] = definition.default_value
111
138
 
112
- # Create short-hand helper methods, making sure that the attribute
139
+ # Create short-hand accessor methods, making sure that the attribute
113
140
  # is method-safe in terms of what characters are allowed
114
141
  attribute = attribute.gsub(/[^A-Za-z0-9_-]/, '').underscore
115
142
 
116
143
  # Query lookup
117
- define_method("prefers_#{attribute}?") do |*group|
118
- prefers?(attribute, group.first)
144
+ define_method("preferred_#{attribute}?") do |*group|
145
+ preferred?(attribute, group.first)
119
146
  end
120
-
121
- # Writer
122
- define_method("prefers_#{attribute}=") do |*args|
123
- set_preference(*([attribute] + [args].flatten))
124
- end
125
- alias_method "preferred_#{attribute}=", "prefers_#{attribute}="
147
+ alias_method "prefers_#{attribute}?", "preferred_#{attribute}?"
126
148
 
127
149
  # Reader
128
150
  define_method("preferred_#{attribute}") do |*group|
129
151
  preferred(attribute, group.first)
130
152
  end
153
+ alias_method "prefers_#{attribute}", "preferred_#{attribute}"
154
+
155
+ # Writer
156
+ define_method("preferred_#{attribute}=") do |*args|
157
+ set_preference(*([attribute] + [args].flatten))
158
+ end
159
+ alias_method "prefers_#{attribute}=", "preferred_#{attribute}="
131
160
 
132
161
  definition
133
162
  end
@@ -156,7 +185,7 @@ module PluginAWeek #:nodoc:
156
185
  # user.preferences
157
186
  # => {"language"=>"English", "color"=>nil, "cars"=>{"language=>"English", "color"=>"red"}}
158
187
  #
159
- # Getting preference values for the owning record:
188
+ # Getting preference values *just* for the owning record (i.e. excluding groups):
160
189
  # user.preferences(nil)
161
190
  # => {"language"=>"English", "color"=>nil}
162
191
  #
@@ -169,6 +198,9 @@ module PluginAWeek #:nodoc:
169
198
  conditions = {}
170
199
  else
171
200
  group = args.first
201
+
202
+ # Split the actual group into its different parts (id/type) in case
203
+ # a record is passed in
172
204
  group_id, group_type = Preference.split_group(group)
173
205
  conditions = {:group_id => group_id, :group_type => group_type}
174
206
  end
@@ -189,61 +221,81 @@ module PluginAWeek #:nodoc:
189
221
  end
190
222
  end
191
223
 
192
- # Queries whether or not a value has been specified for the given attribute.
193
- # This is dependent on how the value is type-casted.
224
+ # Queries whether or not a value is present for the given attribute. This
225
+ # is dependent on how the value is type-casted.
194
226
  #
195
227
  # == Examples
196
228
  #
197
- # user = User.find(:first)
198
- # user.prefers?(:notifications) # => true
229
+ # class User < ActiveRecord::Base
230
+ # preference :color, :string, :default => 'red'
231
+ # end
199
232
  #
200
- # user.prefers(:notifications, 'error') # => true
233
+ # user = User.create
234
+ # user.preferred(:color) # => "red"
235
+ # user.preferred?(:color) # => true
236
+ # user.preferred?(:color, 'cars') # => true
237
+ # user.preferred?(:color, Car.first) # => true
201
238
  #
202
- # newsgroup = Newsgroup.find(:first)
203
- # user.prefers?(:notifications, newsgroup) # => false
204
- def prefers?(attribute, group = nil)
239
+ # user.set_preference(:color, nil)
240
+ # user.preferred(:color) # => nil
241
+ # user.preferred?(:color) # => false
242
+ def preferred?(attribute, group = nil)
205
243
  attribute = attribute.to_s
206
244
 
207
245
  value = preferred(attribute, group)
208
246
  preference_definitions[attribute].query(value)
209
247
  end
248
+ alias_method :prefers?, :preferred?
210
249
 
211
- # Gets the preferred value for the given attribute.
250
+ # Gets the actual value stored for the given attribute, or the default
251
+ # value if nothing is present.
212
252
  #
213
253
  # == Examples
214
254
  #
215
- # user = User.find(:first)
216
- # user.preferred(:color) # => 'red'
255
+ # class User < ActiveRecord::Base
256
+ # preference :color, :string, :default => 'red'
257
+ # end
217
258
  #
218
- # user.preferred(:color, 'cars') # => 'blue'
259
+ # user = User.create
260
+ # user.preferred(:color) # => "red"
261
+ # user.preferred(:color, 'cars') # => "red"
262
+ # user.preferred(:color, Car.first) # => "red"
219
263
  #
220
- # car = Car.find(:first)
221
- # user.preferred(:color, car) # => 'black'
264
+ # user.set_preference(:color, 'blue')
265
+ # user.preferred(:color) # => "blue"
222
266
  def preferred(attribute, group = nil)
223
267
  attribute = attribute.to_s
224
268
 
225
269
  if @preference_values && @preference_values[attribute] && @preference_values[attribute].include?(group)
270
+ # Value for this attribute/group has been written, but not saved yet:
271
+ # grab from the pending values
226
272
  value = @preference_values[attribute][group]
227
273
  else
274
+ # Split the group being filtered
228
275
  group_id, group_type = Preference.split_group(group)
276
+
277
+ # Grab the first preference; if it doesn't exist, use the default value
229
278
  preference = stored_preferences.find(:first, :conditions => {:attribute => attribute, :group_id => group_id, :group_type => group_type})
230
279
  value = preference ? preference.value : preference_definitions[attribute].default_value
231
280
  end
232
281
 
233
282
  value
234
283
  end
284
+ alias_method :prefers, :preferred
235
285
 
236
286
  # Sets a new value for the given attribute. The actual Preference record
237
- # is *not* created until the actual record is saved.
287
+ # is *not* created until this record is saved. In this way, preferences
288
+ # act *exactly* the same as attributes. They can be written to and
289
+ # validated against, but won't actually be written to the database until
290
+ # the record is saved.
238
291
  #
239
292
  # == Examples
240
293
  #
241
294
  # user = User.find(:first)
242
- # user.set_preference(:notifications, false) # => false
295
+ # user.set_preference(:color, 'red') # => "red"
243
296
  # user.save!
244
297
  #
245
- # newsgroup = Newsgroup.find(:first)
246
- # user.set_preference(:notifications, true, newsgroup) # => true
298
+ # user.set_preference(:color, 'blue', Car.first) # => "blue"
247
299
  # user.save!
248
300
  def set_preference(attribute, value, group = nil)
249
301
  attribute = attribute.to_s
@@ -24,25 +24,20 @@ module PluginAWeek #:nodoc:
24
24
  end
25
25
 
26
26
  # Typecasts the value based on the type of preference that was defined.
27
- # This uses ActiveRecord's typecast functionality.
27
+ # This uses ActiveRecord's typecast functionality so the same rules for
28
+ # typecasting a model's columns apply here.
28
29
  def type_cast(value)
29
- if @type == 'any'
30
- value
31
- else
32
- @column.type_cast(value)
33
- end
30
+ @type == 'any' ? value : @column.type_cast(value)
34
31
  end
35
32
 
36
33
  # Typecasts the value to true/false depending on the type of preference
37
34
  def query(value)
38
- unless value = type_cast(value)
35
+ if !(value = type_cast(value))
39
36
  false
37
+ elsif @column.number?
38
+ !value.zero?
40
39
  else
41
- if @column.number?
42
- !value.zero?
43
- else
44
- !value.blank?
45
- end
40
+ !value.blank?
46
41
  end
47
42
  end
48
43
  end
@@ -0,0 +1,2 @@
1
+ class Employee < ActiveRecord::Base
2
+ end
@@ -0,0 +1,3 @@
1
+ class Manager < Employee
2
+ preference :health_insurance
3
+ end
@@ -0,0 +1,12 @@
1
+ class CreateEmployees < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :employees do |t|
4
+ t.string :name, :null => false
5
+ t.string :type
6
+ end
7
+ end
8
+
9
+ def self.down
10
+ drop_table :employees
11
+ end
12
+ end
data/test/factory.rb CHANGED
@@ -39,6 +39,16 @@ module Factory
39
39
  )
40
40
  end
41
41
 
42
+ build Employee do |attributes|
43
+ attributes.reverse_merge!(
44
+ :name => 'John Smith'
45
+ )
46
+ end
47
+
48
+ build Manager do |attributes|
49
+ valid_employee_attributes(attributes)
50
+ end
51
+
42
52
  build Preference do |attributes|
43
53
  attributes[:owner] = create_user unless attributes.include?(:owner)
44
54
  attributes.reverse_merge!(
@@ -12,22 +12,30 @@ class PreferencesTest < Test::Unit::TestCase
12
12
  assert_raise(ArgumentError) {User.preference :notifications, :boolean, :invalid => true}
13
13
  end
14
14
 
15
- def test_should_create_prefers_query_method
16
- assert @user.respond_to?(:prefers_notifications?)
15
+ def test_should_create_preferred_query_method
16
+ assert @user.respond_to?(:preferred_notifications?)
17
17
  end
18
18
 
19
- def test_should_create_prefers_writer
20
- assert @user.respond_to?(:prefers_notifications=)
19
+ def test_should_create_prefers_query_method
20
+ assert @user.respond_to?(:prefers_notifications?)
21
21
  end
22
22
 
23
23
  def test_should_create_preferred_reader
24
24
  assert @user.respond_to?(:preferred_notifications)
25
25
  end
26
26
 
27
+ def test_should_create_prefers_reader
28
+ assert @user.respond_to?(:prefers_notifications)
29
+ end
30
+
27
31
  def test_should_create_preferred_writer
28
32
  assert @user.respond_to?(:preferred_notifications=)
29
33
  end
30
34
 
35
+ def test_should_create_prefers_writer
36
+ assert @user.respond_to?(:prefers_notifications=)
37
+ end
38
+
31
39
  def test_should_create_preference_definitions
32
40
  assert User.respond_to?(:preference_definitions)
33
41
  end
@@ -53,10 +61,12 @@ class UserByDefaultTest < Test::Unit::TestCase
53
61
 
54
62
  def test_should_not_prefer_hot_salsa
55
63
  assert_nil @user.preferred_hot_salsa
64
+ assert_nil @user.prefers_hot_salsa
56
65
  end
57
66
 
58
67
  def test_should_prefer_dark_chocolate
59
68
  assert_equal true, @user.preferred_dark_chocolate
69
+ assert_equal true, @user.prefers_dark_chocolate
60
70
  end
61
71
 
62
72
  def test_should_not_have_a_preferred_color
@@ -120,6 +130,11 @@ class UserTest < Test::Unit::TestCase
120
130
  assert_equal 'Latin', @user.preferred_language
121
131
  end
122
132
 
133
+ def test_should_be_able_to_use_generic_preferred_query_method
134
+ @user.prefers_hot_salsa = true
135
+ assert @user.preferred?(:hot_salsa)
136
+ end
137
+
123
138
  def test_should_be_able_to_use_generic_prefers_query_method
124
139
  @user.prefers_hot_salsa = true
125
140
  assert @user.prefers?(:hot_salsa)
@@ -130,6 +145,11 @@ class UserTest < Test::Unit::TestCase
130
145
  assert_equal 'blue', @user.preferred(:color)
131
146
  end
132
147
 
148
+ def test_should_be_able_to_use_generic_prefers_method
149
+ @user.preferred_color = 'blue'
150
+ assert_equal 'blue', @user.prefers(:color)
151
+ end
152
+
133
153
  def test_should_be_able_to_use_generic_set_preference_method
134
154
  @user.set_preference(:color, 'blue')
135
155
  assert_equal 'blue', @user.preferred(:color)
@@ -95,8 +95,8 @@ class PreferenceDefinitionWithBooleanTypeTest < Test::Unit::TestCase
95
95
  assert_equal true, @definition.type_cast('true')
96
96
  end
97
97
 
98
- def test_should_type_cast_to_false_if_value_is_not_true_string
99
- assert_equal false, @definition.type_cast('')
98
+ def test_should_type_cast_to_nil_if_value_is_not_true_string
99
+ assert_nil @definition.type_cast('')
100
100
  end
101
101
 
102
102
  def test_should_query_false_if_value_is_nil
@@ -43,19 +43,19 @@ class PreferenceTest < Test::Unit::TestCase
43
43
  def test_should_require_an_attribute
44
44
  preference = new_preference(:attribute => nil)
45
45
  assert !preference.valid?
46
- assert_equal 1, Array(preference.errors.on(:attribute)).size
46
+ assert preference.errors.invalid?(:attribute)
47
47
  end
48
48
 
49
49
  def test_should_require_an_owner_id
50
50
  preference = new_preference(:owner => nil)
51
51
  assert !preference.valid?
52
- assert_equal 1, Array(preference.errors.on(:owner_id)).size
52
+ assert preference.errors.invalid?(:owner_id)
53
53
  end
54
54
 
55
55
  def test_should_require_an_owner_type
56
56
  preference = new_preference(:owner => nil)
57
57
  assert !preference.valid?
58
- assert_equal 1, Array(preference.errors.on(:owner_type)).size
58
+ assert preference.errors.invalid?(:owner_type)
59
59
  end
60
60
 
61
61
  def test_should_not_require_a_group_id
@@ -78,7 +78,7 @@ class PreferenceTest < Test::Unit::TestCase
78
78
  preference = new_preference(:group => nil)
79
79
  preference.group_id = 1
80
80
  assert !preference.valid?
81
- assert_equal 1, Array(preference.errors.on(:group_type)).size
81
+ assert preference.errors.invalid?(:group_type)
82
82
  end
83
83
 
84
84
  def test_should_protect_attributes_from_mass_assignment
@@ -209,3 +209,26 @@ class PreferenceWithBooleanAttributeTest < Test::Unit::TestCase
209
209
  User.default_preferences.delete('notifications')
210
210
  end
211
211
  end
212
+
213
+ class PreferenceWithSTIOwnerTest < Test::Unit::TestCase
214
+ def setup
215
+ @manager = create_manager
216
+ @preference = create_preference(:owner => @manager, :attribute => 'health_insurance', :value => true)
217
+ end
218
+
219
+ def test_should_have_an_owner
220
+ assert_equal @manager, @preference.owner
221
+ end
222
+
223
+ def test_should_have_an_owner_type
224
+ assert_equal 'Employee', @preference.owner_type
225
+ end
226
+
227
+ def test_should_have_a_definition
228
+ assert_not_nil @preference.definition
229
+ end
230
+
231
+ def test_should_have_a_value
232
+ assert_equal true, @preference.value
233
+ end
234
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: preferences
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Pfeifer
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-10-26 00:00:00 -04:00
12
+ date: 2008-12-04 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -27,25 +27,28 @@ files:
27
27
  - lib/preferences
28
28
  - lib/preferences/preference_definition.rb
29
29
  - lib/preferences.rb
30
- - test/app_root
31
- - test/app_root/app
32
- - test/app_root/app/models
33
- - test/app_root/app/models/car.rb
34
- - test/app_root/app/models/user.rb
35
- - test/app_root/config
36
- - test/app_root/config/environment.rb
37
- - test/app_root/db
38
- - test/app_root/db/migrate
39
- - test/app_root/db/migrate/001_create_users.rb
40
- - test/app_root/db/migrate/003_migrate_preferences_to_version_1.rb
41
- - test/app_root/db/migrate/002_create_cars.rb
30
+ - test/factory.rb
31
+ - test/test_helper.rb
42
32
  - test/functional
43
33
  - test/functional/preferences_test.rb
44
- - test/test_helper.rb
45
- - test/factory.rb
46
34
  - test/unit
47
35
  - test/unit/preference_test.rb
48
36
  - test/unit/preference_definition_test.rb
37
+ - test/app_root
38
+ - test/app_root/db
39
+ - test/app_root/db/migrate
40
+ - test/app_root/db/migrate/004_migrate_preferences_to_version_1.rb
41
+ - test/app_root/db/migrate/002_create_cars.rb
42
+ - test/app_root/db/migrate/001_create_users.rb
43
+ - test/app_root/db/migrate/003_create_employees.rb
44
+ - test/app_root/config
45
+ - test/app_root/config/environment.rb
46
+ - test/app_root/app
47
+ - test/app_root/app/models
48
+ - test/app_root/app/models/car.rb
49
+ - test/app_root/app/models/manager.rb
50
+ - test/app_root/app/models/user.rb
51
+ - test/app_root/app/models/employee.rb
49
52
  - CHANGELOG.rdoc
50
53
  - init.rb
51
54
  - LICENSE