active_record-mti 0.4.0.pre.1 → 0.4.0.pre.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e639bd5df85ba2ea0f83ed1b43b4429dd1a40ee627e2a43e17d329ce64c85486
4
- data.tar.gz: 7c2103fc368f13f065a2004accfe8f0b83808446c12cc074c30b5bc588bb8f81
3
+ metadata.gz: 7c8aad5e387fec209700a57d5366938eaa5149b5436d10b3c6a859f75b7c7716
4
+ data.tar.gz: b060126c06310a40843a64652eb11ff821ac4aaadce5a48f203cc801146f905a
5
5
  SHA512:
6
- metadata.gz: b19553d393bf02d1292215d8d36bacdddb8d1f5901e8e0e184a4a3cb0e9b488c8d6f73fe9c827ad306742b60c206f6f55cd87641d455b7937eaaf0a1496905ed
7
- data.tar.gz: ab4c52d72f15bef11c1ed261546bd43175a6f7a084e5b29f31c8cdf0bd3d0dd1de2ad8f2e67b5653baff6b8a783b7ce1d290a3400ec192d3623fd7256658d037
6
+ metadata.gz: d055ffae44af8954bd1eeb9ddc60f7611b9dd2596e44bc96096858a68731eb42030269db6e3e5b3f1a5ead71acbbf6ec1a17500c7ae7410225febeb7af285d96
7
+ data.tar.gz: '04865157eb1a894b3867a3c2b536838a56f600779178b799e2fb034dfd60e3476a5cf8e5a9d6aa6c8723f02a8a48feaf2b489c59375d946257b589ac301fb6ef'
data/README.md CHANGED
@@ -73,19 +73,6 @@ Conventionally—to indicate the nature of inheritance—`ActiveRecord::MTI` exp
73
73
 
74
74
  Note, `ActiveRecord::MTI` will fall back on the unnested `table_name` if it's unable to find the nested form, and short of that, it will use the superclass's `table_name`.
75
75
 
76
- ### Configuration
77
- `ActiveRecord::MTI` can be configured using a configure block.
78
-
79
- ```ruby
80
- # config/initializers/active_record_mti.rb
81
-
82
- ActiveRecord::MTI.configure do |config|
83
- config.table_name_nesting = true
84
- config.nesting_seperator = '/'
85
- config.singular_parent = true
86
- end
87
- ```
88
-
89
76
  ### Migrations
90
77
 
91
78
  In your migrations define a table to inherit from another table:
@@ -2,7 +2,6 @@ require 'active_record/mti/version'
2
2
  require 'active_record/mti/railtie' if defined?(Rails::Railtie)
3
3
 
4
4
  require 'registry'
5
- require 'active_record/mti/config'
6
5
  require 'active_record/mti/table'
7
6
  require 'core_ext/thread'
8
7
 
@@ -18,37 +18,43 @@ module ActiveRecord
18
18
  # we manually add it. Lastly we also create indexes on the child table to match those
19
19
  # on the parent table since indexes are also not inherited.
20
20
  def create_table(table_name, options = {})
21
- if options[:inherits]
21
+ if (inherited_table = options.delete(:inherits))
22
22
  options[:id] = false
23
23
  options.delete(:primary_key)
24
- end
25
-
26
- if (inherited_table = options.delete(:inherits))
27
- # options[:options] = options[:options].sub("INHERITS", "() INHERITS") if td.columns.empty?
28
24
  options[:options] = [%(INHERITS ("#{inherited_table}")), options[:options]].compact.join
29
25
  end
30
26
 
31
- results = super(table_name, options)
27
+ super(table_name, options) do |td|
28
+ yield(td) if block_given?
32
29
 
33
- if inherited_table
34
- inherited_table_primary_key = primary_key(inherited_table)
35
- execute %(ALTER TABLE "#{table_name}" ADD PRIMARY KEY ("#{inherited_table_primary_key}"))
30
+ fix_inherits_statement(td) if inherited_table
31
+ end.tap do |result|
32
+ inherit_indexes(table_name, inherited_table) if inherited_table
33
+ end
34
+ end
36
35
 
37
- indexes(inherited_table).each do |index|
38
- attributes = index_attributes(index)
36
+ def fix_inherits_statement(td)
37
+ if td.columns.empty? && ActiveRecord.version >= Gem::Version.new('5.0')
38
+ td.options.gsub!('INHERITS', '() INHERITS')
39
+ end
40
+ end
41
+
42
+ def inherit_indexes(table_name, inherited_table)
43
+ inherited_table_primary_key = primary_key(inherited_table)
44
+ execute %(ALTER TABLE "#{table_name}" ADD PRIMARY KEY ("#{inherited_table_primary_key}"))
39
45
 
40
- # Why rails insists on being inconsistant with itself is beyond me.
41
- attributes[:order] = attributes.delete(:orders)
46
+ indexes(inherited_table).each do |index|
47
+ attributes = index_attributes(index)
42
48
 
43
- if (index_name = build_index_name(attributes.delete(:name), inherited_table, table_name))
44
- attributes[:name] = index_name
45
- end
49
+ # Why rails insists on being inconsistant with itself is beyond me.
50
+ attributes[:order] = attributes.delete(:orders)
46
51
 
47
- add_index table_name, index.columns, attributes
52
+ if (index_name = build_index_name(attributes.delete(:name), inherited_table, table_name))
53
+ attributes[:name] = index_name
48
54
  end
49
- end
50
55
 
51
- results
56
+ add_index table_name, index.columns, attributes
57
+ end
52
58
  end
53
59
 
54
60
  def index_attributes(index)
