activerecord_enum 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +19 -0
- data/README.markdown +18 -7
- data/Rakefile +1 -0
- data/activerecord_enum.gemspec +1 -0
- data/lib/activerecord_enum.rb +16 -16
- data/lib/activerecord_enum/version.rb +1 -1
- data/spec/.travis.database.yml +5 -0
- data/spec/database.yml.tmpl +5 -0
- data/spec/enum_spec.rb +13 -9
- data/spec/schema/set_new.rb +1 -1
- data/spec/set_spec.rb +13 -45
- data/spec/spec_helper.rb +17 -1
- metadata +26 -18
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
CHANGED
@@ -1,18 +1,29 @@
|
|
1
|
-
# ActiveRecord Enum
|
1
|
+
# ActiveRecord Enum #
|
2
2
|
|
3
3
|
Provides ActiveRecord support for the nonstandard `ENUM` and `SET` data types.
|
4
4
|
|
5
5
|
[![Build Status](http://travis-ci.org/iangreenleaf/activerecord_enum.png)](http://travis-ci.org/iangreenleaf/activerecord_enum)
|
6
6
|
|
7
|
-
## Hypothetically asked questions
|
7
|
+
## Hypothetically asked questions ##
|
8
8
|
|
9
|
-
### Y U NO WORK?!
|
10
|
-
Sorry, it currently only works with Rails 3.0.x and the mysql2 adapter. I plan to support 3.1.x and other adapters at some point.
|
9
|
+
### Y U NO WORK?! ###
|
11
10
|
|
12
|
-
|
13
|
-
|
11
|
+
Sorry, it currently only works with Rails 3.0.x and the mysql2 and sqlite adapters. I plan to support 3.1.x and other standard adapters at some point.
|
12
|
+
|
13
|
+
### Why doesn't it validate anything? ###
|
14
|
+
|
15
|
+
Following ActiveRecord's lead, this plugin doesn't do any validation work itself.
|
16
|
+
|
17
|
+
For ENUM columns, you may be satisfied with something simple:
|
18
|
+
|
19
|
+
validates_inclusion_of :attr, :in => [ :possible, :values ]
|
20
|
+
|
21
|
+
Or if you prefer more bells and whistles, try [nofxx/symbolize](https://github.com/nofxx/symbolize).
|
22
|
+
|
23
|
+
For SET columns, you may be interested in [iangreenleaf/active_set](https://github.com/iangreenleaf/active_set).
|
24
|
+
|
25
|
+
### Nonstandard SQL?! What's your problem, jerkweed? ###
|
14
26
|
|
15
|
-
### Nonstandard SQL?! What's your problem, jerkweed?
|
16
27
|
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).
|
17
28
|
|
18
29
|
However, sometimes we can't or won't avoid working with these data types. When that happens, I got you covered.
|
data/Rakefile
CHANGED
data/activerecord_enum.gemspec
CHANGED
@@ -18,6 +18,7 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.add_development_dependency "rake"
|
19
19
|
s.add_development_dependency "bundler"
|
20
20
|
s.add_development_dependency "mysql2", "~> 0.2.0"
|
21
|
+
s.add_development_dependency "sqlite3", "~>1.3.4"
|
21
22
|
s.add_development_dependency "rspec", "~> 2.6.0"
|
22
23
|
|
23
24
|
s.files = `git ls-files`.split("\n")
|
data/lib/activerecord_enum.rb
CHANGED
@@ -1,23 +1,8 @@
|
|
1
1
|
require 'active_record'
|
2
2
|
require 'active_record/base'
|
3
3
|
require 'active_record/connection_adapters/mysql2_adapter'
|
4
|
+
require 'active_record/connection_adapters/sqlite3_adapter'
|
4
5
|
require 'active_record/connection_adapters/abstract/schema_definitions.rb'
|
5
|
-
require 'active_record/attribute_methods/write'
|
6
|
-
|
7
|
-
module ActiveRecord
|
8
|
-
module AttributeMethods
|
9
|
-
module Write
|
10
|
-
def write_attribute_with_enum(attr_name, value)
|
11
|
-
if (column = column_for_attribute(attr_name)) && column.set? && value.respond_to?(:join)
|
12
|
-
value = value.join ","
|
13
|
-
end
|
14
|
-
write_attribute_without_enum attr_name, value
|
15
|
-
end
|
16
|
-
alias_method :write_attribute_without_enum, :write_attribute
|
17
|
-
alias_method :write_attribute, :write_attribute_with_enum
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
6
|
|
22
7
|
module ActiveRecord
|
23
8
|
module ConnectionAdapters
|
@@ -46,6 +31,21 @@ module ActiveRecord
|
|
46
31
|
end
|
47
32
|
end
|
48
33
|
|
34
|
+
module ActiveRecord
|
35
|
+
module ConnectionAdapters
|
36
|
+
class SQLite3Adapter < SQLiteAdapter
|
37
|
+
def type_to_sql_with_enum type, limit=nil, *args
|
38
|
+
if type.to_s == "enum" || type.to_s == "set"
|
39
|
+
type, limit = :string, nil
|
40
|
+
end
|
41
|
+
type_to_sql_without_enum type, limit, *args
|
42
|
+
end
|
43
|
+
alias_method :type_to_sql_without_enum, :type_to_sql
|
44
|
+
alias_method :type_to_sql, :type_to_sql_with_enum
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
49
|
module ActiveRecord
|
50
50
|
module ConnectionAdapters
|
51
51
|
class Column
|
data/spec/.travis.database.yml
CHANGED
data/spec/database.yml.tmpl
CHANGED
data/spec/enum_spec.rb
CHANGED
@@ -2,7 +2,7 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe "ENUM datatype" do
|
4
4
|
|
5
|
-
describe "schema dump" do
|
5
|
+
describe "schema dump", :db_support => true do
|
6
6
|
before { load_schema "enum_old" }
|
7
7
|
subject { dumped_schema }
|
8
8
|
|
@@ -21,23 +21,27 @@ describe "ENUM datatype" do
|
|
21
21
|
|
22
22
|
describe "schema loading" do
|
23
23
|
before { load_schema "enum_new" }
|
24
|
-
subject {
|
24
|
+
subject { column_props :balloons, :color }
|
25
25
|
|
26
|
-
it "loads native format" do
|
27
|
-
subject[
|
26
|
+
it "loads native format", :db_support => true do
|
27
|
+
subject[ :type ].should == "enum('red','gold')"
|
28
|
+
end
|
29
|
+
|
30
|
+
it "falls back to text when missing db support", :db_support => false do
|
31
|
+
subject[ :type ].should =~ /varchar/
|
28
32
|
end
|
29
33
|
|
30
34
|
it "loads default option" do
|
31
|
-
subject[
|
35
|
+
subject[ :default ].should == "gold"
|
32
36
|
end
|
33
37
|
|
34
38
|
it "loads null option" do
|
35
|
-
subject[
|
39
|
+
subject[ :null ].should be_false
|
36
40
|
end
|
37
41
|
|
38
|
-
it "loads native column format" do
|
39
|
-
subject =
|
40
|
-
subject[
|
42
|
+
it "loads native column format", :db_support => true do
|
43
|
+
subject = column_props :balloons, :size
|
44
|
+
subject[ :type ].should == "enum('small','medium','large')"
|
41
45
|
end
|
42
46
|
end
|
43
47
|
end
|
data/spec/schema/set_new.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
ActiveRecord::Schema.define do
|
2
2
|
create_table :balloons, :force => true do |t|
|
3
3
|
t.integer "id"
|
4
|
-
t.set "ribbons", :limit => ['red', 'green', 'gold'], :default => ['
|
4
|
+
t.set "ribbons", :limit => ['red', 'green', 'gold'], :default => ['green','gold'], :null => false
|
5
5
|
t.column "gasses", :set, :limit => ['helium', 'hydrogen']
|
6
6
|
end
|
7
7
|
end
|
data/spec/set_spec.rb
CHANGED
@@ -2,7 +2,7 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe "SET datatype" do
|
4
4
|
|
5
|
-
describe "schema dump" do
|
5
|
+
describe "schema dump", :db_support => true do
|
6
6
|
before { load_schema "set_old" }
|
7
7
|
subject { dumped_schema }
|
8
8
|
|
@@ -21,60 +21,28 @@ describe "SET datatype" do
|
|
21
21
|
|
22
22
|
describe "schema loading" do
|
23
23
|
before { load_schema "set_new" }
|
24
|
-
subject {
|
24
|
+
subject { column_props :balloons, :ribbons }
|
25
25
|
|
26
|
-
it "loads native format" do
|
27
|
-
subject[
|
26
|
+
it "loads native format", :db_support => true do
|
27
|
+
subject[ :type ].should == "set('red','green','gold')"
|
28
28
|
end
|
29
29
|
|
30
|
-
it "
|
31
|
-
subject[
|
32
|
-
end
|
33
|
-
|
34
|
-
it "loads null option" do
|
35
|
-
subject[ "Null" ].should == "NO"
|
30
|
+
it "falls back to text when missing db support", :db_support => false do
|
31
|
+
subject[ :type ].should =~ /varchar/
|
36
32
|
end
|
37
33
|
|
38
|
-
it "loads
|
39
|
-
subject
|
40
|
-
subject[ "Type" ].should == "set('helium','hydrogen')"
|
34
|
+
it "loads default option" do
|
35
|
+
subject[ :default ].should == "green,gold"
|
41
36
|
end
|
42
|
-
end
|
43
37
|
|
44
|
-
|
45
|
-
|
46
|
-
it "accepts single value" do
|
47
|
-
b = Balloon.create :gasses => "helium"
|
48
|
-
b.should be_valid
|
49
|
-
b.reload.gasses.should == "helium"
|
50
|
-
end
|
51
|
-
it "accepts array of values" do
|
52
|
-
b = Balloon.create :gasses => [ "helium", "hydrogen" ]
|
53
|
-
b.should be_valid
|
54
|
-
b.reload.gasses.should == "helium,hydrogen"
|
55
|
-
end
|
56
|
-
it "accepts comma-separated values" do
|
57
|
-
b = Balloon.create :gasses => "helium,hydrogen"
|
58
|
-
b.should be_valid
|
59
|
-
b.reload.gasses.should == "helium,hydrogen"
|
60
|
-
end
|
61
|
-
it "accepts empty list" do
|
62
|
-
b = Balloon.create :gasses => [ ]
|
63
|
-
b.should be_valid
|
64
|
-
b.reload.gasses.should == ""
|
38
|
+
it "loads null option" do
|
39
|
+
subject[ :null ].should be_false
|
65
40
|
end
|
66
|
-
end
|
67
41
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
ActiveRecord::Base.connection.execute "INSERT INTO balloons (gasses) VALUES ('helium,hydrogen')"
|
72
|
-
@b = Balloon.first
|
73
|
-
end
|
74
|
-
it "returns comma-separated values by default" do
|
75
|
-
@b.gasses.should == "helium,hydrogen"
|
42
|
+
it "loads native column format", :db_support => true do
|
43
|
+
subject = column_props :balloons, :gasses
|
44
|
+
subject[ :type ].should == "set('helium','hydrogen')"
|
76
45
|
end
|
77
|
-
it "returns array of values when config option is set"
|
78
46
|
end
|
79
47
|
end
|
80
48
|
|
data/spec/spec_helper.rb
CHANGED
@@ -20,6 +20,22 @@ def dumped_schema
|
|
20
20
|
stream.string.lines.select {|l| /^\s*#/.match(l).nil? }.join
|
21
21
|
end
|
22
22
|
|
23
|
-
|
23
|
+
def column_props table, column
|
24
|
+
case ENV["DB"]
|
25
|
+
when "mysql"
|
26
|
+
result = ActiveRecord::Base.connection.select_one "SHOW FIELDS FROM #{table} WHERE Field='#{column}'"
|
27
|
+
{ :type => result["Type"], :default => result["Default"], :null => ( result["Null"] == "YES" ) }
|
28
|
+
when "sqlite"
|
29
|
+
result = ActiveRecord::Base.connection.select_value "SELECT sql FROM sqlite_master WHERE type='table' AND name='#{table}'"
|
30
|
+
matches = /"#{column}" ([^[:space:]]+) (?:DEFAULT '([^[:space:]]+)')?( NOT NULL)?,/.match result
|
31
|
+
{ :type => matches[1], :default => matches[2], :null => matches[3].nil? }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
db_config = YAML::load(IO.read("spec/database.yml"))
|
36
|
+
ActiveRecord::Base.configurations = db_config
|
24
37
|
db = ENV["DB"] || "mysql"
|
25
38
|
ActiveRecord::Base.establish_connection db
|
39
|
+
RSpec.configure do |c|
|
40
|
+
c.filter_run_excluding :db_support => ! db_config[db]["supports_enums"]
|
41
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Ian Young
|
@@ -15,8 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
19
|
-
default_executable:
|
18
|
+
date: 2011-11-03 00:00:00 Z
|
20
19
|
dependencies:
|
21
20
|
- !ruby/object:Gem::Dependency
|
22
21
|
name: activerecord
|
@@ -79,9 +78,25 @@ dependencies:
|
|
79
78
|
type: :development
|
80
79
|
version_requirements: *id004
|
81
80
|
- !ruby/object:Gem::Dependency
|
82
|
-
name:
|
81
|
+
name: sqlite3
|
83
82
|
prerelease: false
|
84
83
|
requirement: &id005 !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ~>
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
hash: 19
|
89
|
+
segments:
|
90
|
+
- 1
|
91
|
+
- 3
|
92
|
+
- 4
|
93
|
+
version: 1.3.4
|
94
|
+
type: :development
|
95
|
+
version_requirements: *id005
|
96
|
+
- !ruby/object:Gem::Dependency
|
97
|
+
name: rspec
|
98
|
+
prerelease: false
|
99
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
85
100
|
none: false
|
86
101
|
requirements:
|
87
102
|
- - ~>
|
@@ -93,7 +108,7 @@ dependencies:
|
|
93
108
|
- 0
|
94
109
|
version: 2.6.0
|
95
110
|
type: :development
|
96
|
-
version_requirements: *
|
111
|
+
version_requirements: *id006
|
97
112
|
description: Adds the ENUM data type natively to ActiveRecord.
|
98
113
|
email:
|
99
114
|
- ian.greenleaf+github@gmail.com
|
@@ -107,6 +122,7 @@ files:
|
|
107
122
|
- .gitignore
|
108
123
|
- .travis.yml
|
109
124
|
- Gemfile
|
125
|
+
- LICENSE
|
110
126
|
- README.markdown
|
111
127
|
- Rakefile
|
112
128
|
- activerecord_enum.gemspec
|
@@ -121,7 +137,6 @@ files:
|
|
121
137
|
- spec/schema/set_old.rb
|
122
138
|
- spec/set_spec.rb
|
123
139
|
- spec/spec_helper.rb
|
124
|
-
has_rdoc: true
|
125
140
|
homepage: ""
|
126
141
|
licenses: []
|
127
142
|
|
@@ -151,16 +166,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
151
166
|
requirements: []
|
152
167
|
|
153
168
|
rubyforge_project: activerecord_enum
|
154
|
-
rubygems_version: 1.
|
169
|
+
rubygems_version: 1.7.2
|
155
170
|
signing_key:
|
156
171
|
specification_version: 3
|
157
172
|
summary: Enum data types for ActiveRecord
|
158
|
-
test_files:
|
159
|
-
|
160
|
-
- spec/enum_spec.rb
|
161
|
-
- spec/schema/enum_new.rb
|
162
|
-
- spec/schema/enum_old.rb
|
163
|
-
- spec/schema/set_new.rb
|
164
|
-
- spec/schema/set_old.rb
|
165
|
-
- spec/set_spec.rb
|
166
|
-
- spec/spec_helper.rb
|
173
|
+
test_files: []
|
174
|
+
|