polymorpheus 2.2.0 → 3.0.0

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
  SHA1:
3
- metadata.gz: 2b4fa428b1261c05072970feddd72eca1d4e0d70
4
- data.tar.gz: 189801e622b209426bb6e39b86a32b7c34bbc954
3
+ metadata.gz: d0a7b04ed03c563cb471078e8595129ea9e429a7
4
+ data.tar.gz: 912c0b0dbf5690c3fdf0ae34b62acec60849203b
5
5
  SHA512:
6
- metadata.gz: 1624b4a33ce2a7875acda4688ebad9662e2f6201e01d329196cee018acfcbec0bafe86f3d07c732c383525e13b96e3fda65b510f29201615aa7637803300b32c
7
- data.tar.gz: f207481aef2d8e20c22c7efb2d86691c2d9ed6ba241356e7eb977a27c3d7c90c1f9aceab5f3b4972956f730ca0baa850d6c5b3e0a38866f54a478e6b8daaeb29
6
+ metadata.gz: cb4cb74d2cb1381f91478899fe4a5d4bd9d9b8a522cd6410ef2ddc144c1e3750e46f24006a082bd28b5a3907eae06469e45afc6e0ea75bc1013dd3edb15306a0
7
+ data.tar.gz: 5d64aae54b033191a7b134c61a7d2bcb2072ba236f2d897021fcb5c71212202c833f02ee873fb38b394be91485b8087e9f40966d360979479020ee6634384d67
data/README.md CHANGED
@@ -5,6 +5,23 @@
5
5
  **Polymorphic relationships in Rails that keep your database happy with almost
6
6
  no setup**
7
7
 
8
+ ### Installation
9
+
10
+ If you are using Bundler, you can add the gem to your Gemfile:
11
+
12
+ ```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
+ gem 'polymorpheus'
23
+ ```
24
+
8
25
  ### Background
