rails_column_enumerator 1.0.2 → 1.1.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: c6142a21527dc7356777b36c1d29f0ecb092cbf7
4
- data.tar.gz: 5f5af75ce9697d802da55f2afd630c79b2ff2130
3
+ metadata.gz: 39c202ca855e2b163e88de24c8d9bc756b12a465
4
+ data.tar.gz: f1d96c5f3f5fda7107958ef157fa55f103394244
5
5
  SHA512:
6
- metadata.gz: f4122a21688fb8d6d72f862b1e9a1855a3d1f8877a7087d3df00a26e4c04f6edadd14c93e672fb6a5c69ad1ca9d731e90077cd1c8419517a549428fa46812635
7
- data.tar.gz: 983bf0c595fc9bcd4e182858a459296a6327d01625f97aa127f6139591f98e68874e09547125a3c0aadf489257cea103b84691ecc3a4ef958021f11152fb2630
6
+ metadata.gz: b783c4d7ebea0659a40a7267700e06547b52c182ddf93d7b5e919d64d80c9641d3636ba41ca109bb10ffb23017b89287ebb9c69265d501e71813bb776e14a9af
7
+ data.tar.gz: 7b15862ae3199453d4c0b775dae9da7cdf623c0c6f45e314bb92383c716c73e914956cf98bd06ffe95ce9e14a413f0c00ccd280a80a708fb28e2c5358b8141c8
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rails_column_enumerator (1.0.0)
4
+ rails_column_enumerator (1.1.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -23,9 +23,9 @@ GEM
23
23
  builder (3.2.2)
24
24
  i18n (0.7.0)
25
25
  json (1.8.3)
26
- minitest (5.8.3)
26
+ minitest (5.8.4)
27
27
  mysql2 (0.3.18)
28
- rake (10.4.2)
28
+ rake (10.5.0)
29
29
  thread_safe (0.3.5)
30
30
  tzinfo (1.2.2)
31
31
  thread_safe (~> 0.1)
data/README.md CHANGED
@@ -20,20 +20,20 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- Add ```extends RailsColumnEnumerator``` to the top of whatever class you want to add an enumerated column to.
23
+ Add ```extend RailsColumnEnumerator``` to the top of whatever class you want to add an enumerated column to.
24
24
 
25
25
  Then define the attribute with ```enumerated_column``` method.
26
26
 
27
27
  example:
28
28
  ```ruby
29
29
  class TestClass < ActiveRecord::Base
30
- extends RailsColumnEnumerator
30
+ extend RailsColumnEnumerator
31
31
 
32
- enumerated_column :state, { open: 1, closed: 2, failed: 3 }
32
+ enumerated_column :state, { open: 1, closed: 2, failed: 3 }, scopes: { not_failed: [1, 2], not_open: [:closed, :failed] }
33
33
  end
34
34
 
35
35
  # The following two consts would be added to your class (Their name is derived from the attribute name you pass into 'enumerated_column')
36
- TestClass::STATES # { "open" => 1, "closed" => 2, "failed" => 3}
36
+ TestClass::STATES # { "open" => 1, "closed" => 2, "failed" => 3 }
37
37
  TestClass::STATE_NAMES # { 1 => :open, 2 => :closed, 3 => :failed }
38
38
 
39
39
  # You can use your enumerated attribute as follows
@@ -59,7 +59,26 @@ example.state # nil
59
59
  example.state = 4
60
60
  example.state # 4
61
61
 
62
- example.state.save! # raises "ActiveRecord::RecordInvalid, 'Validation failed: Sample column is not included in the list'"
62
+ example.state.save! # raises "ActiveRecord::RecordInvalid, 'Validation failed: State is not included in the list'"
63
+
64
+ # You can add scopes on your enumerated column by passing in the scope name and an array containing values that should be included in your scope
65
+
66
+ TestClass.not_failed # SELECT `test_class`.* FROM `test_class` WHERE `state` IN (1, 2);
67
+ TestClass.not_open # SELECT `test_class`.* FROM `test_class` WHERE `state` IN (2, 3);
68
+
69
+ # Values can be added to scopes as either their integer value or their key.
70
+ # If the values you enter are not valid values for your enumerator, or if you don't provide any, it will raise an error.
71
+
72
+ enumerated_column :state, { open: 1, closed: 2, failed: 3 }, scopes: { not_open: [1, 2, 5], finished: [:closed, :failed, :pending] } # Raises RuntimeError "Values in options must be valid for enumerated column."
73
+ enumerated_column :state, { open: 1, closed: 2, failed: 3 }, scopes: { not_open: [] } # Raises RuntimeError "You must provide some values for the scope."
74
+
75
+ # When you create a scope you also get an 'include?' method using that scope's name that allows you to check if a value is part of that scope
76
+
77
+ example.not_failed_include?(1) #true
78
+ example.not_failed_include?(:open) #true
79
+
80
+ example.not_failed_include?(3) #false
81
+ example.not_failed_include?(:failed) #false
63
82
  ```
64
83
 
65
84
  ## Development
@@ -1,3 +1,3 @@
1
1
  module RailsColumnEnumerator
2
- VERSION = "1.0.2"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -1,6 +1,14 @@
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)
5
+ if val.is_a?(String) || val.is_a?(Symbol)
6
+ val = enum_values[val.to_s.downcase]
7
+ end
8
+ raise "Values in options must be valid for enumerated column." if valid_values && valid_values[val].blank?
9
+ val
10
+ end
11
+
4
12
  def enumerated_column(column_name, enumerated_values, options = {})
5
13
  enum_name = column_name.to_s.pluralize.upcase.to_sym
6
14
  enum_symbols_name = (column_name.to_s.upcase + '_NAMES').to_sym
@@ -12,12 +20,21 @@ module RailsColumnEnumerator
12
20
  return_symbol ? self.class.const_get(enum_symbols_name)[read_attribute(column_name)].to_sym : read_attribute(column_name)
13
21
  end
14
22
 
15
- self.send(:define_method, (column_name.to_s + '=').to_sym)do |val|
16
- if val.is_a?(String) || val.is_a?(Symbol)
17
- val = val.to_s.downcase
18
- write_attribute(column_name, self.class.const_get(enum_name)[val])
19
- else
20
- write_attribute(column_name, val)
23
+ self.send(:define_method, column_name.to_s + '=') do |val|
24
+ write_attribute(column_name, RailsColumnEnumerator.get_int(val, self.class.const_get(enum_name), self.class.const_get(enum_symbols_name)))
25
+ end
26
+
27
+ enumerator_associations = options.delete(:scopes)
28
+ if enumerator_associations
29
+ enumerator_associations.each do |key,values|
30
+ found_values = values.map { |val| RailsColumnEnumerator.get_int(val, self.const_get(enum_name), self.const_get(enum_symbols_name)) }
31
+ raise "You must provide some values for the scope." if found_values.blank?
32
+ self.define_singleton_method(key) do
33
+ self.where(column_name => found_values)
34
+ end
35
+ self.send(:define_method, key.to_s + "_include?") do |val|
36
+ found_values.include?(RailsColumnEnumerator.get_int(val, self.class.const_get(enum_name)))
37
+ end
21
38
  end
22
39
  end
23
40
 
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.0.2
4
+ version: 1.1.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: 2015-11-19 00:00:00.000000000 Z
11
+ date: 2016-02-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler