schema_qualified_tables 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,11 @@
1
1
  schema_qualified_tables history
2
2
  ===============================
3
3
 
4
+ 1.1.1
5
+ -----
6
+
7
+ - Don't prepend the schema if it is already present in AR 3.2. (#8, #9)
8
+
4
9
  1.1.0
5
10
  -----
6
11
 
@@ -13,6 +13,7 @@ For example
13
13
 
14
14
  Say you have an application using a legacy schema that has models like this:
15
15
 
16
+ # Rails 2.3, 3.0 and 3.1
16
17
  class Surgery < ActiveRecord::Base
17
18
  belongs_to :surgeon, :class_name => "Person", :foreign_key => "surgeon_id"
18
19
  set_table_name "t_surgeries"
@@ -22,6 +23,16 @@ Say you have an application using a legacy schema that has models like this:
22
23
  set_table_name "hr.t_personnel"
23
24
  end
24
25
 
26
+ # Rails 3.2+
27
+ class Surgery < ActiveRecord::Base
28
+ belongs_to :surgeon, :class_name => "Person", :foreign_key => "surgeon_id"
29
+ self.table_name = "t_surgeries"
30
+ end
31
+
32
+ class Person < ActiveRecord::Base
33
+ self.table_name = "hr.t_personnel"
34
+ end
35
+
25
36
  These models map to tables in two schemas: the default schema, which
26
37
  contains `t_surgeries`; and the schema `hr`, which contains
27
38
  `t_personnel`.
@@ -45,17 +56,21 @@ logical schema name for your models which is resolved into the actual
45
56
  schema name based on runtime configuration. In this case, you'd
46
57
  re-write `Person` like so:
47
58
 
59
+ # Rails 2.3, 3.0 and 3.1
48
60
  class Person < ActiveRecord::Base
49
61
  set_schema :hr
50
62
  set_table_name :t_personnel
51
63
  end
52
64
 
53
- # For Rails 3.2+ this syntax is also supported
65
+ # Rails 3.2+
54
66
  class Person < ActiveRecord::Base
55
67
  self.schema = :hr
56
68
  self.table_name = :t_personnel
57
69
  end
58
70
 
71
+ (Please note: The deprecated-in-Rails-3.2 syntax `set_table_name` and
72
+ `set_sequence_name` _does not work_ with schema qualified tables in Rails 3.2.)
73
+
59
74
  Then, if you need to override the actual schema name in some
60
75
  environments, configure `ActiveRecord::Base.schemas`:
61
76
 
@@ -1,7 +1,7 @@
1
1
  module Bcdatabase
2
2
  module ActiveRecord
3
3
  module SchemaQualifiedTables
4
- VERSION = '1.1.0'
4
+ VERSION = '1.1.1'
5
5
  end
6
6
  end
7
7
  end
@@ -22,20 +22,20 @@ module Bcdatabase
22
22
 
23
23
  def table_name
24
24
  unless abstract_class?
25
- if schema_name
26
- "#{schema_name}.#{super}"
27
- else
25
+ if schema_name_prepended?(schema_name, super)
28
26
  super
27
+ else
28
+ "#{schema_name}.#{super}"
29
29
  end
30
30
  end
31
31
  end
32
32
 
33
33
  def sequence_name
34
34
  unless abstract_class?
35
- if schema_name && (super =~ /^#{schema_name}\./).nil?
36
- "#{schema_name}.#{super}"
37
- else
35
+ if schema_name_prepended?(schema_name, super)
38
36
  super
37
+ else
38
+ "#{schema_name}.#{super}"
39
39
  end
40
40
  end
41
41
  end
@@ -51,6 +51,10 @@ module Bcdatabase
51
51
 
52
52
  protected
53
53
 
54
+ def schema_name_prepended?(name, super_klass)
55
+ !(name && (super_klass =~ /^#{name}\./).nil?)
56
+ end
57
+
54
58
  def schema_name
55
59
  ::ActiveRecord::Base.schemas[self.schema] || self.schema
56
60
  end
@@ -59,4 +63,4 @@ module Bcdatabase
59
63
  end
60
64
  end
61
65
 
62
- ::ActiveRecord::Base.send(:include, Bcdatabase::ActiveRecord::SchemaQualifiedTables)
66
+ ::ActiveRecord::Base.send(:include, Bcdatabase::ActiveRecord::SchemaQualifiedTables)
@@ -24,7 +24,7 @@ describe "SchemaQualifiedTables" do
24
24
 
25
25
  after do
26
26
  Object.class_eval do
27
- %w(ReadingMaterialBase Book Magazine Newspaper Pamphlet).each do |clazz|
27
+ %w(ReadingMaterialBase Book Magazine Newspaper Pamphlet AnotherMagazine).each do |clazz|
28
28
  remove_const clazz if const_defined? clazz
29
29
  end
30
30
  end
@@ -150,6 +150,17 @@ describe "SchemaQualifiedTables" do
150
150
  Magazine.table_name.should == "periodicals.some_magazines"
151
151
  Newspaper.table_name.should == "deprecated.newspapers"
152
152
  end
153
+
154
+ it "prepends the schema name only once when inheriting a table name" do
155
+ class Magazine < ReadingMaterialBase
156
+ sqt_table_name("some_magazines")
157
+ end
158
+
159
+ class AnotherMagazine < Magazine
160
+ end
161
+
162
+ AnotherMagazine.table_name.should == "reading_material.some_magazines"
163
+ end
153
164
  end
154
165
 
155
166
  describe "with name overrides" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schema_qualified_tables
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-10-08 00:00:00.000000000 Z
13
+ date: 2012-10-12 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord
@@ -143,18 +143,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
143
143
  - - ! '>='
144
144
  - !ruby/object:Gem::Version
145
145
  version: '0'
146
- segments:
147
- - 0
148
- hash: -3344378071871990966
149
146
  required_rubygems_version: !ruby/object:Gem::Requirement
150
147
  none: false
151
148
  requirements:
152
149
  - - ! '>='
153
150
  - !ruby/object:Gem::Version
154
151
  version: '0'
155
- segments:
156
- - 0
157
- hash: -3344378071871990966
158
152
  requirements: []
159
153
  rubyforge_project:
160
154
  rubygems_version: 1.8.24