assignable_values 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -94,15 +94,15 @@ The default is applied to new records:
94
94
 
95
95
  Song.new.genre # => 'rock'
96
96
 
97
- Defaults can be lambdas:
97
+ Defaults can be procs:
98
98
 
99
99
  class Song < ActiveRecord::Base
100
- assignable_values_for :genre, :default => lambda { Date.today.year } do
100
+ assignable_values_for :genre, :default => proc { Date.today.year } do
101
101
  1980 .. 2011
102
102
  end
103
103
  end
104
104
 
105
- The lambda will be evaluated in the context of the record instance.
105
+ The proc will be evaluated in the context of the record instance.
106
106
 
107
107
  You can also default a secondary default that is only set if the primary default value is not assignable:
108
108
 
@@ -41,7 +41,11 @@ module AssignableValues
41
41
  assignable_values = []
42
42
  old_value = previously_saved_value(record)
43
43
  assignable_values << old_value if old_value.present?
44
- assignable_values |= raw_assignable_values(record)
44
+ parsed_values = parsed_assignable_values(record)
45
+ assignable_values |= parsed_values.delete(:values)
46
+ parsed_values.each do |meta_name, meta_content|
47
+ assignable_values.singleton_class.send(:define_method, meta_name) { meta_content }
48
+ end
45
49
  if decorate
46
50
  assignable_values = decorate_values(assignable_values)
47
51
  end
@@ -77,7 +81,7 @@ module AssignableValues
77
81
  end
78
82
 
79
83
  def parse_values(values)
80
- values.to_a
84
+ { :values => values.to_a }
81
85
  end
82
86
 
83
87
  def current_value(record)
@@ -166,7 +170,7 @@ module AssignableValues
166
170
  end
167
171
  end
168
172
 
169
- def raw_assignable_values(record)
173
+ def parsed_assignable_values(record)
170
174
  if delegate?
171
175
  values = assignable_values_from_delegate(record)
172
176
  else
@@ -9,10 +9,10 @@ module AssignableValues
9
9
  define_humanized_values_method
10
10
  end
11
11
 
12
- def humanized_value(value)
12
+ def humanized_value(values, value) # we take the values because that contains the humanizations in case humanizations are hard-coded as a hash
13
13
  if value.present?
14
- if @hardcoded_humanizations
15
- @hardcoded_humanizations[value]
14
+ if values.respond_to?(:humanizations)
15
+ values.humanizations[value]
16
16
  else
17
17
  dictionary_scope = "assignable_values.#{model.name.underscore}.#{property}"
18
18
  I18n.t(value, :scope => dictionary_scope, :default => default_humanization_for_value(value))
@@ -21,8 +21,9 @@ module AssignableValues
21
21
  end
22
22
 
23
23
  def humanized_values(record)
24
- assignable_values(record).collect do |value|
25
- HumanizedValue.new(value, humanized_value(value))
24
+ values = assignable_values(record)
25
+ values.collect do |value|
26
+ HumanizedValue.new(value, humanized_value(values, value))
26
27
  end
27
28
  end
28
29
 
@@ -38,8 +39,18 @@ module AssignableValues
38
39
 
39
40
  def parse_values(values)
40
41
  if values.is_a?(Hash)
41
- @hardcoded_humanizations = values
42
- values = values.keys
42
+ { :values => values.keys,
43
+ :humanizations => values }
44
+ #puts "----"
45
+ #puts "HAS HUMANIZATIONS"
46
+ #humanizations = values
47
+ #values = values.keys.dup
48
+ #values.singleton_class.send(:define_method, :humanizations) do
49
+ # puts "----"
50
+ # puts "HUMANIZATIONS used"
51
+ # humanizations
52
+ #end
53
+ #values
43
54
  else
44
55
  super
45
56
  end
@@ -49,9 +60,10 @@ module AssignableValues
49
60
  restriction = self
50
61
  enhance_model do
51
62
  define_method "humanized_#{restriction.property}" do |*args|
63
+ values = restriction.assignable_values(self)
52
64
  given_value = args[0]
53
65
  value = given_value || send(restriction.property)
54
- restriction.humanized_value(value)
66
+ restriction.humanized_value(values, value)
55
67
  end
56
68
  end
57
69
  end
@@ -71,7 +83,7 @@ module AssignableValues
71
83
  if value.is_a?(String)
72
84
  value = value.dup
73
85
  value.singleton_class.send(:define_method, :humanized) do
