mini_record 0.3.4 → 0.3.6
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 +4 -4
- data/Gemfile +1 -0
- data/README.md +3 -0
- data/lib/mini_record/auto_schema.rb +13 -9
- data/lib/mini_record/version.rb +1 -1
- data/test/test_mini_record.rb +4 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ea0ce6ed20bb680e42d5f368b217d35da8cc604f
|
4
|
+
data.tar.gz: 8e8c428ebb7757f08d9cbe6a58eb26e298305668
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3c1802bb520244e113e46d475c87af7424f1e295238a8cc931afc2357a5dc9a46c2903f44885fb2000b1538a3f4904a60c87d09d178faf34de2c959d88bdca74
|
7
|
+
data.tar.gz: cd615e68bd67c671e49ba836a36a619a4cdd70d7c5c2fbbfbe532f62ec7fce382d9a24167fde115b791ceabd4970557548b06cb0ae8b82729188d11d6ffdaca9
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -98,6 +98,9 @@ end
|
|
98
98
|
```
|
99
99
|
The same as in the previous case, but foreign key will be added to the database with help of [foreigner](https://github.com/matthuhiggins/foreigner) gem.
|
100
100
|
|
101
|
+
To remove the key please use :foreign => false
|
102
|
+
If you simple remove the index, the foreign key will not be removed.
|
103
|
+
|
101
104
|
#### belongs_to (polymorphic)
|
102
105
|
```ruby
|
103
106
|
class Address < ActiveRecord::Base
|
@@ -120,16 +120,20 @@ module MiniRecord
|
|
120
120
|
end
|
121
121
|
end
|
122
122
|
|
123
|
-
|
124
|
-
def remove_foreign_keys
|
123
|
+
def foreign_keys
|
125
124
|
# fk cache to minimize quantity of sql queries
|
126
|
-
@foreign_keys
|
125
|
+
@foreign_keys ||= {}
|
126
|
+
@foreign_keys[:table_name] ||= connection.foreign_keys(table_name)
|
127
|
+
end
|
128
|
+
|
129
|
+
# Remove foreign keys for indexes with :foreign=>false option
|
130
|
+
def remove_foreign_keys
|
127
131
|
indexes.each do |name, options|
|
128
|
-
|
129
|
-
foreign_key =
|
132
|
+
if options[:foreign]==false
|
133
|
+
foreign_key = foreign_keys.detect { |fk| fk.options[:column] == options[:column].to_s }
|
130
134
|
if foreign_key
|
131
135
|
connection.remove_foreign_key(table_name, :name => foreign_key.options[:name])
|
132
|
-
|
136
|
+
foreign_keys.delete(foreign_key)
|
133
137
|
end
|
134
138
|
end
|
135
139
|
end
|
@@ -140,10 +144,10 @@ module MiniRecord
|
|
140
144
|
indexes.each do |name, options|
|
141
145
|
if options[:foreign]
|
142
146
|
column = options[:column].to_s
|
143
|
-
unless
|
144
|
-
to_table =
|
147
|
+
unless foreign_keys.detect { |fk| fk[:options][:column] == column }
|
148
|
+
to_table = reflect_on_all_associations.detect { |a| a.foreign_key.to_s==column }.table_name
|
145
149
|
connection.add_foreign_key(table_name, to_table, options)
|
146
|
-
|
150
|
+
foreign_keys << { :options=> { :column=>column } }
|
147
151
|
end
|
148
152
|
end
|
149
153
|
end
|
data/lib/mini_record/version.rb
CHANGED
data/test/test_mini_record.rb
CHANGED
@@ -226,6 +226,7 @@ describe MiniRecord do
|
|
226
226
|
end
|
227
227
|
|
228
228
|
describe 'relation #belongs_to' do
|
229
|
+
|
229
230
|
it 'creates a column and index based on relation' do
|
230
231
|
Article.create(:title => 'Hello', :publisher_id => 1)
|
231
232
|
Article.first.tap do |a|
|
@@ -356,6 +357,7 @@ describe MiniRecord do
|
|
356
357
|
Object.send(:remove_const, :Book)
|
357
358
|
class Book < ActiveRecord::Base
|
358
359
|
belongs_to :publisher
|
360
|
+
index :publisher_id, :foreign => false
|
359
361
|
end
|
360
362
|
Book.auto_upgrade!
|
361
363
|
|
@@ -379,7 +381,7 @@ describe MiniRecord do
|
|
379
381
|
Object.send(:remove_const, :Book)
|
380
382
|
class Book < ActiveRecord::Base
|
381
383
|
belongs_to :publisher
|
382
|
-
index :publisher_id, :name => 'my_super_publisher_id_fk'
|
384
|
+
index :publisher_id, :name => 'my_super_publisher_id_fk', :foreign => false
|
383
385
|
end
|
384
386
|
Book.auto_upgrade!
|
385
387
|
|
@@ -404,6 +406,7 @@ describe MiniRecord do
|
|
404
406
|
Object.send(:remove_const, :Book)
|
405
407
|
class Book < ActiveRecord::Base
|
406
408
|
belongs_to :second_publisher, :foreign_key => 'second_publisher_id', :class_name => 'Publisher'
|
409
|
+
index :second_publisher_id, :foreign => false
|
407
410
|
end
|
408
411
|
Book.auto_upgrade!
|
409
412
|
|
@@ -575,5 +578,4 @@ describe MiniRecord do
|
|
575
578
|
assert_equal 2, Foo.db_fields[:currency].scale
|
576
579
|
assert_equal 4, Foo.db_fields[:currency].limit
|
577
580
|
end
|
578
|
-
|
579
581
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mini_record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Davide D'Agostino
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-07-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|