rails_column_enumerator 1.2.0 → 1.3.0
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.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +8 -0
- data/lib/rails_column_enumerator.rb +9 -5
- data/lib/rails_column_enumerator/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cab423e5aafb991c2d0078aba5a69d519c8a3455
|
4
|
+
data.tar.gz: 7778867ce83935b8cdb2984dbc8f935f19a5f34d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 447ca04146e24e7221a0672a1fcb94991b9187d8f0055778d14dde68efbfe8e91ea41818031e4e22680593c35f543b4ea80ad56c84b4ea18f363bf887c46fece
|
7
|
+
data.tar.gz: 0e58eabc5520c44cee57c4ff442ea6b6cf3ef8d7b7e612c22f917d3b9b90d585cf80cb2fc9422d462c170d15a214d856ed9a8b6924abbcfd6a73ad1ce2e24428
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -41,6 +41,14 @@ enumerated_column :state, { open: 1, closed: 2, failed: 3 }, selections: { not_f
|
|
41
41
|
TestClass::NOT_FAILED # [["Open", 1], ["Closed", 2]]
|
42
42
|
TestClass::NOT_OPEN # [["Closed", 2], ["Failed", 3]]
|
43
43
|
|
44
|
+
# You can also add localization options for these select helper outputs. These are matched to the name of the selection.
|
45
|
+
# These are optional and provide support for a default value.
|
46
|
+
default_localizations = { failed: "OMG WTF BBQ!?" }
|
47
|
+
all_states_localizations = { open: "I am open" }
|
48
|
+
|
49
|
+
enumerated_column :state, { open: 1, closed: 2, failed: 3 }, selections: { all_states: [1, 2, 3] }, localizations: { default: default_localizations, all_states: all_states_localizations }
|
50
|
+
TestClass::ALL_STATES # [["I am open", 1], ["Closed", 2], ["OMG WTF BBQ!?", 3]]
|
51
|
+
|
44
52
|
# You can use your enumerated attribute as follows
|
45
53
|
example = TestClass.new
|
46
54
|
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require "rails_column_enumerator/version"
|
2
2
|
|
3
3
|
module RailsColumnEnumerator
|
4
|
-
def self.get_int(val, enum_values
|
4
|
+
def self.get_int(val, enum_values, valid_values = nil)
|
5
5
|
if val.is_a?(String) || val.is_a?(Symbol)
|
6
6
|
val = enum_values[val.to_s.downcase]
|
7
7
|
end
|
@@ -9,9 +9,11 @@ module RailsColumnEnumerator
|
|
9
9
|
val
|
10
10
|
end
|
11
11
|
|
12
|
-
def self.get_record(val, enum_values
|
12
|
+
def self.get_record(val, enum_values, valid_values, localization_values, helper_name)
|
13
13
|
val = get_int(val, enum_values, valid_values)
|
14
|
-
|
14
|
+
key = valid_values[val]
|
15
|
+
text = localization_values[helper_name].try(:[], key) || localization_values[:default].try(:[], key) || valid_values[val].to_s.titleize
|
16
|
+
[text, val]
|
15
17
|
end
|
16
18
|
|
17
19
|
def enumerated_column(column_name, enumerated_values, options = {})
|
@@ -21,10 +23,12 @@ module RailsColumnEnumerator
|
|
21
23
|
self.const_set(enum_name, enumerated_values.with_indifferent_access)
|
22
24
|
self.const_set(enum_symbols_name, enumerated_values.invert)
|
23
25
|
|
26
|
+
localization_options = options.delete(:localizations) || {}
|
27
|
+
|
24
28
|
extra_select_options = options.delete(:selections)
|
25
29
|
if extra_select_options
|
26
30
|
extra_select_options.each do |name, options|
|
27
|
-
self.const_set(name.to_s.upcase, options.map{ |key| RailsColumnEnumerator.get_record(key, self.const_get(enum_name), self.const_get(enum_symbols_name)) })
|
31
|
+
self.const_set(name.to_s.upcase, options.map{ |key| RailsColumnEnumerator.get_record(key, self.const_get(enum_name), self.const_get(enum_symbols_name), localization_options, name) })
|
28
32
|
end
|
29
33
|
end
|
30
34
|
|
@@ -33,7 +37,7 @@ module RailsColumnEnumerator
|
|
33
37
|
end
|
34
38
|
|
35
39
|
self.send(:define_method, column_name.to_s + '=') do |val|
|
36
|
-
write_attribute(column_name, RailsColumnEnumerator.get_int(val, self.class.const_get(enum_name)
|
40
|
+
write_attribute(column_name, RailsColumnEnumerator.get_int(val, self.class.const_get(enum_name)))
|
37
41
|
end
|
38
42
|
|
39
43
|
enumerator_associations = options.delete(:scopes)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_column_enumerator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brian O'Reilly
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-02-
|
11
|
+
date: 2016-02-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|