rails_attr_enum 0.0.6 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/test/dummy/rails ADDED
@@ -0,0 +1,2 @@
1
+ #!/bin/bash
2
+ bundle exec rails "$@"
data/test/dummy/rake ADDED
@@ -0,0 +1,2 @@
1
+ #!/bin/bash
2
+ bundle exec rspec "$@"
data/test/dummy/rspec ADDED
@@ -0,0 +1,2 @@
1
+ #!/bin/bash
2
+ bundle exec rspec "$@"
@@ -0,0 +1,9 @@
1
+ FactoryGirl.define do
2
+ factory :user do
3
+ [:admin, :editor, :author, :user].each do |role_key|
4
+ trait "role_#{role_key}".to_sym do
5
+ role User::Role.const_get(role_key.to_s.upcase)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe User do
4
+ before :all do
5
+ # Create test users
6
+ create(:user, :role_admin)
7
+ 2.times { create(:user, :role_editor) }
8
+ 5.times { create(:user, :role_author) }
9
+ 20.times { create(:user, :role_user) }
10
+ end
11
+
12
+ before { default_user_roles }
13
+
14
+ it 'adds the scope methods for each possible enum value' do
15
+ scopes = [:role_admin, :role_editor, :role_admin, :role_user]
16
+ expect(User).to respond_to(*scopes)
17
+ end
18
+
19
+ it 'returns the correct count from the db' do
20
+ expect(User.role_admin.count).to eq(1)
21
+ expect(User.role_editor.count).to eq(2)
22
+ expect(User.role_author.count).to eq(5)
23
+ expect(User.role_user.count).to eq(20)
24
+ end
25
+ end
@@ -10,57 +10,52 @@ describe User do
10
10
  shared_examples 'it sets default values' do
11
11
  it 'sets the default values' do
12
12
  expect(User::Role::ADMIN).to eq(0)
13
- expect(User::Role::AUTHOR).to eq(1)
14
- expect(User::Role::EDITOR).to eq(2)
13
+ expect(User::Role::EDITOR).to eq(1)
14
+ expect(User::Role::AUTHOR).to eq(2)
15
15
  expect(User::Role::USER).to eq(3)
16
16
  end
17
17
 
18
18
  it 'sets the right value for an instance' do
19
- expect(u_admin.role).to eq(0)
20
- expect(u_author.role).to eq(1)
21
- expect(u_editor.role).to eq(2)
22
- expect(u_user.role).to eq(3)
19
+ expect(admin.role).to eq(0)
20
+ expect(editor.role).to eq(1)
21
+ expect(author.role).to eq(2)
22
+ expect(user.role).to eq(3)
23
23
  end
24
24
  end
25
25
 
26
26
  shared_examples 'it sets default labels' do
27
27
  it 'sets the default labels' do
28
28
  expect(User::Role::ADMIN_LABEL).to eq('Admin')
29
- expect(User::Role::AUTHOR_LABEL).to eq('Author')
30
29
  expect(User::Role::EDITOR_LABEL).to eq('Editor')
30
+ expect(User::Role::AUTHOR_LABEL).to eq('Author')
31
31
  expect(User::Role::USER_LABEL).to eq('User')
32
32
  end
33
33
  end
34
34
 
35
- shared_examples 'it sets the appropriate values' do |admin_value, author_value, editor_value, user_value|
35
+ shared_examples 'it sets the appropriate values' do |admin_value, editor_value, author_value, user_value|
36
36
  it 'sets the appropriate values' do
37
37
  expect(User::Role::ADMIN).to eq(admin_value)
38
- expect(User::Role::AUTHOR).to eq(author_value)
39
38
  expect(User::Role::EDITOR).to eq(editor_value)
39
+ expect(User::Role::AUTHOR).to eq(author_value)
40
40
  expect(User::Role::USER).to eq(user_value)
41
41
  end
42
42
 
43
43
  it 'sets the right value for an instance' do
44
- expect(u_admin.role).to eq(admin_value)
45
- expect(u_author.role).to eq(author_value)
46
- expect(u_editor.role).to eq(editor_value)
47
- expect(u_user.role).to eq(user_value)
44
+ expect(admin.role).to eq(admin_value)
45
+ expect(author.role).to eq(author_value)
46
+ expect(editor.role).to eq(editor_value)
47
+ expect(user.role).to eq(user_value)
48
48
  end
