belongs_to_enum 0.2 → 0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README +0 -2
- data/belongs_to_enum.gemspec +1 -1
- data/lib/belongs_to_enum/enum.rb +19 -6
- data/lib/belongs_to_enum/hook.rb +2 -2
- data/lib/belongs_to_enum/version.rb +1 -1
- data/spec/belongs_to_enum.rb +101 -17
- data/spec/spec_helper.rb +1 -10
- metadata +4 -8
- data/spec/test_classes.rb +0 -7
data/README
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
Adds support for transparent enums to ActiveRecord.
|
2
2
|
Adds a belongs_to_enum to ActiveRecord that takes an array of possible values.
|
3
3
|
|
4
|
-
Adds an add_extra_data method to ActiveRecord that invisibly includes an extra data table. Use with STI to keep your database clean. It removes the need to have lots of nullable columns.
|
5
|
-
|
6
4
|
Usage
|
7
5
|
---------
|
8
6
|
Use in Rails 3 app. Add to bundler:
|
data/belongs_to_enum.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |s|
|
|
10
10
|
s.email = ["jez.walker@gmail.com"]
|
11
11
|
s.homepage = ""
|
12
12
|
s.summary = %q{Adds support for transparent enums to ActiveRecord}
|
13
|
-
s.description = %q{Adds
|
13
|
+
s.description = %q{Adds belongs_to_enum method to ActiveRecord::Base, allowing enums to be created with associated scopes and more.}
|
14
14
|
|
15
15
|
s.rubyforge_project = "belongs_to_enum"
|
16
16
|
|
data/lib/belongs_to_enum/enum.rb
CHANGED
@@ -31,7 +31,7 @@ module BelongsToEnum
|
|
31
31
|
end
|
32
32
|
|
33
33
|
def Enum.items
|
34
|
-
warn "DEPRECIATED belongs_to_enum: Use
|
34
|
+
warn "DEPRECIATED belongs_to_enum: Use items_for_select instead"
|
35
35
|
items_for_select
|
36
36
|
end
|
37
37
|
|
@@ -47,7 +47,15 @@ module BelongsToEnum
|
|
47
47
|
@hash
|
48
48
|
end
|
49
49
|
|
50
|
-
def
|
50
|
+
def Enum.valid?(value)
|
51
|
+
@hash.has_value?(value.to_i)
|
52
|
+
end
|
53
|
+
|
54
|
+
def Enum.default
|
55
|
+
@hash.values.first
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.create(name, parent, keys, options = {})
|
51
59
|
|
52
60
|
# Check to see if this has already been defined...
|
53
61
|
return if parent.const_defined?(name.to_s.camelize)
|
@@ -64,15 +72,20 @@ module BelongsToEnum
|
|
64
72
|
keys.each do |k,v|
|
65
73
|
klass.add_item(k,v)
|
66
74
|
end
|
67
|
-
#elsif(keys.respond_to? :raw_hash)
|
68
|
-
# keys.send(:raw_hash).each do |k,v|
|
69
|
-
# klass.add_item(k,v)
|
70
|
-
# end
|
71
75
|
else
|
72
76
|
keys.each_with_index do |key, index|
|
73
77
|
klass.add_item(key, index + 1)
|
74
78
|
end
|
75
79
|
end
|
80
|
+
|
81
|
+
# Create scopes if required
|
82
|
+
if options[:create_scopes]
|
83
|
+
klass.raw_hash.each do |key, value|
|
84
|
+
parent.instance_exec do
|
85
|
+
scope key.to_s.pluralize, where("#{name.to_s.underscore}_id" == value)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
76
89
|
end
|
77
90
|
end
|
78
91
|
end
|
data/lib/belongs_to_enum/hook.rb
CHANGED
@@ -2,8 +2,8 @@ require File.join(File.dirname(__FILE__), "enum")
|
|
2
2
|
|
3
3
|
module BelongsToEnum
|
4
4
|
module Hook
|
5
|
-
def belongs_to_enum(name, keys)
|
6
|
-
BelongsToEnum::Enum.create(name, self, keys)
|
5
|
+
def belongs_to_enum(name, keys, options = {})
|
6
|
+
BelongsToEnum::Enum.create(name, self, keys, options)
|
7
7
|
name = name.to_s
|
8
8
|
class_eval <<-EOS
|
9
9
|
def #{name}?
|
data/spec/belongs_to_enum.rb
CHANGED
@@ -3,51 +3,135 @@ require 'spec_helper'
|
|
3
3
|
describe "Belongs To Enum models" do
|
4
4
|
|
5
5
|
it "constants should be set" do
|
6
|
-
|
6
|
+
klass = Class.new(ActiveRecord::Base)
|
7
|
+
klass.class_exec do
|
8
|
+
belongs_to_enum :role, [:normal]
|
9
|
+
end
|
10
|
+
|
11
|
+
klass.const_defined?("Role").should be_true
|
7
12
|
end
|
8
13
|
|
9
14
|
it "constant has keys" do
|
10
|
-
|
15
|
+
klass = Class.new(ActiveRecord::Base)
|
16
|
+
klass.class_exec do
|
17
|
+
belongs_to_enum :role, [:normal]
|
18
|
+
end
|
19
|
+
klass::Role.should respond_to(:normal)
|
11
20
|
end
|
12
21
|
|
13
22
|
it "keys should start at 1" do
|
14
|
-
|
23
|
+
klass = Class.new(ActiveRecord::Base)
|
24
|
+
klass.class_exec do
|
25
|
+
belongs_to_enum :role, [:normal]
|
26
|
+
end
|
27
|
+
klass::Role.normal.should == 1
|
28
|
+
end
|
29
|
+
|
30
|
+
it "can check to see if a key is valid" do
|
31
|
+
klass = Class.new(ActiveRecord::Base)
|
32
|
+
klass.class_exec do
|
33
|
+
belongs_to_enum :role, [:normal]
|
34
|
+
end
|
35
|
+
klass::Role.valid?(1).should be_true
|
36
|
+
klass::Role.valid?(2).should be_false
|
37
|
+
end
|
38
|
+
|
39
|
+
it "has a default value" do
|
40
|
+
klass = Class.new(ActiveRecord::Base)
|
41
|
+
klass.class_exec do
|
42
|
+
belongs_to_enum :role1, [:normal]
|
43
|
+
belongs_to_enum :role2, {:normal => 10}
|
44
|
+
end
|
45
|
+
klass::Role1.default.should == 1
|
46
|
+
klass::Role2.default.should == 10
|
15
47
|
end
|
16
48
|
|
17
49
|
it "keys should respect hash values" do
|
18
|
-
|
50
|
+
klass = Class.new(ActiveRecord::Base)
|
51
|
+
klass.class_exec do
|
52
|
+
belongs_to_enum :role, {:normal => 10, :admin => 20}
|
53
|
+
end
|
54
|
+
klass::Role.normal.should == 10
|
55
|
+
klass::Role.admin.should == 20
|
19
56
|
end
|
20
57
|
|
21
58
|
it "keys can be iterated through" do
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
59
|
+
klass = Class.new(ActiveRecord::Base)
|
60
|
+
klass.class_exec do
|
61
|
+
belongs_to_enum :role, [:normal, :admin]
|
62
|
+
end
|
63
|
+
i = 1
|
64
|
+
klass::Role.each do |role, role_id|
|
65
|
+
role.should == (i == 1 ? :normal : :admin)
|
66
|
+
role_id.should == i
|
67
|
+
i += 1
|
26
68
|
end
|
27
69
|
end
|
28
70
|
|
29
71
|
it "has items_for_select" do
|
30
|
-
|
31
|
-
|
72
|
+
klass = Class.new(ActiveRecord::Base)
|
73
|
+
klass.class_exec do
|
74
|
+
belongs_to_enum :role, [:normal]
|
75
|
+
end
|
76
|
+
klass::Role.items_for_select.first[0].should == "Normal"
|
77
|
+
klass::Role.items_for_select.first[1].should == 1
|
32
78
|
end
|
33
79
|
|
34
80
|
it "has depreciated items method that maps to items_for_select" do
|
35
|
-
|
36
|
-
|
81
|
+
klass = Class.new(ActiveRecord::Base)
|
82
|
+
klass.class_exec do
|
83
|
+
belongs_to_enum :role, [:normal]
|
84
|
+
end
|
85
|
+
klass::Role.items.first[0].should == "Normal"
|
86
|
+
klass::Role.items.first[1].should == 1
|
37
87
|
end
|
38
88
|
|
39
89
|
it "can get item from id" do
|
40
|
-
|
90
|
+
klass = Class.new(ActiveRecord::Base)
|
91
|
+
klass.class_exec do
|
92
|
+
belongs_to_enum :role, [:normal]
|
93
|
+
end
|
94
|
+
klass::Role.get(1).should == :normal
|
41
95
|
end
|
42
96
|
|
43
97
|
it "pretty displays item" do
|
44
|
-
|
98
|
+
klass = Class.new(ActiveRecord::Base)
|
99
|
+
klass.class_exec do
|
100
|
+
belongs_to_enum :role, [:normal]
|
101
|
+
end
|
102
|
+
klass::Role.display(1).should == "Normal"
|
45
103
|
end
|
46
104
|
|
47
105
|
it "can access raw hash" do
|
48
|
-
|
49
|
-
|
50
|
-
|
106
|
+
klass = Class.new(ActiveRecord::Base)
|
107
|
+
klass.class_exec do
|
108
|
+
belongs_to_enum :role, [:normal]
|
109
|
+
end
|
110
|
+
klass::Role.raw_hash.is_a?(Hash)
|
111
|
+
klass::Role.raw_hash.keys[0].should == :normal
|
112
|
+
klass::Role.raw_hash.values[0].should == 1
|
113
|
+
end
|
114
|
+
|
115
|
+
it "can create scopes when requested" do
|
116
|
+
ActiveRecord::Schema.define(:version => 1) do
|
117
|
+
create_table :scope_checkers do |t|
|
118
|
+
t.integer :role_id, :null => false
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
klass = Class.new(ActiveRecord::Base)
|
123
|
+
klass.class_exec do
|
124
|
+
set_table_name "scope_checkers"
|
125
|
+
scope :sanity_scope, where(:sanity_scope_id => 1)
|
126
|
+
belongs_to_enum :role1, [:normal], :create_scopes => true
|
127
|
+
belongs_to_enum :role2, [:admin]
|
128
|
+
end
|
129
|
+
klass.should respond_to("sanity_scope")
|
130
|
+
klass.should respond_to("normals")
|
131
|
+
klass.should_not respond_to("admins")
|
132
|
+
|
133
|
+
klass.method("normals").source_location.should == klass.method("sanity_scope").source_location
|
134
|
+
klass.send("normals").to_sql.strip.should == 'SELECT "scope_checkers".* FROM "scope_checkers"'
|
51
135
|
end
|
52
136
|
|
53
137
|
|
data/spec/spec_helper.rb
CHANGED
@@ -13,13 +13,4 @@ ActiveRecord::Base.send(:extend, BelongsToEnum::Hook)
|
|
13
13
|
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
14
14
|
|
15
15
|
# AR keeps printing annoying schema statements
|
16
|
-
$stdout = StringIO.new
|
17
|
-
|
18
|
-
ActiveRecord::Base.logger
|
19
|
-
ActiveRecord::Schema.define(:version => 1) do
|
20
|
-
create_table :users do |t|
|
21
|
-
t.integer :role_id, :null => false
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
require 'test_classes'
|
16
|
+
$stdout = StringIO.new
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: belongs_to_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: "0.
|
5
|
+
version: "0.3"
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Jeremy walker
|
@@ -10,8 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
14
|
-
default_executable:
|
13
|
+
date: 2011-06-17 00:00:00 Z
|
15
14
|
dependencies:
|
16
15
|
- !ruby/object:Gem::Dependency
|
17
16
|
name: rails
|
@@ -35,7 +34,7 @@ dependencies:
|
|
35
34
|
version: "0"
|
36
35
|
type: :development
|
37
36
|
version_requirements: *id002
|
38
|
-
description: Adds
|
37
|
+
description: Adds belongs_to_enum method to ActiveRecord::Base, allowing enums to be created with associated scopes and more.
|
39
38
|
email:
|
40
39
|
- jez.walker@gmail.com
|
41
40
|
executables: []
|
@@ -58,8 +57,6 @@ files:
|
|
58
57
|
- lib/belongs_to_enum/version.rb
|
59
58
|
- spec/belongs_to_enum.rb
|
60
59
|
- spec/spec_helper.rb
|
61
|
-
- spec/test_classes.rb
|
62
|
-
has_rdoc: true
|
63
60
|
homepage: ""
|
64
61
|
licenses: []
|
65
62
|
|
@@ -83,11 +80,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
83
80
|
requirements: []
|
84
81
|
|
85
82
|
rubyforge_project: belongs_to_enum
|
86
|
-
rubygems_version: 1.
|
83
|
+
rubygems_version: 1.7.2
|
87
84
|
signing_key:
|
88
85
|
specification_version: 3
|
89
86
|
summary: Adds support for transparent enums to ActiveRecord
|
90
87
|
test_files:
|
91
88
|
- spec/belongs_to_enum.rb
|
92
89
|
- spec/spec_helper.rb
|
93
|
-
- spec/test_classes.rb
|