assignable_values 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -59,14 +59,15 @@ Or you can retrieve the humanized version of any given value by passing it as an
59
59
 
60
60
  song.humanized_genre('rock') # => 'Rock music'
61
61
 
62
- When obtaining a list of assignable values, each value will have a method `#humanized` that returns the translation:
62
+ You can obtain a list of all assignable values with their humanizations:
63
63
 
64
- song.assignable_genres.first # => "pop"
65
- song.assignable_genres.first.humanized # => "Pop music"
64
+ song.humanized_genres.size # => 3
65
+ song.humanized_genres.first.value # => "pop"
66
+ song.humanized_genres.first.humanized # => "Pop music"
66
67
 
67
- You can populate a `<select>` tag with pairs of internal values and human labels like this:
68
+ A good way to populate a `<select>` tag with pairs of internal values and human labels is to use the `collection_select` helper from Rails:
68
69
 
69
- form.collection_select :genre, form.object.assignable_genres, :to_s, :humanized
70
+ form.collection_select :genre, form.object.humanized_genres, :value, :humanized
70
71
 
71
72
  If you don't like to use your I18n dictionary for humanizations, you can also declare them directly in your model like this:
72
73
 
data/Rakefile CHANGED
@@ -14,11 +14,23 @@ namespace :all do
14
14
  end
15
15
  end
16
16
 
17
- desc "Bundle all spec apps"
18
- task :bundle do
19
- for_each_directory_of('spec/**/Gemfile') do |directory|
20
- system("cd #{directory} && bundle install")
17
+ namespace :bundle do
18
+
19
+ desc "Bundle all spec apps"
20
+ task :install do
21
+ for_each_directory_of('spec/**/Gemfile') do |directory|
22
+ system("cd #{directory} && bundle install")
23
+ end
24
+ end
25
+
26
+ desc "Update a gem given by the GEM environment variable"
27
+ task :update do
28
+ gem = ENV['GEM'] or raise "Name the gem you wish to update by setting a environment variable GEM"
29
+ for_each_directory_of('spec/**/Gemfile') do |directory|
30
+ system("cd #{directory} && bundle update #{gem}")
31
+ end
21
32
  end
33
+
22
34
  end
23
35
 
24
36
  end
@@ -37,12 +37,14 @@ module AssignableValues
37
37
  assignable_values(record).include?(value)
38
38
  end
39
39
 
40
- def assignable_values(record)
40
+ def assignable_values(record, decorate = false)
41
41
  assignable_values = []
42
42
  old_value = previously_saved_value(record)
43
43
  assignable_values << old_value if old_value.present?
44
44
  assignable_values |= raw_assignable_values(record)
45
- assignable_values = decorate_values(assignable_values)
45
+ if decorate
46
+ assignable_values = decorate_values(assignable_values)
47
+ end
46
48
  assignable_values
47
49
  end
48
50
 
@@ -159,7 +161,7 @@ module AssignableValues
159
161
  enhance_model do
160
162
  assignable_values_method = "assignable_#{restriction.property.to_s.pluralize}"
161
163
  define_method assignable_values_method do
162
- restriction.assignable_values(self)
164
+ restriction.assignable_values(self, true)
163
165
  end
164
166
  end
165
167
  end
@@ -5,22 +5,37 @@ module AssignableValues
5
5
 
6
6
  def initialize(*args)
7
7
  super
8
- define_humanized_method
8
+ define_humanized_value_method
9
+ define_humanized_values_method
9
10
  end
10
11
 
11
- def humanize_string_value(value)
12
+ def humanized_value(value)
12
13
  if value.present?
13
14
  if @hardcoded_humanizations
14
15
  @hardcoded_humanizations[value]
15
16
  else
16
- dictionary_key = "assignable_values.#{model.name.underscore}.#{property}.#{value}"
17
- I18n.t(dictionary_key, :default => value.humanize)
17
+ dictionary_scope = "assignable_values.#{model.name.underscore}.#{property}"
18
+ I18n.t(value, :scope => dictionary_scope, :default => default_humanization_for_value(value))
18
19
  end
19
20
  end
20
21
  end
21
22
 
23
+ def humanized_values(record)
24
+ assignable_values(record).collect do |value|
25
+ HumanizedValue.new(value, humanized_value(value))
26
+ end
27
+ end
28
+
22
29
  private
23
30
 
