attribute_choices 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/attribute_choices.rb +16 -14
- metadata +1 -24
data/lib/attribute_choices.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
1
3
|
module AttributeChoices
|
2
4
|
def self.included(base) #:nodoc:
|
3
5
|
base.extend(AttributeChoicesMacro)
|
@@ -43,39 +45,39 @@ module AttributeChoices
|
|
43
45
|
|
44
46
|
assert_valid_attribute(attribute.to_s)
|
45
47
|
|
46
|
-
write_inheritable_hash :attribute_choices_storage, {}
|
47
|
-
class_inheritable_reader :attribute_choices_storage
|
48
|
-
|
49
|
-
write_inheritable_hash :attribute_choices_options, {}
|
50
|
-
class_inheritable_reader :attribute_choices_options
|
51
|
-
|
52
48
|
attribute = attribute.to_sym
|
53
49
|
|
54
50
|
options = args.extract_options!
|
55
51
|
options.reverse_merge!(:validate => false, :localize => false)
|
56
52
|
options.assert_valid_keys(:validate, :localize)
|
57
|
-
|
53
|
+
|
54
|
+
attribute_choices_options[attribute] = options
|
58
55
|
|
59
|
-
if options[:localize]
|
60
|
-
|
56
|
+
choices = if options[:localize]
|
57
|
+
choices.to_a.collect {|t| [t.first, I18n.translate(t.last)]}
|
61
58
|
else
|
62
|
-
|
59
|
+
choices.to_a
|
63
60
|
end
|
64
61
|
|
65
62
|
define_method("#{attribute.to_s}_display") do
|
66
|
-
|
63
|
+
# The local variable `choices` is saved inside this Proc
|
64
|
+
tupple = choices.assoc(read_attribute(attribute))
|
67
65
|
tupple && tupple.last
|
68
66
|
end
|
69
67
|
|
70
68
|
(class << self; self; end).send(:define_method, "#{attribute.to_s}_choices") do
|
71
|
-
|
69
|
+
choices.collect(&:reverse)
|
72
70
|
end
|
73
71
|
|
74
|
-
if
|
75
|
-
validates_inclusion_of attribute.to_sym, :in =>
|
72
|
+
if options[:validate]
|
73
|
+
validates_inclusion_of attribute.to_sym, :in => choices.collect {|i| i.first}
|
76
74
|
end
|
77
75
|
|
78
76
|
end
|
77
|
+
|
78
|
+
def attribute_choices_options
|
79
|
+
@attribute_choices_options ||= {}
|
80
|
+
end
|
79
81
|
|
80
82
|
private
|
81
83
|
def assert_valid_attribute(attr_name)
|
metadata
CHANGED
@@ -1,13 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attribute_choices
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 23
|
5
4
|
prerelease:
|
6
|
-
|
7
|
-
- 1
|
8
|
-
- 0
|
9
|
-
- 0
|
10
|
-
version: 1.0.0
|
5
|
+
version: 1.0.1
|
11
6
|
platform: ruby
|
12
7
|
authors:
|
13
8
|
- Christos Zisopoulos
|
@@ -26,9 +21,6 @@ dependencies:
|
|
26
21
|
requirements:
|
27
22
|
- - ">="
|
28
23
|
- !ruby/object:Gem::Version
|
29
|
-
hash: 3
|
30
|
-
segments:
|
31
|
-
- 0
|
32
24
|
version: "0"
|
33
25
|
type: :development
|
34
26
|
version_requirements: *id001
|
@@ -40,9 +32,6 @@ dependencies:
|
|
40
32
|
requirements:
|
41
33
|
- - ">="
|
42
34
|
- !ruby/object:Gem::Version
|
43
|
-
hash: 3
|
44
|
-
segments:
|
45
|
-
- 0
|
46
35
|
version: "0"
|
47
36
|
type: :development
|
48
37
|
version_requirements: *id002
|
@@ -54,9 +43,6 @@ dependencies:
|
|
54
43
|
requirements:
|
55
44
|
- - ">="
|
56
45
|
- !ruby/object:Gem::Version
|
57
|
-
hash: 3
|
58
|
-
segments:
|
59
|
-
- 0
|
60
46
|
version: "0"
|
61
47
|
type: :runtime
|
62
48
|
version_requirements: *id003
|
@@ -68,9 +54,6 @@ dependencies:
|
|
68
54
|
requirements:
|
69
55
|
- - ">="
|
70
56
|
- !ruby/object:Gem::Version
|
71
|
-
hash: 3
|
72
|
-
segments:
|
73
|
-
- 0
|
74
57
|
version: "0"
|
75
58
|
type: :runtime
|
76
59
|
version_requirements: *id004
|
@@ -101,18 +84,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
101
84
|
requirements:
|
102
85
|
- - ">="
|
103
86
|
- !ruby/object:Gem::Version
|
104
|
-
hash: 3
|
105
|
-
segments:
|
106
|
-
- 0
|
107
87
|
version: "0"
|
108
88
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
89
|
none: false
|
110
90
|
requirements:
|
111
91
|
- - ">="
|
112
92
|
- !ruby/object:Gem::Version
|
113
|
-
hash: 3
|
114
|
-
segments:
|
115
|
-
- 0
|
116
93
|
version: "0"
|
117
94
|
requirements: []
|
118
95
|
|