@@ -4,37 +4,6 @@ module ActiveRecord
4
4
  module MTI
5
5
  module Relation
6
6
 
7
- # Consider using a join to avoid complicated selects?
8
- # Maybe each "mti" belongs_to :mti_table foreign_key: :tableoid?
9
- # SELECT p.relname, c.name, c.altitude
10
- # FROM cities c, pg_class p
11
- # WHERE c.altitude > 500 AND c.tableoid = p.oid;
12
-
13
-
14
- # TODO: Introduce natural joins
15
- # https://dba.stackexchange.com/questions/94050/query-parent-table-and-get-child-tables-columns
16
- # EXPLAIN ANALYSE SELECT * FROM ONLY listeners NATURAL FULL JOIN "listeners/bridge";
17
-
18
- # EXPLAIN ANALYSE SELECT * FROM "listeners/bridge";
19
-
20
- # EXPLAIN ANALYSE SELECT * FROM listeners;
21
-
22
- # EXPLAIN ANALYSE SELECT * FROM ONLY listeners
23
- # NATURAL FULL JOIN "listeners/bridge"
24
- # NATURAL FULL JOIN "listeners/integration"
25
- # NATURAL FULL JOIN "listeners/nest_thermostat"
26
- # NATURAL FULL JOIN "listeners/sensor"
27
- # NATURAL FULL JOIN "listeners/system"
28
- # NATURAL FULL JOIN "listeners/system_users"
29
- # NATURAL FULL JOIN "listeners/user";
30
-
31
- # table_name = mti_table.name
32
- # scope :naturally, -> {
33
- # descendants.inject(only) { |ar|
34
- # ar.join
35
- # }
36
- # }
37
-
38
7
  def build_arel(*)
39
8
  super.tap do |ar|
40
9
  build_mti(ar)
@@ -58,8 +27,8 @@ module ActiveRecord
58
27
  case projection
59
28
  when Arel::Attributes::Attribute
60
29
  projection.relation.name == klass.table_name && projection.name.to_s == 'tableoid'
61
- # when Arel::Nodes::As
62
- # tableoid_projection?(projection.left)
30
+ when Arel::Nodes::As
31
+ tableoid_projection?(projection.left)
63
32
  when Arel::Nodes::SqlLiteral
64
33
  projection == 'tableoid'
65
34
  else
@@ -67,14 +36,6 @@ module ActiveRecord
67
36
  end
68
37
  end
69
38
 
70
- # def exclusively(tables=[klass])
71
- # @table = table.dup.tap do |t|
72
- # t.only!
73
- # end
74
- # binding.pry
75
- # self
76
- # end
77
-
78
39
  private
79
40
 
80
41
  def perform_calculation(*)
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module MTI
3
- VERSION = '0.4.0-1'.freeze
3
+ VERSION = '0.4.0-2'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record-mti
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0.pre.1
4
+ version: 0.4.0.pre.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dale Stevens
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-12 00:00:00.000000000 Z
11
+ date: 2019-06-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -161,7 +161,6 @@ files:
161
161
  - LICENSE
162
162
  - README.md
163
163
  - lib/active_record/mti.rb
164
- - lib/active_record/mti/config.rb
165
164
  - lib/active_record/mti/connection_adapters/postgresql/adapter.rb
166
165
  - lib/active_record/mti/connection_adapters/postgresql/schema_statements.rb
167
166
  - lib/active_record/mti/core_extension.rb
@@ -169,9 +168,7 @@ files:
169
168
  - lib/active_record/mti/relation.rb
170
169
  - lib/active_record/mti/schema_dumper.rb
171
170
  - lib/active_record/mti/table.rb
172
- - lib/active_record/mti/table_oid.rb
173
171
  - lib/active_record/mti/version.rb
174
- - lib/core_ext/array.rb
175
172
  - lib/core_ext/hash.rb
176
173
  - lib/core_ext/thread.rb
177
174
  homepage: https://github.com/twilightcoders/active_record-mti
@@ -195,7 +192,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
195
192
  - !ruby/object:Gem::Version
196
193
  version: 1.3.1
197
194
  requirements: []
198
- rubygems_version: 3.0.3
195
+ rubyforge_project:
196
+ rubygems_version: 2.7.6.2
199
197
  signing_key:
200
198
  specification_version: 4
201
199
  summary: Multi Table Inheritance for PostgreSQL in Rails
@@ -1,26 +0,0 @@
1
- module ActiveRecord
2
- module MTI
3
- class << self
4
- attr_accessor :configuration
5
- end
6
-
7
- DEFAULT_CONFIG = {
8
- table_name_nesting: true,
9
- nesting_seperator: '/',
10
- singular_parent: true,
11
- prefix_parent: true,
12
- suffix_parent: false,
13
- namespace_depth: 0 # -1 for all
14
- }
15
-
16
- def self.reset_configuration
17
- self.configuration = OpenStruct.new(DEFAULT_CONFIG)
18
- end
19
-
20
- self.reset_configuration
21
-
22
- def self.configure
23
- yield(configuration)
24
- end
25
- end
26
- end
@@ -1,6 +0,0 @@
1
- module ActiveRecord
2
- module MTI
3
- module TableOID
4
- end
5
- end
6
- end
@@ -1,14 +0,0 @@
1
- Array.class_eval do
2
-
3
- alias_method :join_original, :join
4
- def join(separator=$,)
5
- if block_given?
6
- inject(String.new) do |collector, item|
7
- collector << yield(item).to_s + separator
8
- end.chomp(separator)
9
- else
10
- join_original(separator)
11
- end
12
- end
13
-
14
- end