74
- restriction.humanized_value(value)
86
+ restriction.humanized_value(values, value)
75
87
  end
76
88
  end
77
89
  value
@@ -1,3 +1,3 @@
1
1
  module AssignableValues
2
- VERSION = '0.5.0'
2
+ VERSION = '0.5.1'
3
3
  end
@@ -5,6 +5,7 @@ class CreateSongs < ActiveRecord::Migration
5
5
  t.integer :artist_id
6
6
  t.string :genre
7
7
  t.integer :year
8
+ t.integer :duration
8
9
  end
9
10
  end
10
11
 
@@ -379,30 +379,6 @@ describe AssignableValues::ActiveRecord do
379
379
 
380
380
  context 'humanization' do
381
381
 
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
382
  it 'should define a method that return pairs of values and their humanization' do
407
383
  klass = Song.disposable_copy do
408
384
  assignable_values_for :genre do
@@ -435,13 +411,28 @@ describe AssignableValues::ActiveRecord do
435
411
  years.collect(&:humanized).should == ['The year a new hope was born', 'The year the Empire stroke back', 'The year the Jedi returned']
436
412
  end
437
413
 
438
- it 'should allow to directly declare humanized values by passing a hash to assignable_values_for' do
439
- klass = Song.disposable_copy do
440
- assignable_values_for :genre do
441
- { 'pop' => 'Pop music', 'rock' => 'Rock music' }
414
+ context 'hardcoded humanizations' do
415
+
416
+ it 'should allow to directly declare humanized values by passing a hash to assignable_values_for' do
417
+ klass = Song.disposable_copy do
418
+ assignable_values_for :genre do
419
+ { 'pop' => 'Pop music', 'rock' => 'Rock music' }
420
+ end
442
421
  end
422
+ klass.new.humanized_genres.collect(&:humanized).sort.should =~ ['Pop music', 'Rock music']
443
423
  end
444
- klass.new.humanized_genres.collect(&:humanized).should =~ ['Pop music', 'Rock music']
424
+
425
+ it "should correctly humanize values if the humanizations were declared using a hash, the values are not strings, and the list of humanized values hasn't been called before (bugfix)" do
426
+ klass = Song.disposable_copy do
427
+ assignable_values_for :duration do
428
+ { 60 => '1:00',
429
+ 90 => '1:30' }
430
+ end
431
+ end
432
+ klass.new(:duration => 60).humanized_duration.should == '1:00'
433
+ klass.new(:duration => 90).humanized_duration.should == '1:30'
434
+ end
435
+
445
436
  end
446
437
 
447
438
  it 'should properly look up humanizations for namespaced models' do
@@ -454,6 +445,30 @@ describe AssignableValues::ActiveRecord do
454
445
  years.collect(&:humanized).should == ['The year a new hope was born', 'The year the Empire stroke back', 'The year the Jedi returned']
455
446
  end
456
447
 
448
+ context 'legacy methods for API compatibility' do
449
+
450
+ it "should define a method #humanized on assignable string values, which return up the value's' translation" do
451
+ klass = Song.disposable_copy do
452
+ assignable_values_for :genre do
453
+ %w[pop rock]
454
+ end
455
+ end
456
+ klass.new.assignable_genres.collect(&:humanized).should == ['Pop music', 'Rock music']
457
+ end
458
+
459
+ it 'should not define a method #humanized on values that are not strings' do
460
+ klass = Song.disposable_copy do
461
+ assignable_values_for :year do
462
+ [1999, 2000, 2001]
463
+ end
464
+ end
465
+ years = klass.new.assignable_years
466
+ years.should == [1999, 2000, 2001]
467
+ years.first.should_not respond_to(:humanized)
468
+ end
469
+
470
+ end
471
+
457
472
  end
458
473
 
459
474
  context 'with :through option' do
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: 11
4
+ hash: 9
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 5
9
- - 0
10
- version: 0.5.0
9
+ - 1
10
+ version: 0.5.1
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-14 00:00:00 +02:00
18
+ date: 2012-11-28 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -207,7 +207,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
207
207
  requirements: []
208
208
 
209
209
  rubyforge_project:
210
- rubygems_version: 1.3.9.3
210
+ rubygems_version: 1.3.9.4
211
211
  signing_key:
212
212
  specification_version: 3
213
213
  summary: Restrict the values assignable to ActiveRecord attributes or associations. Or enums on steroids.