polymorpheus 3.1.0 → 3.1.1

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.
@@ -1,2 +1,34 @@
1
- require 'support/custom_matchers.rb'
2
- require 'support/db_setup.rb'
1
+ require 'active_record'
2
+ require 'polymorpheus'
3
+ require 'stringio'
4
+
5
+ require 'support/active_record/connection_adapters/abstract_mysql_adapter'
6
+
7
+ ActiveRecord::Base.establish_connection({
8
+ adapter: 'mysql2',
9
+ username: 'travis',
10
+ database: 'polymorpheus_test'
11
+ })
12
+
13
+ Dir[File.dirname(__FILE__) + '/support/*.rb'].sort.each { |path| require path }
14
+
15
+ Polymorpheus::Adapter.load!
16
+
17
+ # This is normally done via a Railtie in non-testing situations.
18
+ ActiveRecord::SchemaDumper.class_eval { include Polymorpheus::SchemaDumper }
19
+ ActiveRecord::Migration.verbose = false
20
+
21
+ RSpec.configure do |config|
22
+ config.order = :random
23
+ Kernel.srand config.seed
24
+
25
+ config.include ConnectionHelpers
26
+ config.include SchemaHelpers
27
+ config.include SqlTestHelpers
28
+
29
+ config.after do
30
+ data_sources.each do |table|
31
+ ActiveRecord::Base.connection.drop_table(table)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,9 @@
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
@@ -42,6 +42,11 @@ class Battle < ActiveRecord::Base
42
42
  has_many :heros, through: :story_arcs
43
43
  end
44
44
 
45
+ class Issue < ActiveRecord::Base
46
+ has_many :story_arcs
47
+ has_many :heros, through: :story_arcs
48
+ end
49
+
45
50
  # But only super-people have superpowers
46
51
  class Superpower < ActiveRecord::Base
47
52
  belongs_to_polymorphic :superhero, :supervillain, as: :wielder
@@ -71,3 +76,10 @@ end
71
76
  class PrintedWork < ActiveRecord::Base
72
77
  has_many_as_polymorph :pictures, inverse_of: :printed_work
73
78
  end
79
+
80
+ class Pet < ActiveRecord::Base
81
+ end
82
+ class Cat < ActiveRecord::Base
83
+ end
84
+ class Dog < ActiveRecord::Base
85
+ end
@@ -0,0 +1,21 @@
1
+ module ConnectionHelpers
2
+ def add_polymorphic_constraints(*args)
3
+ connection.add_polymorphic_constraints(*args)
4
+ end
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
+ def remove_polymorphic_constraints(*args)
15
+ connection.remove_polymorphic_constraints(*args)
16
+ end
17
+
18
+ def triggers
19
+ connection.triggers
20
+ end
21
+ end
@@ -0,0 +1,17 @@
1
+ module SchemaHelpers
2
+ def create_table(name, options = {})
3
+ options.merge!(force: true)
4
+ ActiveRecord::Schema.define do
5
+ create_table(name, options) do |t|
6
+ yield(t) if block_given?
7
+ end
8
+ end
9
+ name.to_s.classify.constantize.reset_column_information
10
+ end
11
+
12
+ def drop_table(name)
13
+ ActiveRecord::Schema.define do
14
+ drop_table(name)
15
+ end
16
+ end
17
+ end
@@ -21,7 +21,7 @@ module Polymorpheus
21
21
  @stubbed[sql]
22
22
  else
23
23
  sql_statements << sql
24
- sql
24
+ original_execute(sql, name)
25
25
  end
26
26
  end
27
27
 
