polymorpheus 2.1.1 → 2.2.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: 8ed8d4d7c368cb6a0881483bee736be9da580d24
4
- data.tar.gz: 91f6db54c6323d392b470c97dfbc26c01d2f5946
3
+ metadata.gz: 2b4fa428b1261c05072970feddd72eca1d4e0d70
4
+ data.tar.gz: 189801e622b209426bb6e39b86a32b7c34bbc954
5
5
  SHA512:
6
- metadata.gz: 6ae1e2d62b17fb7634779da45165fbec63ce59684dcd038db73d0730f301653a2d5223373fdc0fb4bdab39569432db043d95d8820c449a7972332ceb4088ef2d
7
- data.tar.gz: 70a440da6a5470585de16d9478e9899f0ba428ea3c5b8b1cd6e37ce4eb1070f2f575f96f826a47caaf98669e1a572a69360751ebb4ac13701dde79827a23547f
6
+ metadata.gz: 1624b4a33ce2a7875acda4688ebad9662e2f6201e01d329196cee018acfcbec0bafe86f3d07c732c383525e13b96e3fda65b510f29201615aa7637803300b32c
7
+ data.tar.gz: f207481aef2d8e20c22c7efb2d86691c2d9ed6ba241356e7eb977a27c3d7c90c1f9aceab5f3b4972956f730ca0baa850d6c5b3e0a38866f54a478e6b8daaeb29
data/README.md CHANGED
@@ -198,6 +198,6 @@ pic.polymorpheus.query_condition
198
198
  * It uses the [Foreigner gem](https://github.com/matthuhiggins/foreigner) under
199
199
  the hood for a few things
200
200
 
201
- polymorpheus is Copyright © 2011-2012 Barun Singh and [WegoWise](
201
+ polymorpheus is Copyright © 2011-2015 Barun Singh and [WegoWise](
202
202
  http://wegowise.com). It is free software, and may be redistributed under the
203
203
  terms specified in the LICENSE file.
@@ -5,37 +5,59 @@ module Polymorpheus
5
5
  INSERT = 'INSERT'
6
6
  UPDATE = 'UPDATE'
7
7
 
8
- # See the README for explanations regarding the use of these methods
8
+ # See the README for more information about the use of these methods.
9
9
  #
10
10
  # table: a string equal to the name of the db table
11
11
  #
12
12
  # columns: a hash, with keys equal to the column names in the table we
13
13
  # are operating on, and values indicating the foreign key
14
- # association through the form "table.column". so,
15
- # { 'employee_id' => 'employees.ssn',
16
- # 'product_id' => 'products.id' }
17
- # indicates that the `employee_id` column in `table` should have
18
- # a foreign key constraint connecting it to the `ssn` column
19
- # in the `employees` table, and the `product_id` column should
20
- # have a foreign key constraint with the `id` column in the
21
- # `products` table
22
- #
23
- # options: a hash, corrently only accepts one option that allows us to
24
- # add an additional uniqueness constraint.
25
- # if the columns hash was specified as above, and we supplied
26
- # options of
27
- # { :unique => true }
28
- # then this would create a uniqueness constraint in the database
29
- # that would ensure that any given employee_id could only be in
30
- # the table once, and that any given product_id could only be in
31
- # the table once.
32
- #
33
- # alternatively, the user can also supply a column name or array
34
- # of column names to the :unique option:
35
- # { :unique => 'picture_url' }
36
- # This would allow an employee_id (or product_id) to appear
37
- # multiple times in the table, but no two employee ids would
38
- # be able to have the same picture_url.
14
+ # association through the form "table.column".
15
+ #
16
+ # For example:
17
+ #
18
+ # {
19
+ # 'employee_id' => 'employees.ssn',
20
+ # 'product_id' => 'products.id'
21
+ # }
22
+ #
23
+ # This indicates that the `employee_id` column in `table`
24
+ # should have a foreign key constraint connecting it to the
25
+ # `ssn` column in the `employees` table, and the `product_id`
26
+ # column should have a foreign key constraint with the `id`
27
+ # column in the `products` table.
28
+ #
29
+ # options: a hash, accepting the following options
30
+ #
31
+ # :unique
32
+ #
33
+ # If the columns hash was specified as above, and :unique is true:
34
+ #
35
+ # { :unique => true }
36
+ #
37
+ # Then this creates a uniqueness constraint in the database that
38
+ # will ensure that any given employee_id can only be in the table
39
+ # once, and that any given product_id can only be in the table
40
+ # once.
41
+ #
42
+ # Alternatively, you can supply a column name or array of column
43
+ # names to the :unique option:
44
+ #
45
+ # { :unique => 'picture_url' }
46
+ #
47
+ # This will allow an employee_id (or product_id) to appear multiple
48
+ # times in the table, but no two employee IDs would be able to have
49
+ # the same picture_url.
50
+ #
51
+ # :on_delete
52
+ #
53
+ # Action that happens ON DELETE. Valid values are :nullify,
54
+ # :cascade and :restrict.
55
+ #
56
+ # :on_update
57
+ #
58
+ # Action that happens ON UPDATE. Valid values are :nullify,
59
+ # :cascade and :restrict.
60
+
39
61
 
40
62
  def add_polymorphic_constraints(table, columns, options={})
41
63
  column_names = columns.keys.sort
@@ -48,7 +70,8 @@ module Polymorpheus
48
70
  ref_table, ref_col = columns[col_name].to_s.split('.')
49
71
  add_foreign_key table, ref_table,
50
72
  :column => col_name,
51
- :primary_key => (ref_col || 'id')
73
+ :primary_key => (ref_col || 'id'),
74
+ :options => generate_constraints(options)
52
75
  end
53
76
  end
54
77
 
@@ -167,6 +190,27 @@ module Polymorpheus
167
190
  columns.map { |c| c.to_s.gsub('_','').first(col_length-1) }.join('_')
168
191
  end
169
192
 
193
+ 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(' ')
212
+ end
213
+
170
214
  end
171
215
  end
172
216
  end
@@ -179,4 +223,3 @@ end
179
223
  rescue
180
224
  end
181
225
  end
182
-
@@ -1,3 +1,3 @@
1
1
  module Polymorpheus
2
- VERSION = '2.1.1'
2
+ VERSION = '2.2.0'
3
3
  end
@@ -19,6 +19,8 @@ Gem::Specification.new do |s|
19
19
 
20
20
  s.add_dependency('foreigner')
21
21
  s.add_dependency('activerecord', '>= 3.2', '< 4.2')
22
+
23
+ s.add_development_dependency('rake', '~> 10.4.2')
22
24
  s.add_development_dependency('rspec-rails', '~> 2.14.0')
23
- s.add_development_dependency('mysql2', '~> 0.3')
25
+ s.add_development_dependency('mysql2', '~> 0.3.10')
24
26
  end
@@ -99,6 +99,39 @@ describe Polymorpheus::ConnectionAdapters::MysqlAdapter do
99
99
  it_behaves_like "mysql2 migration statements"
100
100
  end
101
101
 
102
+ context "specifying an on update constraint" do
103
+ include_context "columns with short names"
104
+ let(:options) { { :on_update => :cascade } }
105
+ let(:fkey_sql) do
106
+ %{ ALTER TABLE `pets` ADD CONSTRAINT `pets_dog_id_fk` FOREIGN KEY (`dog_id`) REFERENCES `dogs`(id) ON UPDATE CASCADE
107
+ ALTER TABLE `pets` ADD CONSTRAINT `pets_kitty_id_fk` FOREIGN KEY (`kitty_id`) REFERENCES `cats`(name) ON UPDATE CASCADE }
108
+ end
109
+
110
+ it_behaves_like "mysql2 migration statements"
111
+ end
112
+
113
+ context "specifying on delete and on update constraints" do
114
+ include_context "columns with short names"
115
+ let(:options) { { :on_update => :cascade, :on_delete => :restrict } }
116
+ let(:fkey_sql) do
117
+ %{ ALTER TABLE `pets` ADD CONSTRAINT `pets_dog_id_fk` FOREIGN KEY (`dog_id`) REFERENCES `dogs`(id) ON DELETE RESTRICT ON UPDATE CASCADE
118
+ ALTER TABLE `pets` ADD CONSTRAINT `pets_kitty_id_fk` FOREIGN KEY (`kitty_id`) REFERENCES `cats`(name) ON DELETE RESTRICT ON UPDATE CASCADE }
119
+ end
120
+
121
+ it_behaves_like "mysql2 migration statements"
122
+ end
123
+
124
+ context "when on_delete and on_update have invalid arguments" do
125
+ include_context "columns with short names"
126
+ let(:options) { { :on_update => :invalid, :on_delete => nil } }
127
+ let(:fkey_sql) do
128
+ %{ ALTER TABLE `pets` ADD CONSTRAINT `pets_dog_id_fk` FOREIGN KEY (`dog_id`) REFERENCES `dogs`(id)
129
+ ALTER TABLE `pets` ADD CONSTRAINT `pets_kitty_id_fk` FOREIGN KEY (`kitty_id`) REFERENCES `cats`(name) }
130
+ end
131
+
132
+ it_behaves_like "mysql2 migration statements"
133
+ end
134
+
102
135
  context "when table and column names combined are very long" do
103
136
  include_context "columns with long names"
104
137
 
metadata CHANGED
@@ -1,77 +1,91 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: polymorpheus
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.1
4
+ version: 2.2.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: 2015-08-21 00:00:00.000000000 Z
11
+ date: 2015-09-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: foreigner
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activerecord
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '3.2'
34
- - - <
34
+ - - "<"
35
35
  - !ruby/object:Gem::Version
36
36
  version: '4.2'
37
37
  type: :runtime
38
38
  prerelease: false
39
39
  version_requirements: !ruby/object:Gem::Requirement
40
40
  requirements:
41
- - - '>='
41
+ - - ">="
42
42
  - !ruby/object:Gem::Version
43
43
  version: '3.2'
44
- - - <
44
+ - - "<"
45
45
  - !ruby/object:Gem::Version
46
46
  version: '4.2'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: 10.4.2
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: 10.4.2
47
61
  - !ruby/object:Gem::Dependency
48
62
  name: rspec-rails
49
63
  requirement: !ruby/object:Gem::Requirement
50
64
  requirements:
51
- - - ~>
65
+ - - "~>"
52
66
  - !ruby/object:Gem::Version
53
67
  version: 2.14.0
54
68
  type: :development
55
69
  prerelease: false
56
70
  version_requirements: !ruby/object:Gem::Requirement
57
71
  requirements:
58
- - - ~>
72
+ - - "~>"
59
73
  - !ruby/object:Gem::Version
60
74
  version: 2.14.0
61
75
  - !ruby/object:Gem::Dependency
62
76
  name: mysql2
63
77
  requirement: !ruby/object:Gem::Requirement
64
78
  requirements:
65
- - - ~>
79
+ - - "~>"
66
80
  - !ruby/object:Gem::Version
67
- version: '0.3'
81
+ version: 0.3.10
68
82
  type: :development
69
83
  prerelease: false
70
84
  version_requirements: !ruby/object:Gem::Requirement
71
85
  requirements:
72
- - - ~>
86
+ - - "~>"
73
87
  - !ruby/object:Gem::Version
74
- version: '0.3'
88
+ version: 0.3.10
75
89
  description: Provides a database-friendly method for polymorphic relationships
76
90
  email: bsingh@wegowise.com
77
91
  executables: []
@@ -120,12 +134,12 @@ require_paths:
120
134
  - lib
121
135
  required_ruby_version: !ruby/object:Gem::Requirement
122
136
  requirements:
123
- - - '>='
137
+ - - ">="
124
138
  - !ruby/object:Gem::Version
125
139
  version: '0'
126
140
  required_rubygems_version: !ruby/object:Gem::Requirement
127
141
  requirements:
128
- - - '>='
142
+ - - ">="
129
143
  - !ruby/object:Gem::Version
130
144
  version: 1.3.6
131
145
  requirements: []