31
+ def default_humanization_for_value(value)
32
+ if value.is_a?(String)
33
+ value.humanize
34
+ else
35
+ value.to_s
36
+ end
37
+ end
38
+
24
39
  def parse_values(values)
25
40
  if values.is_a?(Hash)
26
41
  @hardcoded_humanizations = values
@@ -30,13 +45,22 @@ module AssignableValues
30
45
  end
31
46
  end
32
47
 
33
- def define_humanized_method
48
+ def define_humanized_value_method
34
49
  restriction = self
35
50
  enhance_model do
36
51
  define_method "humanized_#{restriction.property}" do |*args|
37
52
  given_value = args[0]
38
53
  value = given_value || send(restriction.property)
39
- restriction.humanize_string_value(value)
54
+ restriction.humanized_value(value)
55
+ end
56
+ end
57
+ end
58
+
59
+ def define_humanized_values_method
60
+ restriction = self
61
+ enhance_model do
62
+ define_method "humanized_#{restriction.property.to_s.pluralize}" do
63
+ restriction.humanized_values(self)
40
64
  end
41
65
  end
42
66
  end
@@ -47,7 +71,7 @@ module AssignableValues
47
71
  if value.is_a?(String)
48
72
  value = value.dup
49
73
  value.singleton_class.send(:define_method, :humanized) do
50
- restriction.humanize_string_value(value)
74
+ restriction.humanized_value(value)
51
75
  end
52
76
  end
53
77
  value
@@ -0,0 +1,20 @@
1
+ module AssignableValues
2
+ class HumanizedValue
3
+
4
+ attr_reader :value, :humanized
5
+
6
+ def initialize(value, humanized)
7
+ @value = value
8
+ @humanized = humanized
9
+ end
10
+
11
+ def to_s
12
+ humanized
13
+ end
14
+
15
+ def inspect
16
+ "#<#{self.class.name} value: #{value.inspect}, humanized: #{humanized.inspect}>"
17
+ end
18
+
19
+ end
20
+ end
@@ -1,3 +1,3 @@
1
1
  module AssignableValues
2
- VERSION = '0.4.1'
2
+ VERSION = '0.5.0'
3
3
  end
@@ -3,4 +3,5 @@ require 'assignable_values/active_record'
3
3
  require 'assignable_values/active_record/restriction/base'
4
4
  require 'assignable_values/active_record/restriction/belongs_to_association'
5
5
  require 'assignable_values/active_record/restriction/scalar_attribute'
6
+ require 'assignable_values/humanized_value'
6
7
 
@@ -5,4 +5,5 @@ gem 'rails', '=2.3.10'
5
5
  gem 'rspec', '<2'
6
6
  gem 'rspec-rails', '<2'
7
7
  gem 'ruby-debug'
8
+ gem 'rspec_candy'
8
9
  gem 'assignable_values', :path => '../..'
@@ -6,6 +6,7 @@ ENV['RAILS_ENV'] ||= 'test'
6
6
  # Load the Rails environment and testing framework
7
7
  require "#{File.dirname(__FILE__)}/app_root/config/environment"
8
8
  require 'spec/rails'
9
+ require 'rspec_candy/all'
9
10
 
10
11
  # Load dependencies
11
12
  # require 'has_defaults'
@@ -5,5 +5,6 @@ gem 'rails', '=3.0.3'
5
5
  gem 'rspec'
6
6
  gem 'rspec-rails'
7
7
  gem 'ruby-debug'
8
+ gem 'rspec_candy'
8
9
  gem 'assignable_values', :path => '../..'
9
10
 
@@ -7,6 +7,7 @@ ENV['RAILS_ROOT'] = 'app_root'
7
7
  # Load the Rails environment and testing framework
8
8
  require "#{File.dirname(__FILE__)}/app_root/config/environment"
9
9
  require 'rspec/rails'
10
+ require 'rspec_candy/all'
10
11
 
11
12
  # Load dependencies
12
13
  # require 'has_defaults'
@@ -5,5 +5,6 @@ gem 'rails', '~>3.2'
5
5
  gem 'rspec'
6
6
  gem 'rspec-rails'
7
7
  gem 'ruby-debug'
8
+ gem 'rspec_candy'
8
9
  gem 'assignable_values', :path => '../..'
9
10
 
@@ -7,6 +7,7 @@ ENV['RAILS_ROOT'] = 'app_root'
7
7
  # Load the Rails environment and testing framework
