simple_enum 0.3.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 0
3
- :minor: 3
3
+ :minor: 9
4
4
  :patch: 0
data/lib/simple_enum.rb CHANGED
@@ -179,6 +179,12 @@ module SimpleEnum
179
179
  return class_variable_get(:@@SE_#{self_name.upcase}) if sym.nil?
180
180
  class_variable_get(:@@SE_#{self_name.upcase})[sym]
181
181
  end
182
+
183
+ def self.#{self_name}_for_select(&block)
184
+ self.#{self_name}.map do |k,v|
185
+ [block_given? ? yield(k,v) : self.human_enum_name(#{self_name.inspect}, k), v]
186
+ end.sort { |a,b| a[1] <=> b[1] }
187
+ end
182
188
  EOM
183
189
 
184
190
  # only create if :slim is not defined
@@ -207,6 +213,15 @@ module SimpleEnum
207
213
  end
208
214
 
209
215
  include Validation
216
+
217
+ def human_enum_name(enum, key, options = {})
218
+ defaults = self_and_descendants_from_active_record.map { |klass| :"#{klass.name.underscore}.#{enum}.#{key}" }
219
+ defaults << :"#{enum}.#{key}"
220
+ defaults << options.delete(:default) if options[:default]
221
+ defaults << "#{key}".humanize
222
+ options[:count] ||= 1
223
+ I18n.translate(defaults.shift, options.merge(:default => defaults.flatten, :scope => [:activerecord, :enums]))
224
+ end
210
225
 
211
226
  protected
212
227
  # Returns enum definitions as defined by each call to
@@ -23,7 +23,7 @@ module SimpleEnum
23
23
  def default(k = nil)
24
24
  @sym_value_lookup[k.to_enum_sym] if k
25
25
  end
26
-
26
+
27
27
  def method_missing(symbol, *args)
28
28
  if @sym_value_lookup.has_key?(symbol.to_enum_sym)
29
29
  return @reverse_sym_lookup[symbol.to_enum_sym] if args.first
data/locales/en.yml CHANGED
@@ -3,4 +3,12 @@ en:
3
3
  activerecord:
4
4
  errors:
5
5
  messages:
6
- invalid_enum: invalid option supplied.
6
+ invalid_enum: invalid option supplied.
7
+ enums:
8
+ dummy:
9
+ genders:
10
+ female: Girl
11
+ didums:
12
+ foo:
13
+ one: Foo
14
+ other: Foos
data/simple_enum.gemspec CHANGED
@@ -1,15 +1,15 @@
1
1
  # Generated by jeweler
2
- # DO NOT EDIT THIS FILE
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{simple_enum}
8
- s.version = "0.3.0"
8
+ s.version = "0.9.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Lukas Westermann"]
12
- s.date = %q{2009-10-07}
12
+ s.date = %q{2009-12-01}
13
13
  s.email = %q{lukas.westermann@gmail.com}
14
14
  s.extra_rdoc_files = [
15
15
  "README.rdoc"
@@ -72,3 +72,4 @@ Gem::Specification.new do |s|
72
72
  else
73
73
  end
74
74
  end
75
+
@@ -91,4 +91,11 @@ class ClassMethodsTest < ActiveSupport::TestCase
91
91
  assert_same 0, with_upcase.GENDERS.male
92
92
  assert_same 1, with_upcase.GENDERS[:female]
93
93
  end
94
+
95
+ test "that the human_enum_name method returns translated/humanized values" do
96
+ assert_equal :male.to_s.humanize, Dummy.human_enum_name(:genders, :male)
97
+ assert_equal "Girl", Dummy.human_enum_name(:genders, :female)
98
+ assert_equal "Foo", Dummy.human_enum_name(:didums, :foo)
99
+ assert_equal "Foos", Dummy.human_enum_name(:didums, :foo, :count => 5)
100
+ end
94
101
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lukas Westermann
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-07 00:00:00 +02:00
12
+ date: 2009-12-01 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies: []
15
15