simple_enum 2.3.0 → 2.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3d9087e9c59c6e44c222f89f74e64ce59ddafa95
4
- data.tar.gz: c0c6198ccfb33ef5073e13d8658ddf56f2f2674d
3
+ metadata.gz: bc6b8efefb2a56dd5cca3e9f9751a2dc63dbf026
4
+ data.tar.gz: 973c2ac8568b97a5c384ef150cbd4721a42a606b
5
5
  SHA512:
6
- metadata.gz: 94d9588a9eaf13c492e0cafaca3b83e36692025c0c3e58cd23178dd0b130e364d959aceb3970292d64e38a48fd77390b12f15d704260819aa5a1c52ff2fb0602
7
- data.tar.gz: 00ac3629cd9e0fb9f64691070a5150adf82edafa5be16d05621857e18c1278a469d0c3f4fdf7ffd165bea338e39c9ac6caf17e6ca4f41e132deff48333fdf8e6
6
+ metadata.gz: 5f618673f56b4777f5c7320eb16276a54ecfcca341925ce21bca89d89f162ba331110b6f4df2669f6407ea448c8af7ab66baa80e6afc64cfdfa24d81009fc5dc
7
+ data.tar.gz: d5cbba0c10de4983a916282b803e11b6acdf6146fce6f2aa5e7f7f28640eda2cb3a48e18e5f8c0c248c88c5c2213ddc8a792497ee9a56f7d70f295796a407414
data/Gemfile CHANGED
@@ -1,5 +1,8 @@
1
1
  source "http://rubygems.org"
2
2
 
3
+ gem 'activesupport', '~> 5.0.0'
4
+ gem 'activerecord', '~> 5.0.0'
5
+
3
6
  gemspec
4
7
 
5
8
  # some development deps
data/README.md CHANGED
@@ -2,7 +2,7 @@ SimpleEnum
2
2
  ==========
3
3
 
