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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 98a59ee2236f4261819d1a068760b68bd15558f0
4
- data.tar.gz: bae5a8c5661f83e8044d232422e37a11dba7ea83
3
+ metadata.gz: cab423e5aafb991c2d0078aba5a69d519c8a3455
4
+ data.tar.gz: 7778867ce83935b8cdb2984dbc8f935f19a5f34d
5
5
  SHA512:
6
- metadata.gz: 8c08da24bd22fcac1e6aed3bdbcf06cecb5abbcb64941c533cdb460f2a94d6688e6f6ecfef8065ddac5e0f825339497c79f4c4f2d7c5b74f1d0bbdae3d907cbc
7
- data.tar.gz: aa49cce48f9267de94c868e846dc5adeb314763708b7c8e4719f8186bc6130388ffaa6b9d9228743d21268f4170d124c4926418a24b44eda78ee925f5d512a4a
6
+ metadata.gz: 447ca04146e24e7221a0672a1fcb94991b9187d8f0055778d14dde68efbfe8e91ea41818031e4e22680593c35f543b4ea80ad56c84b4ea18f363bf887c46fece
7
+ data.tar.gz: 0e58eabc5520c44cee57c4ff442ea6b6cf3ef8d7b7e612c22f917d3b9b90d585cf80cb2fc9422d462c170d15a214d856ed9a8b6924abbcfd6a73ad1ce2e24428
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rails_column_enumerator (1.2.0)
4
+ rails_column_enumerator (1.3.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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 = nil, valid_values = nil)
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 = nil, valid_values = nil)
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
- [valid_values[val].to_s.titleize, val]
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), self.class.const_get(enum_symbols_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)
@@ -1,3 +1,3 @@
1
1
  module RailsColumnEnumerator
2
- VERSION = "1.2.0"
2
+ VERSION = "1.3.0"
3
3
  end
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.2.0
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-02 00:00:00.000000000 Z
11
+ date: 2016-02-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler