simple_enum 2.0.0.rc2 → 2.0.0.rc3

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: 1f8cd2d424c3387cd12f6c4a4e22873641f8fc1b
4
- data.tar.gz: 0d1728690e77c485e5dc1770644248eda9e2ba50
3
+ metadata.gz: 22d1d0e212642ba9b9a282e1c4911521535f8ac9
4
+ data.tar.gz: ecad12d55b79a775755f98c4e814e2d2919b3fa5
5
5
  SHA512:
6
- metadata.gz: 916b55125dbb05aee70240b765486b4410633aca3649552d3bbc53ab3c28bacf574de4b653bb52a4e0160649bcd0b3edbada87ce4fc2c0c237b0d7f7a7ff581d
7
- data.tar.gz: 300f0e6a4916004ea5970b2716d46244d40c83c59ebe63851047cdc04b11137abe7e5b936d5a7ae71f6754fb1517c2888abe1260f5ff85de41208211189dcf73
6
+ metadata.gz: 22eee27ae766df5adcf4e6604a56db93673c56b2b7338c3dd4dc12bf22931a1e57872f25323cab5a5c44483ae61e9ab42912d770ece3c3fbf8a08d66c825df5e
7
+ data.tar.gz: 4299c17b1f7f1930040cdb7d6f2351de7854a4ca5963708b5302937699507a81f64c86387c636e003905bee0a8ff0bb8a52d5f0b43fbc5689e977399b8ff7d01
data/Gemfile CHANGED
@@ -2,13 +2,10 @@ source "http://rubygems.org"
2
2
 
3
3
  gemspec
4
4
 
5
- # Fix at 1.9.x, because 1.10 errors on jruby
6
- gem 'mongo', '~> 1.9.0', :platform => :jruby
7
-
8
5
  # some development deps
9
- gem 'activerecord-jdbcsqlite3-adapter', :platform => :jruby
10
- gem 'sqlite3', :platform => :ruby
11
- gem 'bson_ext', :platform => :ruby
6
+ gem 'activerecord-jdbcsqlite3-adapter', platform: :jruby
7
+ gem 'sqlite3', platform: :ruby
8
+ gem 'bson_ext', platform: :ruby
12
9
 
13
10
  # Code coverage on CI only
14
- gem "codeclimate-test-reporter", group: :test, require: nil
11
+ gem 'codeclimate-test-reporter', group: :test, require: nil
data/README.md CHANGED
@@ -98,7 +98,7 @@ 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.female # => #<ActiveRecord::Relation:0x0.....> (WHERE gender_cd = 1)
101
+ User.females # => #<ActiveRecord::Relation:0x0.....> (WHERE gender_cd = 1)
102
102
  ```
103
103
 
104
104
  ### Wait, there's more!
@@ -167,15 +167,15 @@ User.female # => #<ActiveRecord::Relation:0x0.....> (WHERE gender_cd
167
167
  If an invalid value is assigned, the gender is set to `nil` by default.
168
168
  - If the shortcut methods (like `female?`, `female!` or `User.male`) conflict with something in your class, it's possible to
169
169
  define a prefix:
170
+ ```ruby
171
+ class User < ActiveRecord::Base
172
+ as_enum :gender, %w{male female}, prefix: true
173
+ end
170
174
 
171
- class User < ActiveRecord::Base
172
- as_enum :gender, %w{male female}, prefix: true
173
- end
174
-
175
- jane = User.new gender: :female
176
- jane.gender_female? # => true
177
- User.gender_female # => <ActiveRecord::Relation...WHERE gender_cd = 1.>
178
-
175
+ jane = User.new gender: :female
176
+ jane.gender_female? # => true
177
+ User.gender_females # => <ActiveRecord::Relation...WHERE gender_cd = 1.>
178
+ ```
179
179
  The `:prefix` option not only takes a boolean value as an argument, but instead can also be supplied a custom
180
180
  prefix, so with `prefix: 'foo'` all shortcut methods would look like: `foo_<symbol>`
181
181
  - To define which methods are generated it's possible to set `with:` option, by
@@ -225,7 +225,7 @@ as_enum :handle, [:new, :create, :update], :prefix => true
225
225
  Searching for certain values by using the finder methods:
226
226
 
227
227
  ```ruby