8
8
  require "#{File.dirname(__FILE__)}/app_root/config/environment"
9
9
  require 'rspec/rails'
10
+ require 'rspec_candy/all'
10
11
 
11
12
  # Load dependencies
12
13
  # require 'has_defaults'
@@ -0,0 +1,5 @@
1
+ module Recording
2
+ class Vinyl < ActiveRecord::Base
3
+ self.table_name = 'vinyl_recordings'
4
+ end
5
+ end
@@ -12,3 +12,12 @@ en:
12
12
  genre:
13
13
  pop: 'Pop music'
14
14
  rock: 'Rock music'
15
+ year:
16
+ '1977': 'The year a new hope was born'
17
+ '1980': 'The year the Empire stroke back'
18
+ '1983': 'The year the Jedi returned'
19
+ recording/vinyl:
20
+ year:
21
+ '1977': 'The year a new hope was born'
22
+ '1980': 'The year the Empire stroke back'
23
+ '1983': 'The year the Jedi returned'
@@ -0,0 +1,13 @@
1
+ class CreateRecordings < ActiveRecord::Migration
2
+
3
+ def self.up
4
+ create_table :vinyl_recordings do |t|
5
+ t.integer :year
6
+ end
7
+ end
8
+
9
+ def self.down
10
+ drop_table :vinyl_recordings
11
+ end
12
+
13
+ end
@@ -3,20 +3,11 @@ require 'ostruct'
3
3
 
4
4
  describe AssignableValues::ActiveRecord do
5
5
 
6
- def disposable_song_class(&block)
7
- klass = Class.new(Song) #, &block)
8
- def klass.name
9
- 'Song'
10
- end
11
- klass.class_eval(&block) if block
12
- klass
13
- end
14
-
15
6
  describe '.assignable_values' do
16
7
 
17
8
  it 'should raise an error when not called with a block or :through option' do
18
9
  expect do
19
- disposable_song_class do
10
+ Song.disposable_copy do
20
11
  assignable_values_for :genre
21
12
  end
22
13
  end.to raise_error(AssignableValues::NoValuesGiven)
@@ -27,7 +18,7 @@ describe AssignableValues::ActiveRecord do
27
18
  context 'without options' do
28
19
 
29
20
  before :each do
30
- @klass = disposable_song_class do
21
+ @klass = Song.disposable_copy do
31
22
  assignable_values_for :genre do
32
23
  %w[pop rock]
33
24
  end
@@ -81,7 +72,7 @@ describe AssignableValues::ActiveRecord do
81
72
  context 'if the :allow_blank option is set' do
82
73
 
83
74
  before :each do
84
- @klass = disposable_song_class do
75
+ @klass = Song.disposable_copy do
85
76
  assignable_values_for :genre, :allow_blank => true do
86
77
  %w[pop rock]
87
78
  end
@@ -105,7 +96,7 @@ describe AssignableValues::ActiveRecord do
105
96
  it 'should validate that the association is allowed' do
106
97
  allowed_association = Artist.create!
107
98
  disallowed_association = Artist.create!
108
- klass = disposable_song_class do
99
+ klass = Song.disposable_copy do
109
100
  assignable_values_for :artist do
110
101
  [allowed_association]
111
102
  end
@@ -115,7 +106,7 @@ describe AssignableValues::ActiveRecord do
115
106
  end
116
107
 
117
108
  it 'should allow a nil association if the :allow_blank option is set' do
118
- klass = disposable_song_class do
109
+ klass = Song.disposable_copy do
119
110
  assignable_values_for :artist, :allow_blank => true do
120
111
  []
121
112
  end
@@ -128,7 +119,7 @@ describe AssignableValues::ActiveRecord do
128
119
  it 'should allow a previously saved association even if that association is no longer allowed' do
129
120
  allowed_association = Artist.create!
130
121
  disallowed_association = Artist.create!
131
- klass = disposable_song_class
122
+ klass = Song.disposable_copy
132
123
  record = klass.create!(:artist => disallowed_association)
133
124
  klass.class_eval do
134
125
  assignable_values_for :artist do
@@ -140,7 +131,7 @@ describe AssignableValues::ActiveRecord do
140
131
 
141
132
  it "should not load a previously saved association if the association's foreign key hasn't changed" do
142
133
  association = Artist.create!
143
- klass = disposable_song_class do
134
+ klass = Song.disposable_copy do
144
135
  assignable_values_for :artist do
