enumerations 1.0.0 → 1.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +3 -0
- data/.travis.yml +5 -0
- data/Gemfile +3 -0
- data/Rakefile +13 -0
- data/Readme.md +128 -0
- data/enumerations.gemspec +19 -0
- data/lib/enumerations.rb +96 -0
- data/lib/enumerations/base.rb +187 -0
- data/lib/enumerations/reflection.rb +18 -0
- data/lib/enumerations/version.rb +3 -0
- data/test/base_test.rb +101 -0
- data/test/enumerations_test.rb +41 -0
- data/test/reflection_test.rb +11 -0
- data/test/test_helper.rb +25 -0
- data/test/version_test.rb +7 -0
- metadata +82 -19
- data/init.rb +0 -1
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6c202450cd19bc9f524f6ef5756c44bd304a4ef6047d274abda8d165601b6e64
|
4
|
+
data.tar.gz: 61977fd20143ade7cb63242cc0b6098a981861783b412b62fa6b36e28b195f35
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 60cb0a9f041e566e090e7b26b5a099f912e6a63a94a652a165b1e83f1d71fd5b7d280c25ebe13e1bca12d16c4dd0407f8db736ad7893ec2ed491a21ef199ffbc
|
7
|
+
data.tar.gz: 04f4aff2e90b826b5e47022083c919d28f561b9370963b9c37251c83eb1101c68b1500624c3ef0e4e039c88465da46a1afe707cd5cda15778dfb47a4bae16ed4
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task default: :test
|
7
|
+
|
8
|
+
desc 'Run unit tests.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
data/Readme.md
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
Enumerations
|
2
|
+
============
|
3
|
+
|
4
|
+
Rails plugin for enumerations in ActiveRecord models.
|
5
|
+
|
6
|
+
[![Build Status](https://travis-ci.org/infinum/enumerations.svg?branch=master)](https://travis-ci.org/infinum/enumerations)
|
7
|
+
|
8
|
+
Installation
|
9
|
+
============
|
10
|
+
|
11
|
+
Inside your `Gemfile` add the following:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'enumerations'
|
15
|
+
```
|
16
|
+
|
17
|
+
Usage
|
18
|
+
=====
|
19
|
+
|
20
|
+
Create a model for your enumerations:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
class Status < Enumeration::Base
|
24
|
+
values draft: { id: 1, name: 'Draft' },
|
25
|
+
review_pending: { id: 2, name: 'Review pending' },
|
26
|
+
published: { id: 3, name: 'Published' }
|
27
|
+
end
|
28
|
+
```
|
29
|
+
|
30
|
+
Or you can use `value` method for defining your enumerations:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
class Status < Enumeration::Base
|
34
|
+
value :draft, id: 1, name: 'Draft'
|
35
|
+
value :review_pending, id: 2, name: 'Review pending'
|
36
|
+
value :published, id: 3, name: 'Published'
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
Include enumerations for integer fields in other models:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
class Post < ActiveRecord::Base
|
44
|
+
enumeration :status
|
45
|
+
validates :status_id, presence: true
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
You can pass attributes to specify which enumeratior and which column to use:
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
class Post < ActiveRecord::Base
|
53
|
+
enumeration :status,
|
54
|
+
foreign_key: :post_status_id, # specifies which column to use
|
55
|
+
class_name: Post::Status # specifies the class of the enumerator
|
56
|
+
validates :post_status_id, presence: true
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
Set enumerations, find enumerations by `symbol`:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
@post = Post.first
|
64
|
+
@post.status = Status.find(:draft)
|
65
|
+
@post.save
|
66
|
+
```
|
67
|
+
|
68
|
+
Or you can set enumerations on this way:
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
@post.status = Status.draft
|
72
|
+
```
|
73
|
+
|
74
|
+
Find enumerations by `id`:
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
@post.status = Status.find(2) # => Review pending
|
78
|
+
@post.save
|
79
|
+
```
|
80
|
+
|
81
|
+
Compare enumerations:
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
@post.status == :published # => true
|
85
|
+
@post.status == 3 # => true
|
86
|
+
@post.status == Status.find(:published) # => true
|
87
|
+
@post.status.published? # => true
|
88
|
+
```
|
89
|
+
|
90
|
+
Get all enumerations:
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
Status.all
|
94
|
+
```
|
95
|
+
|
96
|
+
Use in forms:
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
%p
|
100
|
+
= f.label :status_id
|
101
|
+
%br
|
102
|
+
= f.collection_select :status_id, Status.all, :id, :name
|
103
|
+
```
|
104
|
+
|
105
|
+
Advance Usage
|
106
|
+
=====
|
107
|
+
|
108
|
+
Except `id` and `name` you can specify other attributes to your enumerations:
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
class Status < Enumeration::Base
|
112
|
+
value :draft, id: 1, name: 'Draft'
|
113
|
+
value :review_pending, id: 2, name: 'Review pending', description: 'Some description...'
|
114
|
+
value :published, id: 3, name: 'Published'
|
115
|
+
end
|
116
|
+
```
|
117
|
+
|
118
|
+
Every enumeration has `id`, `name` and `description` methods. If you call method that is not in attribute list for enumeration, it will return `nil`.
|
119
|
+
|
120
|
+
```ruby
|
121
|
+
Status.review_pending.description # => 'Some description...'
|
122
|
+
Status.draft.description # => nil
|
123
|
+
```
|
124
|
+
|
125
|
+
Author
|
126
|
+
======
|
127
|
+
|
128
|
+
Copyright © 2010 Tomislav Car, Infinum Ltd.
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require File.expand_path('../lib/enumerations/version', __FILE__)
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = 'enumerations'
|
5
|
+
s.version = Enumeration::VERSION
|
6
|
+
s.date = '2010-08-20'
|
7
|
+
s.summary = "Enumerations for ActiveRecord!"
|
8
|
+
s.description = 'Extends ActiveRecord with enumeration capabilites.'
|
9
|
+
s.authors = ['Tomislav Car', 'Nikica Jokic', 'Nikola Santic']
|
10
|
+
s.email = ['tomislav@infinum.hr', 'nikica.jokic@infinum.hr', 'nikola.santic@infinum.hr']
|
11
|
+
s.homepage = 'https://github.com/infinum/enumerations'
|
12
|
+
|
13
|
+
s.add_dependency 'activerecord'
|
14
|
+
s.add_dependency 'activesupport'
|
15
|
+
s.add_development_dependency 'pry-byebug'
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
end
|
data/lib/enumerations.rb
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
require 'active_support/concern'
|
3
|
+
require 'active_support/core_ext/class/attribute'
|
4
|
+
require 'active_support/core_ext/string/inflections'
|
5
|
+
|
6
|
+
require 'enumerations/version'
|
7
|
+
require 'enumerations/base'
|
8
|
+
require 'enumerations/reflection'
|
9
|
+
|
10
|
+
# TODO: rename to Enumeration(s) in a major version change
|
11
|
+
module Enumeration
|
12
|
+
extend ActiveSupport::Concern
|
13
|
+
|
14
|
+
included do
|
15
|
+
class_attribute :_enumerations
|
16
|
+
self._enumerations = []
|
17
|
+
end
|
18
|
+
|
19
|
+
module ClassMethods
|
20
|
+
# Create an enumeration for the symbol <tt>name</tt>.
|
21
|
+
# Options include <tt>foreign_key</tt> attribute and <tt>class_name</tt>
|
22
|
+
#
|
23
|
+
# Example:
|
24
|
+
#
|
25
|
+
# class User < ActiveRecord::Base
|
26
|
+
# enumeration :role
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# user.role_id = 1
|
30
|
+
# user.role => #<Enumeration::Value:0x007fff45d7ec30 @base=Role, @symbol=:admin...>
|
31
|
+
#
|
32
|
+
# user.role = Role.staff
|
33
|
+
# user.role_id => 2
|
34
|
+
#
|
35
|
+
# TODO: add documentation for foreign_key and class_name
|
36
|
+
def enumeration(name, options = {})
|
37
|
+
options[:foreign_key] ||= "#{name}_id".to_sym
|
38
|
+
options[:class_name] ||= name.to_s.camelize
|
39
|
+
|
40
|
+
add_enumeration(name, options)
|
41
|
+
end
|
42
|
+
|
43
|
+
# Output all the enumerations that this model has defined
|
44
|
+
# Returns an array of Reflection objects for all the
|
45
|
+
# enumerations in the class.
|
46
|
+
#
|
47
|
+
# Example:
|
48
|
+
#
|
49
|
+
# User.reflect_on_all_enumerations => # [
|
50
|
+
# #<Enumeration::Reflection:0x007fe894724320 @name=:role...>,
|
51
|
+
# #<Enumeration::Reflection:0x007fe89471d020 @name=:status...>]
|
52
|
+
#
|
53
|
+
def reflect_on_all_enumerations
|
54
|
+
_enumerations
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def add_enumeration(name, options)
|
60
|
+
# Getter for belongs_to
|
61
|
+
#
|
62
|
+
# Example:
|
63
|
+
#
|
64
|
+
# user.role_id = 1
|
65
|
+
# user.role => #<Enumeration::Value:0x007fff45d7ec30 @base=Role, @symbol=:admin...>
|
66
|
+
#
|
67
|
+
define_method name do
|
68
|
+
enumerator_class = if options[:class_name].is_a?(Class)
|
69
|
+
options[:class_name]
|
70
|
+
else
|
71
|
+
options[:class_name].constantize
|
72
|
+
end
|
73
|
+
|
74
|
+
enumerator_class.find(send(options[:foreign_key]))
|
75
|
+
end
|
76
|
+
|
77
|
+
# Setter for belongs_to
|
78
|
+
#
|
79
|
+
# Example:
|
80
|
+
#
|
81
|
+
# user.role = Role.admin
|
82
|
+
# user.role_id => 1
|
83
|
+
#
|
84
|
+
define_method "#{name}=" do |other|
|
85
|
+
send("#{options[:foreign_key]}=", other.id)
|
86
|
+
end
|
87
|
+
|
88
|
+
self._enumerations += [Reflection.new(name, options)]
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
# Extend ActiveRecord with Enumeration capabilites
|
94
|
+
ActiveSupport.on_load(:active_record) do
|
95
|
+
include Enumeration
|
96
|
+
end
|
@@ -0,0 +1,187 @@
|
|
1
|
+
require 'active_support/core_ext/class/attribute'
|
2
|
+
require 'active_support/core_ext/string/inflections'
|
3
|
+
|
4
|
+
module Enumeration
|
5
|
+
class Base
|
6
|
+
class_attribute :_values, :_symbol_index
|
7
|
+
self._values = {}
|
8
|
+
self._symbol_index = {}
|
9
|
+
|
10
|
+
# Adding new value to enumeration
|
11
|
+
#
|
12
|
+
# Example:
|
13
|
+
#
|
14
|
+
# value :admin, id: 1, name: 'Admin', description: 'Some description...'
|
15
|
+
#
|
16
|
+
# Role.admin.id => # 1
|
17
|
+
# Role.find(:admin).name => # "Admin"
|
18
|
+
# Role.find(1).description => # "Some description..."
|
19
|
+
#
|
20
|
+
def self.value(symbol, attributes)
|
21
|
+
# TODO: make this errors better if needed
|
22
|
+
# TODO: test this errors
|
23
|
+
raise 'Enumeration id is required' if attributes[:id].nil?
|
24
|
+
raise "Duplicate symbol #{symbol}" if find(symbol)
|
25
|
+
raise "Duplicate id #{attributes[:id]}" if find(attributes[:id])
|
26
|
+
|
27
|
+
self._values = _values.merge(symbol => new(symbol, attributes))
|
28
|
+
self._symbol_index = _symbol_index.merge(symbol => attributes[:id])
|
29
|
+
|
30
|
+
# Adds name base finder methods
|
31
|
+
#
|
32
|
+
# Example:
|
33
|
+
#
|
34
|
+
# Role.admin => #<Enumeration::Value:0x007fff45d7ec30 @base=Role, @symbol=:admin...>
|
35
|
+
# Role.staff => #<Enumeration::Value:0x007f980e9cb0a0 @base=Role, @symbol=:staff...>
|
36
|
+
#
|
37
|
+
singleton_class.send(:define_method, symbol) do
|
38
|
+
find(symbol)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# Adding multiple values to enumeration
|
43
|
+
#
|
44
|
+
# Example:
|
45
|
+
#
|
46
|
+
# values admin: { id: 1, name: 'Admin' },
|
47
|
+
# manager: { id: 2, name: 'Manager' },
|
48
|
+
# staff: { id: 3, name: 'Staff', description: 'Some description...' }
|
49
|
+
#
|
50
|
+
# Role.admin.id => # 1
|
51
|
+
# Role.find(:manager).name => # "Manager"
|
52
|
+
# Role.find(3).description => # "Some description..."
|
53
|
+
#
|
54
|
+
def self.values(values)
|
55
|
+
values.each do |symbol, attributes|
|
56
|
+
value(symbol, attributes)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# Returns an array of all enumeration symbols
|
61
|
+
#
|
62
|
+
# Example:
|
63
|
+
#
|
64
|
+
# Role.symbols => # [:admin, :manager, :staff]
|
65
|
+
#
|
66
|
+
def self.symbols
|
67
|
+
_values.keys
|
68
|
+
end
|
69
|
+
|
70
|
+
# Returns an array of all enumeration values
|
71
|
+
#
|
72
|
+
# Example:
|
73
|
+
#
|
74
|
+
# Role.all => # [#<Enumeration::Value:0x007f8ed7f46100 @base=Role, @symbol=:admin...>,
|
75
|
+
# #<Enumeration::Value:0x007f8ed7f45de0 @base=Role, @symbol=:manager...>,
|
76
|
+
# #<Enumeration::Value:0x007f8ed7f45ae8 @base=Role, @symbol=:staff...>]
|
77
|
+
#
|
78
|
+
def self.all
|
79
|
+
_values.values
|
80
|
+
end
|
81
|
+
|
82
|
+
# Finds an enumeration by symbol, id or name
|
83
|
+
#
|
84
|
+
# Example:
|
85
|
+
#
|
86
|
+
# Role.find(:admin) => #<Enumeration::Value:0x007f8ed7f46100 @base=Role, @symbol=:admin...>
|
87
|
+
# Role.find(2) => #<Enumeration::Value:0x007f8ed7f45de0 @base=Role, @symbol=:manager...>
|
88
|
+
# Role.find('2') => #<Enumeration::Value:0x007f8ed7f45de0 @base=Role, @symbol=:manager...>
|
89
|
+
# Role.find('staff') => #<Enumeration::Value:0x007f8ed7f45ae8 @base=Role, @symbol=:staff...>
|
90
|
+
#
|
91
|
+
def self.find(key)
|
92
|
+
case key
|
93
|
+
when Symbol then find_by_key(key)
|
94
|
+
when String then find_by_key(key.to_sym) || find_by_id(key.to_i)
|
95
|
+
when Integer then find_by_id(key)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
# Finds an enumeration by defined attribute. Simmilar to AcriveRecord::FinderMethods#find_by
|
100
|
+
#
|
101
|
+
# Example:
|
102
|
+
#
|
103
|
+
# Role.find_by(name: 'Admin') => #<Enumeration::Value:0x007f8ed7f46100 @base=Role, @symbol=:admin...>
|
104
|
+
#
|
105
|
+
def self.find_by(**args)
|
106
|
+
_values.values.find { |value| args.map { |k, v| value.send(k) == v }.all? }
|
107
|
+
end
|
108
|
+
|
109
|
+
def self.find_by_key(key)
|
110
|
+
_values[key]
|
111
|
+
end
|
112
|
+
|
113
|
+
def self.find_by_id(id)
|
114
|
+
_values[_symbol_index.key(id)]
|
115
|
+
end
|
116
|
+
|
117
|
+
def initialize(symbol, attributes)
|
118
|
+
@symbol = symbol
|
119
|
+
@attributes = attributes
|
120
|
+
create_instance_methods
|
121
|
+
end
|
122
|
+
|
123
|
+
attr_reader :symbol
|
124
|
+
|
125
|
+
def to_i
|
126
|
+
id
|
127
|
+
end
|
128
|
+
|
129
|
+
def to_s
|
130
|
+
name
|
131
|
+
end
|
132
|
+
|
133
|
+
def to_sym
|
134
|
+
@symbol
|
135
|
+
end
|
136
|
+
|
137
|
+
def to_param
|
138
|
+
id
|
139
|
+
end
|
140
|
+
|
141
|
+
# Comparison by id, symbol or object
|
142
|
+
#
|
143
|
+
# Example:
|
144
|
+
#
|
145
|
+
# Role.admin == 1 => true
|
146
|
+
# Role.admin == :admin => true
|
147
|
+
# Role.admin == Role.admin => true
|
148
|
+
# Role.admin == 2 => false
|
149
|
+
# Role.admin == :staff => false
|
150
|
+
# Role.admin == Role.staff => false
|
151
|
+
#
|
152
|
+
# TODO: test if case..when is working with this
|
153
|
+
def ==(other)
|
154
|
+
case other
|
155
|
+
when Integer then other == id
|
156
|
+
when Symbol then other == @symbol
|
157
|
+
else super
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
private
|
162
|
+
|
163
|
+
# Getters for all attributes
|
164
|
+
#
|
165
|
+
# Example:
|
166
|
+
#
|
167
|
+
# Role.admin => #<Enumeration::Value:0x007fff45d7ec30 @base=Role, @symbol=:admin,
|
168
|
+
# @attributes={:id=>1, :name=>"Admin", :description=>"Some description..."}>
|
169
|
+
# user.role.id => # 1
|
170
|
+
# user.role.name => # "Admin"
|
171
|
+
# user.role.description => # "Some description..."
|
172
|
+
# user.role.admin? => # true
|
173
|
+
# user.role.staff? => # false
|
174
|
+
#
|
175
|
+
def create_instance_methods
|
176
|
+
@attributes.each do |key, _|
|
177
|
+
self.class.send :define_method, key do
|
178
|
+
@attributes[key]
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
self.class.send :define_method, "#{@symbol}?" do
|
183
|
+
__callee__[0..-2].to_sym == @symbol
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Enumeration
|
2
|
+
class Reflection
|
3
|
+
attr_reader :name
|
4
|
+
|
5
|
+
def initialize(name, options)
|
6
|
+
@name = name
|
7
|
+
@options = options
|
8
|
+
end
|
9
|
+
|
10
|
+
def class_name
|
11
|
+
@options[:class_name]
|
12
|
+
end
|
13
|
+
|
14
|
+
def foreign_key
|
15
|
+
@options[:foreign_key]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/test/base_test.rb
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
require_relative 'test_helper'
|
2
|
+
|
3
|
+
class BaseTest < Minitest::Test
|
4
|
+
def test_lookup_by_symbol
|
5
|
+
status = Status.find(:draft)
|
6
|
+
|
7
|
+
assert_equal :draft, status.symbol
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_lookup_fail_by_symbol
|
11
|
+
status = Status.find(:draft)
|
12
|
+
|
13
|
+
refute_same :published, status.symbol
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_find_by
|
17
|
+
status = Status.find_by(name: 'Draft')
|
18
|
+
|
19
|
+
assert_equal :draft, status.symbol
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_fail_find_by
|
23
|
+
status = Status.find_by(name: 'Draft1')
|
24
|
+
|
25
|
+
assert_equal nil, status
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_all
|
29
|
+
statuses = Status.all
|
30
|
+
|
31
|
+
assert_equal 5, statuses.size
|
32
|
+
assert_equal statuses.first, Status.draft
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_duplicated_id
|
36
|
+
assert_raises 'Duplicate id 1' do
|
37
|
+
Class.new.values draft: { id: 1, name: 'Draft' },
|
38
|
+
test: { id: 1, name: 'Draft' }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_duplicated_symbol
|
43
|
+
assert_raises 'Duplicate symbol draft' do
|
44
|
+
obj = Class.new
|
45
|
+
|
46
|
+
obj.value :draft, id: 1, name: 'Draft'
|
47
|
+
obj.value :draft, id: 2, name: 'Draft Again'
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_all_has_custom_attributes
|
52
|
+
statuses = Status.all
|
53
|
+
|
54
|
+
assert_silent do
|
55
|
+
statuses.map(&:visible)
|
56
|
+
statuses.map(&:deleted)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_with_defined_custom_attributes_visible
|
61
|
+
status = Status.find(:none)
|
62
|
+
|
63
|
+
assert_equal true, status.visible
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_with_defined_custom_attributes_deleted
|
67
|
+
status = Status.find(:deleted)
|
68
|
+
|
69
|
+
assert_equal true, status.deleted
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_without_defined_custom_attributes
|
73
|
+
status = Status.find(:draft)
|
74
|
+
|
75
|
+
assert_equal nil, status.visible
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_equal_by_id
|
79
|
+
status = Status.find(:draft)
|
80
|
+
|
81
|
+
assert_equal true, status == 1
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_equal_by_symbol
|
85
|
+
status = Status.draft
|
86
|
+
|
87
|
+
assert_equal true, status == :draft
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_equal_by_enumeration
|
91
|
+
status = Status.draft
|
92
|
+
|
93
|
+
assert_equal true, status == Status.draft
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_not_equal_by_enumeration
|
97
|
+
status = Status.draft
|
98
|
+
|
99
|
+
assert_equal false, status == Status.published
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/test_helper')
|
2
|
+
|
3
|
+
class EnumerationsTest < Minitest::Test
|
4
|
+
def test_reflect_on_all_enumerations
|
5
|
+
enumerations = Post.reflect_on_all_enumerations
|
6
|
+
|
7
|
+
assert_equal 2, enumerations.size
|
8
|
+
assert_equal :status, enumerations.first.name
|
9
|
+
assert_equal 'Status', enumerations.first.class_name
|
10
|
+
|
11
|
+
assert_equal :some_other_status_id, enumerations[1].foreign_key
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_model_enumeration_assignment
|
15
|
+
p = Post.new
|
16
|
+
p.status = Status.draft
|
17
|
+
|
18
|
+
assert_equal 'Draft', p.status.to_s
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_model_via_id_assignment
|
22
|
+
p = Post.new
|
23
|
+
p.some_other_status_id = Status.published.id
|
24
|
+
|
25
|
+
assert_equal 'Published', p.different_status.to_s
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_boolean_lookup
|
29
|
+
p = Post.new
|
30
|
+
p.status = Status.draft
|
31
|
+
|
32
|
+
assert_equal true, p.status.draft?
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_false_boolean_lookup
|
36
|
+
p = Post.new
|
37
|
+
p.status = Status.draft
|
38
|
+
|
39
|
+
assert_equal false, p.status.published?
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require_relative 'test_helper'
|
2
|
+
|
3
|
+
class ReflectionTest < Minitest::Test
|
4
|
+
def test_reflections
|
5
|
+
reflection = Enumeration::Reflection.new(:role, class_name: 'Role', foreign_key: :role_id)
|
6
|
+
|
7
|
+
assert_equal :role, reflection.name
|
8
|
+
assert_equal 'Role', reflection.class_name
|
9
|
+
assert_equal :role_id, reflection.foreign_key
|
10
|
+
end
|
11
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# TODO: improve tests
|
2
|
+
require 'minitest/autorun'
|
3
|
+
require 'enumerations'
|
4
|
+
require 'pry'
|
5
|
+
|
6
|
+
# Faking ActiveRecord
|
7
|
+
class MockActiveRecordBase
|
8
|
+
include Enumeration
|
9
|
+
end
|
10
|
+
|
11
|
+
class Status < Enumeration::Base
|
12
|
+
values draft: { id: 1, name: 'Draft' },
|
13
|
+
review_pending: { id: 2, name: 'Review pending' },
|
14
|
+
published: { id: 3, name: 'Published' }
|
15
|
+
|
16
|
+
value :none, id: 4, name: 'None', visible: true
|
17
|
+
value :deleted, id: 5, deleted: true
|
18
|
+
end
|
19
|
+
|
20
|
+
class Post < MockActiveRecordBase
|
21
|
+
attr_accessor :status_id, :some_other_status_id
|
22
|
+
|
23
|
+
enumeration :status
|
24
|
+
enumeration :different_status, foreign_key: :some_other_status_id, class_name: 'Status'
|
25
|
+
end
|
metadata
CHANGED
@@ -1,45 +1,108 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enumerations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
5
|
-
prerelease:
|
4
|
+
version: 1.3.2
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Tomislav Car
|
9
|
-
|
8
|
+
- Nikica Jokic
|
9
|
+
- Nikola Santic
|
10
|
+
autorequire:
|
10
11
|
bindir: bin
|
11
12
|
cert_chain: []
|
12
|
-
date: 2010-
|
13
|
-
dependencies:
|
13
|
+
date: 2010-08-20 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activerecord
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
requirements:
|
19
|
+
- - ">="
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: '0'
|
29
|
+
- !ruby/object:Gem::Dependency
|
30
|
+
name: activesupport
|
31
|
+
requirement: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
type: :runtime
|
37
|
+
prerelease: false
|
38
|
+
version_requirements: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
- !ruby/object:Gem::Dependency
|
44
|
+
name: pry-byebug
|
45
|
+
requirement: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
type: :development
|
51
|
+
prerelease: false
|
52
|
+
version_requirements: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
14
57
|
description: Extends ActiveRecord with enumeration capabilites.
|
15
|
-
email:
|
58
|
+
email:
|
59
|
+
- tomislav@infinum.hr
|
60
|
+
- nikica.jokic@infinum.hr
|
61
|
+
- nikola.santic@infinum.hr
|
16
62
|
executables: []
|
17
63
|
extensions: []
|
18
64
|
extra_rdoc_files: []
|
19
65
|
files:
|
20
|
-
-
|
21
|
-
|
66
|
+
- ".gitignore"
|
67
|
+
- ".travis.yml"
|
68
|
+
- Gemfile
|
69
|
+
- Rakefile
|
70
|
+
- Readme.md
|
71
|
+
- enumerations.gemspec
|
72
|
+
- lib/enumerations.rb
|
73
|
+
- lib/enumerations/base.rb
|
74
|
+
- lib/enumerations/reflection.rb
|
75
|
+
- lib/enumerations/version.rb
|
76
|
+
- test/base_test.rb
|
77
|
+
- test/enumerations_test.rb
|
78
|
+
- test/reflection_test.rb
|
79
|
+
- test/test_helper.rb
|
80
|
+
- test/version_test.rb
|
81
|
+
homepage: https://github.com/infinum/enumerations
|
22
82
|
licenses: []
|
23
|
-
|
83
|
+
metadata: {}
|
84
|
+
post_install_message:
|
24
85
|
rdoc_options: []
|
25
86
|
require_paths:
|
26
87
|
- lib
|
27
88
|
required_ruby_version: !ruby/object:Gem::Requirement
|
28
|
-
none: false
|
29
89
|
requirements:
|
30
|
-
- -
|
90
|
+
- - ">="
|
31
91
|
- !ruby/object:Gem::Version
|
32
92
|
version: '0'
|
33
93
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
-
none: false
|
35
94
|
requirements:
|
36
|
-
- -
|
95
|
+
- - ">="
|
37
96
|
- !ruby/object:Gem::Version
|
38
97
|
version: '0'
|
39
98
|
requirements: []
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
99
|
+
rubygems_version: 3.0.3
|
100
|
+
signing_key:
|
101
|
+
specification_version: 4
|
102
|
+
summary: Enumerations for ActiveRecord!
|
103
|
+
test_files:
|
104
|
+
- test/base_test.rb
|
105
|
+
- test/enumerations_test.rb
|
106
|
+
- test/reflection_test.rb
|
107
|
+
- test/test_helper.rb
|
108
|
+
- test/version_test.rb
|
data/init.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
ActiveRecord::Base.send(:include, Enumeration)
|