polymorpheus 3.3.0 → 3.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -12
- data/lib/polymorpheus/adapter.rb +5 -1
- data/lib/polymorpheus/interface/belongs_to_polymorphic.rb +1 -1
- data/lib/polymorpheus/interface/{rails4/has_many_as_polymorph.rb → has_many_as_polymorph.rb} +1 -1
- data/lib/polymorpheus/mysql_adapter.rb +1 -6
- data/lib/polymorpheus/version.rb +1 -1
- data/lib/polymorpheus.rb +1 -14
- data/polymorpheus.gemspec +2 -1
- data/spec/spec_helper.rb +8 -7
- data/spec/support/connection_helpers.rb +0 -8
- data/spec/support/schema_helpers.rb +1 -1
- data/spec/support/sql_query_subscriber.rb +17 -0
- data/spec/support/sql_test_helpers.rb +10 -12
- metadata +10 -13
- data/lib/polymorpheus/interface/rails3/has_many_as_polymorph.rb +0 -29
- data/lib/polymorpheus/mysql_adapter/foreigner_constraints.rb +0 -30
- data/spec/support/active_record/connection_adapters/abstract_mysql_adapter.rb +0 -9
- data/spec/support/sql_logger.rb +0 -30
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 60273820617c2e5ef7e4eab45965a2729ae9ecb3e8ba05357cf9c0ae432d36be
|
4
|
+
data.tar.gz: 156743a57b80836a20280d0bdef6bd4e3f895e79cbf93aac60b986fe5deb4bac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5bb967919da9877c1129879492445d4972aff408399c0c6da01804cc4848382e9565b1cf32aa72c63101cf791ef331e5382cededa0a146e0d439a828f34fade2
|
7
|
+
data.tar.gz: cb3b1f649f7317ef555b78b6932a5385c8f78ba7e8b5bbdb3d37a552a4b4c20dce0ef72fd1deb410745744885020766e674acd50a1826989b720fe99abe71e65
|
data/README.md
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
[![Build Status](https://travis-ci.org/wegowise/polymorpheus.png?branch=master)](https://travis-ci.org/wegowise/polymorpheus)
|
2
1
|
[![Code Climate](https://codeclimate.com/github/wegowise/polymorpheus.png)](https://codeclimate.com/github/wegowise/polymorpheus)
|
3
2
|
|
4
3
|
# Polymorpheus
|
@@ -10,15 +9,6 @@ no setup**
|
|
10
9
|
If you are using Bundler, you can add the gem to your Gemfile:
|
11
10
|
|
12
11
|
```ruby
|
13
|
-
# with Rails >= 4.2
|
14
|
-
gem 'polymorpheus'
|
15
|
-
```
|
16
|
-
|
17
|
-
Or:
|
18
|
-
|
19
|
-
```ruby
|
20
|
-
# with Rails < 4.2
|
21
|
-
gem 'foreigner'
|
22
12
|
gem 'polymorpheus'
|
23
13
|
```
|
24
14
|
|
@@ -212,8 +202,8 @@ pic.polymorpheus.query_condition
|
|
212
202
|
## Credits and License
|
213
203
|
|
214
204
|
* This gem was written by [Barun Singh](https://github.com/barunio)
|
215
|
-
*
|
216
|
-
the hood for Rails < 4.2.
|
205
|
+
* Older, unsupported versions of this gem use [Foreigner gem](https://github.com/matthuhiggins/foreigner)
|
206
|
+
under the hood for Rails < 4.2.
|
217
207
|
|
218
208
|
polymorpheus is Copyright © 2011-2015 Barun Singh and [WegoWise](
|
219
209
|
http://wegowise.com). It is free software, and may be redistributed under the
|
data/lib/polymorpheus/adapter.rb
CHANGED
@@ -15,7 +15,11 @@ module Polymorpheus
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def configured_adapter
|
18
|
-
ActiveRecord::Base.
|
18
|
+
if ActiveRecord::Base.respond_to?(:connection_db_config)
|
19
|
+
ActiveRecord::Base.connection_db_config.adapter # ActiveRecord >= 6.1
|
20
|
+
else
|
21
|
+
ActiveRecord::Base.connection_pool.spec.config[:adapter]
|
22
|
+
end
|
19
23
|
end
|
20
24
|
end
|
21
25
|
|
@@ -21,7 +21,7 @@ module Polymorpheus
|
|
21
21
|
|
22
22
|
# Set belongs_to associations
|
23
23
|
builder.associations.each do |association|
|
24
|
-
belongs_to association.name.to_sym, association.options
|
24
|
+
belongs_to association.name.to_sym, **association.options
|
25
25
|
end
|
26
26
|
|
27
27
|
# Exposed interface for introspection
|
@@ -74,7 +74,7 @@ module Polymorpheus
|
|
74
74
|
:name => "#{table}_#{col_name}_fk",
|
75
75
|
:primary_key => (ref_col || 'id' )
|
76
76
|
}.merge(generate_constraints(options))
|
77
|
-
add_foreign_key(table, ref_table, fk_options)
|
77
|
+
add_foreign_key(table, ref_table, **fk_options)
|
78
78
|
end
|
79
79
|
end
|
80
80
|
|
@@ -208,8 +208,3 @@ end
|
|
208
208
|
rescue
|
209
209
|
end
|
210
210
|
end
|
211
|
-
|
212
|
-
if ::Polymorpheus.require_foreigner?
|
213
|
-
require 'foreigner/connection_adapters/mysql2_adapter'
|
214
|
-
require 'polymorpheus/mysql_adapter/foreigner_constraints'
|
215
|
-
end
|
data/lib/polymorpheus/version.rb
CHANGED
data/lib/polymorpheus.rb
CHANGED
@@ -8,12 +8,7 @@ module Polymorpheus
|
|
8
8
|
module Interface
|
9
9
|
autoload :BelongsToPolymorphic, 'polymorpheus/interface/belongs_to_polymorphic'
|
10
10
|
autoload :ValidatesPolymorph, 'polymorpheus/interface/validates_polymorph'
|
11
|
-
|
12
|
-
if ActiveRecord::VERSION::MAJOR >= 4
|
13
|
-
autoload :HasManyAsPolymorph, 'polymorpheus/interface/rails4/has_many_as_polymorph'
|
14
|
-
else
|
15
|
-
autoload :HasManyAsPolymorph, 'polymorpheus/interface/rails3/has_many_as_polymorph'
|
16
|
-
end
|
11
|
+
autoload :HasManyAsPolymorph, 'polymorpheus/interface/has_many_as_polymorph'
|
17
12
|
end
|
18
13
|
|
19
14
|
class InterfaceBuilder
|
@@ -23,16 +18,8 @@ module Polymorpheus
|
|
23
18
|
module ConnectionAdapters
|
24
19
|
autoload :SchemaStatements, 'polymorpheus/schema_statements'
|
25
20
|
end
|
26
|
-
|
27
|
-
def self.require_foreigner?
|
28
|
-
ActiveRecord::VERSION::MAJOR < 5 &&
|
29
|
-
!(::ActiveRecord::VERSION::MAJOR >= 4 &&
|
30
|
-
::ActiveRecord::VERSION::MINOR >= 2)
|
31
|
-
end
|
32
21
|
end
|
33
22
|
|
34
|
-
require 'foreigner' if ::Polymorpheus.require_foreigner?
|
35
|
-
|
36
23
|
Polymorpheus::Adapter.register 'mysql2', 'polymorpheus/mysql_adapter'
|
37
24
|
Polymorpheus::Adapter.register 'postgresql', 'polymorpheus/postgresql_adapter'
|
38
25
|
|
data/polymorpheus.gemspec
CHANGED
@@ -12,12 +12,13 @@ Gem::Specification.new do |s|
|
|
12
12
|
s.summary = "Provides a database-friendly method for polymorphic relationships"
|
13
13
|
s.description = "Provides a database-friendly method for polymorphic relationships"
|
14
14
|
|
15
|
+
s.required_ruby_version = ">= 2.6"
|
15
16
|
s.required_rubygems_version = ">= 1.3.6"
|
16
17
|
s.files = Dir.glob(%w[{lib,spec}/**/*.rb [A-Z]*.{txt,rdoc,md} *.gemspec]) + %w{Rakefile}
|
17
18
|
s.extra_rdoc_files = ["README.md", "LICENSE.txt"]
|
18
19
|
s.license = 'MIT'
|
19
20
|
|
20
|
-
s.add_dependency('activerecord', '>=
|
21
|
+
s.add_dependency('activerecord', '>= 5.2', '< 7.1')
|
21
22
|
|
22
23
|
s.add_development_dependency('rake', '~> 12.3.3')
|
23
24
|
s.add_development_dependency('rspec', '~> 3.9.0')
|
data/spec/spec_helper.rb
CHANGED
@@ -2,13 +2,14 @@ require 'active_record'
|
|
2
2
|
require 'polymorpheus'
|
3
3
|
require 'stringio'
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
ActiveRecord::Base.establish_connection({
|
5
|
+
ActiveRecord::Base.establish_connection(
|
8
6
|
adapter: 'mysql2',
|
9
|
-
|
10
|
-
|
11
|
-
|
7
|
+
database: ENV.fetch('DB_NAME', 'polymorpheus_test'),
|
8
|
+
host: ENV.fetch('DB_HOST', '127.0.0.1'),
|
9
|
+
password: ENV.fetch('DB_PASSWORD', ''),
|
10
|
+
port: ENV.fetch('DB_PORT', '3306'),
|
11
|
+
username: ENV.fetch('DB_USERNAME', 'root')
|
12
|
+
)
|
12
13
|
|
13
14
|
Dir[File.dirname(__FILE__) + '/support/*.rb'].sort.each { |path| require path }
|
14
15
|
|
@@ -27,7 +28,7 @@ RSpec.configure do |config|
|
|
27
28
|
config.include SqlTestHelpers
|
28
29
|
|
29
30
|
config.after do
|
30
|
-
|
31
|
+
ActiveRecord::Base.connection.tables.each do |table|
|
31
32
|
ActiveRecord::Base.connection.drop_table(table)
|
32
33
|
end
|
33
34
|
end
|
@@ -3,14 +3,6 @@ module ConnectionHelpers
|
|
3
3
|
connection.add_polymorphic_constraints(*args)
|
4
4
|
end
|
5
5
|
|
6
|
-
def data_sources
|
7
|
-
if ActiveRecord::VERSION::MAJOR >= 5
|
8
|
-
ActiveRecord::Base.connection.data_sources
|
9
|
-
else
|
10
|
-
ActiveRecord::Base.connection.tables
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
6
|
def remove_polymorphic_constraints(*args)
|
15
7
|
connection.remove_polymorphic_constraints(*args)
|
16
8
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Polymorpheus
|
2
|
+
class SqlQuerySubscriber
|
3
|
+
attr_reader :sql_statements
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@sql_statements = []
|
7
|
+
end
|
8
|
+
|
9
|
+
def call(_name, _start, _finish, _id, payload)
|
10
|
+
sql_statements << payload[:sql]
|
11
|
+
end
|
12
|
+
|
13
|
+
def clear_sql_history
|
14
|
+
@sql_statements.clear
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -2,22 +2,16 @@ module SqlTestHelpers
|
|
2
2
|
extend ActiveSupport::Concern
|
3
3
|
|
4
4
|
included do
|
5
|
-
before
|
6
|
-
|
7
|
-
include Polymorpheus::SqlLogger
|
8
|
-
alias_method :original_execute, :execute
|
9
|
-
alias_method :execute, :log_sql_statements
|
10
|
-
end
|
5
|
+
before do
|
6
|
+
ActiveSupport::Notifications.subscribe('sql.active_record', sql_logger)
|
11
7
|
end
|
12
8
|
|
13
|
-
after
|
14
|
-
|
15
|
-
alias_method :execute, :original_execute
|
16
|
-
end
|
9
|
+
after do
|
10
|
+
ActiveSupport::Notifications.unsubscribe(sql_logger)
|
17
11
|
end
|
18
12
|
|
19
13
|
let(:connection) { ActiveRecord::Base.connection }
|
20
|
-
let(:
|
14
|
+
let(:sql_logger) { Polymorpheus::SqlQuerySubscriber.new }
|
21
15
|
|
22
16
|
def clean_sql(sql_string)
|
23
17
|
sql_string
|
@@ -31,11 +25,15 @@ module SqlTestHelpers
|
|
31
25
|
end
|
32
26
|
|
33
27
|
def clear_sql_history
|
34
|
-
|
28
|
+
sql_logger.clear_sql_history
|
35
29
|
end
|
36
30
|
|
37
31
|
def should_execute_sql(expected)
|
38
32
|
expect(clean_sql(sql.join("\n"))).to include(clean_sql(expected))
|
39
33
|
end
|
34
|
+
|
35
|
+
def sql
|
36
|
+
sql_logger.sql_statements
|
37
|
+
end
|
40
38
|
end
|
41
39
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: polymorpheus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Barun Singh
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-03-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -16,20 +16,20 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '5.2'
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '
|
22
|
+
version: '7.1'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
26
26
|
requirements:
|
27
27
|
- - ">="
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version: '
|
29
|
+
version: '5.2'
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '
|
32
|
+
version: '7.1'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: rake
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -73,13 +73,11 @@ files:
|
|
73
73
|
- lib/polymorpheus/adapter.rb
|
74
74
|
- lib/polymorpheus/interface.rb
|
75
75
|
- lib/polymorpheus/interface/belongs_to_polymorphic.rb
|
76
|
-
- lib/polymorpheus/interface/
|
77
|
-
- lib/polymorpheus/interface/rails4/has_many_as_polymorph.rb
|
76
|
+
- lib/polymorpheus/interface/has_many_as_polymorph.rb
|
78
77
|
- lib/polymorpheus/interface/validates_polymorph.rb
|
79
78
|
- lib/polymorpheus/interface_builder.rb
|
80
79
|
- lib/polymorpheus/interface_builder/association.rb
|
81
80
|
- lib/polymorpheus/mysql_adapter.rb
|
82
|
-
- lib/polymorpheus/mysql_adapter/foreigner_constraints.rb
|
83
81
|
- lib/polymorpheus/postgresql_adapter.rb
|
84
82
|
- lib/polymorpheus/railtie.rb
|
85
83
|
- lib/polymorpheus/schema_dumper.rb
|
@@ -94,12 +92,11 @@ files:
|
|
94
92
|
- spec/mysql2_adapter_spec.rb
|
95
93
|
- spec/schema_dumper_spec.rb
|
96
94
|
- spec/spec_helper.rb
|
97
|
-
- spec/support/active_record/connection_adapters/abstract_mysql_adapter.rb
|
98
95
|
- spec/support/class_defs.rb
|
99
96
|
- spec/support/connection_helpers.rb
|
100
97
|
- spec/support/custom_matchers.rb
|
101
98
|
- spec/support/schema_helpers.rb
|
102
|
-
- spec/support/
|
99
|
+
- spec/support/sql_query_subscriber.rb
|
103
100
|
- spec/support/sql_test_helpers.rb
|
104
101
|
- spec/trigger_spec.rb
|
105
102
|
homepage: http://github.com/wegowise/polymorpheus
|
@@ -114,14 +111,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
114
111
|
requirements:
|
115
112
|
- - ">="
|
116
113
|
- !ruby/object:Gem::Version
|
117
|
-
version: '
|
114
|
+
version: '2.6'
|
118
115
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
116
|
requirements:
|
120
117
|
- - ">="
|
121
118
|
- !ruby/object:Gem::Version
|
122
119
|
version: 1.3.6
|
123
120
|
requirements: []
|
124
|
-
rubygems_version: 3.
|
121
|
+
rubygems_version: 3.3.6
|
125
122
|
signing_key:
|
126
123
|
specification_version: 4
|
127
124
|
summary: Provides a database-friendly method for polymorphic relationships
|
@@ -1,29 +0,0 @@
|
|
1
|
-
module Polymorpheus
|
2
|
-
module Interface
|
3
|
-
module HasManyAsPolymorph
|
4
|
-
def has_many_as_polymorph(association, options = {})
|
5
|
-
options.symbolize_keys!
|
6
|
-
fkey = name.foreign_key
|
7
|
-
|
8
|
-
class_name = options[:class_name] || association.to_s.classify
|
9
|
-
|
10
|
-
options[:conditions] = proc do
|
11
|
-
keys = class_name.constantize
|
12
|
-
.const_get('POLYMORPHEUS_ASSOCIATIONS')
|
13
|
-
.map(&:foreign_key)
|
14
|
-
keys.delete(fkey)
|
15
|
-
|
16
|
-
nil_columns = keys.reduce({}) { |hash, key| hash.merge!(key => nil) }
|
17
|
-
|
18
|
-
if self.is_a?(ActiveRecord::Associations::JoinDependency::JoinAssociation)
|
19
|
-
{ aliased_table_name => nil_columns }
|
20
|
-
else
|
21
|
-
{ association => nil_columns }
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
has_many association, options
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
@@ -1,30 +0,0 @@
|
|
1
|
-
module Polymorpheus
|
2
|
-
module ConnectionAdapters
|
3
|
-
module MysqlAdapter
|
4
|
-
def generate_constraints(options)
|
5
|
-
constraints = []
|
6
|
-
|
7
|
-
['delete', 'update'].each do |event|
|
8
|
-
option = "on_#{event}".to_sym
|
9
|
-
next unless options.has_key?(option) &&
|
10
|
-
options[option].respond_to?(:to_sym)
|
11
|
-
|
12
|
-
action = case options[option].to_sym
|
13
|
-
when :nullify then 'SET NULL'
|
14
|
-
when :cascade then 'CASCADE'
|
15
|
-
when :restrict then 'RESTRICT'
|
16
|
-
else
|
17
|
-
fail ArgumentError, <<-EOS
|
18
|
-
'#{options[option]}' is not supported for :on_update or :on_delete.
|
19
|
-
Supported values are: :nullify, :cascade, :restrict
|
20
|
-
EOS
|
21
|
-
end
|
22
|
-
|
23
|
-
constraints << "ON #{event.upcase} #{action}"
|
24
|
-
end
|
25
|
-
|
26
|
-
{ :options => constraints.join(' ') }
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
@@ -1,9 +0,0 @@
|
|
1
|
-
# Patch support for MySQL 5.7+ onto ActiveRecord < 4.1.
|
2
|
-
if ActiveRecord::VERSION::MAJOR < 4 ||
|
3
|
-
(ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR < 1)
|
4
|
-
|
5
|
-
require 'active_record/connection_adapters/abstract_mysql_adapter'
|
6
|
-
class ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter
|
7
|
-
NATIVE_DATABASE_TYPES[:primary_key] = "int(11) auto_increment PRIMARY KEY"
|
8
|
-
end
|
9
|
-
end
|
data/spec/support/sql_logger.rb
DELETED
@@ -1,30 +0,0 @@
|
|
1
|
-
module Polymorpheus
|
2
|
-
module SqlLogger
|
3
|
-
|
4
|
-
def sql_statements
|
5
|
-
@sql_statements ||= []
|
6
|
-
end
|
7
|
-
|
8
|
-
def clear_sql_history
|
9
|
-
@sql_statements = nil
|
10
|
-
end
|
11
|
-
|
12
|
-
def stub_sql(statement, response)
|
13
|
-
@stubbed ||= {}
|
14
|
-
@stubbed[statement] = response
|
15
|
-
end
|
16
|
-
|
17
|
-
private
|
18
|
-
|
19
|
-
def log_sql_statements(sql, name = nil)
|
20
|
-
if @stubbed && @stubbed.has_key?(sql)
|
21
|
-
@stubbed[sql]
|
22
|
-
else
|
23
|
-
sql_statements << sql
|
24
|
-
original_execute(sql, name)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|