9
26
  * **What is polymorphism?** [Rails Guides has a great overview of what
10
27
  polymorphic relationships are and how Rails handles them](
@@ -196,7 +213,7 @@ pic.polymorpheus.query_condition
196
213
 
197
214
  * This gem was written by [Barun Singh](https://github.com/barunio)
198
215
  * It uses the [Foreigner gem](https://github.com/matthuhiggins/foreigner) under
199
- the hood for a few things
216
+ the hood for Rails < 4.2.
200
217
 
201
218
  polymorpheus is Copyright © 2011-2015 Barun Singh and [WegoWise](
202
219
  http://wegowise.com). It is free software, and may be redistributed under the
@@ -23,8 +23,15 @@ module Polymorpheus
23
23
  module ConnectionAdapters
24
24
  autoload :SchemaStatements, 'polymorpheus/schema_statements'
25
25
  end
26
+
27
+ def self.require_foreigner?
28
+ !(::ActiveRecord::VERSION::MAJOR >= 4 &&
29
+ ::ActiveRecord::VERSION::MINOR >= 2)
30
+ end
26
31
  end
27
32
 
33
+ require 'foreigner' if ::Polymorpheus.require_foreigner?
34
+
28
35
  Polymorpheus::Adapter.register 'mysql2', 'polymorpheus/mysql_adapter'
29
36
  Polymorpheus::Adapter.register 'postgresql', 'polymorpheus/postgresql_adapter'
30
37
 
@@ -66,19 +66,22 @@ module Polymorpheus
66
66
  if options[:unique].present?
67
67
  poly_create_indexes(table, column_names, Array(options[:unique]))
68
68
  end
69
+
69
70
  column_names.each do |col_name|
70
71
  ref_table, ref_col = columns[col_name].to_s.split('.')
71
- add_foreign_key table, ref_table,
72
- :column => col_name,
73
- :primary_key => (ref_col || 'id'),
74
- :options => generate_constraints(options)
72
+ fk_options = {
73
+ :column => col_name,
74
+ :name => "#{table}_#{col_name}_fk",
75
+ :primary_key => (ref_col || 'id' )
76
+ }.merge(generate_constraints(options))
77
+ add_foreign_key(table, ref_table, fk_options)
75
78
  end
76
79
  end
77
80
 
78
81
  def remove_polymorphic_constraints(table, columns, options = {})
79
82
  poly_drop_triggers(table, columns.keys.sort)
80
83
  columns.each do |(col, reference)|
81
- remove_foreign_key table, :column => col
84
+ remove_foreign_key table, :column => col, :name => "#{table}_#{col}_fk"
82
85
  end
83
86
  if options[:unique].present?
84
87
  poly_remove_indexes(table, columns.keys, Array(options[:unique]))
@@ -191,26 +194,8 @@ module Polymorpheus
191
194
  end
192
195
 
193
196
  def generate_constraints(options)
194
- constraints = []
195
-
196
- ['delete', 'update'].each do |event|
197
- option = "on_#{event}".to_sym
198
- next unless options.has_key?(option) &&
199
- options[option].respond_to?(:to_sym)
200
-
201
- action = case options[option].to_sym
202
- when :nullify then 'SET NULL'
203
- when :cascade then 'CASCADE'
204
- when :restrict then 'RESTRICT'
205
- end
206
- next unless action
207
-
208
- constraints << "ON #{event.upcase} #{action}"
209
- end
210
-
211
- constraints.join(' ')
197
+ options.slice(:on_delete, :on_update)
212
198
  end
213
-
214
199
  end
215
200
  end
216
201
  end
@@ -223,3 +208,8 @@ end
223
208
  rescue
224
209
  end
225
210
  end
211
+
212
+ if ::Polymorpheus.require_foreigner?
213
+ require 'foreigner/connection_adapters/mysql2_adapter'
214
+ require 'polymorpheus/mysql_adapter/foreigner_constraints'
215
+ end
@@ -0,0 +1,30 @@
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,3 +1,3 @@
1
1
  module Polymorpheus
2
- VERSION = '2.2.0'
2
+ VERSION = '3.0.0'
3
3
  end
@@ -17,8 +17,7 @@ Gem::Specification.new do |s|
17
17
  s.extra_rdoc_files = ["README.md", "LICENSE.txt"]
18
18
  s.license = 'MIT'
19
19
 
20
- s.add_dependency('foreigner')
21
- s.add_dependency('activerecord', '>= 3.2', '< 4.2')
20
+ s.add_dependency('activerecord', '>= 3.2', '< 5')
22
21
 
23
22
  s.add_development_dependency('rake', '~> 10.4.2')
24
23
  s.add_development_dependency('rspec-rails', '~> 2.14.0')
@@ -1,8 +1,6 @@
1
1
  require 'active_record'
2
2
  require 'spec_helper'
3
3
  require 'sql_logger'
4
- require 'foreigner'
5
- require 'foreigner/connection_adapters/mysql2_adapter'
6
4
  require 'polymorpheus'
7
5
  require 'polymorpheus/trigger'
8
6
  require 'shared_examples'
@@ -35,6 +33,12 @@ describe Polymorpheus::ConnectionAdapters::MysqlAdapter do
35
33
  def clean_sql(sql_string)
36
34
  sql_string.gsub(/^\n\s*/,'').gsub(/\s*\n\s*$/,'')
37
35
  .gsub(/\n\s*/,"\n").gsub(/\s*$/,"")
36
+ .gsub('`', '')
37
+ .gsub(/\ FOREIGN KEY/, "\nFOREIGN KEY")
38
+ .gsub(/\ REFERENCES/, "\nREFERENCES")
39
+ .gsub(/\ ON DELETE/, "\nON DELETE")
40
+ .gsub(/\ ON UPDATE/, "\nON UPDATE")
41
+ .gsub(/([[:alpha:]])\(/, '\1 (')
38
42
  end
39
43
 
40
44
  before do
@@ -129,7 +133,14 @@ describe Polymorpheus::ConnectionAdapters::MysqlAdapter do
129
133
  ALTER TABLE `pets` ADD CONSTRAINT `pets_kitty_id_fk` FOREIGN KEY (`kitty_id`) REFERENCES `cats`(name) }
130
134
  end
131
135
 
132
- it_behaves_like "mysql2 migration statements"
136
+ it "#add_polymorphic_constraints raises an argument error" do
137
+ expect do
138
+ connection.add_polymorphic_constraints(table, columns, options)
139
+ end.to raise_error ArgumentError
140
+ end
141
+
142
+ it_behaves_like 'mysql2 add sql for polymorphic triggers'
143
+ it_behaves_like 'mysql2 remove sql for polymorphic constraints'
133
144
  end
134
145
 
135
146
  context "when table and column names combined are very long" do
@@ -1,8 +1,6 @@
1
1
  require 'active_record'
2
2
  require 'spec_helper'
3
3
  require 'sql_logger'
4
- require 'foreigner'
5
- require 'foreigner/connection_adapters/mysql2_adapter'
6
4
  require 'polymorpheus'
7
5
  require 'polymorpheus/trigger'
8
6
  require 'stringio'
@@ -1,4 +1,4 @@
1
- shared_examples_for "mysql2 migration statements" do
1
+ shared_examples_for 'mysql2 add sql for polymorphic constraints' do
2
2
  describe "#add_polymorphic_constraints" do
3
3
  before { connection.add_polymorphic_constraints(table, columns, options) }
4
4
 
@@ -6,7 +6,9 @@ shared_examples_for "mysql2 migration statements" do
6
6
  clean_sql(sql.join("\n")).should == clean_sql(full_constraints_sql)
7
7
  end
8
8
  end
9
+ end
9
10
 
11
+ shared_examples_for 'mysql2 add sql for polymorphic triggers' do
10
12
  describe "#add_polymorphic_triggers" do
11
13
  before { connection.add_polymorphic_triggers(table, columns.keys) }
12
14
 
@@ -14,7 +16,9 @@ shared_examples_for "mysql2 migration statements" do
14
16
  clean_sql(sql.join("\n")).should == clean_sql(trigger_sql)
15
17
  end
16
18
  end
19
+ end
17
20
 
21
+ shared_examples_for 'mysql2 remove sql for polymorphic constraints' do
18
22
  describe "#remove_polymorphic_constraints" do
19
23
  before { connection.remove_polymorphic_constraints(table, columns, options) }
20
24
 
@@ -24,6 +28,12 @@ shared_examples_for "mysql2 migration statements" do
24
28
  end
25
29
  end
26
30
 
31
+ shared_examples_for "mysql2 migration statements" do
32
+ it_behaves_like 'mysql2 add sql for polymorphic constraints'
33
+ it_behaves_like 'mysql2 add sql for polymorphic triggers'
34
+ it_behaves_like 'mysql2 remove sql for polymorphic constraints'
35
+ end
36
+
27
37
  shared_context "columns with short names" do
28
38
  let(:table) { 'pets' }
29
39
  let(:columns) { { 'kitty_id' => 'cats.name', 'dog_id' => 'dogs.id' } }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: polymorpheus
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Barun Singh
@@ -10,20 +10,6 @@ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2015-09-17 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: foreigner
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: activerecord
29
15
  requirement: !ruby/object:Gem::Requirement
@@ -33,7 +19,7 @@ dependencies:
33
19
  version: '3.2'
34
20
  - - "<"
35
21
  - !ruby/object:Gem::Version
36
- version: '4.2'
22
+ version: '5'
37
23
  type: :runtime
38
24
  prerelease: false
39
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -43,7 +29,7 @@ dependencies:
43
29
  version: '3.2'
44
30
  - - "<"
45
31
  - !ruby/object:Gem::Version
46
- version: '4.2'
32
+ version: '5'
47
33
  - !ruby/object:Gem::Dependency
48
34
  name: rake
49
35
  requirement: !ruby/object:Gem::Requirement
@@ -107,6 +93,7 @@ files:
107
93
  - lib/polymorpheus/interface_builder.rb
108
94
  - lib/polymorpheus/interface_builder/association.rb
109
95
  - lib/polymorpheus/mysql_adapter.rb
96
+ - lib/polymorpheus/mysql_adapter/foreigner_constraints.rb
110
97
  - lib/polymorpheus/postgresql_adapter.rb
111
98
  - lib/polymorpheus/railtie.rb
112
99
  - lib/polymorpheus/schema_dumper.rb