rails_column_enumerator 1.1.0 → 1.2.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: 39c202ca855e2b163e88de24c8d9bc756b12a465
4
- data.tar.gz: f1d96c5f3f5fda7107958ef157fa55f103394244
3
+ metadata.gz: 98a59ee2236f4261819d1a068760b68bd15558f0
4
+ data.tar.gz: bae5a8c5661f83e8044d232422e37a11dba7ea83
5
5
  SHA512:
6
- metadata.gz: b783c4d7ebea0659a40a7267700e06547b52c182ddf93d7b5e919d64d80c9641d3636ba41ca109bb10ffb23017b89287ebb9c69265d501e71813bb776e14a9af
7
- data.tar.gz: 7b15862ae3199453d4c0b775dae9da7cdf623c0c6f45e314bb92383c716c73e914956cf98bd06ffe95ce9e14a413f0c00ccd280a80a708fb28e2c5358b8141c8
6
+ metadata.gz: 8c08da24bd22fcac1e6aed3bdbcf06cecb5abbcb64941c533cdb460f2a94d6688e6f6ecfef8065ddac5e0f825339497c79f4c4f2d7c5b74f1d0bbdae3d907cbc
7
+ data.tar.gz: aa49cce48f9267de94c868e846dc5adeb314763708b7c8e4719f8186bc6130388ffaa6b9d9228743d21268f4170d124c4926418a24b44eda78ee925f5d512a4a
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rails_column_enumerator (1.1.0)
4
+ rails_column_enumerator (1.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -36,6 +36,11 @@ end
36
36
  TestClass::STATES # { "open" => 1, "closed" => 2, "failed" => 3 }
37
37
  TestClass::STATE_NAMES # { 1 => :open, 2 => :closed, 3 => :failed }
38
38
 
39
+ # You can define options for select helpers using the 'selections' option
40
+ enumerated_column :state, { open: 1, closed: 2, failed: 3 }, selections: { not_failed: [1, 2], not_open: [:closed, :failed] }
41
+ TestClass::NOT_FAILED # [["Open", 1], ["Closed", 2]]
42
+ TestClass::NOT_OPEN # [["Closed", 2], ["Failed", 3]]
43
+
39
44
  # You can use your enumerated attribute as follows
40
45
  example = TestClass.new
41
46
 
@@ -1,3 +1,3 @@
1
1
  module RailsColumnEnumerator
2
- VERSION = "1.1.0"
3
- end
2
+ VERSION = "1.2.0"
3
+ end
@@ -9,6 +9,11 @@ module RailsColumnEnumerator
9
9
  val
10
10
  end
11
11
 
12
+ def self.get_record(val, enum_values = nil, valid_values = nil)
13
+ val = get_int(val, enum_values, valid_values)
14
+ [valid_values[val].to_s.titleize, val]
15
+ end
16
+
12
17
  def enumerated_column(column_name, enumerated_values, options = {})
13
18
  enum_name = column_name.to_s.pluralize.upcase.to_sym
14
19
  enum_symbols_name = (column_name.to_s.upcase + '_NAMES').to_sym
@@ -16,6 +21,13 @@ module RailsColumnEnumerator
16
21
  self.const_set(enum_name, enumerated_values.with_indifferent_access)
17
22
  self.const_set(enum_symbols_name, enumerated_values.invert)
18
23
 
24
+ extra_select_options = options.delete(:selections)
25
+ if extra_select_options
26
+ 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)) })
28
+ end
29
+ end
30
+
19
31
  self.send(:define_method, column_name) do |return_symbol = false|
20
32
  return_symbol ? self.class.const_get(enum_symbols_name)[read_attribute(column_name)].to_sym : read_attribute(column_name)
21
33
  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.1.0
4
+ version: 1.2.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-01 00:00:00.000000000 Z
11
+ date: 2016-02-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler