schema_associations 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +41 -4
- data/lib/schema_associations/active_record/associations.rb +20 -2
- data/lib/schema_associations/version.rb +1 -1
- data/runspecs +30 -8
- data/spec/association_spec.rb +16 -1
- data/spec/connection.rb +1 -0
- metadata +27 -28
data/README.rdoc
CHANGED
@@ -44,15 +44,36 @@ Then all you need for your models is:
|
|
44
44
|
|
45
45
|
and SchemaAssociations defines the appropriate associations under the hood
|
46
46
|
|
47
|
-
|
47
|
+
=== What if I want something special?
|
48
48
|
|
49
49
|
You're always free to define associations yourself, if for example you want
|
50
50
|
to pass special options. SchemaAssociations won't clobber any existing
|
51
51
|
definitions.
|
52
52
|
|
53
53
|
You can also control the behavior with various options, globally via
|
54
|
-
SchemaAssociations::setup or per-model via SchemaAssociations::ActiveRecord#schema_associations
|
55
|
-
|
54
|
+
SchemaAssociations::setup or per-model via SchemaAssociations::ActiveRecord#schema_associations, such as:
|
55
|
+
|
56
|
+
class Post < ActiveRecord::Base
|
57
|
+
schema_associations :concise_names => false
|
58
|
+
end
|
59
|
+
|
60
|
+
See SchemaAssociations::Config for the available options.
|
61
|
+
|
62
|
+
=== This seems cool, but I'm worried about too much automagic
|
63
|
+
|
64
|
+
You can globally turn off automatic creation in <tt>config/initializers/schema_associations.rb</tt>:
|
65
|
+
|
66
|
+
SchemaAssociations.setup do |config|
|
67
|
+
config.auto_create = false
|
68
|
+
end
|
69
|
+
|
70
|
+
Then in any model where you want automatic associations, just do
|
71
|
+
|
72
|
+
class Post < ActiveRecord::Base
|
73
|
+
schema_associations
|
74
|
+
end
|
75
|
+
|
76
|
+
You can also pass options as per above.
|
56
77
|
|
57
78
|
== Full Details
|
58
79
|
|
@@ -211,6 +232,22 @@ i.e. these associations would be defined:
|
|
211
232
|
|
212
233
|
If you like the formality of using full names for the asociations, you can turn off concise names globally or per-model, see SchemaAssociations::Config
|
213
234
|
|
235
|
+
=== Ordering +has_many+ using +position+
|
236
|
+
|
237
|
+
If the target of a +has_many+ association has a column named +position+, SchemaAssociation will specify <tt>:order => :position</tt> for the association. That is,
|
238
|
+
|
239
|
+
create_table :comments do |t|
|
240
|
+
t.integer post_id
|
241
|
+
t.integer position
|
242
|
+
end
|
243
|
+
|
244
|
+
leads to
|
245
|
+
|
246
|
+
class Post < ActiveRecord::Base
|
247
|
+
has_many :comments, :order => :position
|
248
|
+
end
|
249
|
+
|
250
|
+
|
214
251
|
== How do I know what it did?
|
215
252
|
|
216
253
|
If you're curious (or dubious) about what associations SchemaAssociations defines, you can check the log file. For every assocation that SchemaAssociations defines, it generates an info entry such as
|
@@ -250,7 +287,7 @@ full combo of tests, after you've forked & cloned:
|
|
250
287
|
$ ./runspecs --install # do this once to install gem dependencies for all versions (slow)
|
251
288
|
$ ./runspecs # as many times as you like
|
252
289
|
|
253
|
-
You can also pick a specific version of rails and ruby to use, such as:
|
290
|
+
See <tt>./runspecs --help</tt> for other options. You can also pick a specific version of rails and ruby to use, such as:
|
254
291
|
$ rvm use 1.9.2
|
255
292
|
$ export SCHEMA_ASSOCIATIONS_RAILS_VERSION=3.1
|
256
293
|
$ bundle update rails --local
|
@@ -42,8 +42,26 @@ module SchemaAssociations
|
|
42
42
|
# class MyModel < ActiveRecord::Base
|
43
43
|
# schema_associations :auto_create => false
|
44
44
|
# end
|
45
|
-
|
46
|
-
|
45
|
+
#
|
46
|
+
# If <tt>:auto_create</tt> is not specified, it is implicitly
|
47
|
+
# specified as true. This allows the "non-invasive" style of using
|
48
|
+
# SchemaAssociations in which you set the global Config to
|
49
|
+
# <tt>auto_create = false</tt>, then in any model that you want auto
|
50
|
+
# associations you simply do:
|
51
|
+
#
|
52
|
+
# class MyModel < ActiveRecord::Base
|
53
|
+
# schema_associations
|
54
|
+
# end
|
55
|
+
#
|
56
|
+
# Of course other options can be passed, such as
|
57
|
+
#
|
58
|
+
# class MyModel < ActiveRecord::Base
|
59
|
+
# schema_associations :concise_names => false, :except_type => :has_and_belongs_to_many
|
60
|
+
# end
|
61
|
+
#
|
62
|
+
#
|
63
|
+
def schema_associations(opts={})
|
64
|
+
@schema_associations_config = SchemaAssociations.config.merge({:auto_create => true}.merge(opts))
|
47
65
|
end
|
48
66
|
|
49
67
|
def schema_associations_config # :nodoc:
|
data/runspecs
CHANGED
@@ -1,36 +1,58 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
3
|
require 'optparse'
|
4
|
+
require 'ostruct'
|
5
|
+
|
6
|
+
PROJECT = File.basename(File.expand_path('..', __FILE__))
|
4
7
|
|
5
8
|
RUBY_VERSIONS = %W[1.8.7 1.9.2]
|
6
9
|
RAILS_VERSIONS = %W[2.3 3.0 3.1]
|
7
10
|
|
8
|
-
|
11
|
+
o = OpenStruct.new
|
12
|
+
o.ruby_versions = RUBY_VERSIONS
|
13
|
+
o.rails_versions = RAILS_VERSIONS
|
14
|
+
o.run_cmd = "rake spec"
|
15
|
+
|
9
16
|
OptionParser.new do |opts|
|
10
17
|
opts.banner = "Usage: #{$0} [options]"
|
11
18
|
|
12
19
|
opts.on("--install", "Install gem dependencies") do |v|
|
13
|
-
|
20
|
+
o.install = v
|
21
|
+
end
|
22
|
+
|
23
|
+
opts.on("--ruby version", String, "Choose which version(s) of ruby to run. Default is: #{o.ruby_versions.join(' ')}") do |ruby|
|
24
|
+
o.ruby_versions = ruby.split(' ')
|
25
|
+
end
|
26
|
+
|
27
|
+
opts.on("--rails version", String, "Choose which version(s) of rails to run. Default is: #{o.rails_versions.join(' ')}") do |rails|
|
28
|
+
o.rails_versions = rails.split(' ')
|
29
|
+
end
|
30
|
+
|
31
|
+
opts.on("--quick", "quick run ruby #{RUBY_VERSIONS.last} and rails #{RAILS_VERSIONS.last}") do
|
32
|
+
o.ruby_versions = [RUBY_VERSIONS.last]
|
33
|
+
o.rails_versions = [RAILS_VERSIONS.last]
|
14
34
|
end
|
35
|
+
|
15
36
|
end.parse!
|
16
37
|
|
17
|
-
cmds = if
|
38
|
+
cmds = if o.install
|
18
39
|
['bundle update']
|
19
40
|
else
|
20
|
-
['bundle update --local rails | grep rails',
|
41
|
+
['bundle update --local rails | grep rails', o.run_cmd]
|
21
42
|
end
|
22
43
|
|
23
44
|
n = 1
|
24
|
-
total =
|
25
|
-
|
26
|
-
|
45
|
+
total = o.ruby_versions.size * o.rails_versions.size
|
46
|
+
o.ruby_versions.each do |ruby|
|
47
|
+
o.rails_versions.each do |rails|
|
27
48
|
puts "\n\n*** ruby version #{ruby} - rails version #{rails} [#{n} of #{total}]\n\n"
|
28
49
|
n += 1
|
29
50
|
allcmds = []
|
30
51
|
allcmds << "rvm use #{ruby}"
|
31
|
-
allcmds << "export
|
52
|
+
allcmds << "export #{PROJECT.upcase}_RAILS_VERSION=#{rails}"
|
32
53
|
allcmds += cmds
|
33
54
|
allcmds << 'exit'
|
34
55
|
system %Q{echo '#{allcmds.join(' \n ')}' | bash -i} or abort "aborting #{$0}"
|
35
56
|
end
|
36
57
|
end
|
58
|
+
|
data/spec/association_spec.rb
CHANGED
@@ -178,7 +178,7 @@ describe ActiveRecord::Base do
|
|
178
178
|
|
179
179
|
end
|
180
180
|
|
181
|
-
it "should override auto_create positively" do
|
181
|
+
it "should override auto_create positively explicitly" do
|
182
182
|
with_associations_auto_create(false) do
|
183
183
|
create_tables(
|
184
184
|
"posts", {}, {},
|
@@ -193,6 +193,21 @@ describe ActiveRecord::Base do
|
|
193
193
|
end
|
194
194
|
end
|
195
195
|
|
196
|
+
it "should override auto_create positively implicitly" do
|
197
|
+
with_associations_auto_create(false) do
|
198
|
+
create_tables(
|
199
|
+
"posts", {}, {},
|
200
|
+
"comments", {}, { :post_id => {} }
|
201
|
+
)
|
202
|
+
class Post < ActiveRecord::Base
|
203
|
+
schema_associations
|
204
|
+
end
|
205
|
+
class Comment < ActiveRecord::Base ; end
|
206
|
+
Post.reflect_on_association(:comments).should_not be_nil
|
207
|
+
Comment.reflect_on_association(:post).should be_nil
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
196
211
|
|
197
212
|
context "with unique index" do
|
198
213
|
before(:each) do
|
data/spec/connection.rb
CHANGED
metadata
CHANGED
@@ -1,12 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: schema_associations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
prerelease:
|
5
|
-
|
6
|
-
- 0
|
7
|
-
- 1
|
8
|
-
- 1
|
9
|
-
version: 0.1.1
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.2
|
10
6
|
platform: ruby
|
11
7
|
authors:
|
12
8
|
- Ronen Barzel
|
@@ -15,18 +11,17 @@ autorequire:
|
|
15
11
|
bindir: bin
|
16
12
|
cert_chain: []
|
17
13
|
|
18
|
-
date: 2011-
|
14
|
+
date: 2011-08-02 00:00:00 -07:00
|
19
15
|
default_executable:
|
20
16
|
dependencies:
|
21
17
|
- !ruby/object:Gem::Dependency
|
22
18
|
name: schema_plus
|
23
19
|
prerelease: false
|
24
20
|
requirement: &id001 !ruby/object:Gem::Requirement
|
21
|
+
none: false
|
25
22
|
requirements:
|
26
23
|
- - ">="
|
27
24
|
- !ruby/object:Gem::Version
|
28
|
-
segments:
|
29
|
-
- 0
|
30
25
|
version: "0"
|
31
26
|
type: :runtime
|
32
27
|
version_requirements: *id001
|
@@ -34,13 +29,10 @@ dependencies:
|
|
34
29
|
name: rake
|
35
30
|
prerelease: false
|
36
31
|
requirement: &id002 !ruby/object:Gem::Requirement
|
32
|
+
none: false
|
37
33
|
requirements:
|
38
34
|
- - ~>
|
39
35
|
- !ruby/object:Gem::Version
|
40
|
-
segments:
|
41
|
-
- 0
|
42
|
-
- 8
|
43
|
-
- 7
|
44
36
|
version: 0.8.7
|
45
37
|
type: :development
|
46
38
|
version_requirements: *id002
|
@@ -48,11 +40,10 @@ dependencies:
|
|
48
40
|
name: rspec
|
49
41
|
prerelease: false
|
50
42
|
requirement: &id003 !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
51
44
|
requirements:
|
52
45
|
- - ">="
|
53
46
|
- !ruby/object:Gem::Version
|
54
|
-
segments:
|
55
|
-
- 0
|
56
47
|
version: "0"
|
57
48
|
type: :development
|
58
49
|
version_requirements: *id003
|
@@ -60,11 +51,10 @@ dependencies:
|
|
60
51
|
name: sqlite3
|
61
52
|
prerelease: false
|
62
53
|
requirement: &id004 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
63
55
|
requirements:
|
64
56
|
- - ">="
|
65
57
|
- !ruby/object:Gem::Version
|
66
|
-
segments:
|
67
|
-
- 0
|
68
58
|
version: "0"
|
69
59
|
type: :development
|
70
60
|
version_requirements: *id004
|
@@ -72,11 +62,10 @@ dependencies:
|
|
72
62
|
name: simplecov
|
73
63
|
prerelease: false
|
74
64
|
requirement: &id005 !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
75
66
|
requirements:
|
76
67
|
- - ">="
|
77
68
|
- !ruby/object:Gem::Version
|
78
|
-
segments:
|
79
|
-
- 0
|
80
69
|
version: "0"
|
81
70
|
type: :development
|
82
71
|
version_requirements: *id005
|
@@ -84,14 +73,24 @@ dependencies:
|
|
84
73
|
name: simplecov-gem-adapter
|
85
74
|
prerelease: false
|
86
75
|
requirement: &id006 !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
87
77
|
requirements:
|
88
78
|
- - ">="
|
89
79
|
- !ruby/object:Gem::Version
|
90
|
-
segments:
|
91
|
-
- 0
|
92
80
|
version: "0"
|
93
81
|
type: :development
|
94
82
|
version_requirements: *id006
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: ruby-debug19
|
85
|
+
prerelease: false
|
86
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: "0"
|
92
|
+
type: :development
|
93
|
+
version_requirements: *id007
|
95
94
|
description: SchemaAssociations extends ActiveRecord to automatically create associations by inspecting the database schema. This is more more DRY than the standard behavior, for which in addition to specifying the foreign key in the migration, you must also specify complementary associations in two model files (e.g. a :belongs_to and a :has_many).
|
96
95
|
email:
|
97
96
|
- ronen@barzel.org
|
@@ -128,25 +127,25 @@ rdoc_options: []
|
|
128
127
|
require_paths:
|
129
128
|
- lib
|
130
129
|
required_ruby_version: !ruby/object:Gem::Requirement
|
130
|
+
none: false
|
131
131
|
requirements:
|
132
132
|
- - ">="
|
133
133
|
- !ruby/object:Gem::Version
|
134
|
-
segments:
|
135
|
-
- 0
|
136
134
|
version: "0"
|
137
135
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
136
|
+
none: false
|
138
137
|
requirements:
|
139
138
|
- - ">="
|
140
139
|
- !ruby/object:Gem::Version
|
141
|
-
segments:
|
142
|
-
- 0
|
143
140
|
version: "0"
|
144
141
|
requirements: []
|
145
142
|
|
146
143
|
rubyforge_project: schema_associations
|
147
|
-
rubygems_version: 1.
|
144
|
+
rubygems_version: 1.6.2
|
148
145
|
signing_key:
|
149
146
|
specification_version: 3
|
150
147
|
summary: ActiveRecord extension that automatically (DRY) creates associations based on the schema
|
151
|
-
test_files:
|
152
|
-
|
148
|
+
test_files:
|
149
|
+
- spec/association_spec.rb
|
150
|
+
- spec/connection.rb
|
151
|
+
- spec/spec_helper.rb
|