has_enum 0.7.1 → 0.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,8 @@
1
1
  module HasEnum::ClassMethods
2
2
 
3
3
  def enums
4
- @enums ||= read_inheritable_attribute(:enums)
4
+ read_inheritable_attribute(:enums) ||
5
+ write_inheritable_attribute(:enums, HashWithIndifferentAccess.new)
5
6
  end
6
7
 
7
8
  def enum_values(attribute)
@@ -68,15 +69,19 @@ module HasEnum::ClassMethods
68
69
  end
69
70
 
70
71
  def human_enum_values(enum)
71
- begin
72
- options = {:default => nil, :raise => true, :count => nil}
73
- HashWithIndifferentAccess.new(human_attribute_name("#{enum}_enum", options))
74
- rescue I18n::MissingTranslationData
75
- (enums[enum] || []).inject HashWithIndifferentAccess.new do |hash, value|
72
+ values = human_attribute_name("#{enum}_enum", :count => nil)
73
+ unless values.is_a? Hash
74
+ values = (enums[enum] || []).inject({}) do |hash, value|
76
75
  hash[value] = value.humanize
77
76
  hash
78
77
  end
79
78
  end
79
+ values.with_indifferent_access
80
+ end
81
+
82
+
83
+ def values_for_select_tag(enum)
84
+ human_enums[enum].invert.to_a
80
85
  end
81
86
 
82
87
  end
@@ -0,0 +1,24 @@
1
+ begin
2
+ require 'formtastic'
3
+
4
+ Formtastic::SemanticFormBuilder.class_eval do
5
+
6
+ def enum_input(method, options = {})
7
+ options.merge! :collection => object.class.values_for_select_tag(method),
8
+ :wrapper_html => {:class => :enum}
9
+ self.select_input(method, options)
10
+ end
11
+
12
+ def default_input_type_with_enum(method, options={})
13
+ if object.class.respond_to?(:enums) && object.class.has_enum?(method)
14
+ :enum
15
+ else
16
+ default_input_type_without_enum(method, options)
17
+ end
18
+ end
19
+
20
+ alias_method_chain :default_input_type, :enum
21
+
22
+ end
23
+ rescue LoadError
24
+ end
@@ -25,7 +25,7 @@ class ActionView::Helpers::InstanceTag
25
25
  end
26
26
 
27
27
  def values_for_enum_tag
28
- object.class.human_enums[method_name].invert.to_a
28
+ object.class.values_for_select_tag(method_name)
29
29
  end
30
30
  end
31
31
 
@@ -1,3 +1,3 @@
1
1
  module HasEnum
2
- VERSION = "0.7.1"
2
+ VERSION = "0.7.2"
3
3
  end
data/lib/has_enum.rb CHANGED
@@ -3,7 +3,6 @@ module HasEnum
3
3
  autoload :ClassMethods, 'has_enum/class_methods'
4
4
 
5
5
  def self.included(base)
6
- base.write_inheritable_attribute(:enums, HashWithIndifferentAccess.new)
7
6
  base.extend ClassMethods
8
7
  end
9
8
 
@@ -13,28 +12,4 @@ class ActiveRecord::Base
13
12
  include HasEnum
14
13
  end
15
14
 
16
- begin
17
- require 'formtastic'
18
-
19
- Formtastic::SemanticFormBuilder.class_eval do
20
-
21
- def enum_input(method, options = {})
22
- value = @object.send(method)
23
- options.reverse_merge! :as => :select,
24
- :collection => @object.class.human_enums[method].invert.to_a
25
- self.input(method, options).gsub(/class="select/, 'class="enum')
26
- end
27
-
28
- def input_with_enum(method, options={})
29
- if @object.class.respond_to?(:enums) && @object.class.enums[method] && !options[:as]
30
- enum_input(method, options)
31
- else
32
- input_without_enum(method, options)
33
- end
34
- end
35
-
36
- alias_method_chain :input, :enum
37
-
38
- end
39
- rescue LoadError
40
- end
15
+ require 'has_enum/formtastic'
data/spec/ru.yml CHANGED
@@ -1,11 +1,13 @@
1
1
  ru:
2
+ attributes:
3
+ size_enum:
4
+ small: "Маленький"
5
+ medium: "Средний"
6
+ large: "Большой"
7
+
2
8
  activerecord:
3
9
  attributes:
4
10
  test_model:
5
- size_enum:
6
- small: "Маленький"
7
- medium: "Средний"
8
- large: "Большой"
9
11
  status_enum:
10
12
  pending: "На рассмотрении"
11
13
  failed: "Обработано с ошибкой"
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 7
8
- - 1
9
- version: 0.7.1
8
+ - 2
9
+ version: 0.7.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Andreas Korth
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-03-28 00:00:00 +07:00
19
+ date: 2011-03-29 00:00:00 +07:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -84,6 +84,7 @@ files:
84
84
  - has_enum.gemspec
85
85
  - lib/has_enum.rb
86
86
  - lib/has_enum/class_methods.rb
87
+ - lib/has_enum/formtastic.rb
87
88
  - lib/has_enum/helpers.rb
88
89
  - lib/has_enum/version.rb
89
90
  - spec/has_enum_spec.rb