145
136
  [association] # This example doesn't care about what's assignable. We're only interested in behavior up to the validation.
146
137
  end
@@ -152,7 +143,7 @@ describe AssignableValues::ActiveRecord do
152
143
 
153
144
  it 'should not fail or allow nil if a previously saved association no longer exists in the database' do
154
145
  allowed_association = Artist.create!
155
- klass = disposable_song_class do
146
+ klass = Song.disposable_copy do
156
147
  assignable_values_for :artist do
157
148
  [allowed_association]
158
149
  end
@@ -163,7 +154,7 @@ describe AssignableValues::ActiveRecord do
163
154
  end
164
155
 
165
156
  it 'should uncache a stale association before validating' do
166
- klass = disposable_song_class do
157
+ klass = Song.disposable_copy do
167
158
  assignable_values_for :artist do
168
159
  [] # This example doesn't care about what's assignable. We're only interested in behavior up to the validation.
169
160
  end
@@ -177,7 +168,7 @@ describe AssignableValues::ActiveRecord do
177
168
  end
178
169
 
179
170
  it 'should not uncache a fresh association before validating' do
180
- klass = disposable_song_class do
171
+ klass = Song.disposable_copy do
181
172
  assignable_values_for :artist do
182
173
  [] # This example doesn't care about what's assignable. We're only interested in behavior up to the validation.
183
174
  end
@@ -194,7 +185,7 @@ describe AssignableValues::ActiveRecord do
194
185
  context 'when delegating using the :through option' do
195
186
 
196
187
  it 'should obtain allowed values from a method with the given name' do
197
- klass = disposable_song_class do
188
+ klass = Song.disposable_copy do
198
189
  assignable_values_for :genre, :through => :delegate
199
190
  def delegate
200
191
  OpenStruct.new(:assignable_song_genres => %w[pop rock])
@@ -205,7 +196,7 @@ describe AssignableValues::ActiveRecord do
205
196
  end
206
197
 
207
198
  it 'should be able to delegate to a lambda, which is evaluated in the context of the record instance' do
208
- klass = disposable_song_class do
199
+ klass = Song.disposable_copy do
209
200
  assignable_values_for :genre, :through => lambda { delegate }
210
201
  def delegate
211
202
  OpenStruct.new(:assignable_song_genres => %w[pop rock])
@@ -216,7 +207,7 @@ describe AssignableValues::ActiveRecord do
216
207
  end
217
208
 
218
209
  it 'should skip the validation if that method returns nil' do
219
- klass = disposable_song_class do
210
+ klass = Song.disposable_copy do
220
211
  assignable_values_for :genre, :through => :delegate
221
212
  def delegate
222
213
  nil
@@ -230,7 +221,7 @@ describe AssignableValues::ActiveRecord do
230
221
  context 'with :default option' do
231
222
 
232
223
  it 'should allow to set a default' do
233
- klass = disposable_song_class do
224
+ klass = Song.disposable_copy do
234
225
  assignable_values_for :genre, :default => 'pop' do
235
226
  %w[pop rock]
236
227
  end
@@ -239,7 +230,7 @@ describe AssignableValues::ActiveRecord do
239
230
  end
240
231
 
241
232
  it 'should allow to set a default through a lambda' do
242
- klass = disposable_song_class do
233
+ klass = Song.disposable_copy do
243
234
  assignable_values_for :genre, :default => lambda { 'pop' } do
244
235
  %w[pop rock]
245
236
  end
@@ -248,7 +239,7 @@ describe AssignableValues::ActiveRecord do
248
239
  end
249
240
 
250
241
  it 'should evaluate a lambda default in the context of the record instance' do
251
- klass = disposable_song_class do
242
+ klass = Song.disposable_copy do
252
243
  assignable_values_for :genre, :default => lambda { default_genre } do
253
244
  %w[pop rock]
254
245
  end
@@ -264,7 +255,7 @@ describe AssignableValues::ActiveRecord do
264
255
  context 'with :secondary_default option' do
265
256
 
266
257
  it 'should set a secondary default value if the primary value is not assignable' do
267
- klass = disposable_song_class do
258
+ klass = Song.disposable_copy do
268
259
  assignable_values_for :genre, :default => 'techno', :secondary_default => 'rock' do
269
260
  %w[pop rock]
270
261
  end
@@ -273,7 +264,7 @@ describe AssignableValues::ActiveRecord do
273
264
  end
