mini_record 0.3.4 → 0.3.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6e4dc06c60db297bde1fa28fc64dc5e05c33a7d8
4
- data.tar.gz: cd4c70eb0948b45cc0320e9909f5db4285815a74
3
+ metadata.gz: ea0ce6ed20bb680e42d5f368b217d35da8cc604f
4
+ data.tar.gz: 8e8c428ebb7757f08d9cbe6a58eb26e298305668
5
5
  SHA512:
6
- metadata.gz: 0b892d72b036a1d9be13cb12a49da31bc6242e15039260f9a449c8bb11ba961a9b4e88dfadff1fd294a8c741b3502b82aa591f10ed158d50ad719516c30b61cb
7
- data.tar.gz: 0f7d088dc27778e79096f6a3ccc8ae6a43d9c97ae09bc34463df241da8a8e7a648fb7d50d7a0ba6963015895ace7ec790a1e183ea71acc4ec93af9c71ba6d199
6
+ metadata.gz: 3c1802bb520244e113e46d475c87af7424f1e295238a8cc931afc2357a5dc9a46c2903f44885fb2000b1538a3f4904a60c87d09d178faf34de2c959d88bdca74
7
+ data.tar.gz: cd615e68bd67c671e49ba836a36a619a4cdd70d7c5c2fbbfbe532f62ec7fce382d9a24167fde115b791ceabd4970557548b06cb0ae8b82729188d11d6ffdaca9
data/Gemfile CHANGED
@@ -7,6 +7,7 @@ gem 'sqlite3'
7
7
  gem 'mysql2'
8
8
  gem 'mysql'
9
9
  gem 'pg'
10
+ gem 'activerecord', '<= 3.2'
10
11
 
11
12
  group :test do
12
13
  gem 'foreigner', '>= 1.4.2'
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
- # Remove foreign keys for indexes without :foreign=>true option
124
- def remove_foreign_keys
123
+ def foreign_keys
125
124
  # fk cache to minimize quantity of sql queries
126
- @foreign_keys = connection.foreign_keys(table_name)
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
- unless options[:foreign]
129
- foreign_key = @foreign_keys.detect { |fk| fk.options[:column] == options[:column].to_s }
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
- @foreign_keys.delete(foreign_key)
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 @foreign_keys.detect { |fk| fk[:options][:column] == column }
144
- to_table = (reflect_on_all_associations.detect { |a| a.foreign_key==column }).table_name
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
- @foreign_keys << { :options=> { :column=>column } }
150
+ foreign_keys << { :options=> { :column=>column } }
147
151
  end
148
152
  end
149
153
  end
@@ -1,3 +1,3 @@
1
1
  module MiniRecord
2
- VERSION = "0.3.4"
2
+ VERSION = "0.3.6"
3
3
  end
@@ -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
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-06-23 00:00:00.000000000 Z
11
+ date: 2013-07-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord