assignable_values 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
data/README.md
CHANGED
@@ -48,7 +48,7 @@ This is useful for populating <select> tags in web forms:
|
|
48
48
|
|
49
49
|
form.select :genre, form.object.assignable_genres
|
50
50
|
|
51
|
-
###
|
51
|
+
### Humanized labels
|
52
52
|
|
53
53
|
You will often want to present internal values in a humanized form. E.g. `"pop"` should be presented as `"Pop music"`.
|
54
54
|
|
@@ -64,12 +64,12 @@ You can define human labels in your I18n dictionary:
|
|
64
64
|
|
65
65
|
When obtaining a list of assignable values, each value will have a method `#human` that returns the translation:
|
66
66
|
|
67
|
-
song.assignable_genres.first
|
68
|
-
song.assignable_genres.first.
|
67
|
+
song.assignable_genres.first # => 'pop'
|
68
|
+
song.assignable_genres.first.humanized # => 'Pop music'
|
69
69
|
|
70
70
|
You can populate a <select> tag with pairs of internal values and human labels like this:
|
71
71
|
|
72
|
-
form.collection_select :genre, form.object.assignable_genres, :to_s, :
|
72
|
+
form.collection_select :genre, form.object.assignable_genres, :to_s, :humanized
|
73
73
|
|
74
74
|
## Restricting belongs_to associations
|
75
75
|
|
@@ -3,14 +3,36 @@ module AssignableValues
|
|
3
3
|
module Restriction
|
4
4
|
class ScalarAttribute < Base
|
5
5
|
|
6
|
+
def initialize(*args)
|
7
|
+
super
|
8
|
+
define_humanized_method
|
9
|
+
end
|
10
|
+
|
11
|
+
def humanize_string_value(value)
|
12
|
+
if value.present?
|
13
|
+
dictionary_key = "assignable_values.#{model.name.underscore}.#{property}.#{value}"
|
14
|
+
I18n.t(dictionary_key, :default => value.humanize)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
6
18
|
private
|
7
19
|
|
20
|
+
def define_humanized_method
|
21
|
+
restriction = self
|
22
|
+
enhance_model do
|
23
|
+
define_method "humanized_#{restriction.property}" do
|
24
|
+
value = send(restriction.property)
|
25
|
+
restriction.humanize_string_value(value)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
8
30
|
def decorate_values(values)
|
9
31
|
restriction = self
|
10
32
|
values.collect do |value|
|
11
33
|
if value.is_a?(String)
|
12
34
|
value = value.dup
|
13
|
-
value.singleton_class.send(:define_method, :
|
35
|
+
value.singleton_class.send(:define_method, :humanized) do
|
14
36
|
restriction.humanize_string_value(value)
|
15
37
|
end
|
16
38
|
end
|
@@ -22,6 +44,7 @@ module AssignableValues
|
|
22
44
|
record.send("#{property}_was")
|
23
45
|
end
|
24
46
|
|
47
|
+
|
25
48
|
end
|
26
49
|
end
|
27
50
|
end
|
@@ -57,6 +57,19 @@ describe AssignableValues::ActiveRecord do
|
|
57
57
|
song.should be_valid
|
58
58
|
end
|
59
59
|
|
60
|
+
it 'should generate a method returning the humanized value' do
|
61
|
+
song = @klass.new(:genre => 'pop')
|
62
|
+
song.humanized_genre.should == 'Pop music'
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'should generate a method returning the humanized value, which is nil when the value is blank' do
|
66
|
+
song = @klass.new
|
67
|
+
song.genre = nil
|
68
|
+
song.humanized_genre.should be_nil
|
69
|
+
song.genre = ''
|
70
|
+
song.humanized_genre.should be_nil
|
71
|
+
end
|
72
|
+
|
60
73
|
end
|
61
74
|
|
62
75
|
context 'if the :allow_blank option is set' do
|
@@ -253,13 +266,13 @@ describe AssignableValues::ActiveRecord do
|
|
253
266
|
@klass.new.assignable_genres.should == %w[pop rock]
|
254
267
|
end
|
255
268
|
|
256
|
-
it "should define a method #
|
269
|
+
it "should define a method #humanized on strings in that list, which return up the value's' translation" do
|
257
270
|
@klass = disposable_song_class do
|
258
271
|
assignable_values_for :genre do
|
259
272
|
%w[pop rock]
|
260
273
|
end
|
261
274
|
end
|
262
|
-
@klass.new.assignable_genres.collect(&:
|
275
|
+
@klass.new.assignable_genres.collect(&:humanized).should == ['Pop music', 'Rock music']
|
263
276
|
end
|
264
277
|
|
265
278
|
it 'should use String#humanize as a default translation' do
|
@@ -268,10 +281,10 @@ describe AssignableValues::ActiveRecord do
|
|
268
281
|
%w[electronic]
|
269
282
|
end
|
270
283
|
end
|
271
|
-
@klass.new.assignable_genres.collect(&:
|
284
|
+
@klass.new.assignable_genres.collect(&:humanized).should == ['Electronic']
|
272
285
|
end
|
273
286
|
|
274
|
-
it 'should not define a method #
|
287
|
+
it 'should not define a method #humanized on values that are not strings' do
|
275
288
|
@klass = disposable_song_class do
|
276
289
|
assignable_values_for :year do
|
277
290
|
[1999, 2000, 2001]
|
@@ -279,7 +292,7 @@ describe AssignableValues::ActiveRecord do
|
|
279
292
|
end
|
280
293
|
years = @klass.new.assignable_years
|
281
294
|
years.should == [1999, 2000, 2001]
|
282
|
-
years.first.should_not respond_to(:
|
295
|
+
years.first.should_not respond_to(:humanized)
|
283
296
|
end
|
284
297
|
|
285
298
|
it 'should call #to_a on the list of assignable values, allowing ranges and scopes to be passed as allowed value descriptors' 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:
|
4
|
+
hash: 25
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Henning Koch
|