274
265
 
275
266
  it 'should not change the default value if the default value is assignable' do
276
- klass = disposable_song_class do
267
+ klass = Song.disposable_copy do
277
268
  assignable_values_for :genre, :default => 'pop', :secondary_default => 'rock' do
278
269
  %w[pop rock]
279
270
  end
@@ -282,7 +273,7 @@ describe AssignableValues::ActiveRecord do
282
273
  end
283
274
 
284
275
  it "should not change the primary default if the secondary default value isn't assignable either" do
285
- klass = disposable_song_class do
276
+ klass = Song.disposable_copy do
286
277
  assignable_values_for :genre, :default => 'techno', :secondary_default => 'jazz' do
287
278
  %w[pop rock]
288
279
  end
@@ -292,7 +283,7 @@ describe AssignableValues::ActiveRecord do
292
283
 
293
284
  it 'should raise an error if used without a :default option' do
294
285
  expect do
295
- disposable_song_class do
286
+ Song.disposable_copy do
296
287
  assignable_values_for :genre, :secondary_default => 'pop' do
297
288
  %w[pop rock]
298
289
  end
@@ -301,7 +292,7 @@ describe AssignableValues::ActiveRecord do
301
292
  end
302
293
 
303
294
  it 'should allow to set a secondary default through a lambda' do
304
- klass = disposable_song_class do
295
+ klass = Song.disposable_copy do
305
296
  assignable_values_for :genre, :default => 'techno', :secondary_default => lambda { 'pop' } do
306
297
  %w[pop rock]
307
298
  end
@@ -310,7 +301,7 @@ describe AssignableValues::ActiveRecord do
310
301
  end
311
302
 
312
303
  it 'should evaluate a secondary lambda default in the context of the record instance' do
313
- klass = disposable_song_class do
304
+ klass = Song.disposable_copy do
314
305
  assignable_values_for :genre, :default => 'techno', :secondary_default => lambda { default_genre } do
315
306
  %w[pop rock]
316
307
  end
@@ -322,7 +313,7 @@ describe AssignableValues::ActiveRecord do
322
313
  end
323
314
 
324
315
  it "should not raise an error or change the primary default if assignable values are retrieved through a delegate, and the delegate is nil" do
325
- klass = disposable_song_class do
316
+ klass = Song.disposable_copy do
326
317
  assignable_values_for :genre, :default => 'techno', :secondary_default => 'pop', :through => lambda { nil }
327
318
  end
328
319
  expect do
@@ -331,7 +322,7 @@ describe AssignableValues::ActiveRecord do
331
322
  end
332
323
 
333
324
  it 'should not cause the list of assignable values to be evaluated if the :secondary_default option is not used' do
334
- klass = disposable_song_class do
325
+ klass = Song.disposable_copy do
335
326
  assignable_values_for :genre, :default => 'techno' do
336
327
  raise "block called!"
337
328
  end
@@ -346,7 +337,7 @@ describe AssignableValues::ActiveRecord do
346
337
  context 'when generating methods to list assignable values' do
347
338
 
348
339
  it 'should generate an instance method returning a list of assignable values' do
349
- klass = disposable_song_class do
340
+ klass = Song.disposable_copy do
350
341
  assignable_values_for :genre do
351
342
  %w[pop rock]
352
343
  end
@@ -355,7 +346,7 @@ describe AssignableValues::ActiveRecord do
355
346
  end
356
347
 
357
348
  it 'should call #to_a on the list of assignable values, allowing ranges and scopes to be passed as allowed value descriptors' do
358
- klass = disposable_song_class do
349
+ klass = Song.disposable_copy do
359
350
  assignable_values_for :year do
360
351
  1999..2001
361
352
  end
@@ -364,7 +355,7 @@ describe AssignableValues::ActiveRecord do
364
355
  end
365
356
 
366
357
  it 'should evaluate the value block in the context of the record instance' do
367
- klass = disposable_song_class do
358
+ klass = Song.disposable_copy do
368
359
  assignable_values_for :genre do
369
360
  genres
370
361
  end
@@ -376,7 +367,7 @@ describe AssignableValues::ActiveRecord do
376
367
  end
377
368
 
378
369
  it 'should include a previously saved value, even if is no longer allowed' do
379
- klass = disposable_song_class do
370
+ klass = Song.disposable_copy do
380
371
  assignable_values_for :genre do
