Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

allow to define humanizations by passing a hash to .assignable_values…

…_for
  • Loading branch information...
commit fe130f9c53aae46604cfcbb2350fa5f722596620 1 parent 0d2d4a2
@henning-koch henning-koch authored
View
10 README.md
@@ -64,6 +64,16 @@ You can populate a `<select>` tag with pairs of internal values and human labels
form.collection_select :genre, form.object.assignable_genres, :to_s, :humanized
+If you don't like to use your I18n dictionary for humanizations, you can also declare them directly in your model like this:
+
+ class Song < ActiveRecord::Base
+ assignable_values_for :genre do
+ { 'pop' => 'Pop music',
+ 'rock' => 'Rock music',
+ 'electronic' => 'Electronic music' }
+ end
+ end
+
### Defining default values
View
11 lib/assignable_values/active_record/restriction/base.rb
@@ -48,6 +48,10 @@ def set_default(record)
private
+ def parse_values(values)
+ values.to_a
+ end
+
def current_value(record)
record.send(property)
end
@@ -115,10 +119,11 @@ def define_assignable_values_method
def raw_assignable_values(record)
if delegate?
- assignable_values_from_delegate(record)
+ values = assignable_values_from_delegate(record)
else
- record.instance_eval(&@values)
- end.to_a
+ values = record.instance_eval(&@values)
+ end
+ parse_values(values)
end
def delegate(record)
View
18 lib/assignable_values/active_record/restriction/scalar_attribute.rb
@@ -10,13 +10,26 @@ def initialize(*args)
def humanize_string_value(value)
if value.present?
- dictionary_key = "assignable_values.#{model.name.underscore}.#{property}.#{value}"
- I18n.t(dictionary_key, :default => value.humanize)
+ if @hardcoded_humanizations
+ @hardcoded_humanizations[value]
+ else
+ dictionary_key = "assignable_values.#{model.name.underscore}.#{property}.#{value}"
+ I18n.t(dictionary_key, :default => value.humanize)
+ end
end
end
private
+ def parse_values(values)
+ if values.is_a?(Hash)
+ @hardcoded_humanizations = values
+ values = values.keys
+ else
+ super
+ end
+ end
+
def define_humanized_method
restriction = self
enhance_model do
@@ -44,7 +57,6 @@ def previously_saved_value(record)
record.send("#{property}_was")
end
-
end
end
end
View
2  lib/assignable_values/version.rb
@@ -1,3 +1,3 @@
module AssignableValues
- VERSION = '0.1.2'
+ VERSION = '0.2.0'
end
View
71 spec/assignable_values/active_record_spec.rb
@@ -264,35 +264,6 @@ def default_genre
klass.new.assignable_genres.should == %w[pop rock]
end
- it "should define a method #humanized on strings in that list, which return up the value's' translation" do
- klass = disposable_song_class do
- assignable_values_for :genre do
- %w[pop rock]
- end
- end
- klass.new.assignable_genres.collect(&:humanized).should == ['Pop music', 'Rock music']
- end
-
- it 'should use String#humanize as a default translation' do
- klass = disposable_song_class do
- assignable_values_for :genre do
- %w[electronic]
- end
- end
- klass.new.assignable_genres.collect(&:humanized).should == ['Electronic']
- end
-
- it 'should not define a method #humanized on values that are not strings' do
- klass = disposable_song_class do
- assignable_values_for :year do
- [1999, 2000, 2001]
- end
- end
- years = klass.new.assignable_years
- years.should == [1999, 2000, 2001]
- years.first.should_not respond_to(:humanized)
- end
-
it 'should call #to_a on the list of assignable values, allowing ranges and scopes to be passed as allowed value descriptors' do
klass = disposable_song_class do
assignable_values_for :year do
@@ -325,6 +296,48 @@ def genres
record.assignable_genres.should =~ %w[pop rock ballad]
end
+ context 'humanization' do
+
+ it "should define a method #humanized on strings in that list, which return up the value's' translation" do
+ klass = disposable_song_class do
+ assignable_values_for :genre do
+ %w[pop rock]
+ end
+ end
+ klass.new.assignable_genres.collect(&:humanized).should == ['Pop music', 'Rock music']
+ end
+
+ it 'should use String#humanize as a default translation' do
+ klass = disposable_song_class do
+ assignable_values_for :genre do
+ %w[electronic]
+ end
+ end
+ klass.new.assignable_genres.collect(&:humanized).should == ['Electronic']
+ end
+
+ it 'should not define a method #humanized on values that are not strings' do
+ klass = disposable_song_class do
+ assignable_values_for :year do
+ [1999, 2000, 2001]
+ end
+ end
+ years = klass.new.assignable_years
+ years.should == [1999, 2000, 2001]
+ years.first.should_not respond_to(:humanized)
+ end
+
+ it 'should allow to directly declare humanized values by passing a hash to assignable_values_for' do
+ klass = disposable_song_class do
+ assignable_values_for :genre do
+ { 'pop' => 'Pop music', 'rock' => 'Rock music' }
+ end
+ end
+ klass.new.assignable_genres.collect(&:humanized).should =~ ['Pop music', 'Rock music']
+ end
+
+ end
+
context 'with :through option' do
it 'should retrieve assignable values from the given method' do
Please sign in to comment.
Something went wrong with that request. Please try again.