4
4
  [![Build Status](https://travis-ci.org/lwe/simple_enum.svg)](https://travis-ci.org/lwe/simple_enum)
5
- [![Code Climate](https://codeclimate.com/github/lwe/simple_enum.png)](https://codeclimate.com/github/lwe/simple_enum)
5
+ [![Code Climate](https://codeclimate.com/github/lwe/simple_enum.svg)](https://codeclimate.com/github/lwe/simple_enum)
6
6
 
7
7
  Unobtrusive enum-like fields for ActiveRecord and Ruby, brings enums functionality
8
8
  to ActiveRecord and Mongoid models (built for Rails 4+).
@@ -50,7 +50,7 @@ to use simple_enum with another version of mongoid, use version 1.6 instead.
50
50
  Load mongoid support in the `Gemfile`:
51
51
 
52
52
  ```ruby
53
- gem 'simple_enum', '~> 2.0.0' , require: 'simple_enum/mongoid'
53
+ gem 'simple_enum', '~> 2.3.0' , require: 'simple_enum/mongoid'
54
54
  ```
55
55
 
56
56
  Add this to a model:
@@ -98,10 +98,40 @@ Accessing actual enum values is possible at the class level:
98
98
  ```ruby
99
99
  User.genders # => #<SimpleEnum::Enum:0x0....>
100
100
  User.genders[:male] # => 0
101
- User.genders.values_at(:male, :female) # => [0, 1]
101
+ User.genders.values_at(:male, :female) # => [0, 1] (since 2.1.0)
102
102
  User.females # => #<ActiveRecord::Relation:0x0.....> (WHERE gender_cd = 1)
103
103
  ```
104
104
 
105
+ By default, scope names are generated as pluralized forms of the defined enum values e.g.
106
+
107
+ ```ruby
108
+ class Booking < ActiveRecord::Base
109
+ as_enum :status, active: 1, cancelled: 2, pending: 3
110
+ end
111
+ ```
112
+
113
+ would generate the following:
114
+ ```ruby
115
+ Booking.actives # => #<ActiveRecord::Relation:0x0.....> (WHERE status_cd = 1)
116
+ Booking.cancelleds # => #<ActiveRecord::Relation:0x0.....> (WHERE status_cd = 2)
117
+ Booking.pendings # => #<ActiveRecord::Relation:0x0.....> (WHERE status_cd = 3)
118
+ ```
119
+
120
+ By setting `pluralize_scopes: false` will not generate pluralized versions of scopes e.g.
121
+
122
+ ```ruby
123
+ class Booking < ActiveRecord::Base
124
+ as_enum :status, active: 1, cancelled: 2, pending: 3, pluralize_scopes: false
125
+ end
126
+ ```
127
+
128
+ would generate the following:
129
+ ```ruby
130
+ Booking.active # => #<ActiveRecord::Relation:0x0.....> (WHERE status_cd = 1)
131
+ Booking.cancelled # => #<ActiveRecord::Relation:0x0.....> (WHERE status_cd = 2)
132
+ Booking.pending # => #<ActiveRecord::Relation:0x0.....> (WHERE status_cd = 3)
133
+ ```
134
+
105
135
  ### Wait, there's more!
106
136
 
107
137
  - Too tired of always adding the integer values? Try:
@@ -223,9 +253,9 @@ so expect them to change in future versions of SimpleEnum.
223
253
  translate_enum user, :gender # => "Frau" # assuming :de and translations exist
224
254
  te user, :gender # translate_enum is also aliased to te
225
255
  ```
226
-
256
+
227
257
  Provide translations in the i18n yaml file like:
228
-
258
+
229
259
  ```ruby
230
260
  de:
231
261
  enums:
@@ -233,13 +263,13 @@ so expect them to change in future versions of SimpleEnum.
233
263
  female: 'Frau'
234
264
  male: 'Mann'
235
265
  ```
236
-
266
+
237
267
  - Build a select tag with a translated dropdown and symbol as value:
238
268
 
239
269
  ```ruby
240
270
  select :user, :gender, enum_option_pairs(User, :gender)
241
271
  ```
242
-
272
+
243
273
  - ...and one with the index as value:
244
274
 
245
275
  ```ruby
@@ -255,6 +285,8 @@ the following extensions can be used:
255
285
  [simple_enum-multiple](https://github.com/bbtfr/simple_enum-multiple)
256
286
  - **Persistence values**, i.e. store values in the DB:
257
287
  [simple_enum-persistence](https://github.com/bbtfr/simple_enum-persistence)
288
+ - **Scopes**, scopes helper for ActiveRecord enum attributes:
289
+ [simple_enum-scopes](https://github.com/aovertus/simple_enum-scopes)
258
290
 
259
291
  ## Best practices
260
292
 
@@ -33,6 +33,9 @@ module SimpleEnum
33
33
  mattr_accessor :field
34
34
  @@field = {}
35
35
 
36
+ mattr_accessor :pluralize_scopes
37
+ @@pluralize_scopes = true
38
+
36
39
  def self.configure
37
40
  yield(self)
38
41
  end
@@ -17,7 +17,7 @@ module SimpleEnum
17
17
  EXTENSIONS = []
18
18
 
19
19
  def as_enum(name, values, options = {})
20
- options.assert_valid_keys(:source, :prefix, :with, :accessor, :map)
20
+ options.assert_valid_keys(:source, :prefix, :with, :accessor, :map, :pluralize_scopes)
21
21
 
22
22
  hash = SimpleEnum::Hasher.map(values, options)
23
23
  enum = SimpleEnum::Enum.new(name, hash)
@@ -25,10 +25,7 @@ module SimpleEnum
25
25
 
26
26
  generate_enum_class_accessors_for(enum, accessor)
27
27
  generate_enum_instance_accessors_for(enum, accessor)
28
-
29
- Array.wrap(options.fetch(:with, SimpleEnum.with)).each do |feature|
30
- send "generate_enum_#{feature}_methods_for", enum, accessor
31
- end
28
+ generate_additional_enum_methods_for(enum, accessor, options)
32
29
 
33
30
  EXTENSIONS.uniq.each do |extension|
34
31
  send "generate_enum_#{extension}_extension_for", enum, accessor
@@ -57,6 +54,20 @@ module SimpleEnum
57
54
  end
58
55
  end
59
56
 
57
+ def generate_additional_enum_methods_for(enum, accessor, options)
58
+ with_options = Array.wrap(options.fetch(:with, SimpleEnum.with))
59
+ scope_option = with_options.delete(:scope)
60
+
61
+ with_options.each do |feature|
62
+ send "generate_enum_#{feature}_methods_for", enum, accessor
63
+ end
64
+
65
+ if scope_option
66
+ pluralize_scopes = options.fetch(:pluralize_scopes, SimpleEnum.pluralize_scopes)
67
+ generate_enum_scope_methods_for(enum, accessor, pluralize_scopes)
68
+ end
69
+ end
70
+
60
71
  def generate_enum_dirty_methods_for(enum, accessor)
61
72
  simple_enum_module.module_eval do
62
73
  define_method("#{accessor}_changed?") { accessor.changed?(self) }
@@ -73,11 +84,12 @@ module SimpleEnum
73
84
  end
74
85
  end
75
86
 
76
- def generate_enum_scope_methods_for(enum, accessor)
87
+ def generate_enum_scope_methods_for(enum, accessor, pluralize_scopes)
77
88
  return unless respond_to?(:scope)
78
89
 
79
90
  enum.each_pair do |key, value|
80
- scope "#{accessor.prefix}#{key.pluralize}", -> { accessor.scope(self, value) }
91
+ scope_key = pluralize_scopes ? key.pluralize : key
92
+ scope "#{accessor.prefix}#{scope_key}", -> { accessor.scope(self, value) }
81
93
  end
82
94
  end
83
95
  end
@@ -1,5 +1,5 @@
1
1
  module SimpleEnum
2
2
 
3
3
  # The current `SimpleEnum` version.
4
- VERSION = "2.3.0"
4
+ VERSION = "2.3.1"
5
5
  end
@@ -223,6 +223,38 @@ describe SimpleEnum::Attribute do
223
223
  subject { klass.gender_females }
224
224
  it_behaves_like 'delegates to accessor#scope', 1
225
225
  end
226
+
227
+ context 'and pluralize scopes option is set to false' do
228
+ fake_active_record(:klass) {
229
+ as_enum :gender, [:male, :female], with: [:scope], prefix: true, pluralize_scopes: false
230
+ }
231
+
232
+ context '.gender_male' do
233
+ subject { klass.gender_male }
234
+ it_behaves_like 'delegates to accessor#scope', 0
235
+ end
236
+
237
+ context '.gender_female' do
238
+ subject { klass.gender_female }
239
+ it_behaves_like 'delegates to accessor#scope', 1
240
+ end
241
+ end
242
+ end
243
+
244
+ context 'when pluralize scopes option is set to false' do
245
+ fake_active_record(:klass) {
246
+ as_enum :gender, [:male, :female], with: [:scope], pluralize_scopes: false
247
+ }
248
+
249
+ context '.male' do
250
+ subject { klass.male }
251
+ it_behaves_like 'delegates to accessor#scope', 0
252
+ end
253
+
254
+ context '.female' do
255
+ subject { klass.female }
256
+ it_behaves_like 'delegates to accessor#scope', 1
257
+ end
226
258
  end
227
259
 
228
260
  context 'without scope method' do
@@ -6,8 +6,8 @@ require 'active_record'
6
6
  require 'mongoid'
7
7
 
8
8
  if ENV['CODECLIMATE_REPO_TOKEN']
9
- require "codeclimate-test-reporter"
10
- CodeClimate::TestReporter.start
9
+ require 'simplecov'
10
+ SimpleCov.start
11
11
  end
12
12
 
13
13
  require 'simple_enum'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.0
4
+ version: 2.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lukas Westermann
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-24 00:00:00.000000000 Z
11
+ date: 2018-03-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -138,7 +138,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
138
138
  version: 2.0.0
139
139
  requirements: []
140
140
  rubyforge_project:
141
- rubygems_version: 2.2.2
141
+ rubygems_version: 2.6.13
142
142
  signing_key:
143
143
  specification_version: 4
144
144
  summary: Simple enum-like field support for models.