enumify 0.0.7 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +6 -0
- data/Guardfile +20 -0
- data/Readme.md +28 -1
- data/gemfiles/rails30.gemfile +5 -5
- data/gemfiles/rails31.gemfile +5 -5
- data/gemfiles/rails32.gemfile +5 -5
- data/gemfiles/rails40.gemfile +5 -5
- data/gemfiles/rails41.gemfile +5 -5
- data/lib/enumify/model.rb +60 -40
- data/lib/enumify/railtie.rb +2 -2
- data/lib/enumify/version.rb +1 -1
- data/spec/enumify/enum_spec.rb +110 -40
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e186a8d9675c9e5b1959d6bda11d3883329a8b96
|
4
|
+
data.tar.gz: 122e5bdcb684221bb5c2d585fa7e79af75506e0e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9a728aa7bed29637d532075d31611d9224cb55c27fc467594eb4d4a15c632146401b1ea48d647198223c205e0ee5af729bb1a1bc124b6a24ef4f353bc0b2cde3
|
7
|
+
data.tar.gz: 674ca49a2f592cd96dcb5abe9d2d6a8129ae025d21d5ceeb66a108b3473722272792e61bbd40fceec74e4aec2d9c82ca0b8eb0af654be958ee3db76c9c455952
|
data/Gemfile
CHANGED
@@ -5,5 +5,11 @@ gem 'rubinius-developer_tools', :platforms => :rbx
|
|
5
5
|
gem 'sqlite3', '1.3.8', :platforms => [:ruby, :rbx]
|
6
6
|
gem 'activerecord-jdbcsqlite3-adapter', :platforms => :jruby
|
7
7
|
|
8
|
+
group :development do
|
9
|
+
gem 'guard'
|
10
|
+
gem 'guard-rspec'
|
11
|
+
gem 'terminal-notifier-guard'
|
12
|
+
end
|
13
|
+
|
8
14
|
# Specify your gem's dependencies in enumify.gemspec
|
9
15
|
gemspec
|
data/Guardfile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
guard :rspec, cmd: "bundle exec rspec" do
|
2
|
+
watch(%r{^spec/.+_spec\.rb$})
|
3
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
4
|
+
watch('spec/spec_helper.rb') { "spec" }
|
5
|
+
|
6
|
+
# Rails example
|
7
|
+
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
8
|
+
watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
|
9
|
+
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
|
10
|
+
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
|
11
|
+
watch('config/routes.rb') { "spec/routing" }
|
12
|
+
watch('app/controllers/application_controller.rb') { "spec/controllers" }
|
13
|
+
|
14
|
+
# Capybara features specs
|
15
|
+
watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
|
16
|
+
|
17
|
+
# Turnip features and steps
|
18
|
+
watch(%r{^spec/acceptance/(.+)\.feature$})
|
19
|
+
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
|
20
|
+
end
|
data/Readme.md
CHANGED
@@ -12,7 +12,7 @@ gem 'enumify'
|
|
12
12
|
|
13
13
|
## How to use
|
14
14
|
|
15
|
-
Just call the enum function in any ActiveRecord object, the function accepts the field name as the first variable and the possible values as an array
|
15
|
+
Just call the `enum` function in any ActiveRecord object, the function accepts the field name as the first variable and the possible values as an array
|
16
16
|
|
17
17
|
```ruby
|
18
18
|
class Event < ActiveRecord::Base
|
@@ -20,6 +20,19 @@ class Event < ActiveRecord::Base
|
|
20
20
|
end
|
21
21
|
```
|
22
22
|
|
23
|
+
## Rails 4.1+
|
24
|
+
|
25
|
+
You can also instantiate a _Enumify_ enum by calling the `enumify` method instead of `enum`.
|
26
|
+
This is especially helpful when your app is running Rail 4.1 or better which has it's own built in enum (although not as good)
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
class Event < ActiveRecord::Base
|
30
|
+
enumify :status, [:available, :canceled, :completed]
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
34
|
+
## Usage
|
35
|
+
|
23
36
|
After that you get several autogenerated commands to use with the enum
|
24
37
|
|
25
38
|
```ruby
|
@@ -51,6 +64,20 @@ end
|
|
51
64
|
Event.create! # Is valid and does not throw an exception.
|
52
65
|
```
|
53
66
|
|
67
|
+
#### :prefix
|
68
|
+
By default all enum values are available as scopes, bang and query methods based on the value.
|
69
|
+
You can add a prefix for the enum values in order to differentiate different enums on the same object.
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
class Event < ActiveRecord::Base
|
73
|
+
enum :status, [:available, :canceled, :completed], :prefix => true
|
74
|
+
enum :subtype, [:company, :personal], :prefix => 'type'
|
75
|
+
end
|
76
|
+
|
77
|
+
event.available? # Not available anymore
|
78
|
+
event.status_available? # when prefix true
|
79
|
+
event.type_company? # you can set a specific name for your prefix
|
80
|
+
```
|
54
81
|
|
55
82
|
|
56
83
|
## Callbacks
|
data/gemfiles/rails30.gemfile
CHANGED
@@ -2,10 +2,10 @@
|
|
2
2
|
|
3
3
|
source "http://rubygems.org"
|
4
4
|
|
5
|
-
gem "rubysl", "~> 2.0", :platforms
|
6
|
-
gem "rubinius-developer_tools", :platforms
|
7
|
-
gem "sqlite3", "1.3.8", :platforms=>[:ruby, :rbx]
|
8
|
-
gem "activerecord-jdbcsqlite3-adapter", :platforms
|
5
|
+
gem "rubysl", "~> 2.0", :platforms => :rbx
|
6
|
+
gem "rubinius-developer_tools", :platforms => :rbx
|
7
|
+
gem "sqlite3", "1.3.8", :platforms => [:ruby, :rbx]
|
8
|
+
gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
|
9
9
|
gem "activerecord", "~> 3.0.11"
|
10
10
|
|
11
|
-
gemspec :path=>"../"
|
11
|
+
gemspec :path => "../"
|
data/gemfiles/rails31.gemfile
CHANGED
@@ -2,10 +2,10 @@
|
|
2
2
|
|
3
3
|
source "http://rubygems.org"
|
4
4
|
|
5
|
-
gem "rubysl", "~> 2.0", :platforms
|
6
|
-
gem "rubinius-developer_tools", :platforms
|
7
|
-
gem "sqlite3", "1.3.8", :platforms=>[:ruby, :rbx]
|
8
|
-
gem "activerecord-jdbcsqlite3-adapter", :platforms
|
5
|
+
gem "rubysl", "~> 2.0", :platforms => :rbx
|
6
|
+
gem "rubinius-developer_tools", :platforms => :rbx
|
7
|
+
gem "sqlite3", "1.3.8", :platforms => [:ruby, :rbx]
|
8
|
+
gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
|
9
9
|
gem "activerecord", "~> 3.1.12"
|
10
10
|
|
11
|
-
gemspec :path=>"../"
|
11
|
+
gemspec :path => "../"
|
data/gemfiles/rails32.gemfile
CHANGED
@@ -2,10 +2,10 @@
|
|
2
2
|
|
3
3
|
source "http://rubygems.org"
|
4
4
|
|
5
|
-
gem "rubysl", "~> 2.0", :platforms
|
6
|
-
gem "rubinius-developer_tools", :platforms
|
7
|
-
gem "sqlite3", "1.3.8", :platforms=>[:ruby, :rbx]
|
8
|
-
gem "activerecord-jdbcsqlite3-adapter", :platforms
|
5
|
+
gem "rubysl", "~> 2.0", :platforms => :rbx
|
6
|
+
gem "rubinius-developer_tools", :platforms => :rbx
|
7
|
+
gem "sqlite3", "1.3.8", :platforms => [:ruby, :rbx]
|
8
|
+
gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
|
9
9
|
gem "activerecord", "~> 3.2.14"
|
10
10
|
|
11
|
-
gemspec :path=>"../"
|
11
|
+
gemspec :path => "../"
|
data/gemfiles/rails40.gemfile
CHANGED
@@ -2,10 +2,10 @@
|
|
2
2
|
|
3
3
|
source "http://rubygems.org"
|
4
4
|
|
5
|
-
gem "rubysl", "~> 2.0", :platforms
|
6
|
-
gem "rubinius-developer_tools", :platforms
|
7
|
-
gem "sqlite3", "1.3.8", :platforms=>[:ruby, :rbx]
|
8
|
-
gem "activerecord-jdbcsqlite3-adapter", :platforms
|
5
|
+
gem "rubysl", "~> 2.0", :platforms => :rbx
|
6
|
+
gem "rubinius-developer_tools", :platforms => :rbx
|
7
|
+
gem "sqlite3", "1.3.8", :platforms => [:ruby, :rbx]
|
8
|
+
gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
|
9
9
|
gem "activerecord", "~> 4.0.0"
|
10
10
|
|
11
|
-
gemspec :path=>"../"
|
11
|
+
gemspec :path => "../"
|
data/gemfiles/rails41.gemfile
CHANGED
@@ -2,10 +2,10 @@
|
|
2
2
|
|
3
3
|
source "http://rubygems.org"
|
4
4
|
|
5
|
-
gem "rubysl", "~> 2.0", :platforms
|
6
|
-
gem "rubinius-developer_tools", :platforms
|
7
|
-
gem "sqlite3", "1.3.8", :platforms=>[:ruby, :rbx]
|
8
|
-
gem "activerecord-jdbcsqlite3-adapter", :platforms
|
5
|
+
gem "rubysl", "~> 2.0", :platforms => :rbx
|
6
|
+
gem "rubinius-developer_tools", :platforms => :rbx
|
7
|
+
gem "sqlite3", "1.3.8", :platforms => [:ruby, :rbx]
|
8
|
+
gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
|
9
9
|
gem "activerecord", "~> 4.1.0"
|
10
10
|
|
11
|
-
gemspec :path=>"../"
|
11
|
+
gemspec :path => "../"
|
data/lib/enumify/model.rb
CHANGED
@@ -1,62 +1,82 @@
|
|
1
1
|
module Enumify
|
2
2
|
module Model
|
3
|
-
def enum(parameter, vals=[], opts={})
|
4
3
|
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
def self.included(base)
|
5
|
+
base.extend(ClassMethods)
|
6
|
+
end
|
8
7
|
|
9
|
-
|
10
|
-
|
11
|
-
(attr.nil? || attr.empty?) ? nil : attr.to_sym
|
12
|
-
end
|
8
|
+
module ClassMethods
|
9
|
+
def enumify(parameter, vals=[], opts={})
|
13
10
|
|
14
|
-
|
15
|
-
|
16
|
-
end
|
11
|
+
validates_inclusion_of parameter, :in => vals, :allow_nil => !!opts[:allow_nil]
|
12
|
+
paramater_string = parameter.to_s
|
17
13
|
|
18
|
-
|
14
|
+
prefix = ''
|
15
|
+
if opts[:prefix] == true
|
16
|
+
prefix = "#{paramater_string}_"
|
17
|
+
elsif opts[:prefix].present?
|
18
|
+
prefix = "#{opts[:prefix].to_s}_"
|
19
|
+
end
|
19
20
|
|
20
|
-
|
21
|
+
const_set("#{paramater_string.pluralize.upcase}", vals)
|
21
22
|
|
22
|
-
define_method "
|
23
|
+
define_method "#{paramater_string}" do
|
24
|
+
attr = read_attribute(parameter)
|
25
|
+
(attr.nil? || attr.empty?) ? nil : attr.to_sym
|
26
|
+
end
|
23
27
|
|
24
|
-
|
25
|
-
|
26
|
-
return value if old == value
|
27
|
-
write_attribute(parameter, (value and value.to_s))
|
28
|
-
save if should_save
|
29
|
-
send("#{parameter.to_s}_changed", old, value) if respond_to?("#{parameter.to_s}_changed", true) and !old.nil?
|
30
|
-
return value
|
28
|
+
define_method "#{paramater_string}=" do |value|
|
29
|
+
send("_set_#{paramater_string}", value, false)
|
31
30
|
end
|
32
31
|
|
33
|
-
|
32
|
+
self.class_eval do
|
34
33
|
|
35
|
-
|
36
|
-
raise "Collision in enum values method #{val}" if respond_to?("#{val.to_s}?") or respond_to?("#{val.to_s}!") or respond_to?("#{val.to_s}")
|
34
|
+
private
|
37
35
|
|
38
|
-
|
39
|
-
|
40
|
-
|
36
|
+
define_method "_set_#{paramater_string}" do |value, should_save|
|
37
|
+
|
38
|
+
value = value and value.to_sym
|
39
|
+
old = read_attribute(parameter) ? read_attribute(parameter).to_sym : nil
|
40
|
+
return value if old == value
|
41
|
+
write_attribute(parameter, (value and value.to_s))
|
42
|
+
save if should_save
|
43
|
+
send("#{paramater_string}_changed", old, value) if respond_to?("#{paramater_string}_changed", true) and !old.nil?
|
44
|
+
return value
|
45
|
+
end
|
41
46
|
|
42
|
-
define_method "#{val.to_s}!" do
|
43
|
-
send("_set_#{parameter.to_s}", val, true)
|
44
47
|
end
|
45
48
|
|
46
|
-
|
47
|
-
|
49
|
+
vals.each do |val|
|
50
|
+
attribute = prefix + val.to_s
|
51
|
+
query_method = "#{attribute}?"
|
52
|
+
bang_method = "#{attribute}!"
|
53
|
+
|
54
|
+
raise "Collision in enum values method #{attribute}" if respond_to?(query_method) or respond_to?(bang_method) or respond_to?(attribute)
|
55
|
+
|
56
|
+
define_method query_method do
|
57
|
+
send("#{paramater_string}") == val
|
58
|
+
end
|
48
59
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
# it will fail on ambiguous column name.
|
55
|
-
unless respond_to?("not_#{val}")
|
56
|
-
scope "not_#{val}", lambda { where("#{self.table_name}.#{parameter} != ?", val.to_s) }
|
60
|
+
define_method bang_method do
|
61
|
+
send("_set_#{paramater_string}", val, true)
|
62
|
+
end
|
63
|
+
|
64
|
+
scope attribute.to_sym, lambda { where(parameter.to_sym => val.to_s) }
|
57
65
|
end
|
58
|
-
end
|
59
66
|
|
67
|
+
# We want to first define all the "positive" scopes and only then define
|
68
|
+
# the "negation scopes", to make sure they don't override previous scopes
|
69
|
+
vals.each do |val|
|
70
|
+
# We need to prefix the field with the table name since if this scope will
|
71
|
+
# be used in a joined query with other models that have the same enum field then
|
72
|
+
# it will fail on ambiguous column name.
|
73
|
+
negative_scope = "not_" + prefix + val.to_s
|
74
|
+
unless respond_to?(negative_scope)
|
75
|
+
scope negative_scope, lambda { where("#{self.table_name}.#{parameter} != ?", val.to_s) }
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
alias_method :enum, :enumify
|
60
80
|
end
|
61
81
|
|
62
82
|
end
|
data/lib/enumify/railtie.rb
CHANGED
data/lib/enumify/version.rb
CHANGED
data/spec/enumify/enum_spec.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
class Model < ActiveRecord::Base
|
4
|
-
|
4
|
+
include Enumify::Model
|
5
5
|
|
6
6
|
enum :status, [:available, :canceled, :completed]
|
7
7
|
end
|
8
8
|
|
9
9
|
class OtherModel < ActiveRecord::Base
|
10
|
-
|
10
|
+
include Enumify::Model
|
11
11
|
|
12
12
|
belongs_to :model
|
13
13
|
|
@@ -15,13 +15,9 @@ class OtherModel < ActiveRecord::Base
|
|
15
15
|
end
|
16
16
|
|
17
17
|
class ModelAllowingNil < ActiveRecord::Base
|
18
|
+
include Enumify::Model
|
18
19
|
self.table_name = 'models'
|
19
|
-
|
20
|
-
extend Enumify::Model
|
21
|
-
|
22
|
-
belongs_to :model
|
23
|
-
|
24
|
-
enum :status, [:active, :expired, :not_expired], :allow_nil => true
|
20
|
+
enum :status, [:available, :canceled, :completed], :allow_nil => true
|
25
21
|
end
|
26
22
|
|
27
23
|
|
@@ -50,7 +46,7 @@ describe :Enumify do
|
|
50
46
|
describe "model allowing enum value to be nil" do
|
51
47
|
subject { @obj_allowing_nil }
|
52
48
|
it "should be valid" do
|
53
|
-
subject.
|
49
|
+
expect(subject).to be_valid
|
54
50
|
end
|
55
51
|
|
56
52
|
it 'should not raise error when setting value to nil' do
|
@@ -58,21 +54,21 @@ describe :Enumify do
|
|
58
54
|
subject.status = nil
|
59
55
|
}.to_not raise_error
|
60
56
|
|
61
|
-
subject.status.
|
57
|
+
expect(subject.status).to be_nil
|
62
58
|
end
|
63
59
|
end
|
64
60
|
|
65
61
|
describe "model not allowing enum value to be nil" do
|
66
62
|
subject { @obj_not_allowing_nil }
|
67
63
|
it "should be invalid" do
|
68
|
-
subject.
|
64
|
+
expect(subject).to be_invalid
|
69
65
|
end
|
70
66
|
|
71
67
|
it 'should not raise error when setting value to nil' do
|
72
68
|
expect {
|
73
69
|
subject.status = nil
|
74
70
|
}.to_not raise_error
|
75
|
-
subject.
|
71
|
+
expect(subject).to be_invalid
|
76
72
|
end
|
77
73
|
end
|
78
74
|
|
@@ -81,11 +77,11 @@ describe :Enumify do
|
|
81
77
|
describe "short hand methods" do
|
82
78
|
describe "question mark (?)" do
|
83
79
|
it "should return true if value of enum equals a value" do
|
84
|
-
@obj.available
|
80
|
+
expect(@obj.available?).to be_truthy
|
85
81
|
end
|
86
82
|
|
87
83
|
it "should return false if value of enum is different " do
|
88
|
-
@obj.canceled
|
84
|
+
expect(@obj.canceled?).to be_falsey
|
89
85
|
end
|
90
86
|
|
91
87
|
end
|
@@ -93,13 +89,13 @@ describe :Enumify do
|
|
93
89
|
describe "exclemation mark (!)" do
|
94
90
|
it "should change the value of the enum to the methods value" do
|
95
91
|
@obj.canceled!
|
96
|
-
@obj.
|
92
|
+
expect(@obj).to be_canceled
|
97
93
|
end
|
98
94
|
|
99
95
|
context 'trying to set the value to the same value' do
|
100
96
|
before { @obj.available! }
|
101
97
|
it 'should not save the object again' do
|
102
|
-
@obj.
|
98
|
+
expect(@obj).to_not receive(:save)
|
103
99
|
@obj.available!
|
104
100
|
end
|
105
101
|
end
|
@@ -108,17 +104,19 @@ describe :Enumify do
|
|
108
104
|
|
109
105
|
it "should have two shorthand methods for each possible value" do
|
110
106
|
Model::STATUSES.each do |val|
|
111
|
-
@obj.respond_to?("#{val}?").
|
112
|
-
@obj.respond_to?("#{val}!").
|
107
|
+
expect(@obj.respond_to?("#{val}?")).to be_truthy
|
108
|
+
expect(@obj.respond_to?("#{val}!")).to be_truthy
|
113
109
|
end
|
114
110
|
end
|
115
111
|
end
|
116
112
|
|
117
113
|
describe "getting value" do
|
118
114
|
it "should always return the enums value as a symbol" do
|
119
|
-
|
120
|
-
|
121
|
-
|
115
|
+
expect {
|
116
|
+
@obj.status = "canceled"
|
117
|
+
}.to change{
|
118
|
+
@obj.status
|
119
|
+
}.from(:available).to(:canceled)
|
122
120
|
end
|
123
121
|
|
124
122
|
end
|
@@ -126,43 +124,43 @@ describe :Enumify do
|
|
126
124
|
describe "setting value" do
|
127
125
|
it "should except values as symbol" do
|
128
126
|
@obj.status = :canceled
|
129
|
-
@obj.
|
127
|
+
expect(@obj).to be_canceled
|
130
128
|
end
|
131
129
|
|
132
130
|
it "should except values as string" do
|
133
131
|
@obj.status = "canceled"
|
134
|
-
@obj.
|
132
|
+
expect(@obj).to be_canceled
|
135
133
|
end
|
136
134
|
end
|
137
135
|
|
138
136
|
describe "validations" do
|
137
|
+
let (:obj) { Model.new(:status => :available) }
|
138
|
+
|
139
139
|
it "should not except a value outside the given list" do
|
140
|
-
|
141
|
-
|
142
|
-
@obj.should_not be_valid
|
140
|
+
obj.status = :foobar
|
141
|
+
expect(obj).to_not be_valid
|
143
142
|
end
|
144
143
|
|
145
144
|
it "should except value in the list" do
|
146
|
-
|
147
|
-
|
148
|
-
@obj.should be_valid
|
145
|
+
obj.status = :canceled
|
146
|
+
expect(obj).to be_valid
|
149
147
|
end
|
150
148
|
end
|
151
149
|
|
152
150
|
describe "callbacks" do
|
153
151
|
it "should receive a callback on change of value" do
|
154
|
-
@obj.
|
152
|
+
expect(@obj).to receive(:status_changed).with(:available,:canceled)
|
155
153
|
@obj.canceled!
|
156
154
|
end
|
157
155
|
|
158
156
|
it "should not receive a callback on initial value" do
|
159
157
|
@obj = Model.new
|
160
|
-
@obj.
|
158
|
+
expect(@obj).to_not receive(:status_changed)
|
161
159
|
@obj.canceled!
|
162
160
|
end
|
163
161
|
|
164
162
|
it "should not receive a callback on value change to same" do
|
165
|
-
@obj.
|
163
|
+
expect(@obj).to_not receive(:status_changed)
|
166
164
|
@obj.available!
|
167
165
|
end
|
168
166
|
|
@@ -170,38 +168,110 @@ describe :Enumify do
|
|
170
168
|
|
171
169
|
describe "scopes" do
|
172
170
|
it "should return objects with given value" do
|
173
|
-
Model.available.
|
174
|
-
Model.canceled.
|
171
|
+
expect(Model.available).to eq [@obj]
|
172
|
+
expect(Model.canceled).to eq [@canceled_obj]
|
175
173
|
end
|
176
174
|
|
177
175
|
it "should return objects with given value when joined with models who have the same enum field" do
|
178
|
-
OtherModel.joins(:model).active.
|
176
|
+
expect(OtherModel.joins(:model).active).to eq [@active_obj]
|
179
177
|
end
|
180
178
|
|
181
179
|
describe "negation scopes" do
|
182
180
|
|
183
181
|
it "should return objects that do not have the given value" do
|
184
|
-
Model.not_available.
|
182
|
+
expect(Model.not_available).to include(@canceled_obj, @completed_obj)
|
185
183
|
end
|
186
184
|
|
187
185
|
it "should return objects that do not have the given value when joined with models who have the same enum field" do
|
188
|
-
OtherModel.joins(:model).not_active.
|
186
|
+
expect(OtherModel.joins(:model).not_active).to include(@expired_obj, @not_expired_obj)
|
189
187
|
end
|
190
188
|
|
191
189
|
it "should not override positive scopes" do
|
192
190
|
# We want here to verify that the not_expired scope return only the models with
|
193
191
|
# status == "not_expired" and not all the models with status != "expired",
|
194
192
|
# since negation scopes should not override the "positive" scopes.
|
195
|
-
OtherModel.not_expired.
|
193
|
+
expect(OtherModel.not_expired).to eq [@not_expired_obj]
|
196
194
|
end
|
197
195
|
|
198
196
|
end
|
199
197
|
|
200
198
|
end
|
201
199
|
|
202
|
-
|
203
200
|
it "class should have a CONST that holds all the available options of the enum" do
|
204
|
-
Model::STATUSES.
|
201
|
+
expect(Model::STATUSES).to eq [:available, :canceled, :completed]
|
202
|
+
end
|
203
|
+
|
204
|
+
describe 'prefix' do
|
205
|
+
|
206
|
+
|
207
|
+
context 'when prefix set to string' do
|
208
|
+
|
209
|
+
class ModelWithPrefix < ActiveRecord::Base
|
210
|
+
include Enumify::Model
|
211
|
+
self.table_name = 'models'
|
212
|
+
enum :status, [:available, :canceled, :completed], :prefix => 'foo'
|
213
|
+
end
|
214
|
+
|
215
|
+
subject { ModelWithPrefix.new(:status => :available) }
|
216
|
+
it 'does not allow access through unprefixed enum' do
|
217
|
+
expect(subject).to_not respond_to(:available?)
|
218
|
+
end
|
219
|
+
|
220
|
+
it 'allows access to the attributes when prefixed by that string' do
|
221
|
+
expect(subject).to respond_to(:foo_available?)
|
222
|
+
end
|
223
|
+
|
224
|
+
it 'has a scope available with the prefix' do
|
225
|
+
expect(ModelWithPrefix).to respond_to(:foo_available)
|
226
|
+
end
|
227
|
+
|
228
|
+
it 'has no scope for unprefixed methods' do
|
229
|
+
expect(ModelWithPrefix).to_not respond_to(:available)
|
230
|
+
end
|
231
|
+
|
232
|
+
it 'has a negative scope available with the prefix' do
|
233
|
+
expect(ModelWithPrefix).to respond_to(:not_foo_available)
|
234
|
+
end
|
235
|
+
|
236
|
+
it 'has no negative scope for unprefixed methods' do
|
237
|
+
expect(ModelWithPrefix).to_not respond_to(:not_available)
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
context 'when prefix set to true' do
|
242
|
+
|
243
|
+
class ModelWithPrefixTrue < ActiveRecord::Base
|
244
|
+
include Enumify::Model
|
245
|
+
self.table_name = 'models'
|
246
|
+
enum :status, [:available, :canceled, :completed], :prefix => true
|
247
|
+
end
|
248
|
+
|
249
|
+
subject { ModelWithPrefixTrue.new(:status => :available) }
|
250
|
+
it 'does not allow access through unprefixed enum' do
|
251
|
+
expect(subject).to_not respond_to(:available?)
|
252
|
+
end
|
253
|
+
|
254
|
+
it 'allows access to the attributes when prefixed by that string' do
|
255
|
+
expect(subject).to respond_to(:status_available?)
|
256
|
+
end
|
257
|
+
|
258
|
+
it 'has a scope available with the prefix' do
|
259
|
+
expect(ModelWithPrefixTrue).to respond_to(:status_available)
|
260
|
+
end
|
261
|
+
|
262
|
+
it 'has no scope for unprefixed methods' do
|
263
|
+
expect(ModelWithPrefixTrue).to_not respond_to(:available)
|
264
|
+
end
|
265
|
+
|
266
|
+
it 'has a negative scope available with the prefix' do
|
267
|
+
expect(ModelWithPrefixTrue).to respond_to(:not_status_available)
|
268
|
+
end
|
269
|
+
|
270
|
+
it 'has no negative scope for unprefixed methods' do
|
271
|
+
expect(ModelWithPrefixTrue).to_not respond_to(:not_available)
|
272
|
+
end
|
273
|
+
end
|
274
|
+
|
205
275
|
end
|
206
276
|
|
207
277
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enumify
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- yon
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-09-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -86,6 +86,7 @@ files:
|
|
86
86
|
- Appraisals
|
87
87
|
- CHANGELOG.md
|
88
88
|
- Gemfile
|
89
|
+
- Guardfile
|
89
90
|
- LICENSE
|
90
91
|
- Rakefile
|
91
92
|
- Readme.md
|
@@ -121,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
121
122
|
version: '0'
|
122
123
|
requirements: []
|
123
124
|
rubyforge_project: enumify
|
124
|
-
rubygems_version: 2.0
|
125
|
+
rubygems_version: 2.4.0
|
125
126
|
signing_key:
|
126
127
|
specification_version: 4
|
127
128
|
summary: enumify adds an enum command to all ActiveRecord models which enables you
|