381
372
  %w[pop rock]
382
373
  end
@@ -388,42 +379,79 @@ describe AssignableValues::ActiveRecord do
388
379
 
389
380
  context 'humanization' do
390
381
 
391
- it "should define a method #humanized on strings in that list, which return up the value's' translation" do
392
- klass = disposable_song_class do
382
+ context 'legacy methods for API compatibility' do
383
+
384
+ it "should define a method #humanized on assignable string values, which return up the value's' translation" do
385
+ klass = Song.disposable_copy do
386
+ assignable_values_for :genre do
387
+ %w[pop rock]
388
+ end
389
+ end
390
+ klass.new.assignable_genres.collect(&:humanized).should == ['Pop music', 'Rock music']
391
+ end
392
+
393
+ it 'should not define a method #humanized on values that are not strings' do
394
+ klass = Song.disposable_copy do
395
+ assignable_values_for :year do
396
+ [1999, 2000, 2001]
397
+ end
398
+ end
399
+ years = klass.new.assignable_years
400
+ years.should == [1999, 2000, 2001]
401
+ years.first.should_not respond_to(:humanized)
402
+ end
403
+
404
+ end
405
+
406
+ it 'should define a method that return pairs of values and their humanization' do
407
+ klass = Song.disposable_copy do
393
408
  assignable_values_for :genre do
394
409
  %w[pop rock]
395
410
  end
396
411
  end
397
- klass.new.assignable_genres.collect(&:humanized).should == ['Pop music', 'Rock music']
412
+ genres = klass.new.humanized_genres
413
+ genres.collect(&:value).should == ['pop', 'rock']
414
+ genres.collect(&:humanized).should == ['Pop music', 'Rock music']
415
+ genres.collect(&:to_s).should == ['Pop music', 'Rock music']
398
416
  end
399
417
 
400
418
  it 'should use String#humanize as a default translation' do
401
- klass = disposable_song_class do
419
+ klass = Song.disposable_copy do
402
420
  assignable_values_for :genre do
403
421
  %w[electronic]
404
422
  end
405
423
  end
406
- klass.new.assignable_genres.collect(&:humanized).should == ['Electronic']
424
+ klass.new.humanized_genres.collect(&:humanized).should == ['Electronic']
407
425
  end
408
426
 
409
- it 'should not define a method #humanized on values that are not strings' do
410
- klass = disposable_song_class do
427
+ it 'should allow to define humanizations for values that are not strings' do
428
+ klass = Song.disposable_copy do
411
429
  assignable_values_for :year do
412
- [1999, 2000, 2001]
430
+ [1977, 1980, 1983]
413
431
  end
414
432
  end
415
- years = klass.new.assignable_years
416
- years.should == [1999, 2000, 2001]
417
- years.first.should_not respond_to(:humanized)
433
+ years = klass.new.humanized_years
434
+ years.collect(&:value).should == [1977, 1980, 1983]
435
+ years.collect(&:humanized).should == ['The year a new hope was born', 'The year the Empire stroke back', 'The year the Jedi returned']
418
436
  end
419
437
 
420
438
  it 'should allow to directly declare humanized values by passing a hash to assignable_values_for' do
421
- klass = disposable_song_class do
439
+ klass = Song.disposable_copy do
422
440
  assignable_values_for :genre do
423
441
  { 'pop' => 'Pop music', 'rock' => 'Rock music' }
424
442
  end
425
443
  end
426
- klass.new.assignable_genres.collect(&:humanized).should =~ ['Pop music', 'Rock music']
444
+ klass.new.humanized_genres.collect(&:humanized).should =~ ['Pop music', 'Rock music']
445
+ end
446
+
447
+ it 'should properly look up humanizations for namespaced models' do
448
+ klass = Recording::Vinyl.disposable_copy do
449
+ assignable_values_for :year do
450
+ [1977, 1980, 1983]
451
+ end
452
+ end
453
+ years = klass.new.humanized_years
454
+ years.collect(&:humanized).should == ['The year a new hope was born', 'The year the Empire stroke back', 'The year the Jedi returned']
427
455
  end
428
456
 
429
457
  end
@@ -431,7 +459,7 @@ describe AssignableValues::ActiveRecord do
431
459
  context 'with :through option' do
432
460
 
433
461
  it 'should retrieve assignable values from the given method' do
