dr_nic_magic_models 0.9.1 → 0.9.2
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +9 -0
- data/lib/dr_nic_magic_models/magic_model.rb +19 -13
- data/lib/dr_nic_magic_models/validations.rb +3 -0
- data/lib/dr_nic_magic_models/version.rb +1 -1
- data/website/index.html +2 -2
- data/website/index.txt +1 -1
- data/website/version-raw.js +1 -1
- data/website/version.js +1 -1
- metadata +2 -2
data/History.txt
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
*** 0.9.2 / 2007-4-30
|
2
|
+
|
3
|
+
+ 1 major buxfix:
|
4
|
+
+ #generate_validations now works if you haven't already created a connection to the database; previously
|
5
|
+
validations wouldn't get created until you had already established the connection; now it does it for
|
6
|
+
you if its not already established
|
7
|
+
+ Associations can be generated via the assignment methods, e.g. @membership.group= will generate the "belongs_to :group" association now. This allows the website tutorial to work correctly! Yay. That is, you can now do: Membership.create(:person => person, :group => group)
|
8
|
+
+ has_many's should work now
|
9
|
+
|
1
10
|
*** 0.9.1 / 2007-4-11
|
2
11
|
|
3
12
|
+ 1 minor enhancement:
|
@@ -47,9 +47,10 @@ module DrNicMagicModels::MagicModel
|
|
47
47
|
end
|
48
48
|
|
49
49
|
def find_belongs_to(method, *args, &block)
|
50
|
+
method_clean = clean_method method
|
50
51
|
fkc =
|
51
52
|
begin
|
52
|
-
self.class.connection.foreign_key_constraints(self.class.table_name,
|
53
|
+
self.class.connection.foreign_key_constraints(self.class.table_name, method_clean)
|
53
54
|
rescue NotImplementedError
|
54
55
|
nil
|
55
56
|
end
|
@@ -59,19 +60,20 @@ module DrNicMagicModels::MagicModel
|
|
59
60
|
:foreign_key => fkc.first.foreign_key,
|
60
61
|
:class_name => self.class.class_name(fkc.first.reference_table)}
|
61
62
|
else
|
62
|
-
foreign_key = self.class.columns.select {|column| column.name ==
|
63
|
+
foreign_key = self.class.columns.select {|column| column.name == method_clean.to_s.foreign_key}.first
|
63
64
|
end
|
64
65
|
options ||= {}
|
65
|
-
return add_belongs_to(method, options, *args, &block) if foreign_key
|
66
|
+
return add_belongs_to(method, method_clean, options, *args, &block) if foreign_key
|
66
67
|
end
|
67
68
|
|
68
|
-
def add_belongs_to(method, options, *args, &block)
|
69
|
-
self.class.send 'belongs_to',
|
69
|
+
def add_belongs_to(method, method_clean, options, *args, &block)
|
70
|
+
self.class.send 'belongs_to', method_clean.to_sym, options rescue puts $!
|
70
71
|
self.send(method, *args, &block)
|
71
72
|
end
|
72
73
|
|
73
74
|
def find_has_some(method, *args, &block)
|
74
|
-
|
75
|
+
method_clean = clean_method method
|
76
|
+
fkc = [method_clean.to_s.pluralize, method_clean.to_s.singularize].inject({}) do |pair, table_name|
|
75
77
|
fkc = begin
|
76
78
|
self.class.connection.foreign_key_constraints(table_name)
|
77
79
|
rescue NotImplementedError
|
@@ -91,17 +93,16 @@ module DrNicMagicModels::MagicModel
|
|
91
93
|
end
|
92
94
|
end
|
93
95
|
unless foreign_key
|
94
|
-
klass = Module.const_get
|
96
|
+
klass = Module.const_get method_clean.to_s.downcase.singularize.camelize rescue nil
|
95
97
|
foreign_key = klass.columns.select {|column| column.name == self.class.name.foreign_key}.first if klass
|
96
98
|
end
|
97
99
|
options ||= {}
|
98
|
-
return add_has_some(method, options, *args, &block) if foreign_key
|
100
|
+
return add_has_some(method, method_clean, options, *args, &block) if foreign_key
|
99
101
|
end
|
100
102
|
|
101
|
-
def add_has_some(method, options, *args, &block)
|
102
|
-
|
103
|
-
association
|
104
|
-
self.class.send association, method, options rescue puts $!
|
103
|
+
def add_has_some(method, method_clean, options, *args, &block)
|
104
|
+
association = method_clean.singularize == method_clean ? 'has_one' : 'has_many'
|
105
|
+
self.class.send association, method_clean.to_sym, options rescue puts $!
|
105
106
|
self.send(method, *args, &block)
|
106
107
|
end
|
107
108
|
|
@@ -119,9 +120,14 @@ module DrNicMagicModels::MagicModel
|
|
119
120
|
end
|
120
121
|
|
121
122
|
def add_has_some_through(join_table, method, *args, &block)
|
122
|
-
puts "#{self.class}.has_many #{method.inspect}, #{join_table.inspect}, #{args.inspect}"
|
123
123
|
self.class.send 'has_many', method, :through => join_table.to_sym
|
124
124
|
self.send(method, *args, &block)
|
125
125
|
end
|
126
|
+
|
127
|
+
private
|
128
|
+
|
129
|
+
def clean_method(method)
|
130
|
+
method.to_s.gsub(/=$/,'') # remove any = from the end of the method name
|
131
|
+
end
|
126
132
|
end
|
127
133
|
end
|
@@ -5,6 +5,9 @@ module DrNicMagicModels
|
|
5
5
|
|
6
6
|
logger = DrNicMagicModels::Logger
|
7
7
|
|
8
|
+
# Ensure that the connection to db is established, else validations don't get created.
|
9
|
+
ActiveRecord::Base.connection
|
10
|
+
|
8
11
|
# Code reworked from http://www.redhillconsulting.com.au/rails_plugins.html
|
9
12
|
# Thanks Red Hill Consulting for using an MIT licence :o)
|
10
13
|
|
data/website/index.html
CHANGED
@@ -275,7 +275,7 @@ demonstrated above) to have the brilliantly simple support that Rails/ActiveReco
|
|
275
275
|
</pre></p>
|
276
276
|
|
277
277
|
|
278
|
-
<p>
|
278
|
+
<p>That final association trick is a ripper. Automatic generation of <code>has_many :through</code> associations…</p>
|
279
279
|
|
280
280
|
|
281
281
|
<p><pre class="syntax">
|
@@ -390,7 +390,7 @@ other stories and things.</p>
|
|
390
390
|
<p>Comments are welcome. Send an email to <a href="mailto:drnicwilliams@gmail.com">Dr Nic Williams</a>
|
391
391
|
or via his blog at <a href="http://www.drnicwilliams.com">http://www.drnicwilliams.com</a></p>
|
392
392
|
<p class="coda">
|
393
|
-
<a href="mailto:drnicwilliams@gmail.com">Dr Nic</a>,
|
393
|
+
<a href="mailto:drnicwilliams@gmail.com">Dr Nic</a>, 30th April 2007<br>
|
394
394
|
Theme extended from <a href="http://rb2js.rubyforge.org/">Paul Battley</a>
|
395
395
|
</p>
|
396
396
|
</div>
|
data/website/index.txt
CHANGED
@@ -200,7 +200,7 @@ group.memberships
|
|
200
200
|
</pre>
|
201
201
|
|
202
202
|
|
203
|
-
|
203
|
+
That final association trick is a ripper. Automatic generation of <code>has_many :through</code> associations...
|
204
204
|
|
205
205
|
<pre syntax="ruby">
|
206
206
|
>> person.groups
|
data/website/version-raw.js
CHANGED
data/website/version.js
CHANGED
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.2
|
|
3
3
|
specification_version: 1
|
4
4
|
name: dr_nic_magic_models
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.9.
|
7
|
-
date: 2007-04-
|
6
|
+
version: 0.9.2
|
7
|
+
date: 2007-04-30 00:00:00 +02:00
|
8
8
|
summary: Dr Nic's Magic Models - Invisible validations, assocations and Active Record models themselves!
|
9
9
|
require_paths:
|
10
10
|
- lib
|