49
49
  end
50
50
 
51
- let(:u_admin) { User.new(role: User::Role::ADMIN) }
52
- let(:u_author) { User.new(role: User::Role::AUTHOR) }
53
- let(:u_editor) { User.new(role: User::Role::EDITOR) }
54
- let(:u_user) { User.new(role: User::Role::USER) }
51
+ let(:admin) { User.new(role: User::Role::ADMIN) }
52
+ let(:editor) { User.new(role: User::Role::EDITOR) }
53
+ let(:author) { User.new(role: User::Role::AUTHOR) }
54
+ let(:user) { User.new(role: User::Role::USER) }
55
55
 
56
56
  context 'when passing all symbols' do
57
57
  before :each do
58
- clear_user
59
-
60
- User.class_eval do
61
- extend RailsAttrEnum
62
- attr_enum :role, :admin, :author, :editor, :user
63
- end
58
+ default_user_roles
64
59
  end
65
60
 
66
61
  it_behaves_like 'it has the enum module'
@@ -75,7 +70,7 @@ describe User do
75
70
 
76
71
  User.class_eval do
77
72
  extend RailsAttrEnum
78
- attr_enum :role, :admin, { author: 12 }, :editor, { user: 42 }
73
+ attr_enum :role, :admin, { editor: 12 }, :author, { user: 42 }
79
74
  end
80
75
  end
81
76
 
@@ -90,7 +85,7 @@ describe User do
90
85
 
91
86
  User.class_eval do
92
87
  extend RailsAttrEnum
93
- attr_enum :role, admin: 1, author: 2, editor: 4, user: 8
88
+ attr_enum :role, admin: 1, editor: 2, author: 4, user: 8
94
89
  end
95
90
  end
96
91
 
@@ -39,6 +39,23 @@ RSpec.configure do |config|
39
39
  # the seed, which is printed after each run.
40
40
  # --seed 1234
41
41
  config.order = "random"
42
+
43
+ # FactoryGirl configuration
44
+ config.include FactoryGirl::Syntax::Methods
45
+
46
+ # DatabaseCleaner configuration
47
+ config.before(:suite) do
48
+ DatabaseCleaner.strategy = :transaction
49
+ DatabaseCleaner.clean_with(:truncation)
50
+ end
51
+
52
+ config.before(:each) do
53
+ DatabaseCleaner.start
54
+ end
55
+
56
+ config.after(:each) do
57
+ DatabaseCleaner.clean
58
+ end
42
59
  end
43
60
 
44
61
  def clear_user
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_attr_enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Fairbank
@@ -93,10 +93,15 @@ files:
93
93
  - test/dummy/public/422.html
94
94
  - test/dummy/public/500.html
95
95
  - test/dummy/public/favicon.ico
96
+ - test/dummy/rails
97
+ - test/dummy/rake
96
98
  - test/dummy/Rakefile
97
99
  - test/dummy/README.rdoc
100
+ - test/dummy/rspec
101
+ - test/dummy/spec/factories/users.rb
98
102
  - test/dummy/spec/json_spec.rb
99
- - test/dummy/spec/models/user_spec.rb
103
+ - test/dummy/spec/models/user_enum_scope_spec.rb
104
+ - test/dummy/spec/models/user_enum_spec.rb
100
105
  - test/dummy/spec/spec_helper.rb
101
106
  - test/dummy/spec/to_h_spec.rb
102
107
  homepage: https://github.com/jfairbank/rails_attr_enum
@@ -160,9 +165,14 @@ test_files:
160
165
  - test/dummy/public/422.html
161
166
  - test/dummy/public/500.html
162
167
  - test/dummy/public/favicon.ico
168
+ - test/dummy/rails
169
+ - test/dummy/rake
163
170
  - test/dummy/Rakefile
164
171
  - test/dummy/README.rdoc
172
+ - test/dummy/rspec
173
+ - test/dummy/spec/factories/users.rb
165
174
  - test/dummy/spec/json_spec.rb
166
- - test/dummy/spec/models/user_spec.rb
175
+ - test/dummy/spec/models/user_enum_scope_spec.rb
176
+ - test/dummy/spec/models/user_enum_spec.rb
167
177
  - test/dummy/spec/spec_helper.rb
168
178
  - test/dummy/spec/to_h_spec.rb