rails_column_enumerator 1.3.1 → 1.4.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: cdd8e9b9674ef635e4ca3c28f0e1ce612df79377
4
- data.tar.gz: 115862de8b7427aa5ccd7b3a4afc95c51d9aded7
3
+ metadata.gz: 098d4d6e8f67be3337091d42db922c36434801a7
4
+ data.tar.gz: e7ae2497bcaea1b2f6dbba4cf7cae77f9a514efc
5
5
  SHA512:
6
- metadata.gz: d68e201a398535a731961df29626491d1410849f2dd8ff7806aa84c809c1b409b9ac5897e9469d00efc8172e23beb729fef0b28332a41de25a7ab0f562176bda
7
- data.tar.gz: 7c0a57d2c988d76fcbe852e4565ab0dcc46ffc66942eaa149b7dd62e50efcc2672b8d7c55c5bf3ad34484e631b41c82e841264a07a3a0369c44cc693753e1b70
6
+ metadata.gz: adc5255d57c394d62e6610785cfbbb563b11cc5d79fd51d5076eb2d7a740f9805671cfa6bd48b6988d62a9786cff2f2cb556e96d654a04fc97c49ce730e9ec09
7
+ data.tar.gz: 921fefb082d56243cc077694c44003f89729193221e4f64c7bfa4583d7d0a59effb913c0f49dffc5b6476e0759ae637453b16a567136f35e9a3901aaa735c374
data/README.md CHANGED
@@ -38,8 +38,8 @@ TestClass::STATE_NAMES # { 1 => :open, 2 => :closed, 3 => :failed }
38
38
 
39
39
  # You can define options for select helpers using the 'selections' option
40
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]]
41
+ TestClass.not_failed # [["Open", 1], ["Closed", 2]]
42
+ TestClass.not_open # [["Closed", 2], ["Failed", 3]]
43
43
 
44
44
  # You can also add i18n localizations for these select helper outputs.
45
45
  # The gem matches values in the helpers with i18n entries under .models.class_name.column_name.scope_name(or default).key
@@ -69,7 +69,13 @@ example.state # 4
69
69
 
70
70
  example.state.save! # raises "ActiveRecord::RecordInvalid, 'Validation failed: State is not included in the list'"
71
71
 
72
- # 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
72
+ # You can check if an instance of your class has its enumerated column set to a specific value.
73
+ example.state = 2
74
+
75
+ example.open? # false
76
+ example.closed? # true
77
+
78
+ # 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.
73
79
 
74
80
  TestClass.not_failed # SELECT `test_class`.* FROM `test_class` WHERE `state` IN (1, 2);
75
81
  TestClass.not_open # SELECT `test_class`.* FROM `test_class` WHERE `state` IN (2, 3);
@@ -80,7 +86,7 @@ TestClass.not_open # SELECT `test_class`.* FROM `test_class` WHERE `state` IN (2
80
86
  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."
81
87
  enumerated_column :state, { open: 1, closed: 2, failed: 3 }, scopes: { not_open: [] } # Raises RuntimeError "You must provide some values for the scope."
82
88
 
83
- # 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
89
+ # 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.
84
90
 
85
91
  example.not_failed_include?(1) #true
86
92
  example.not_failed_include?(:open) #true
@@ -1,3 +1,3 @@
1
1
  module RailsColumnEnumerator
2
- VERSION = "1.3.1"
3
- end
2
+ VERSION = "1.4.0"
3
+ end
@@ -12,7 +12,7 @@ module RailsColumnEnumerator
12
12
  def self.get_record(val, enum_values, valid_values, helper_name, column_name, class_name)
13
13
  val = get_int(val, enum_values, valid_values)
14
14
  key = valid_values[val]
15
- text = I18n.t(".models.#{class_name}.#{column_name}.#{helper_name}.#{key}", :default => I18n.t(".models.#{class_name}.#{column_name}.default.#{key}", :default => valid_values[val].to_s.titleize))
15
+ text = I18n.t(".models.#{class_name}.#{column_name}.#{helper_name}.#{key}", default: I18n.t(".models.#{class_name}.#{column_name}.default.#{key}", default: valid_values[val].to_s.titleize))
16
16
  [text, val]
17
17
  end
18
18
 
@@ -38,6 +38,12 @@ module RailsColumnEnumerator
38
38
  write_attribute(column_name, RailsColumnEnumerator.get_int(val, self.class.const_get(enum_name)))
39
39
  end
40
40
 
41
+ enumerated_values.keys.each do |key|
42
+ self.send(:define_method, key.to_s + '?') do
43
+ key.to_sym == self.send(column_name, true)
44
+ end
45
+ end
46
+
41
47
  enumerator_associations = options.delete(:scopes)
42
48
  if enumerator_associations
43
49
  enumerator_associations.each do |key,values|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_column_enumerator
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian O'Reilly