mini_record 0.3.0.a → 0.3.0.b

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -165,11 +165,6 @@ class Fox < ActiveRecord::Base
165
165
  end
166
166
  ```
167
167
 
168
- ## Warnings
169
-
170
- This software is not super well tested in a production project.
171
- Im stated to using it in two customer's projects without any problem.
172
-
173
168
  ## Author
174
169
 
175
170
  DAddYE, you can follow me on twitter [@daddye](http://twitter.com/daddye) or take a look at my site [daddye.it](http://www.daddye.it)
@@ -188,4 +183,4 @@ The above copyright notice and this permission notice shall be included in all c
188
183
  THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
189
184
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM,
190
185
  DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
191
- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
186
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -118,29 +118,32 @@ module MiniRecord
118
118
  # Generate fields from associations
119
119
  if reflect_on_all_associations.any?
120
120
  reflect_on_all_associations.each do |association|
121
- id_key = if association.options[:foreign_key]
122
- association.options[:foreign_key]
123
- else
124
- "#{association.name.to_s}_id".to_sym
125
- end
126
- type_key = "#{association.name.to_s}_type".to_sym
121
+ foreign_key = association.options[:foreign_key] || "#{association.name.to_s}_id"
122
+ type_key = "#{association.name.to_s}_type"
127
123
  case association.macro
128
124
  when :belongs_to
129
- table_definition.send(:integer, id_key)
125
+ table_definition.column(foreign_key, :integer)
130
126
  if association.options[:polymorphic]
131
- table_definition.send(:string, type_key)
132
- add_index [id_key, type_key]
127
+ table_definition.column(type_key, :string)
128
+ add_index [foreign_key, type_key]
133
129
  else
134
- add_index id_key
130
+ add_index foreign_key
135
131
  end
136
132
  when :has_and_belongs_to_many
137
- table = [table_name, association.name.to_s].sort.join("_")
133
+ table = if name = association.options[:join_table]
134
+ name.to_s
135
+ else
136
+ [table_name, association.name.to_s].sort.join("_")
137
+ end
138
138
  index = ""
139
- unless connection.tables.include?(table)
140
- connection.create_table(table)
141
- connection.add_column table, "#{table.singularize}_id", :integer
142
- connection.add_column table, "#{association.name.to_s.singularize}_id", :integer
143
- connection.add_index table.to_sym, ["#{table.singularize}_id", "#{association.name.to_s.singularize}_id"].sort.map(&:to_sym), association.options
139
+ unless connection.tables.include?(table.to_s)
140
+ foreign_key = association.options[:foreign_key] || "#{table.singularize}_id"
141
+ association_foreign_key = association.options[:association_foreign_key] || "#{association.name.to_s.singularize}_id"
142
+ connection.create_table(table, :id => false) do |t|
143
+ t.integer foreign_key
144
+ t.integer association_foreign_key
145
+ end
146
+ connection.add_index table.to_sym, [foreign_key, association_foreign_key].map(&:to_sym), association.options
144
147
  end
145
148
  # Add join table to our schema tables
146
149
  schema_tables << table unless schema_tables.include?(table)
@@ -1,3 +1,3 @@
1
1
  module MiniRecord
2
- VERSION = "0.3.0.a"
2
+ VERSION = "0.3.0.b"
3
3
  end
@@ -237,7 +237,7 @@ describe MiniRecord do
237
237
  it 'creates a join table with indexes for has_and_belongs_to_many relations' do
238
238
  tables = Tool.connection.tables
239
239
  tables.must_include('purposes_tools')
240
- index = 'index_purposes_tools_on_purpose_id_and_purposes_tool_id'
240
+ index = 'index_purposes_tools_on_purposes_tool_id_and_purpose_id'
241
241
  Tool.connection.indexes('purposes_tools').map(&:name).must_include index
242
242
  # Ensure that join table is not deleted on subsequent upgrade
243
243
  Tool.auto_upgrade!
@@ -252,6 +252,19 @@ describe MiniRecord do
252
252
  Tool.connection.tables.wont_include('purposes_tools')
253
253
  end
254
254
 
255
+ it 'has_and_belongs_to_many with custom join_table and foreign keys' do
256
+ class Foo < ActiveRecord::Base
257
+ has_and_belongs_to_many :watchers, :join_table => :watchers, :foreign_key => :custom_foo_id, :association_foreign_key => :customer_id
258
+ end
259
+ Foo.auto_upgrade!
260
+ conn = ActiveRecord::Base.connection
261
+ conn.tables.must_include 'watchers'
262
+ cols = conn.columns('watchers').map(&:name)
263
+ cols.wont_include 'id'
264
+ cols.must_include 'custom_foo_id'
265
+ cols.must_include 'customer_id'
266
+ end
267
+
255
268
  it 'should support #belongs_to with :class_name' do
256
269
  Task.schema_columns.must_include 'author_id'
257
270
  Task.db_columns.must_include 'author_id'
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
- hash: 50
4
+ hash: 51
5
5
  prerelease: 6
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
9
  - 0
10
- - a
11
- version: 0.3.0.a
10
+ - b
11
+ version: 0.3.0.b
12
12
  platform: ruby
13
13
  authors:
14
14
  - Davide D'Agostino
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2012-01-21 00:00:00 Z
19
+ date: 2012-01-22 00:00:00 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  name: activerecord