activerecord_enum 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/.travis.yml +5 -0
- data/README.markdown +3 -1
- data/Rakefile +25 -2
- data/lib/activerecord_enum/version.rb +1 -1
- data/lib/activerecord_enum.rb +23 -1
- data/spec/.travis.database.yml +4 -0
- data/spec/database.yml.tmpl +6 -0
- data/spec/enum_spec.rb +0 -5
- data/spec/set_spec.rb +33 -7
- data/spec/spec_helper.rb +3 -11
- metadata +8 -4
data/.gitignore
CHANGED
data/.travis.yml
ADDED
data/README.markdown
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
Provides ActiveRecord support for the nonstandard `ENUM` and `SET` data types.
|
4
4
|
|
5
|
+
[![Build Status](http://travis-ci.org/iangreenleaf/activerecord_enum.png)](http://travis-ci.org/iangreenleaf/activerecord_enum)
|
6
|
+
|
5
7
|
## Hypothetically asked questions
|
6
8
|
|
7
9
|
### Y U NO WORK?!
|
@@ -11,6 +13,6 @@ Sorry, it currently only works with Rails 3.0.x and the mysql2 adapter. I plan t
|
|
11
13
|
Right now it really only supports schema operations - attribute access is left untouched. I'll be working on that too at some point.
|
12
14
|
|
13
15
|
### Nonstandard SQL?! What's your problem, jerkweed?
|
14
|
-
This isn't a plugin everyone should use. There are a number of plugins to simulate enum behavior backed by standard
|
16
|
+
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).
|
15
17
|
|
16
18
|
However, sometimes we can't or won't avoid working with these data types. When that happens, I got you covered.
|
data/Rakefile
CHANGED
@@ -1,10 +1,33 @@
|
|
1
1
|
require 'bundler'
|
2
2
|
Bundler::GemHelper.install_tasks
|
3
3
|
|
4
|
+
DB_CONFIG = "spec/database.yml"
|
5
|
+
|
4
6
|
require 'rake'
|
5
|
-
desc 'Default: run unit tests.'
|
6
|
-
task :default => :spec
|
7
|
+
desc 'Default: run all unit tests.'
|
8
|
+
task :default => :"spec:all"
|
9
|
+
|
10
|
+
namespace :db do
|
11
|
+
desc 'Prepare the databases.'
|
12
|
+
task :prepare do
|
13
|
+
unless File.exist? DB_CONFIG
|
14
|
+
cp "#{config_file}.tmpl", DB_CONFIG
|
15
|
+
end
|
16
|
+
#TODO would be nice to create the DBs here
|
17
|
+
end
|
18
|
+
end
|
7
19
|
|
8
20
|
require "rspec/core/rake_task"
|
9
21
|
desc 'Run the test suite.'
|
10
22
|
RSpec::Core::RakeTask.new(:spec)
|
23
|
+
|
24
|
+
desc 'Run the test suite for all DBs.'
|
25
|
+
namespace :spec do
|
26
|
+
task :all do
|
27
|
+
db_config = YAML::load(IO.read(DB_CONFIG))
|
28
|
+
db_config.each do |db,config|
|
29
|
+
ENV["DB"] = db
|
30
|
+
Rake::Task["spec"].invoke
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/lib/activerecord_enum.rb
CHANGED
@@ -1,8 +1,22 @@
|
|
1
1
|
require 'active_record'
|
2
|
+
require 'active_record/base'
|
2
3
|
require 'active_record/connection_adapters/mysql2_adapter'
|
3
4
|
require 'active_record/connection_adapters/abstract/schema_definitions.rb'
|
5
|
+
require 'active_record/attribute_methods/write'
|
4
6
|
|
5
|
-
module
|
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
|
6
20
|
end
|
7
21
|
|
8
22
|
module ActiveRecord
|
@@ -67,6 +81,14 @@ module ActiveRecord
|
|
67
81
|
extract_default default
|
68
82
|
end
|
69
83
|
end
|
84
|
+
|
85
|
+
def set?
|
86
|
+
type == :set
|
87
|
+
end
|
88
|
+
|
89
|
+
def enum?
|
90
|
+
type == :enum
|
91
|
+
end
|
70
92
|
end
|
71
93
|
end
|
72
94
|
end
|
data/spec/enum_spec.rb
CHANGED
data/spec/set_spec.rb
CHANGED
@@ -42,14 +42,40 @@ describe "SET datatype" do
|
|
42
42
|
end
|
43
43
|
|
44
44
|
describe "assignment" do
|
45
|
-
|
46
|
-
it "accepts
|
47
|
-
|
45
|
+
before { load_schema "set_new" }
|
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 == ""
|
65
|
+
end
|
48
66
|
end
|
49
67
|
|
50
|
-
describe "
|
51
|
-
|
52
|
-
|
53
|
-
|
68
|
+
describe "getter" do
|
69
|
+
before do
|
70
|
+
load_schema "set_new"
|
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"
|
76
|
+
end
|
77
|
+
it "returns array of values when config option is set"
|
54
78
|
end
|
55
79
|
end
|
80
|
+
|
81
|
+
class Balloon < ActiveRecord::Base; end
|
data/spec/spec_helper.rb
CHANGED
@@ -20,14 +20,6 @@ def dumped_schema
|
|
20
20
|
stream.string.lines.select {|l| /^\s*#/.match(l).nil? }.join
|
21
21
|
end
|
22
22
|
|
23
|
-
ActiveRecord::Base.configurations =
|
24
|
-
|
25
|
-
|
26
|
-
:host => "localhost",
|
27
|
-
:username => "enum_test",
|
28
|
-
:password => "enum_test",
|
29
|
-
:database => "enum_test",
|
30
|
-
:socket => "/tmp/mysql.sock"
|
31
|
-
},
|
32
|
-
}
|
33
|
-
ActiveRecord::Base.establish_connection "enum_test"
|
23
|
+
ActiveRecord::Base.configurations = YAML::load(IO.read("spec/database.yml"))
|
24
|
+
db = ENV["DB"] || "mysql"
|
25
|
+
ActiveRecord::Base.establish_connection db
|
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: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Ian Young
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-09-02 00:00:00 -07:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -105,12 +105,15 @@ extra_rdoc_files: []
|
|
105
105
|
|
106
106
|
files:
|
107
107
|
- .gitignore
|
108
|
+
- .travis.yml
|
108
109
|
- Gemfile
|
109
110
|
- README.markdown
|
110
111
|
- Rakefile
|
111
112
|
- activerecord_enum.gemspec
|
112
113
|
- lib/activerecord_enum.rb
|
113
114
|
- lib/activerecord_enum/version.rb
|
115
|
+
- spec/.travis.database.yml
|
116
|
+
- spec/database.yml.tmpl
|
114
117
|
- spec/enum_spec.rb
|
115
118
|
- spec/schema/enum_new.rb
|
116
119
|
- spec/schema/enum_old.rb
|
@@ -153,6 +156,7 @@ signing_key:
|
|
153
156
|
specification_version: 3
|
154
157
|
summary: Enum data types for ActiveRecord
|
155
158
|
test_files:
|
159
|
+
- spec/database.yml.tmpl
|
156
160
|
- spec/enum_spec.rb
|
157
161
|
- spec/schema/enum_new.rb
|
158
162
|
- spec/schema/enum_old.rb
|