228
- User.female # => returns an ActiveRecord::Relation
228
+ User.females # => returns an ActiveRecord::Relation
229
229
  ```
230
230
 
231
231
  Contributors
@@ -46,6 +46,7 @@ module SimpleEnum
46
46
  simple_enum_module.module_eval do
47
47
  define_method("#{accessor}") { accessor.read(self) }
48
48
  define_method("#{accessor}=") { |value| accessor.write(self, value) }
49
+ define_method("#{accessor}?") { |value = nil| accessor.selected?(self, value) }
49
50
  end
50
51
  end
51
52
 
@@ -58,7 +59,6 @@ module SimpleEnum
58
59
 
59
60
  def generate_enum_attribute_methods_for(enum, accessor)
60
61
  simple_enum_module.module_eval do
61
- define_method("#{accessor}?") { |value = nil| accessor.selected?(self, value) }
62
62
  enum.each_pair do |key, value|
63
63
  define_method("#{accessor.prefix}#{key}?") { accessor.selected?(self, key) }
64
64
  define_method("#{accessor.prefix}#{key}!") { accessor.write(self, key) }
@@ -70,7 +70,7 @@ module SimpleEnum
70
70
  return unless respond_to?(:scope)
71
71
 
72
72
  enum.each_pair do |key, value|
73
- scope "#{accessor.prefix}#{key}", -> { where(accessor.source => value) }
73
+ scope "#{accessor.prefix}#{key.pluralize}", -> { where(accessor.source => value) }
74
74
  end
75
75
  end
76
76
  end
@@ -30,6 +30,10 @@ module SimpleEnum
30
30
  end
31
31
  alias_method :each, :each_pair
32
32
 
33
+ def map(&block)
34
+ hash.map(&block)
35
+ end
36
+
33
37
  def keys
34
38
  hash.keys
35
39
  end
@@ -15,12 +15,12 @@ module SimpleEnum
15
15
  #
16
16
  # When no field is requested:
17
17
  #
18
- # field :gender_cd, :type => Integer
19
- # as_enum :gender, [:female, :male], :field => false
18
+ # field :gender_cd, type: Integer
19
+ # as_enum :gender, [:female, :male], field: false
20
20
  #
21
21
  # or custom field options (like e.g. type want to be passed):
22
22
  #
23
- # as_enum :gender, [:female, :male], :field => { :type => Integer }
23
+ # as_enum :gender, [:female, :male], field: { type: Integer }
24
24
  #
25
25
  module Mongoid
26
26
  def self.included(base)
@@ -0,0 +1,17 @@
1
+ require 'simple_enum'
2
+
3
+ module SimpleEnum
4
+ class Railtie < Rails::Railtie
5
+
6
+ initializer 'simple_enum.extend_active_record' do
7
+ ActiveSupport.on_load(:active_record) do
8
+ ActiveRecord::Base.send :extend, SimpleEnum::Attribute
9
+ ActiveRecord::Base.send :extend, SimpleEnum::Translation
10
+ end
11
+ end
12
+
13
+ initializer 'simple_enum.view_helpers' do
14
+ ActionView::Base.send :include, SimpleEnum::ViewHelpers
15
+ end
16
+ end
17
+ end
@@ -1,3 +1,5 @@
1
+ require 'i18n'
2
+
1
3
  module SimpleEnum
2
4
  module Translation
3
5
  def human_enum_name(enum, key, options = {})
@@ -10,7 +12,7 @@ module SimpleEnum
10
12
  defaults << options.delete(:default) if options[:default]
11
13
  defaults << key.to_s.humanize
12
14
 
13
- options.reverse_merge! :count => 1, :default => defaults
15
+ options.reverse_merge! count: 1, default: defaults
14
16
  I18n.translate(defaults.shift, options)
15
17
  end
16
18
  end
@@ -1,5 +1,5 @@
1
1
  module SimpleEnum
2
2
 
3
3
  # The current `SimpleEnum` version.
4
- VERSION = "2.0.0.rc2"
4
+ VERSION = "2.0.0.rc3"
5
5
  end
@@ -0,0 +1,39 @@
1
+ require 'i18n'
2
+
3
+ module SimpleEnum
4
+ module ViewHelpers
5
+
6
+ # A helper to build forms with Rails' form builder, built to be used with
7
+ # f.select helper.
8
+ #
9
+ # f.select :gender, enum_option_pairs(User, :gender), ...
10
+ #
11
+ # record - The model or Class with the enum
12
+ # enum - The Symbol with the name of the enum to create the options for
13
+ # encode_as_value - The Boolean which defines if either the key or the value
14
+ # should be used as value attribute for the option,
15
+ # defaults to using the key (i.e. false)
16
+ #
17
+ # FIXME: check if the name `enum_option_pairs` is actually good and clear
18
+ # enough...
19
+ #
20
+ # Returns an Array of pairs, like e.g. `[["Translated key", "key"], ...]`
21
+ def enum_option_pairs(record, enum, encode_as_value = false)
22
+ reader = enum.to_s.pluralize
23
+ record = record.class unless record.respond_to?(reader)
24
+
25
+ record.send(reader).map { |key, value|
26
+ name = record.human_enum_name(enum, key) if record.respond_to?(:human_enum_name)
27
+ name ||= translate_enum_key(enum, key)
28
+ [name, encode_as_value ? value : key]
29
+ }
30
+ end
31
+
32
+ private
33
+
34
+ def translate_enum_key(enum, key)
35
+ defaults = [:"enums.#{enum}.#{key}", key.to_s.humanize]
36
+ I18n.translate defaults.shift, default: defaults, count: 1
37
+ end
38
+ end
39
+ end
data/lib/simple_enum.rb CHANGED
@@ -13,6 +13,7 @@ require 'active_support'
13
13
  require 'simple_enum/version'
14
14
  require 'simple_enum/attribute'
15
15
  require 'simple_enum/translation'
16
+ require 'simple_enum/view_helpers'
16
17
 
17
18
  # Base module which gets included in <tt>ActiveRecord::Base</tt>. See documentation
18
19
  # of +SimpleEnum::ClassMethods+ for more details.
@@ -37,8 +38,5 @@ module SimpleEnum
37
38
  end
38
39
  end
39
40
 
40
- # include in AR
41
- ActiveSupport.on_load(:active_record) do
42
- ActiveRecord::Base.send(:extend, SimpleEnum::Attribute)
43
- ActiveRecord::Base.send(:extend, SimpleEnum::Translation)
44
- end
41
+ # Load rails support
42
+ require 'simple_enum/railtie' if defined?(Rails)
@@ -44,6 +44,13 @@ describe SimpleEnum::Attribute do
44
44
  subject.gender = :male
45
45
  end
46
46
  end
47
+
48
+ context '#gender?' do
49
+ it 'delegates to accessor' do
50
+ expect(accessor).to receive(:selected?).with(subject, nil) { nil }
51
+ expect(subject.gender?).to be_false
52
+ end
53
+ end
47
54
  end
48
55
 
49
56
  context 'generate_enum_dirty_methods_for' do
@@ -76,10 +83,6 @@ describe SimpleEnum::Attribute do
76
83
  context 'generate_enum_attribute_methods_for' do
77
84
  subject { klass.new }
78
85
 
79
- it 'does not respond to #gender?' do
80
- expect(subject).to_not respond_to(:gender?)
81
- end
82
-
83
86
  it 'does not respond to #male? or #female?' do
84
87
  expect(subject).to_not respond_to(:male?)
85
88
  expect(subject).to_not respond_to(:female?)
@@ -167,13 +170,13 @@ describe SimpleEnum::Attribute do
167
170
  end
168
171
  end
169
172
 
170
- context '.male' do
171
- subject { klass.male }
173
+ context '.males' do
174
+ subject { klass.males }
172
175
  it_behaves_like 'returning a relation', 0
173
176
  end
174
177
 
175
- context '.female' do
176
- subject { klass.female }
178
+ context '.females' do
179
+ subject { klass.females }
177
180
  it_behaves_like 'returning a relation', 1
178
181
  end
179
182
 
@@ -182,13 +185,13 @@ describe SimpleEnum::Attribute do
182
185
  as_enum :gender, [:male, :female], with: [:scope], prefix: true
183
186
  }
184
187
 
185
- context '.gender_male' do
186
- subject { klass.gender_male }
188
+ context '.gender_males' do
189
+ subject { klass.gender_males }
187
190
  it_behaves_like 'returning a relation', 0
188
191
  end
189
192
 
190
- context '.gender_female' do
191
- subject { klass.gender_female }
193
+ context '.gender_females' do
194
+ subject { klass.gender_females }
192
195
  it_behaves_like 'returning a relation', 1
193
196
  end
194
197
  end
@@ -199,9 +202,9 @@ describe SimpleEnum::Attribute do
199
202
  }
200
203
  subject { klass_without_scope_method }
201
204
 
202
- it 'does not add .male nor .female' do
203
- expect(subject).to_not respond_to(:male)
204
- expect(subject).to_not respond_to(:female)
205
+ it 'does not add .males nor .females' do
206
+ expect(subject).to_not respond_to(:males)
207
+ expect(subject).to_not respond_to(:females)
205
208
  end
206
209
  end
207
210
  end
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+
3
+ describe SimpleEnum::ViewHelpers, i18n: true do
4
+ let(:helper) {
5
+ Class.new do
6
+ include SimpleEnum::ViewHelpers
7
+ end.new
8
+ }
9
+
10
+ fake_model(:klass) do
11
+ as_enum :gender, %w{male female}
12
+ end
13
+
14
+ context '#enum_option_pairs' do
15
+ subject { helper.enum_option_pairs(klass, :gender) }
16
+
17
+ it 'returns an Array of Arrays when a model instance is passed in' do
18
+ expect(helper.enum_option_pairs(klass.new, :gender)).to eq [
19
+ ["Male", "male"], ["Female", "female"]
20
+ ]
21
+ end
22
+
23
+ it 'returns an Array of Array when the class is passed in' do
24
+ expect(subject).to eq [
25
+ ["Male", "male"], ["Female", "female"]
26
+ ]
27
+ end
28
+
29
+ it 'returns the value instead of the key when last argument is set to true' do
30
+ expect(helper.enum_option_pairs(klass, :gender, true)).to eq [
31
+ ["Male", 0], ["Female", 1]
32
+ ]
33
+ end
34
+
35
+ context 'with translation in enums.{...}' do
36
+ before {
37
+ store_translations :en, 'enums' => {
38
+ 'gender' => { 'male' => 'Mr.', 'female' => 'Mrs.' }
39
+ }
40
+ }
41
+
42
+ it 'returns the translation as defined in the translations' do
43
+ expect(subject).to eq [
44
+ ["Mr.", "male"], ["Mrs.", "female"]
45
+ ]
46
+ end
47
+ end
48
+
49
+ context 'with .human_enum_name' do
50
+ before {
51
+ expect(klass).to receive(:human_enum_name).with(:gender, "male") { "Mr." }
52
+ expect(klass).to receive(:human_enum_name).with(:gender, "female") { "Mrs." }
53
+ }
54
+
55
+ it 'returns the translation as given #human_enum_name' do
56
+ expect(subject).to eq [
57
+ ["Mr.", "male"], ["Mrs.", "female"]
58
+ ]
59
+ end
60
+ end
61
+ end
62
+ end
@@ -21,3 +21,7 @@ module ActiveRecordSupport
21
21
  end
22
22
  end
23
23
  end
24
+
25
+ # Behave like the railtie.rb
26
+ ActiveRecord::Base.send :extend, SimpleEnum::Attribute
27
+ ActiveRecord::Base.send :extend, SimpleEnum::Translation
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.0.0.rc2
4
+ version: 2.0.0.rc3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lukas Westermann
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-08 00:00:00.000000000 Z
11
+ date: 2014-06-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -101,8 +101,10 @@ files:
101
101
  - lib/simple_enum/enum.rb
102
102
  - lib/simple_enum/hasher.rb
103
103
  - lib/simple_enum/mongoid.rb
104
+ - lib/simple_enum/railtie.rb
104
105
  - lib/simple_enum/translation.rb
105
106
  - lib/simple_enum/version.rb
107
+ - lib/simple_enum/view_helpers.rb
106
108
  - simple_enum.gemspec
107
109
  - spec/simple_enum/accessors_spec.rb
108
110
  - spec/simple_enum/attribute_spec.rb
@@ -110,6 +112,7 @@ files:
110
112
  - spec/simple_enum/hasher_spec.rb
111
113
  - spec/simple_enum/mongoid_spec.rb
112
114
  - spec/simple_enum/translation_spec.rb
115
+ - spec/simple_enum/view_helpers_spec.rb
113
116
  - spec/spec_helper.rb
114
117
  - spec/support/active_record_support.rb
115
118
  - spec/support/i18n_support.rb
@@ -146,6 +149,7 @@ test_files:
146
149
  - spec/simple_enum/hasher_spec.rb
147
150
  - spec/simple_enum/mongoid_spec.rb
148
151
  - spec/simple_enum/translation_spec.rb
152
+ - spec/simple_enum/view_helpers_spec.rb
149
153
  - spec/spec_helper.rb
150
154
  - spec/support/active_record_support.rb
151
155
  - spec/support/i18n_support.rb