activerecord-real_enums 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +85 -0
- data/Rakefile +1 -0
- data/activerecord-real_enums.gemspec +28 -0
- data/lib/activerecord/real_enums.rb +7 -0
- data/lib/activerecord/real_enums/models/postgresql.rb +30 -0
- data/lib/activerecord/real_enums/railtie.rb +18 -0
- data/lib/activerecord/real_enums/version.rb +5 -0
- metadata +144 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f219b30cbc3e02a1183e61e56d728b873828bbfd
|
4
|
+
data.tar.gz: 474b1d789cca513f6f3f2bc563d0b6af00b933ea
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9de24cce1b13f9ee0854ea1d3a4c9861fa65eb607d2003de42ccf17dcf4da904fe85e561df179c477082eb6bb6f48b0600515fd7abc0c41f25c71353a1380b7c
|
7
|
+
data.tar.gz: 8feedcc09b4e0af38e6793b995b3d48fba87c6edb783969f93e6fad3662df0eb445cb34fbce327e68a70803670b02df22791eb61a89bf0416b9e5f14e7feefe6
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Leif Gensert
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
# Activerecord::RealEnums
|
2
|
+
|
3
|
+
Supporting enumerations in Rails has up to now always been done on the application layer.
|
4
|
+
|
5
|
+
The [symbolize](https://github.com/nofxx/symbolize) gem allows storing a limited set of different string values to a field. On a database level it is still possible to store abritrary strings.
|
6
|
+
|
7
|
+
Rails 4.1 introduced [enums](http://api.rubyonrails.org/v4.1.0/classes/ActiveRecord/Enum.html) which fulfils the same purpose but stores the different types as integers. The mapping, validation is also handled on the application layer. And even worse in order to query for a specific enum it is necessary to lookup the according values for each query:
|
8
|
+
|
9
|
+
Conversation.where(status: Conversation.statuses[:archived])
|
10
|
+
|
11
|
+
This gem takes advantage of the [PostgreSQL enum type](http://www.postgresql.org/docs/9.1/static/datatype-enum.html) that will handle all the validations:
|
12
|
+
|
13
|
+
Conversation.where(status: :archived)
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
gem 'activerecord-real_enums'
|
21
|
+
```
|
22
|
+
|
23
|
+
And then execute:
|
24
|
+
|
25
|
+
$ bundle
|
26
|
+
|
27
|
+
Or install it yourself as:
|
28
|
+
|
29
|
+
$ gem install activerecord-real_enums
|
30
|
+
|
31
|
+
## Usage
|
32
|
+
|
33
|
+
### Migrations
|
34
|
+
|
35
|
+
To be able to use PostgreSQL enums, I suggest to create the according type in a separate migration:
|
36
|
+
|
37
|
+
class CreateConversationStatusEnum < ActiveRecord::Migration
|
38
|
+
def up
|
39
|
+
execute "CREATE TYPE conversation_status AS ENUM ("active", "archived");"
|
40
|
+
end
|
41
|
+
|
42
|
+
def down
|
43
|
+
execute "DROP TYPE conversation_status;"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
from then on, the type `conversation_status` can be used as a regular type:
|
48
|
+
|
49
|
+
class AddColumnsToTeam < ActiveRecord::Migration
|
50
|
+
def change
|
51
|
+
add_column :conversations, :status, :conversation_status
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
### Model
|
56
|
+
|
57
|
+
To reference the type correctly (e.g. for queries) the enum needs to be added to the model:
|
58
|
+
|
59
|
+
|
60
|
+
class Converation < ActiveRecord::Base
|
61
|
+
real_enum :status, type: :conversation_status
|
62
|
+
end
|
63
|
+
|
64
|
+
`real_enums` will add the necessary validations in the background.
|
65
|
+
|
66
|
+
From then on, it is possible to query all conversations by status:
|
67
|
+
|
68
|
+
Conversation.where(status: :archived) # strings and symbols, both work
|
69
|
+
|
70
|
+
|
71
|
+
## Caveats
|
72
|
+
|
73
|
+
This is a first draft. Completely usable but not feature complete:
|
74
|
+
|
75
|
+
- no posibility to query all available values for enum
|
76
|
+
- currently only support for PostgreSQL (although [MySQL](http://dev.mysql.com/doc/refman/5.0/en/enum.html) also has an enum type)
|
77
|
+
- no helper methods for migration (AFAIK not officically supported to write own migration methods (that are reversible))
|
78
|
+
|
79
|
+
## Contributing
|
80
|
+
|
81
|
+
1. Fork it ( https://github.com/[my-github-username]/activerecord-real_enums/fork )
|
82
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
83
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
84
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
85
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'activerecord/real_enums/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "activerecord-real_enums"
|
8
|
+
spec.version = Activerecord::RealEnums::VERSION
|
9
|
+
spec.authors = ["Leif Gensert"]
|
10
|
+
spec.email = ["leif@propertybase.com"]
|
11
|
+
spec.summary = "PG Enum support for AcviteRecord "
|
12
|
+
spec.description = %q{Add support for Postgresql Enum type in ActiveRecord}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "activemodel"
|
22
|
+
|
23
|
+
spec.add_development_dependency "activerecord", ">= 4.0", "< 5.0"
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "pry"
|
27
|
+
spec.add_development_dependency "rspec"
|
28
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module RealEnums
|
3
|
+
module Models
|
4
|
+
module Postgresql
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
def self.included(base)
|
8
|
+
base.extend ClassMethods
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def real_enum(name, options)
|
13
|
+
sql = "SELECT unnest(enum_range(NULL::#{options.fetch(:type)}))"
|
14
|
+
|
15
|
+
values = ::ActiveRecord::Base.
|
16
|
+
connection.
|
17
|
+
execute(sql).
|
18
|
+
map { |e| e["unnest"] }
|
19
|
+
|
20
|
+
validates_inclusion_of(name, in: values)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class ActiveRecord::Base
|
29
|
+
include ActiveRecord::RealEnums::Models::Postgresql
|
30
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module RealEnums
|
3
|
+
require "rails"
|
4
|
+
class Railtie < Rails::Railtie
|
5
|
+
initializer "real_enums.insert_into_active_record" do
|
6
|
+
ActiveSupport.on_load :active_record do
|
7
|
+
case ::ActiveRecord::Base.connection
|
8
|
+
when ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
|
9
|
+
require "activerecord/real_enums/models/postgresql"
|
10
|
+
::ActiveRecord::Base.send :include, ActiveRecord::RealEnums::Models::Postgresql
|
11
|
+
else
|
12
|
+
warn("enum type not supported in this DB type")
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord-real_enums
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Leif Gensert
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-10-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activemodel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activerecord
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4.0'
|
34
|
+
- - "<"
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '5.0'
|
37
|
+
type: :development
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '4.0'
|
44
|
+
- - "<"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '5.0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: bundler
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '1.7'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '1.7'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: rake
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '10.0'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - "~>"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '10.0'
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: pry
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
type: :development
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: rspec
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
type: :development
|
97
|
+
prerelease: false
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
description: Add support for Postgresql Enum type in ActiveRecord
|
104
|
+
email:
|
105
|
+
- leif@propertybase.com
|
106
|
+
executables: []
|
107
|
+
extensions: []
|
108
|
+
extra_rdoc_files: []
|
109
|
+
files:
|
110
|
+
- ".gitignore"
|
111
|
+
- Gemfile
|
112
|
+
- LICENSE.txt
|
113
|
+
- README.md
|
114
|
+
- Rakefile
|
115
|
+
- activerecord-real_enums.gemspec
|
116
|
+
- lib/activerecord/real_enums.rb
|
117
|
+
- lib/activerecord/real_enums/models/postgresql.rb
|
118
|
+
- lib/activerecord/real_enums/railtie.rb
|
119
|
+
- lib/activerecord/real_enums/version.rb
|
120
|
+
homepage: ''
|
121
|
+
licenses:
|
122
|
+
- MIT
|
123
|
+
metadata: {}
|
124
|
+
post_install_message:
|
125
|
+
rdoc_options: []
|
126
|
+
require_paths:
|
127
|
+
- lib
|
128
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
129
|
+
requirements:
|
130
|
+
- - ">="
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
134
|
+
requirements:
|
135
|
+
- - ">="
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: '0'
|
138
|
+
requirements: []
|
139
|
+
rubyforge_project:
|
140
|
+
rubygems_version: 2.2.2
|
141
|
+
signing_key:
|
142
|
+
specification_version: 4
|
143
|
+
summary: PG Enum support for AcviteRecord
|
144
|
+
test_files: []
|