has_normalized_sti 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -1
- data/Rakefile +6 -4
- data/VERSION +1 -1
- data/has_normalized_sti.gemspec +11 -10
- data/lib/has_normalized_sti.rb +15 -37
- metadata +46 -55
- data/Gemfile.lock +0 -75
data/Gemfile
CHANGED
data/Rakefile
CHANGED
@@ -4,10 +4,12 @@ begin
|
|
4
4
|
gem.name = 'has_normalized_sti'
|
5
5
|
gem.summary = 'allows rails STI to work when the type is normalized out'
|
6
6
|
gem.description = <<-DESC
|
7
|
-
has_normalzied_sti
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
has_normalzied_sti is a rails extension to allow Single Table Inheritance
|
8
|
+
to work with a database normalized type column.
|
9
|
+
The extension expects the STI model to have a type_id column instead of
|
10
|
+
a type column. type_id should reference a Types table containg all the possible types.
|
11
|
+
The types table will be auto populated with new types as new
|
12
|
+
subclasses are saved.
|
11
13
|
DESC
|
12
14
|
gem.email = 'kevin@glowacz.info'
|
13
15
|
gem.author = 'Kevin Glowacz'
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.1.0
|
data/has_normalized_sti.gemspec
CHANGED
@@ -5,15 +5,17 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{has_normalized_sti}
|
8
|
-
s.version = "1.
|
8
|
+
s.version = "1.1.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
-
s.authors = [
|
12
|
-
s.date = %q{2011-
|
13
|
-
s.description = %q{ has_normalzied_sti
|
14
|
-
|
15
|
-
|
16
|
-
|
11
|
+
s.authors = [%q{Kevin Glowacz}]
|
12
|
+
s.date = %q{2011-09-20}
|
13
|
+
s.description = %q{ has_normalzied_sti is a rails extension to allow Single Table Inheritance
|
14
|
+
to work with a database normalized type column.
|
15
|
+
The extension expects the STI model to have a type_id column instead of
|
16
|
+
a type column. type_id should reference a Types table containg all the possible types.
|
17
|
+
The types table will be auto populated with new types as new
|
18
|
+
subclasses are saved.
|
17
19
|
}
|
18
20
|
s.email = %q{kevin@glowacz.info}
|
19
21
|
s.extra_rdoc_files = [
|
@@ -22,7 +24,6 @@ Gem::Specification.new do |s|
|
|
22
24
|
s.files = [
|
23
25
|
".rspec",
|
24
26
|
"Gemfile",
|
25
|
-
"Gemfile.lock",
|
26
27
|
"README",
|
27
28
|
"Rakefile",
|
28
29
|
"VERSION",
|
@@ -34,8 +35,8 @@ Gem::Specification.new do |s|
|
|
34
35
|
"spec/spec_helper.rb",
|
35
36
|
"spec/speed_test.rb"
|
36
37
|
]
|
37
|
-
s.require_paths = [
|
38
|
-
s.rubygems_version = %q{1.6
|
38
|
+
s.require_paths = [%q{lib}]
|
39
|
+
s.rubygems_version = %q{1.8.6}
|
39
40
|
s.summary = %q{allows rails STI to work when the type is normalized out}
|
40
41
|
|
41
42
|
if s.respond_to? :specification_version then
|
data/lib/has_normalized_sti.rb
CHANGED
@@ -39,7 +39,7 @@ module HasNormalizedSti
|
|
39
39
|
extend HasNormalizedSti::SingletonMethods
|
40
40
|
include HasNormalizedSti::InstanceMethods
|
41
41
|
|
42
|
-
|
42
|
+
class_attribute :sti_config
|
43
43
|
self.sti_config = {
|
44
44
|
:type_class_name => "#{table_name.classify}Type",
|
45
45
|
:foreign_key => 'type_id',
|
@@ -62,48 +62,26 @@ module HasNormalizedSti
|
|
62
62
|
default_scope joins(:normal_type).select("#{table_name}.*, #{sti_config[:type_class_name].constantize.table_name}.#{sti_config[:type_column]}")
|
63
63
|
validates_associated :normal_type
|
64
64
|
validates_presence_of :normal_type
|
65
|
+
set_inheritance_column 'has_normalized_sti_proxy_type'
|
65
66
|
end
|
66
67
|
end
|
67
68
|
|
68
69
|
module SingletonMethods
|
69
|
-
def
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
type_name = associated_record.try(sti_config[:type_column])
|
75
|
-
end
|
76
|
-
model = find_sti_class(type_name).allocate
|
77
|
-
model.init_with('attributes' => record)
|
78
|
-
model
|
70
|
+
def columns_hash
|
71
|
+
ch = super
|
72
|
+
# AR will only do its magic if it thinks the db table has the inheritance_column
|
73
|
+
ch[inheritance_column] = nil
|
74
|
+
ch
|
79
75
|
end
|
80
76
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
ActiveSupport::Dependencies.constantize(type_name)
|
88
|
-
else
|
89
|
-
compute_type(type_name)
|
90
|
-
end
|
91
|
-
rescue NameError
|
92
|
-
raise SubclassNotFound,
|
93
|
-
"The single-table inheritance mechanism failed to locate the subclass: '#{type_name}'. " +
|
94
|
-
"This error is raised because the column '#{inheritance_column}' is reserved for storing the class in case of inheritance. " +
|
95
|
-
"Please rename this column if you didn't intend it to be used for storing the inheritance class " +
|
96
|
-
"or overwrite #{name}.inheritance_column to use another column for that information."
|
97
|
-
end
|
98
|
-
end
|
99
|
-
end
|
77
|
+
# look up the type name on the associated object
|
78
|
+
# put in in the record's inheritance_column to trick AR
|
79
|
+
def instantiate(record)
|
80
|
+
associated_record = sti_config[:type_class_name].constantize.find_by_id(record[sti_config[:foreign_key].to_s])
|
81
|
+
type_name = associated_record.try(sti_config[:type_column])
|
82
|
+
record[inheritance_column] = type_name
|
100
83
|
|
101
|
-
|
102
|
-
if superclass.abstract_class?
|
103
|
-
superclass.descends_from_active_record?
|
104
|
-
else
|
105
|
-
superclass == ActiveRecord::Base
|
106
|
-
end
|
84
|
+
super
|
107
85
|
end
|
108
86
|
|
109
87
|
def type_condition
|
@@ -116,7 +94,7 @@ module HasNormalizedSti
|
|
116
94
|
end
|
117
95
|
|
118
96
|
module InstanceMethods
|
119
|
-
def initialize(
|
97
|
+
def initialize(*args)
|
120
98
|
super
|
121
99
|
self.type = self.class.to_s
|
122
100
|
end
|
metadata
CHANGED
@@ -1,63 +1,62 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_normalized_sti
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.0
|
4
5
|
prerelease:
|
5
|
-
version: 1.0.0
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
7
|
+
authors:
|
8
8
|
- Kevin Glowacz
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
dependencies:
|
16
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2011-09-20 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
17
15
|
name: jeweler
|
18
|
-
requirement: &
|
16
|
+
requirement: &2153251520 !ruby/object:Gem::Requirement
|
19
17
|
none: false
|
20
|
-
requirements:
|
21
|
-
- -
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version:
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
24
22
|
type: :development
|
25
23
|
prerelease: false
|
26
|
-
version_requirements: *
|
27
|
-
- !ruby/object:Gem::Dependency
|
24
|
+
version_requirements: *2153251520
|
25
|
+
- !ruby/object:Gem::Dependency
|
28
26
|
name: mocha
|
29
|
-
requirement: &
|
27
|
+
requirement: &2153251000 !ruby/object:Gem::Requirement
|
30
28
|
none: false
|
31
|
-
requirements:
|
32
|
-
- -
|
33
|
-
- !ruby/object:Gem::Version
|
34
|
-
version:
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
35
33
|
type: :development
|
36
34
|
prerelease: false
|
37
|
-
version_requirements: *
|
38
|
-
- !ruby/object:Gem::Dependency
|
35
|
+
version_requirements: *2153251000
|
36
|
+
- !ruby/object:Gem::Dependency
|
39
37
|
name: i18n
|
40
|
-
requirement: &
|
38
|
+
requirement: &2153250480 !ruby/object:Gem::Requirement
|
41
39
|
none: false
|
42
|
-
requirements:
|
43
|
-
- -
|
44
|
-
- !ruby/object:Gem::Version
|
45
|
-
version:
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
46
44
|
type: :development
|
47
45
|
prerelease: false
|
48
|
-
version_requirements: *
|
49
|
-
description: " has_normalzied_sti
|
46
|
+
version_requirements: *2153250480
|
47
|
+
description: ! " has_normalzied_sti is a rails extension to allow Single Table
|
48
|
+
Inheritance\n to work with a database normalized type column.\n The extension
|
49
|
+
expects the STI model to have a type_id column instead of\n a type column.
|
50
|
+
type_id should reference a Types table containg all the possible types.\n The
|
51
|
+
types table will be auto populated with new types as new\n subclasses are saved.\n"
|
50
52
|
email: kevin@glowacz.info
|
51
53
|
executables: []
|
52
|
-
|
53
54
|
extensions: []
|
54
|
-
|
55
|
-
extra_rdoc_files:
|
55
|
+
extra_rdoc_files:
|
56
56
|
- README
|
57
|
-
files:
|
57
|
+
files:
|
58
58
|
- .rspec
|
59
59
|
- Gemfile
|
60
|
-
- Gemfile.lock
|
61
60
|
- README
|
62
61
|
- Rakefile
|
63
62
|
- VERSION
|
@@ -68,36 +67,28 @@ files:
|
|
68
67
|
- spec/schema.rb
|
69
68
|
- spec/spec_helper.rb
|
70
69
|
- spec/speed_test.rb
|
71
|
-
has_rdoc: true
|
72
70
|
homepage:
|
73
71
|
licenses: []
|
74
|
-
|
75
72
|
post_install_message:
|
76
73
|
rdoc_options: []
|
77
|
-
|
78
|
-
require_paths:
|
74
|
+
require_paths:
|
79
75
|
- lib
|
80
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
77
|
none: false
|
82
|
-
requirements:
|
83
|
-
- -
|
84
|
-
- !ruby/object:Gem::Version
|
85
|
-
|
86
|
-
|
87
|
-
- 0
|
88
|
-
version: "0"
|
89
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ! '>='
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
83
|
none: false
|
91
|
-
requirements:
|
92
|
-
- -
|
93
|
-
- !ruby/object:Gem::Version
|
94
|
-
version:
|
84
|
+
requirements:
|
85
|
+
- - ! '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
95
88
|
requirements: []
|
96
|
-
|
97
89
|
rubyforge_project:
|
98
|
-
rubygems_version: 1.6
|
90
|
+
rubygems_version: 1.8.6
|
99
91
|
signing_key:
|
100
92
|
specification_version: 3
|
101
93
|
summary: allows rails STI to work when the type is normalized out
|
102
94
|
test_files: []
|
103
|
-
|
data/Gemfile.lock
DELETED
@@ -1,75 +0,0 @@
|
|
1
|
-
GEM
|
2
|
-
remote: http://rubygems.org/
|
3
|
-
specs:
|
4
|
-
abstract (1.0.0)
|
5
|
-
actionpack (3.0.8)
|
6
|
-
activemodel (= 3.0.8)
|
7
|
-
activesupport (= 3.0.8)
|
8
|
-
builder (~> 2.1.2)
|
9
|
-
erubis (~> 2.6.6)
|
10
|
-
i18n (~> 0.5.0)
|
11
|
-
rack (~> 1.2.1)
|
12
|
-
rack-mount (~> 0.6.14)
|
13
|
-
rack-test (~> 0.5.7)
|
14
|
-
tzinfo (~> 0.3.23)
|
15
|
-
activemodel (3.0.8)
|
16
|
-
activesupport (= 3.0.8)
|
17
|
-
builder (~> 2.1.2)
|
18
|
-
i18n (~> 0.5.0)
|
19
|
-
activerecord (3.0.8)
|
20
|
-
activemodel (= 3.0.8)
|
21
|
-
activesupport (= 3.0.8)
|
22
|
-
arel (~> 2.0.10)
|
23
|
-
tzinfo (~> 0.3.23)
|
24
|
-
activesupport (3.0.8)
|
25
|
-
arel (2.0.10)
|
26
|
-
builder (2.1.2)
|
27
|
-
diff-lcs (1.1.2)
|
28
|
-
erubis (2.6.6)
|
29
|
-
abstract (>= 1.0.0)
|
30
|
-
git (1.2.5)
|
31
|
-
i18n (0.5.0)
|
32
|
-
jeweler (1.6.2)
|
33
|
-
bundler (~> 1.0)
|
34
|
-
git (>= 1.2.5)
|
35
|
-
rake
|
36
|
-
mocha (0.9.12)
|
37
|
-
rack (1.2.3)
|
38
|
-
rack-mount (0.6.14)
|
39
|
-
rack (>= 1.0.0)
|
40
|
-
rack-test (0.5.7)
|
41
|
-
rack (>= 1.0)
|
42
|
-
railties (3.0.8)
|
43
|
-
actionpack (= 3.0.8)
|
44
|
-
activesupport (= 3.0.8)
|
45
|
-
rake (>= 0.8.7)
|
46
|
-
thor (~> 0.14.4)
|
47
|
-
rake (0.9.2)
|
48
|
-
rspec (2.6.0)
|
49
|
-
rspec-core (~> 2.6.0)
|
50
|
-
rspec-expectations (~> 2.6.0)
|
51
|
-
rspec-mocks (~> 2.6.0)
|
52
|
-
rspec-core (2.6.4)
|
53
|
-
rspec-expectations (2.6.0)
|
54
|
-
diff-lcs (~> 1.1.2)
|
55
|
-
rspec-mocks (2.6.0)
|
56
|
-
rspec-rails (2.6.1)
|
57
|
-
actionpack (~> 3.0)
|
58
|
-
activesupport (~> 3.0)
|
59
|
-
railties (~> 3.0)
|
60
|
-
rspec (~> 2.6.0)
|
61
|
-
sqlite3 (1.3.3)
|
62
|
-
thor (0.14.6)
|
63
|
-
tzinfo (0.3.28)
|
64
|
-
|
65
|
-
PLATFORMS
|
66
|
-
ruby
|
67
|
-
|
68
|
-
DEPENDENCIES
|
69
|
-
activerecord (~> 3.0.0)
|
70
|
-
i18n
|
71
|
-
jeweler
|
72
|
-
mocha
|
73
|
-
rspec
|
74
|
-
rspec-rails
|
75
|
-
sqlite3
|