434
- klass = disposable_song_class do
462
+ klass = Song.disposable_copy do
435
463
  assignable_values_for :genre, :through => :delegate
436
464
  def delegate
437
465
  @delegate ||= 'delegate'
@@ -448,7 +476,7 @@ describe AssignableValues::ActiveRecord do
448
476
  record_received(record)
449
477
  %w[pop rock]
450
478
  end
451
- klass = disposable_song_class do
479
+ klass = Song.disposable_copy do
452
480
  assignable_values_for :genre, :through => :delegate
453
481
  define_method :delegate do
454
482
  delegate
@@ -460,7 +488,7 @@ describe AssignableValues::ActiveRecord do
460
488
  end
461
489
 
462
490
  it 'should raise an error if the given method returns nil' do
463
- klass = disposable_song_class do
491
+ klass = Song.disposable_copy do
464
492
  assignable_values_for :genre, :through => :delegate
465
493
  def delegate
466
494
  nil
@@ -475,25 +503,4 @@ describe AssignableValues::ActiveRecord do
475
503
 
476
504
  end
477
505
 
478
- #describe '.authorize_values_for' do
479
- #
480
- # it 'should be a shortcut for .assignable_values_for :attribute, :through => :power' do
481
- # @klass = disposable_song_class
482
- # @klass.should_receive(:assignable_values_for).with(:attribute, :option => 'option', :through => :power)
483
- # @klass.class_eval do
484
- # authorize_values_for :attribute, :option => 'option'
485
- # end
486
- # end
487
- #
488
- # it 'should generate a getter and setter for a @power field' do
489
- # @klass = disposable_song_class do
490
- # authorize_values_for :attribute
491
- # end
492
- # song = @klass.new
493
- # song.should respond_to(:power)
494
- # song.should respond_to(:power=)
495
- # end
496
- #
497
- #end
498
-
499
506
  end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe AssignableValues::HumanizedValue do
4
+
5
+ describe '#inspect' do
6
+
7
+ it "should be a helpful description of the instance's content" do
8
+ value = AssignableValues::HumanizedValue.new('value', 'humanization')
9
+ value.inspect.should == '#<AssignableValues::HumanizedValue value: "value", humanized: "humanization">'
10
+ end
11
+
12
+ end
13
+
14
+ end
15
+
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: assignable_values
3
3
  version: !ruby/object:Gem::Version
4
- hash: 13
4
+ hash: 11
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 4
9
- - 1
10
- version: 0.4.1
8
+ - 5
9
+ - 0
10
+ version: 0.5.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Henning Koch
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-08-02 00:00:00 +02:00
18
+ date: 2012-08-14 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -111,6 +111,7 @@ files:
111
111
  - lib/assignable_values/active_record/restriction/belongs_to_association.rb
112
112
  - lib/assignable_values/active_record/restriction/scalar_attribute.rb
113
113
  - lib/assignable_values/errors.rb
114
+ - lib/assignable_values/humanized_value.rb
114
115
  - lib/assignable_values/version.rb
115
116
  - spec/rails-2.3/Gemfile
116
117
  - spec/rails-2.3/Rakefile
@@ -168,11 +169,14 @@ files:
168
169
  - spec/rails-3.2/spec_helper.rb
169
170
  - spec/shared/app_root/app/controllers/application_controller.rb
170
171
  - spec/shared/app_root/app/models/artist.rb
172
+ - spec/shared/app_root/app/models/recording/vinyl.rb
171
173
  - spec/shared/app_root/app/models/song.rb
172
174
  - spec/shared/app_root/config/locales/en.yml
173
175
  - spec/shared/app_root/db/migrate/001_create_artists.rb
174
176
  - spec/shared/app_root/db/migrate/002_create_songs.rb
177
+ - spec/shared/app_root/db/migrate/003_create_recordings.rb
175
178
  - spec/shared/assignable_values/active_record_spec.rb
179
+ - spec/shared/assignable_values/humanized_value_spec.rb
176
180
  has_rdoc: true
177
181
  homepage: https://github.com/makandra/assignable_values
178
182
  licenses: []
@@ -203,7 +207,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
203
207
  requirements: []
204
208
 
205
209
  rubyforge_project:
206
- rubygems_version: 1.3.9.4
210
+ rubygems_version: 1.3.9.3
207
211
  signing_key:
208
212
  specification_version: 3
209
213
  summary: Restrict the values assignable to ActiveRecord attributes or associations. Or enums on steroids.