@@ -0,0 +1,41 @@
1
+ module SqlTestHelpers
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ before(:all) do
6
+ class << ActiveRecord::Base.connection
7
+ include Polymorpheus::SqlLogger
8
+ alias_method :original_execute, :execute
9
+ alias_method :execute, :log_sql_statements
10
+ end
11
+ end
12
+
13
+ after(:all) do
14
+ class << ActiveRecord::Base.connection
15
+ alias_method :execute, :original_execute
16
+ end
17
+ end
18
+
19
+ let(:connection) { ActiveRecord::Base.connection }
20
+ let(:sql) { connection.sql_statements }
21
+
22
+ def clean_sql(sql_string)
23
+ sql_string
24
+ .squish
25
+ .gsub('`', '')
26
+ .gsub(/\ FOREIGN KEY/, "\nFOREIGN KEY")
27
+ .gsub(/\ REFERENCES/, "\nREFERENCES")
28
+ .gsub(/\ ON DELETE/, "\nON DELETE")
29
+ .gsub(/\ ON UPDATE/, "\nON UPDATE")
30
+ .gsub(/([[:alpha:]])\(/, '\1 (')
31
+ end
32
+
33
+ def clear_sql_history
34
+ connection.clear_sql_history
35
+ end
36
+
37
+ def should_execute_sql(expected)
38
+ expect(clean_sql(sql.join("\n"))).to include(clean_sql(expected.squish))
39
+ end
40
+ end
41
+ end
@@ -1,8 +1,6 @@
1
- require 'polymorpheus'
2
- require 'polymorpheus/trigger'
3
-
4
- describe Trigger do
1
+ require 'spec_helper'
5
2
 
3
+ describe Polymorpheus::Trigger do
6
4
  let(:name) { "pets_unique_polyfk_on_INSERT" }
7
5
  let(:event) { "INSERT" }
8
6
  let(:table) { "pets"}
@@ -22,8 +20,9 @@ describe Trigger do
22
20
  let(:db_collation) { "utf8_unicode_ci" }
23
21
 
24
22
  subject do
25
- Trigger.new([name, event, table, statement, timing, created, sql_mode,
26
- definer, charset, collation_connection, db_collation])
23
+ described_class.new([name, event, table, statement, timing, created,
24
+ sql_mode, definer, charset, collation_connection,
25
+ db_collation])
27
26
  end
28
27
 
29
28
  its(:name) { should == name }
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.1.0
4
+ version: 3.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Barun Singh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-24 00:00:00.000000000 Z
11
+ date: 2017-06-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: '3.2'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '5'
22
+ version: '5.2'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,7 +29,7 @@ dependencies:
29
29
  version: '3.2'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '5'
32
+ version: '5.2'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: rake
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -45,7 +45,7 @@ dependencies:
45
45
  - !ruby/object:Gem::Version
46
46
  version: 10.4.2
47
47
  - !ruby/object:Gem::Dependency
48
- name: rspec-rails
48
+ name: rspec
49
49
  requirement: !ruby/object:Gem::Requirement
50
50
  requirements:
51
51
  - - "~>"
@@ -58,20 +58,6 @@ dependencies:
58
58
  - - "~>"
59
59
  - !ruby/object:Gem::Version
60
60
  version: 2.14.0
61
- - !ruby/object:Gem::Dependency
62
- name: mysql2
63
- requirement: !ruby/object:Gem::Requirement
64
- requirements:
65
- - - "~>"
66
- - !ruby/object:Gem::Version
67
- version: 0.3.10
68
- type: :development
69
- prerelease: false
70
- version_requirements: !ruby/object:Gem::Requirement
71
- requirements:
72
- - - "~>"
73
- - !ruby/object:Gem::Version
74
- version: 0.3.10
75
61
  description: Provides a database-friendly method for polymorphic relationships
76
62
  email: bsingh@wegowise.com
77
63
  executables: []
@@ -101,15 +87,20 @@ files:
101
87
  - lib/polymorpheus/trigger.rb
102
88
  - lib/polymorpheus/version.rb
103
89
  - polymorpheus.gemspec
90
+ - spec/interface/belongs_to_polymorphic_spec.rb
91
+ - spec/interface/has_many_as_polymorph_spec.rb
92
+ - spec/interface/validates_polymorph_spec.rb
104
93
  - spec/interface_spec.rb
105
94
  - spec/mysql2_adapter_spec.rb
106
95
  - spec/schema_dumper_spec.rb
107
- - spec/shared_examples.rb
108
96
  - spec/spec_helper.rb
109
- - spec/sql_logger.rb
97
+ - spec/support/active_record/connection_adapters/abstract_mysql_adapter.rb
110
98
  - spec/support/class_defs.rb
99
+ - spec/support/connection_helpers.rb
111
100
  - spec/support/custom_matchers.rb
112
- - spec/support/db_setup.rb
101
+ - spec/support/schema_helpers.rb
102
+ - spec/support/sql_logger.rb
103
+ - spec/support/sql_test_helpers.rb
113
104
  - spec/trigger_spec.rb
114
105
  homepage: http://github.com/wegowise/polymorpheus
115
106
  licenses:
@@ -131,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
131
122
  version: 1.3.6
132
123
  requirements: []
133
124
  rubyforge_project:
134
- rubygems_version: 2.4.5
125
+ rubygems_version: 2.6.12
135
126
  signing_key:
136
127
  specification_version: 4
137
128
  summary: Provides a database-friendly method for polymorphic relationships
@@ -1,125 +0,0 @@
1
- shared_examples_for 'mysql2 add sql for polymorphic constraints' do
2
- describe "#add_polymorphic_constraints" do
3
- before { connection.add_polymorphic_constraints(table, columns, options) }
4
-
5
- specify do
6
- clean_sql(sql.join("\n")).should == clean_sql(full_constraints_sql)
7
- end
8
- end
9
- end
10
-
11
- shared_examples_for 'mysql2 add sql for polymorphic triggers' do
12
- describe "#add_polymorphic_triggers" do
13
- before { connection.add_polymorphic_triggers(table, columns.keys) }
14
-
15
- specify do
16
- clean_sql(sql.join("\n")).should == clean_sql(trigger_sql)
17
- end
18
- end
19
- end
20
-
21
- shared_examples_for 'mysql2 remove sql for polymorphic constraints' do
22
- describe "#remove_polymorphic_constraints" do
23
- before { connection.remove_polymorphic_constraints(table, columns, options) }
24
-
25
- specify do
26
- clean_sql(sql.join("\n")).should == clean_sql(remove_constraints_sql)
27
- end
28
- end
29
- end
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
-
37
- shared_context "columns with short names" do
38
- let(:table) { 'pets' }
39
- let(:columns) { { 'kitty_id' => 'cats.name', 'dog_id' => 'dogs.id' } }
40
- let(:trigger_sql) do
41
- %{
42
- DROP TRIGGER IF EXISTS pfki_pets_dogid_kittyid
43
- DROP TRIGGER IF EXISTS pfku_pets_dogid_kittyid
44
- CREATE TRIGGER pfki_pets_dogid_kittyid BEFORE INSERT ON pets
45
- FOR EACH ROW
46
- BEGIN
47
- IF(IF(NEW.dog_id IS NULL, 0, 1) + IF(NEW.kitty_id IS NULL, 0, 1)) <> 1 THEN
48
- SET NEW = 'Error';
49
- END IF;
50
- END
51
- CREATE TRIGGER pfku_pets_dogid_kittyid BEFORE UPDATE ON pets
52
- FOR EACH ROW
53
- BEGIN
54
- IF(IF(NEW.dog_id IS NULL, 0, 1) + IF(NEW.kitty_id IS NULL, 0, 1)) <> 1 THEN
55
- SET NEW = 'Error';
56
- END IF;
57
- END
58
- }
59
- end
60
- let(:fkey_sql) do
61
- %{
62
- ALTER TABLE `pets` ADD CONSTRAINT `pets_dog_id_fk` FOREIGN KEY (`dog_id`) REFERENCES `dogs`(id)
63
- ALTER TABLE `pets` ADD CONSTRAINT `pets_kitty_id_fk` FOREIGN KEY (`kitty_id`) REFERENCES `cats`(name)
64
- }
65
- end
66
- let(:unique_key_sql) { '' }
67
- let(:full_constraints_sql) { trigger_sql + unique_key_sql + fkey_sql }
68
- let(:remove_indices_sql) { '' }
69
- let(:remove_constraints_sql) do
70
- %{
71
- DROP TRIGGER IF EXISTS pfki_pets_dogid_kittyid
72
- DROP TRIGGER IF EXISTS pfku_pets_dogid_kittyid
73
- ALTER TABLE `pets` DROP FOREIGN KEY `pets_kitty_id_fk`
74
- ALTER TABLE `pets` DROP FOREIGN KEY `pets_dog_id_fk`
75
- } +
76
- remove_indices_sql
77
- end
78
- end
79
-
80
- shared_context "columns with long names" do
81
- let(:table) { 'bicycles' }
82
- let(:columns) do
83
- { 'im_too_cool_to_vote_and_ill_only_ride_a_fixie' => 'hipster.id',
84
- 'really_im_not_doping_i_just_practice_a_lot' => 'professional.id' }
85
- end
86
- let(:options) { {} }
87
-
88
- let(:trigger_sql) do
89
- %{
90
- DROP TRIGGER IF EXISTS pfki_bicycles_imtoocooltovoteandillonl_reallyimnotdopingijustpr
91
- DROP TRIGGER IF EXISTS pfku_bicycles_imtoocooltovoteandillonl_reallyimnotdopingijustpr
92
- CREATE TRIGGER pfki_bicycles_imtoocooltovoteandillonl_reallyimnotdopingijustpr BEFORE INSERT ON bicycles
93
- FOR EACH ROW
94
- BEGIN
95
- IF(IF(NEW.im_too_cool_to_vote_and_ill_only_ride_a_fixie IS NULL, 0, 1) + IF(NEW.really_im_not_doping_i_just_practice_a_lot IS NULL, 0, 1)) <> 1 THEN
96
- SET NEW = 'Error';
97
- END IF;
98
- END
99
- CREATE TRIGGER pfku_bicycles_imtoocooltovoteandillonl_reallyimnotdopingijustpr BEFORE UPDATE ON bicycles
100
- FOR EACH ROW
101
- BEGIN
102
- IF(IF(NEW.im_too_cool_to_vote_and_ill_only_ride_a_fixie IS NULL, 0, 1) + IF(NEW.really_im_not_doping_i_just_practice_a_lot IS NULL, 0, 1)) <> 1 THEN
103
- SET NEW = 'Error';
104
- END IF;
105
- END
106
- }
107
- end
108
-
109
- let(:fkey_sql) do
110
- %{
111
- ALTER TABLE `bicycles` ADD CONSTRAINT `bicycles_im_too_cool_to_vote_and_ill_only_ride_a_fixie_fk` FOREIGN KEY (`im_too_cool_to_vote_and_ill_only_ride_a_fixie`) REFERENCES `hipster`(id)
112
- ALTER TABLE `bicycles` ADD CONSTRAINT `bicycles_really_im_not_doping_i_just_practice_a_lot_fk` FOREIGN KEY (`really_im_not_doping_i_just_practice_a_lot`) REFERENCES `professional`(id)
113
- }
114
- end
115
-
116
- let(:full_constraints_sql) { trigger_sql + fkey_sql }
117
- let(:remove_constraints_sql) do
118
- %{
119
- DROP TRIGGER IF EXISTS pfki_bicycles_imtoocooltovoteandillonl_reallyimnotdopingijustpr
120
- DROP TRIGGER IF EXISTS pfku_bicycles_imtoocooltovoteandillonl_reallyimnotdopingijustpr
121
- ALTER TABLE `bicycles` DROP FOREIGN KEY `bicycles_im_too_cool_to_vote_and_ill_only_ride_a_fixie_fk`
122
- ALTER TABLE `bicycles` DROP FOREIGN KEY `bicycles_really_im_not_doping_i_just_practice_a_lot_fk`
123
- }
124
- end
125
- end
@@ -1,38 +0,0 @@
1
- ActiveRecord::Base.establish_connection({
2
- adapter: 'mysql2',
3
- username: 'travis',
4
- database: 'polymorpheus_test'
5
- })
6
-
7
- ActiveRecord::Base.connection.tables.each do |table|
8
- ActiveRecord::Base.connection.drop_table table
9
- end
10
-
11
- ActiveRecord::Schema.define do
12
- create_table :heros
13
- create_table :villains
14
- create_table :superheros
15
- create_table :alien_demigods
16
- create_table :supervillains
17
- create_table :trees
18
- create_table :drawings
19
- create_table :books
20
- create_table :binders
21
- create_table :pictures
22
- create_table :web_pages
23
- create_table :printed_works
24
-
25
- create_table :story_arcs do |t|
26
- t.integer :hero_id
27
- t.integer :villain_id
28
- t.integer :battle_id
29
- t.integer :issue_id
30
- end
31
-
32
- create_table :battles
33
-
34
- create_table :superpowers do |t|
35
- t.integer :superhero_id
36
- t.integer :supervillain_id
37
- end
38
- end