native_enum 1.0.0pre1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +6 -0
- data/.ruby-version +1 -0
- data/.travis.yml +11 -0
- data/Gemfile +4 -0
- data/LICENSE +19 -0
- data/README.markdown +85 -0
- data/Rakefile +52 -0
- data/activerecord_enum.gemspec +28 -0
- data/lib/connection_adapters/mysql2.rb +30 -0
- data/lib/connection_adapters/sqlite3.rb +16 -0
- data/lib/native_enum/activerecord_enum_post42.rb +63 -0
- data/lib/native_enum/activerecord_enum_pre42.rb +46 -0
- data/lib/native_enum/version.rb +3 -0
- data/lib/native_enum.rb +30 -0
- data/spec/.travis.database.yml +9 -0
- data/spec/Gemfile.rails_3_0 +4 -0
- data/spec/Gemfile.rails_3_1 +4 -0
- data/spec/Gemfile.rails_3_2 +4 -0
- data/spec/Gemfile.rails_4_0 +4 -0
- data/spec/Gemfile.rails_4_1 +4 -0
- data/spec/Gemfile.rails_4_2 +4 -0
- data/spec/database.yml.tmpl +11 -0
- data/spec/enum_spec.rb +47 -0
- data/spec/schema/enum_new.rb +6 -0
- data/spec/schema/enum_old.rb +5 -0
- data/spec/schema/set_new.rb +6 -0
- data/spec/schema/set_old.rb +5 -0
- data/spec/set_spec.rb +49 -0
- data/spec/spec_helper.rb +48 -0
- metadata +171 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ace3e7f2ea35ac207901df13c5ec3724f925d249
|
4
|
+
data.tar.gz: 5986573cdb6492749652d5ac586f2480bcba03ef
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 07baf33b4fa0d7460d6d20fc4cd3e1fcc33acbeefe3a469ba55d6df39a7d7700b1459310837cf91666a06d4216cb02a3e021f8511aa707c4eb6aeb06bd057615
|
7
|
+
data.tar.gz: e1678c6a6c4bb07451b55af4df872289efb50138cb529fa9a2e71d4f251598febb4ae39d213cb7ad6fda59b22611cee3aec12f67c191a5e64992ec63ee6d70f6
|
data/.gitignore
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.1.2
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (C) 2011 by Ian Young
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
# Native Enum #
|
2
|
+
|
3
|
+
Provides ActiveRecord (and thus Rails) support for the nonstandard `ENUM` and `SET` data types.
|
4
|
+
|
5
|
+
[](http://travis-ci.org/iangreenleaf/native_enum)
|
6
|
+
|
7
|
+
## How now? ##
|
8
|
+
|
9
|
+
It sucks to have ActiveRecord not understand your `ENUM` columns and constantly write the wrong thing to your `schema.rb`.
|
10
|
+
It also sucks to work with a database that uses `ENUM` in production when you'd prefer sqlite in development.
|
11
|
+
Wait no longer...
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
create_table :balloons, :force => true do |t|
|
15
|
+
t.enum "color", :limit => ['red', 'gold'], :default => 'gold', :null => false
|
16
|
+
# or...
|
17
|
+
t.column "size", :enum, :limit => ['small', 'medium', 'large']
|
18
|
+
end
|
19
|
+
```
|
20
|
+
|
21
|
+
Your schema<->db coupling will work again, and it will fall back to a `VARCHAR` column on any adapters that don't support `ENUM`.
|
22
|
+
|
23
|
+
## Installation ##
|
24
|
+
|
25
|
+
```
|
26
|
+
gem 'native_enum'
|
27
|
+
```
|
28
|
+
|
29
|
+
Boy, that was easy.
|
30
|
+
|
31
|
+
## Hypothetically asked questions ##
|
32
|
+
|
33
|
+
### Y U NO WORK?! ###
|
34
|
+
|
35
|
+
It currently works with:
|
36
|
+
|
37
|
+
* ActiveRecord 3.x and 4.0, 4.1, and 4.2.
|
38
|
+
* The `mysql2` and `sqlite` adapters.
|
39
|
+
* Ruby 1.9.3, 2.0.x, 2.1.x, and 2.2.x.
|
40
|
+
|
41
|
+
If you'd like to support other adapters, pull requests are welcome!
|
42
|
+
|
43
|
+
### I thought Rails 4.1+ did enums? ###
|
44
|
+
|
45
|
+
[ActiveRecord::Enum](http://api.rubyonrails.org/classes/ActiveRecord/Enum.html) is actually a layer that takes an integer column in the DB and *presents* it as an enum type.
|
46
|
+
This gem provides compatibility for *native* `ENUM` types in the DB.
|
47
|
+
These are fundamentally different approaches, with different motivations, costs and benefits.
|
48
|
+
|
49
|
+
### Why doesn't it validate anything? ###
|
50
|
+
|
51
|
+
Following ActiveRecord's lead, this plugin doesn't do any validation work itself.
|
52
|
+
|
53
|
+
For ENUM columns, you may be satisfied with something simple:
|
54
|
+
|
55
|
+
validates_inclusion_of :attr, :in => [ :possible, :values ]
|
56
|
+
|
57
|
+
Or if you prefer more bells and whistles, try [brainspec/enumerize](https://github.com/brainspec/enumerize).
|
58
|
+
|
59
|
+
For SET columns, you may be interested in [iangreenleaf/active_set](https://github.com/iangreenleaf/active_set).
|
60
|
+
|
61
|
+
### Nonstandard SQL?! What's your problem, jerkweed? ###
|
62
|
+
|
63
|
+
This isn't a plugin everyone should use. There are a number of plugins to simulate enum behavior backed by standard data types. Personally, I like [nofxx/symbolize](https://github.com/nofxx/symbolize).
|
64
|
+
|
65
|
+
However, sometimes we can't or won't avoid working with these data types. When that happens, I got you covered.
|
66
|
+
|
67
|
+
## Contributing ##
|
68
|
+
|
69
|
+
Pull requests welcome! Join
|
70
|
+
[this lovely bunch of people](https://github.com/iangreenleaf/native_enum/graphs/contributors).
|
71
|
+
|
72
|
+
|
73
|
+
### Running the tests ###
|
74
|
+
|
75
|
+
To run the tests for all supported database adapters:
|
76
|
+
|
77
|
+
rake spec:all
|
78
|
+
|
79
|
+
To run the tests for all adapters and all versions of ActiveRecord:
|
80
|
+
|
81
|
+
rake spec:rails_all
|
82
|
+
|
83
|
+
To run the tests for just one adapter:
|
84
|
+
|
85
|
+
DB=mysql rake spec
|
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
require 'yaml'
|
3
|
+
Bundler::GemHelper.install_tasks
|
4
|
+
|
5
|
+
DB_CONFIG = "spec/database.yml"
|
6
|
+
GEMFILES = "spec/Gemfile.rails_[0-9]_[0-9]"
|
7
|
+
|
8
|
+
require 'rake'
|
9
|
+
desc 'Default: run all unit tests.'
|
10
|
+
task :default => :"spec:all"
|
11
|
+
|
12
|
+
namespace :db do
|
13
|
+
desc 'Prepare the databases.'
|
14
|
+
task :prepare do
|
15
|
+
unless File.exist? DB_CONFIG
|
16
|
+
cp "#{config_file}.tmpl", DB_CONFIG
|
17
|
+
end
|
18
|
+
#TODO would be nice to create the DBs here
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
require "rspec/core/rake_task"
|
23
|
+
desc 'Run the test suite.'
|
24
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
25
|
+
t.pattern = 'spec/*_spec.rb'
|
26
|
+
t.exclude_pattern = 'spec/**/vendor/*'
|
27
|
+
end
|
28
|
+
|
29
|
+
desc 'Run the test suite for all DBs.'
|
30
|
+
namespace :spec do
|
31
|
+
task :all do
|
32
|
+
db_config = YAML::load(IO.read(DB_CONFIG))
|
33
|
+
db_config.each do |db,config|
|
34
|
+
ENV["DB"] = db
|
35
|
+
Rake::Task["spec"].reenable
|
36
|
+
Rake::Task["spec"].invoke
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
desc 'Run the test suite for all supported versions of rails and all DBs'
|
41
|
+
task :rails_all do
|
42
|
+
STDOUT.sync = true
|
43
|
+
versions = Dir.glob(GEMFILES)
|
44
|
+
versions.each do |gemfile|
|
45
|
+
puts "Running specs for Gemfile: #{gemfile}"
|
46
|
+
Bundler.with_clean_env do
|
47
|
+
sh "bundle install --gemfile '#{gemfile}' --path 'vendor/#{File.extname(gemfile).slice(1..-1)}'"
|
48
|
+
sh "BUNDLE_GEMFILE='#{gemfile}' bundle exec rake spec:all"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "native_enum/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "native_enum"
|
7
|
+
s.version = NativeEnum::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Ian Young"]
|
10
|
+
s.email = ["ian.greenleaf+github@gmail.com"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{Enum data types for ActiveRecord}
|
13
|
+
s.description = %q{Adds the ENUM data type natively to ActiveRecord.}
|
14
|
+
|
15
|
+
s.rubyforge_project = "native_enum"
|
16
|
+
|
17
|
+
s.add_dependency "activerecord", ">= 3.0"
|
18
|
+
s.add_development_dependency "rake"
|
19
|
+
s.add_development_dependency "bundler"
|
20
|
+
s.add_development_dependency "mysql2", "~> 0.3.11"
|
21
|
+
s.add_development_dependency "sqlite3", "~>1.3.4"
|
22
|
+
s.add_development_dependency "rspec", "~> 3.1.0"
|
23
|
+
|
24
|
+
s.files = `git ls-files`.split("\n")
|
25
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
26
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
27
|
+
s.require_paths = ["lib"]
|
28
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'active_record/connection_adapters/mysql2_adapter'
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module ConnectionAdapters
|
5
|
+
existing_class = defined?( Mysql2Adapter ) ? Mysql2Adapter : AbstractMysqlAdapter
|
6
|
+
|
7
|
+
existing_class.class_eval do
|
8
|
+
def native_database_types_with_enum
|
9
|
+
native_database_types_without_enum.merge( :enum => { :name => "enum" }, :set => { :name => "set" } )
|
10
|
+
end
|
11
|
+
alias_method :native_database_types_without_enum, :native_database_types
|
12
|
+
alias_method :native_database_types, :native_database_types_with_enum
|
13
|
+
|
14
|
+
def type_to_sql_with_enum type, limit=nil, *args
|
15
|
+
if type.to_s == "enum" || type.to_s == "set"
|
16
|
+
"#{type}(#{quoted_comma_list limit})"
|
17
|
+
else
|
18
|
+
type_to_sql_without_enum type, limit, *args
|
19
|
+
end
|
20
|
+
end
|
21
|
+
alias_method :type_to_sql_without_enum, :type_to_sql
|
22
|
+
alias_method :type_to_sql, :type_to_sql_with_enum
|
23
|
+
|
24
|
+
private
|
25
|
+
def quoted_comma_list list
|
26
|
+
list.to_a.map{|n| "'#{n}'"}.join(",")
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'active_record/connection_adapters/sqlite3_adapter'
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module ConnectionAdapters
|
5
|
+
class SQLite3Adapter < (defined?(SQLiteAdapter) ? SQLiteAdapter : AbstractAdapter)
|
6
|
+
def type_to_sql_with_enum type, limit=nil, *args
|
7
|
+
if type.to_s == "enum" || type.to_s == "set"
|
8
|
+
type, limit = :string, nil
|
9
|
+
end
|
10
|
+
type_to_sql_without_enum type, limit, *args
|
11
|
+
end
|
12
|
+
alias_method :type_to_sql_without_enum, :type_to_sql
|
13
|
+
alias_method :type_to_sql, :type_to_sql_with_enum
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module ConnectionAdapters
|
3
|
+
if defined?(AbstractMysqlAdapter)
|
4
|
+
class AbstractMysqlAdapter
|
5
|
+
protected
|
6
|
+
def initialize_type_map_with_enum(m)
|
7
|
+
initialize_without_enum(m)
|
8
|
+
register_enum_type(m, %r(^enum)i)
|
9
|
+
register_set_type(m, %r(^set)i)
|
10
|
+
end
|
11
|
+
|
12
|
+
alias_method :initialize_without_enum, :initialize_type_map
|
13
|
+
alias_method :initialize_type_map, :initialize_type_map_with_enum
|
14
|
+
|
15
|
+
def register_enum_type(mapping, key)
|
16
|
+
mapping.register_type(key) do |sql_type|
|
17
|
+
if sql_type =~ /(?:enum)\(([^)]+)\)/i
|
18
|
+
limit = $1.scan( /'([^']*)'/ ).flatten
|
19
|
+
Type::Enum.new(limit: limit)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def register_set_type(mapping, key)
|
25
|
+
mapping.register_type(key) do |sql_type|
|
26
|
+
if sql_type =~ /(?:set)\(([^)]+)\)/i
|
27
|
+
limit = $1.scan( /'([^']*)'/ ).flatten
|
28
|
+
Type::Set.new(limit: limit)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
module Type
|
37
|
+
class Enum < Type::Value
|
38
|
+
def type
|
39
|
+
:enum
|
40
|
+
end
|
41
|
+
|
42
|
+
def initialize(options = {})
|
43
|
+
options.assert_valid_keys(:limit)
|
44
|
+
@limit = options[:limit]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
class Set < Type::Value
|
49
|
+
def type
|
50
|
+
:set
|
51
|
+
end
|
52
|
+
|
53
|
+
def initialize(options = {})
|
54
|
+
options.assert_valid_keys(:limit)
|
55
|
+
@limit = options[:limit]
|
56
|
+
end
|
57
|
+
|
58
|
+
def type_cast_from_database(value)
|
59
|
+
value.split(",")
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module ConnectionAdapters
|
3
|
+
class Column
|
4
|
+
def initialize_with_enum name, default, sql_type=nil, *args
|
5
|
+
initialize_without_enum name, default, sql_type, *args
|
6
|
+
@type = simplified_type_with_enum sql_type
|
7
|
+
@limit = extract_limit_with_enum sql_type
|
8
|
+
@default = extract_default_with_enum default
|
9
|
+
end
|
10
|
+
alias_method :initialize_without_enum, :initialize
|
11
|
+
alias_method :initialize, :initialize_with_enum
|
12
|
+
|
13
|
+
def simplified_type_with_enum field_type
|
14
|
+
if field_type =~ /enum|set/i
|
15
|
+
$&.to_sym
|
16
|
+
else
|
17
|
+
simplified_type field_type
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def extract_limit_with_enum field_type
|
22
|
+
if field_type =~ /(?:enum|set)\(([^)]+)\)/i
|
23
|
+
$1.scan( /'([^']*)'/ ).flatten
|
24
|
+
else
|
25
|
+
extract_limit field_type
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def extract_default_with_enum default
|
30
|
+
if type == :set
|
31
|
+
default.split "," if default.present?
|
32
|
+
else
|
33
|
+
extract_default default
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def set?
|
38
|
+
type == :set
|
39
|
+
end
|
40
|
+
|
41
|
+
def enum?
|
42
|
+
type == :enum
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/lib/native_enum.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'active_record/base'
|
3
|
+
require 'active_record/connection_adapters/abstract/schema_definitions.rb'
|
4
|
+
|
5
|
+
require 'connection_adapters/sqlite3' if defined?( SQLite3 )
|
6
|
+
require 'connection_adapters/mysql2' if defined?( Mysql2 )
|
7
|
+
|
8
|
+
if ActiveRecord::VERSION::MAJOR < 4 || (ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR <= 1)
|
9
|
+
require 'native_enum/activerecord_enum_pre42.rb'
|
10
|
+
else
|
11
|
+
require 'native_enum/activerecord_enum_post42.rb'
|
12
|
+
end
|
13
|
+
|
14
|
+
module ActiveRecord
|
15
|
+
module ConnectionAdapters
|
16
|
+
class TableDefinition
|
17
|
+
def enum *args
|
18
|
+
options = args.extract_options!
|
19
|
+
column_names = args
|
20
|
+
column_names.each { |name| column(name, :enum, options) }
|
21
|
+
end
|
22
|
+
def set *args
|
23
|
+
options = args.extract_options!
|
24
|
+
options[:default] = options[:default].join "," if options[:default].present?
|
25
|
+
column_names = args
|
26
|
+
column_names.each { |name| column(name, :set, options) }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/spec/enum_spec.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "ENUM datatype" do
|
4
|
+
|
5
|
+
describe "schema dump", :db_support => true do
|
6
|
+
before { load_schema "enum_old" }
|
7
|
+
subject { dumped_schema }
|
8
|
+
|
9
|
+
it "dumps native format" do
|
10
|
+
expect(subject).to match %r{t\.enum\s+"color",\s+(:limit =>|limit:) \["blue", "red", "yellow"\]}
|
11
|
+
end
|
12
|
+
|
13
|
+
it "dumps default option" do
|
14
|
+
expect(subject).to match %r{t\.enum\s+"color",.+(:default =>|default:) "red"}
|
15
|
+
end
|
16
|
+
|
17
|
+
it "dumps null option" do
|
18
|
+
expect(subject).to match %r{t\.enum\s+"color",.+(:null =>|null:) false$}
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "schema loading" do
|
23
|
+
before { load_schema "enum_new" }
|
24
|
+
subject { column_props :balloons, :color }
|
25
|
+
|
26
|
+
it "loads native format", :db_support => true do
|
27
|
+
expect(subject[:type]).to eq("enum('red','gold')")
|
28
|
+
end
|
29
|
+
|
30
|
+
it "falls back to text when missing db support", :db_support => false do
|
31
|
+
expect(subject[:type]).to match(/varchar/)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "loads default option" do
|
35
|
+
expect(subject[:default]).to eq("gold")
|
36
|
+
end
|
37
|
+
|
38
|
+
it "loads null option" do
|
39
|
+
expect(subject[:null]).to eq(false)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "loads native column format", :db_support => true do
|
43
|
+
subject = column_props :balloons, :size
|
44
|
+
expect(subject[:type]).to eq("enum('small','medium','large')")
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/spec/set_spec.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "SET datatype" do
|
4
|
+
|
5
|
+
describe "schema dump", :db_support => true do
|
6
|
+
before { load_schema "set_old" }
|
7
|
+
subject { dumped_schema }
|
8
|
+
|
9
|
+
it "dumps native format" do
|
10
|
+
expect(subject).to match %r{t\.set\s+"gadgets",\s+(:limit =>|limit:) \["propeller", "tail gun", "gps"\]}
|
11
|
+
end
|
12
|
+
|
13
|
+
it "dumps default option" do
|
14
|
+
expect(subject).to match %r{t\.set\s+"gadgets",.+(:default =>|default:) \["propeller", "gps"\]}
|
15
|
+
end
|
16
|
+
|
17
|
+
it "dumps null option" do
|
18
|
+
expect(subject).to match %r{t\.set\s+"gadgets",.+(:null =>|null:) false$}
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "schema loading" do
|
23
|
+
before { load_schema "set_new" }
|
24
|
+
subject { column_props :balloons, :ribbons }
|
25
|
+
|
26
|
+
it "loads native format", :db_support => true do
|
27
|
+
expect(subject[:type]).to eq("set('red','green','gold')")
|
28
|
+
end
|
29
|
+
|
30
|
+
it "falls back to text when missing db support", :db_support => false do
|
31
|
+
expect(subject[:type]).to match(/varchar/)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "loads default option" do
|
35
|
+
expect(subject[:default]).to eq("green,gold")
|
36
|
+
end
|
37
|
+
|
38
|
+
it "loads null option" do
|
39
|
+
expect(subject[:null]).to eq(false)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "loads native column format", :db_support => true do
|
43
|
+
subject = column_props :balloons, :gasses
|
44
|
+
expect(subject[:type]).to eq("set('helium','hydrogen')")
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
class Balloon < ActiveRecord::Base; end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
def db
|
5
|
+
ENV["DB"] || "mysql"
|
6
|
+
end
|
7
|
+
|
8
|
+
def load_schema filename
|
9
|
+
# silence verbose schema loading
|
10
|
+
original_stdout = $stdout
|
11
|
+
$stdout = StringIO.new
|
12
|
+
|
13
|
+
root = File.expand_path(File.dirname(__FILE__))
|
14
|
+
load root + "/schema/#{filename}.rb"
|
15
|
+
|
16
|
+
ensure
|
17
|
+
$stdout = original_stdout
|
18
|
+
end
|
19
|
+
|
20
|
+
def dumped_schema
|
21
|
+
stream = StringIO.new
|
22
|
+
ActiveRecord::SchemaDumper.ignore_tables = []
|
23
|
+
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
|
24
|
+
stream.string.lines.select {|l| /^\s*#/.match(l).nil? }.join
|
25
|
+
end
|
26
|
+
|
27
|
+
def column_props table, column
|
28
|
+
case db
|
29
|
+
when "mysql"
|
30
|
+
result = ActiveRecord::Base.connection.select_one "SHOW FIELDS FROM #{table} WHERE Field='#{column}'"
|
31
|
+
{ :type => result["Type"], :default => result["Default"], :null => ( result["Null"] == "YES" ) }
|
32
|
+
when "sqlite"
|
33
|
+
result = ActiveRecord::Base.connection.select_value "SELECT sql FROM sqlite_master WHERE type='table' AND name='#{table}'"
|
34
|
+
matches = /"#{column}" ([^[:space:]]+) (?:DEFAULT '([^[:space:]]+)')?( NOT NULL)?,/.match result
|
35
|
+
{ :type => matches[1], :default => matches[2], :null => matches[3].nil? }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
db_config = YAML::load(IO.read("spec/database.yml"))
|
40
|
+
|
41
|
+
require db_config[db]["adapter"]
|
42
|
+
require 'native_enum'
|
43
|
+
|
44
|
+
ActiveRecord::Base.configurations = db_config
|
45
|
+
ActiveRecord::Base.establish_connection db.to_sym
|
46
|
+
RSpec.configure do |c|
|
47
|
+
c.filter_run_excluding :db_support => ! db_config[db]["supports_enums"]
|
48
|
+
end
|
metadata
ADDED
@@ -0,0 +1,171 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: native_enum
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0pre1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ian Young
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: mysql2
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.3.11
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.3.11
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sqlite3
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 1.3.4
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 1.3.4
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 3.1.0
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 3.1.0
|
97
|
+
description: Adds the ENUM data type natively to ActiveRecord.
|
98
|
+
email:
|
99
|
+
- ian.greenleaf+github@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".ruby-version"
|
106
|
+
- ".travis.yml"
|
107
|
+
- Gemfile
|
108
|
+
- LICENSE
|
109
|
+
- README.markdown
|
110
|
+
- Rakefile
|
111
|
+
- activerecord_enum.gemspec
|
112
|
+
- lib/connection_adapters/mysql2.rb
|
113
|
+
- lib/connection_adapters/sqlite3.rb
|
114
|
+
- lib/native_enum.rb
|
115
|
+
- lib/native_enum/activerecord_enum_post42.rb
|
116
|
+
- lib/native_enum/activerecord_enum_pre42.rb
|
117
|
+
- lib/native_enum/version.rb
|
118
|
+
- spec/.travis.database.yml
|
119
|
+
- spec/Gemfile.rails_3_0
|
120
|
+
- spec/Gemfile.rails_3_1
|
121
|
+
- spec/Gemfile.rails_3_2
|
122
|
+
- spec/Gemfile.rails_4_0
|
123
|
+
- spec/Gemfile.rails_4_1
|
124
|
+
- spec/Gemfile.rails_4_2
|
125
|
+
- spec/database.yml.tmpl
|
126
|
+
- spec/enum_spec.rb
|
127
|
+
- spec/schema/enum_new.rb
|
128
|
+
- spec/schema/enum_old.rb
|
129
|
+
- spec/schema/set_new.rb
|
130
|
+
- spec/schema/set_old.rb
|
131
|
+
- spec/set_spec.rb
|
132
|
+
- spec/spec_helper.rb
|
133
|
+
homepage: ''
|
134
|
+
licenses: []
|
135
|
+
metadata: {}
|
136
|
+
post_install_message:
|
137
|
+
rdoc_options: []
|
138
|
+
require_paths:
|
139
|
+
- lib
|
140
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
141
|
+
requirements:
|
142
|
+
- - ">="
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: '0'
|
145
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
146
|
+
requirements:
|
147
|
+
- - ">"
|
148
|
+
- !ruby/object:Gem::Version
|
149
|
+
version: 1.3.1
|
150
|
+
requirements: []
|
151
|
+
rubyforge_project: native_enum
|
152
|
+
rubygems_version: 2.4.2
|
153
|
+
signing_key:
|
154
|
+
specification_version: 4
|
155
|
+
summary: Enum data types for ActiveRecord
|
156
|
+
test_files:
|
157
|
+
- spec/Gemfile.rails_3_0
|
158
|
+
- spec/Gemfile.rails_3_1
|
159
|
+
- spec/Gemfile.rails_3_2
|
160
|
+
- spec/Gemfile.rails_4_0
|
161
|
+
- spec/Gemfile.rails_4_1
|
162
|
+
- spec/Gemfile.rails_4_2
|
163
|
+
- spec/database.yml.tmpl
|
164
|
+
- spec/enum_spec.rb
|
165
|
+
- spec/schema/enum_new.rb
|
166
|
+
- spec/schema/enum_old.rb
|
167
|
+
- spec/schema/set_new.rb
|
168
|
+
- spec/schema/set_old.rb
|
169
|
+
- spec/set_spec.rb
|
170
|
+
- spec/spec_helper.rb
|
171
|
+
has_rdoc:
|