has_enum 0.7.1 → 0.7.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/has_enum/class_methods.rb +11 -6
- data/lib/has_enum/formtastic.rb +24 -0
- data/lib/has_enum/helpers.rb +1 -1
- data/lib/has_enum/version.rb +1 -1
- data/lib/has_enum.rb +1 -26
- data/spec/ru.yml +6 -4
- metadata +4 -3
@@ -1,7 +1,8 @@
|
|
1
1
|
module HasEnum::ClassMethods
|
2
2
|
|
3
3
|
def enums
|
4
|
-
|
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
|
-
|
72
|
-
|
73
|
-
|
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
|
data/lib/has_enum/helpers.rb
CHANGED
data/lib/has_enum/version.rb
CHANGED
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
|
-
|
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
|
-
-
|
9
|
-
version: 0.7.
|
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-
|
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
|