thumblemonks-load_model 0.1.0 → 0.2.0
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.
- data/README.markdown +29 -17
- data/lib/load_model.rb +58 -12
- data/load_model.gemspec +2 -2
- data/test/functional/keys_controller_test.rb +12 -6
- data/test/functional/require_model_controller_test.rb +12 -7
- data/test/functional/restrict_options_controller_test.rb +9 -6
- data/test/functional/through_controller_test.rb +23 -14
- data/test/test_helper.rb +9 -14
- metadata +2 -2
- data/rails/app/controllers/application.rb +0 -2
- data/rails/config/boot.rb +0 -109
- data/rails/config/database.yml +0 -3
- data/rails/config/environment.rb +0 -71
- data/rails/config/environments/test.rb +0 -19
- data/rails/config/routes.rb +0 -5
- data/rails/db/schema.rb +0 -21
- data/test/functional/controller_helper.rb +0 -82
- data/test/functional/load_model_test.rb +0 -85
- data/test/functional/string_key_load_model_test.rb +0 -52
data/README.markdown
CHANGED
@@ -2,8 +2,6 @@
|
|
2
2
|
|
3
3
|
A glorified before_filter that loads an instance of an `ActiveRecord` object as the result of searching for said object against a model defined by a given model name. The value of the HTTP request parameter `:id` will be used as the default lookup value. `LoadModel` will give you the ability to require an instance be found and/or override several other default behaviors.
|
4
4
|
|
5
|
-
Example
|
6
|
-
|
7
5
|
class SillyFellowController < Application
|
8
6
|
load_model :silly_fellow
|
9
7
|
def action
|
@@ -15,20 +13,14 @@ You can require that a model instance be found for all actions or given actions.
|
|
15
13
|
|
16
14
|
To turn require on for all actions, simply pass *true* to a provided `:require` attribute, like so:
|
17
15
|
|
18
|
-
Example
|
19
|
-
|
20
16
|
load_model :silly_fellow, :require => true
|
21
17
|
|
22
18
|
To turn require on for specific actions, pass an array of action names to `:require`. The model will be loaded for all actions, regardless of whether or not required is provided, but the exception will only be raised when an record is not found for the provided actions.
|
23
19
|
|
24
|
-
Example
|
25
|
-
|
26
20
|
load_model :silly_fellow, :require => [:show, :update]
|
27
21
|
|
28
22
|
To use a different parameter key and model than the default, you can provide the values in the `:paramater_key` and `:class` options (though not necessary to provide them together), like the following:
|
29
23
|
|
30
|
-
Example
|
31
|
-
|
32
24
|
load_model :foo, :class => :user, :parameter_key => :bar_id
|
33
25
|
|
34
26
|
In the above example, `load_model` will assume the parameter_key and the model's primary/foreign key are both the same. For instance, the above example would result in a call like the following:
|
@@ -37,8 +29,6 @@ In the above example, `load_model` will assume the parameter_key and the model's
|
|
37
29
|
|
38
30
|
If you want to use a different lookup/foreign key than the default, you can also provide that key name using the `:foreign_key` parameter; like so:
|
39
31
|
|
40
|
-
Example
|
41
|
-
|
42
32
|
load_model :foo, :class => :user, :parameter_key => :bar_id,
|
43
33
|
:foreign_key => :baz_id
|
44
34
|
|
@@ -48,14 +38,12 @@ Which would result in a call similar to the following:
|
|
48
38
|
|
49
39
|
If you want to only use `load_model` for some actions, you can still name them as you would with a `before_filter` using `:only` or `:except`. If you provide an `:only` and an `:except` value. `:only` will always win out over `:except` when there are collisions (i.e. you provide both in the same call)
|
50
40
|
|
51
|
-
Example
|
52
|
-
|
53
41
|
load_model :foo, :only => [:show]
|
54
42
|
load_model :bar, :except => [:create]
|
55
43
|
|
56
|
-
|
44
|
+
### Through
|
57
45
|
|
58
|
-
|
46
|
+
Load Model supports a :through option. With :through, you can load a model via the association of an existing loaded model. This is especially useful for RESTful controllers.
|
59
47
|
|
60
48
|
load_model :user, :require => true, :parameter_key => :user_id
|
61
49
|
load_model :post, :through => :user
|
@@ -66,8 +54,6 @@ In this example, a @post record will be loaded through the @user record with ess
|
|
66
54
|
|
67
55
|
All of the previously mentioned options still apply (:parameter_key, :foreign_key, :require, :only, and :except) except for the :class option. Meaning you could really mess around!
|
68
56
|
|
69
|
-
Example
|
70
|
-
|
71
57
|
load_model :user, :require => true, :parameter_key => :user_id
|
72
58
|
load_model :post, :through => :person, :parameter_key => :foo_id,
|
73
59
|
:foreign_key => :baz_id
|
@@ -78,7 +64,33 @@ Would result in a call similar to the following:
|
|
78
64
|
|
79
65
|
Require works as you would expect.
|
80
66
|
|
81
|
-
|
67
|
+
If you would like load_model to not assume a pluralized association name, you can provide the association name with the :association option. Like so:
|
68
|
+
|
69
|
+
class Person < ActiveRecord::Base
|
70
|
+
has_many :blog_postings
|
71
|
+
end
|
72
|
+
|
73
|
+
class PostController < ActionController::Base
|
74
|
+
load_model :post, :through => :person, :assocation => :blog_postings
|
75
|
+
end
|
76
|
+
|
77
|
+
### From
|
78
|
+
|
79
|
+
Perhaps you don't need to do a subquery on a model's association and you just need to load a model from another's `belongs_to` or `has_one` association. This would be impossible in the above example. Instead, will want to use the :from option. Like so:
|
80
|
+
|
81
|
+
class Post < ActiveRecord::Base
|
82
|
+
belongs_to :user
|
83
|
+
end
|
84
|
+
|
85
|
+
class PostController < ActionController::Base
|
86
|
+
load_model :post
|
87
|
+
load_model :user, :from => :post
|
88
|
+
end
|
89
|
+
|
90
|
+
The example is contrived, but you get the point. Essentially, this would do the same as writing the following code:
|
91
|
+
|
92
|
+
@post = Post.find_by_id(params[:id])
|
93
|
+
@user = @post.user
|
82
94
|
|
83
95
|
## Installation
|
84
96
|
|
data/lib/load_model.rb
CHANGED
@@ -9,7 +9,7 @@ module ThumbleMonks #:nodoc:
|
|
9
9
|
|
10
10
|
module ClassMethods #:nodoc
|
11
11
|
# A glorified before_filter that loads an instance of an ActiveRecord
|
12
|
-
# object as
|
12
|
+
# object as the result of searching for said object against a model
|
13
13
|
# defined by a given model name. The value of the HTTP request parameter
|
14
14
|
# :id will be used as the default lookup value. LoadModel will give you
|
15
15
|
# the ability to require an instance be found and/or override several
|
@@ -78,7 +78,9 @@ module ThumbleMonks #:nodoc:
|
|
78
78
|
# load_model :foo, :only => [:show]
|
79
79
|
# load_model :bar, :except => [:create]
|
80
80
|
#
|
81
|
-
#
|
81
|
+
# == Through
|
82
|
+
#
|
83
|
+
# Load Model supports a :through option. With :through, you can
|
82
84
|
# load a model via the association of an existing loaded model. This is
|
83
85
|
# especially useful for RESTful controllers.
|
84
86
|
#
|
@@ -104,20 +106,48 @@ module ThumbleMonks #:nodoc:
|
|
104
106
|
#
|
105
107
|
# @person.posts.find_by_baz_id(params[:foo_id])
|
106
108
|
#
|
107
|
-
# Require works as you would expect
|
109
|
+
# Require works as you would expect.
|
110
|
+
#
|
111
|
+
# If you would like load_model to not assume a pluralized association
|
112
|
+
# name, you can provide the association name with the :association
|
113
|
+
# option. Like so:
|
114
|
+
#
|
115
|
+
# Example
|
116
|
+
# class Person < ActiveRecord::Base
|
117
|
+
# has_many :blog_postings
|
118
|
+
# end
|
119
|
+
# class PostController < ActionController::Base
|
120
|
+
# load_model :post, :through => :person, :assocation => :blog_postings
|
121
|
+
# end
|
122
|
+
#
|
123
|
+
# == From
|
108
124
|
#
|
109
|
-
#
|
110
|
-
#
|
111
|
-
#
|
112
|
-
#
|
113
|
-
# change.
|
125
|
+
# Perhaps you don't need to do a subquery on a model's association and
|
126
|
+
# you just need to load a model from another's belongs_to or has_one
|
127
|
+
# association. This would be impossible in the above example. Instead,
|
128
|
+
# will want to use the :from option. Like so:
|
114
129
|
#
|
130
|
+
# Example
|
131
|
+
# class Post < ActiveRecord::Base
|
132
|
+
# belongs_to :user
|
133
|
+
# end
|
134
|
+
# class PostController < ActionController::Base
|
135
|
+
# load_model :post
|
136
|
+
# load_model :user, :from => :post
|
137
|
+
# end
|
138
|
+
#
|
139
|
+
# The example is contrived, but you get the point. Essentially, this
|
140
|
+
# would do the same as writing the following code:
|
141
|
+
#
|
142
|
+
# Example
|
143
|
+
# @post = Post.find_by_id(params[:id])
|
144
|
+
# @user = @post.user
|
115
145
|
def load_model(name, opts={})
|
116
146
|
unless loaders
|
117
147
|
self.class_eval { before_filter :load_specified_models }
|
118
148
|
write_inheritable_attribute(:loaders, [])
|
119
149
|
end
|
120
|
-
loaders << (opts
|
150
|
+
loaders << loader_class(opts).new(name, opts)
|
121
151
|
end
|
122
152
|
|
123
153
|
def loaders; self.read_inheritable_attribute(:loaders); end
|
@@ -180,10 +210,27 @@ module ThumbleMonks #:nodoc:
|
|
180
210
|
end
|
181
211
|
end # ThroughModelLoader
|
182
212
|
|
213
|
+
class FromModelLoader < ModelLoader #:nodoc
|
214
|
+
attr_reader :load_from, :association
|
215
|
+
def initialize(name, opts={})
|
216
|
+
super(name, opts)
|
217
|
+
@load_from = "@#{opts[:from]}".to_sym
|
218
|
+
@association = name.to_s
|
219
|
+
end
|
220
|
+
|
221
|
+
def load_model(controller)
|
222
|
+
controller.instance_variable_get(load_from).send(association)
|
223
|
+
end
|
224
|
+
end # FromModelLoader
|
225
|
+
private
|
226
|
+
def loader_class(opts)
|
227
|
+
return ThroughModelLoader if opts[:through]
|
228
|
+
return FromModelLoader if opts[:from]
|
229
|
+
ModelLoader
|
230
|
+
end
|
183
231
|
end # ClassMethods
|
184
232
|
|
185
233
|
private
|
186
|
-
|
187
234
|
def load_specified_models
|
188
235
|
self.class.loaders.each do |loader|
|
189
236
|
if loader.action_allowed?(action_name)
|
@@ -194,8 +241,7 @@ module ThumbleMonks #:nodoc:
|
|
194
241
|
instance_variable_set(loader.assigns_to, obj)
|
195
242
|
end
|
196
243
|
end
|
197
|
-
end
|
198
|
-
|
244
|
+
end # load_specified_models
|
199
245
|
end # LoadModel
|
200
246
|
end # ThumbleMonks
|
201
247
|
|
data/load_model.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "load_model"
|
3
|
-
s.version = "0.
|
4
|
-
s.date = "2009-
|
3
|
+
s.version = "0.2.0"
|
4
|
+
s.date = "2009-03-24"
|
5
5
|
s.summary = "Rails Controller plugin that provides easy and useful macros for tying models and requests together"
|
6
6
|
s.email = %w[gus@gusg.us gabriel.gironda@gmail.com]
|
7
7
|
s.homepage = "http://github.com/thumblemonks/load_model"
|
@@ -1,12 +1,19 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/../test_helper'
|
2
2
|
|
3
|
-
class
|
3
|
+
class KeysController < ActionController::Base
|
4
|
+
# Expects to use fuzzle_id as parameter key against User class with FK of :id
|
5
|
+
load_model :user, :parameter_key => :fuzzle_id
|
4
6
|
|
7
|
+
# Expects to use :fuzzle_id as FK and parameter key against Fuzzle class
|
8
|
+
# (normal operation)
|
9
|
+
load_model :fuzzler, :parameter_key => :fuzzle_id, :foreign_key => :fuzzle_id,
|
10
|
+
:class => :fuzzle
|
11
|
+
|
12
|
+
def index; render :text => 'hello'; end
|
13
|
+
end
|
14
|
+
|
15
|
+
class KeysControllerTest < ActionController::TestCase
|
5
16
|
def setup
|
6
|
-
super
|
7
|
-
@request = ActionController::TestRequest.new
|
8
|
-
@response = ActionController::TestResponse.new
|
9
|
-
@controller = KeysController.new
|
10
17
|
@user = User.create!(:name => 'Foo')
|
11
18
|
@fuzzler = Fuzzle.create!(:name => 'Bar', :fuzzle_id => 300)
|
12
19
|
end
|
@@ -22,5 +29,4 @@ class KeysControllerTest < Test::Unit::TestCase
|
|
22
29
|
assert_equal @fuzzler.id, assigns(:fuzzler).id
|
23
30
|
assert_nil assigns(:user)
|
24
31
|
end
|
25
|
-
|
26
32
|
end
|
@@ -1,13 +1,19 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
-
require File.dirname(__FILE__) + '/controller_helper'
|
3
2
|
|
4
|
-
class
|
3
|
+
class RequireModelController < ActionController::Base
|
4
|
+
load_model :stuser, :class => :alternate, :parameter_key => :alternate_id,
|
5
|
+
:foreign_key => :alternate_id, :require => nil # never required
|
6
|
+
load_model :fuzzle, :parameter_key => :fuzzle_id, :foreign_key => :fuzzle_id,
|
7
|
+
:require => [:newer] # required for newer action
|
8
|
+
load_model :user, :require => true # required for all actions
|
5
9
|
|
10
|
+
def index; render :text => 'whatever'; end
|
11
|
+
def new; render :text => 'whatever 2'; end
|
12
|
+
def newer; render :text => 'whatever 3'; end
|
13
|
+
end
|
14
|
+
|
15
|
+
class RequireModelControllerTest < ActionController::TestCase
|
6
16
|
def setup
|
7
|
-
super
|
8
|
-
@request = ActionController::TestRequest.new
|
9
|
-
@response = ActionController::TestResponse.new
|
10
|
-
@controller = RequireModelController.new
|
11
17
|
@foo = User.create!(:name => 'Foo')
|
12
18
|
end
|
13
19
|
|
@@ -41,5 +47,4 @@ class RequireModelControllerTest < Test::Unit::TestCase
|
|
41
47
|
get :new, :id => (@foo.id + 1)
|
42
48
|
end
|
43
49
|
end
|
44
|
-
|
45
50
|
end
|
@@ -1,12 +1,16 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/../test_helper'
|
2
2
|
|
3
|
-
class
|
3
|
+
class RestrictOptionsController < ActionController::Base
|
4
|
+
load_model :user, :only => [:index]
|
5
|
+
load_model :alternate, :except => [:index], :parameter_key => :alternate_id,
|
6
|
+
:foreign_key => :alternate_id
|
4
7
|
|
8
|
+
def index; render :text => 'ran index'; end
|
9
|
+
def show; render :text => 'ran show'; end
|
10
|
+
end
|
11
|
+
|
12
|
+
class RestrictOptionsControllerTest < ActionController::TestCase
|
5
13
|
def setup
|
6
|
-
super
|
7
|
-
@request = ActionController::TestRequest.new
|
8
|
-
@response = ActionController::TestResponse.new
|
9
|
-
@controller = RestrictOptionsController.new
|
10
14
|
@foo = User.create!(:name => 'Foo')
|
11
15
|
@alt = Alternate.create!(:name => 'Bar', :alternate_id => 100)
|
12
16
|
end
|
@@ -30,5 +34,4 @@ class RestrictOptionsControllerTest < Test::Unit::TestCase
|
|
30
34
|
get :show, :id => @foo.id, :alternate_id => @alt.alternate_id
|
31
35
|
assert_nil assigns(:user)
|
32
36
|
end
|
33
|
-
|
34
37
|
end
|
@@ -1,12 +1,22 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
-
require File.dirname(__FILE__) + '/controller_helper'
|
3
2
|
|
4
|
-
class
|
3
|
+
class ThroughController < ActionController::Base
|
4
|
+
load_model :user, :parameter_key => :user_id
|
5
|
+
load_model :post, :through => :user, :except => [:show]
|
6
|
+
# proving that except and only work
|
7
|
+
load_model :post, :through => :user, :parameter_key => 'weird_id',
|
8
|
+
:require => true, :only => [:show]
|
9
|
+
|
10
|
+
load_model :post, :through => :user, :association => :unpublished_posts,
|
11
|
+
:require => true, :only => [:show_unpublished]
|
12
|
+
|
13
|
+
def index; render :text => 'index'; end
|
14
|
+
def show; render :text => 'show'; end
|
15
|
+
def show_unpublished; render :text => 'unpublished'; end
|
16
|
+
end
|
17
|
+
|
18
|
+
class ThroughControllerTest < ActionController::TestCase
|
5
19
|
def setup
|
6
|
-
super
|
7
|
-
@request = ActionController::TestRequest.new
|
8
|
-
@response = ActionController::TestResponse.new
|
9
|
-
@controller = ThroughController.new
|
10
20
|
@user = User.create!(:name => 'Foo')
|
11
21
|
@post = @user.posts.create!(:name => 'Foo post')
|
12
22
|
end
|
@@ -16,8 +26,8 @@ class ThroughControllerTest < Test::Unit::TestCase
|
|
16
26
|
get :index, :user_id => @user.id, :id => @post.id
|
17
27
|
end
|
18
28
|
|
19
|
-
should_assign_to
|
20
|
-
should_assign_to
|
29
|
+
should_assign_to(:user) { @user }
|
30
|
+
should_assign_to(:post) { @post }
|
21
31
|
end # with valid ids
|
22
32
|
|
23
33
|
context "show_unpublished with valid id" do
|
@@ -26,8 +36,8 @@ class ThroughControllerTest < Test::Unit::TestCase
|
|
26
36
|
get :show_unpublished, :user_id => @user.id, :id => @unpublished_post.id
|
27
37
|
end
|
28
38
|
|
29
|
-
should_assign_to
|
30
|
-
should_assign_to
|
39
|
+
should_assign_to(:user) { @user }
|
40
|
+
should_assign_to(:post) { @unpublished_post }
|
31
41
|
end
|
32
42
|
|
33
43
|
context "index with invalid post id" do
|
@@ -35,7 +45,7 @@ class ThroughControllerTest < Test::Unit::TestCase
|
|
35
45
|
get :index, :user_id => @user.id, :id => -1
|
36
46
|
end
|
37
47
|
|
38
|
-
should_assign_to
|
48
|
+
should_assign_to(:user) { @user }
|
39
49
|
should_not_assign_to :post
|
40
50
|
end # with invalid post id
|
41
51
|
|
@@ -45,8 +55,8 @@ class ThroughControllerTest < Test::Unit::TestCase
|
|
45
55
|
get :show, :user_id => @user.id, :weird_id => @post.id
|
46
56
|
end
|
47
57
|
|
48
|
-
should_assign_to
|
49
|
-
should_assign_to
|
58
|
+
should_assign_to(:user) { @user }
|
59
|
+
should_assign_to(:post) { @post }
|
50
60
|
end # has existing records
|
51
61
|
|
52
62
|
context "has nonexistent records for required action" do
|
@@ -57,5 +67,4 @@ class ThroughControllerTest < Test::Unit::TestCase
|
|
57
67
|
end
|
58
68
|
end # has nonexistant records for required action
|
59
69
|
end # show with alternative post via weird_id
|
60
|
-
|
61
70
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,9 +1,15 @@
|
|
1
|
+
def require_local_lib(pattern)
|
2
|
+
Dir.glob(File.join(File.dirname(__FILE__), pattern)).each {|f| require f }
|
3
|
+
end
|
4
|
+
|
1
5
|
ENV["RAILS_ENV"] = "test"
|
2
|
-
ENV["RAILS_ROOT"] = File.expand_path(File.join(File.dirname(__FILE__), '..', 'rails'))
|
6
|
+
ENV["RAILS_ROOT"] = File.expand_path(File.join(File.dirname(__FILE__), '..', 'test', 'rails'))
|
3
7
|
require File.expand_path(File.join(ENV["RAILS_ROOT"], 'config', 'environment'))
|
4
8
|
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'load_model'))
|
5
9
|
require 'test_help'
|
6
|
-
load(File.
|
10
|
+
load(File.join(ENV["RAILS_ROOT"], "db", "schema.rb"))
|
11
|
+
|
12
|
+
# Models
|
7
13
|
|
8
14
|
class User < ActiveRecord::Base
|
9
15
|
has_many :posts, :conditions => {:published => true}
|
@@ -13,15 +19,4 @@ class Post < ActiveRecord::Base
|
|
13
19
|
belongs_to :user
|
14
20
|
end
|
15
21
|
class Alternate < ActiveRecord::Base; end
|
16
|
-
class Fuzzle < ActiveRecord::Base; end
|
17
|
-
|
18
|
-
class Test::Unit::TestCase
|
19
|
-
def teardown
|
20
|
-
Fuzzle.delete_all
|
21
|
-
Alternate.delete_all
|
22
|
-
Post.delete_all
|
23
|
-
User.delete_all
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
require File.dirname(__FILE__) + '/functional/controller_helper'
|
22
|
+
class Fuzzle < ActiveRecord::Base; end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: thumblemonks-load_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Justin Knowlden
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2009-
|
13
|
+
date: 2009-03-24 00:00:00 -07:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|
data/rails/config/boot.rb
DELETED
@@ -1,109 +0,0 @@
|
|
1
|
-
# Don't change this file!
|
2
|
-
# Configure your app in config/environment.rb and config/environments/*.rb
|
3
|
-
|
4
|
-
RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
|
5
|
-
|
6
|
-
module Rails
|
7
|
-
class << self
|
8
|
-
def boot!
|
9
|
-
unless booted?
|
10
|
-
preinitialize
|
11
|
-
pick_boot.run
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
def booted?
|
16
|
-
defined? Rails::Initializer
|
17
|
-
end
|
18
|
-
|
19
|
-
def pick_boot
|
20
|
-
(vendor_rails? ? VendorBoot : GemBoot).new
|
21
|
-
end
|
22
|
-
|
23
|
-
def vendor_rails?
|
24
|
-
File.exist?("#{RAILS_ROOT}/vendor/rails")
|
25
|
-
end
|
26
|
-
|
27
|
-
def preinitialize
|
28
|
-
load(preinitializer_path) if File.exist?(preinitializer_path)
|
29
|
-
end
|
30
|
-
|
31
|
-
def preinitializer_path
|
32
|
-
"#{RAILS_ROOT}/config/preinitializer.rb"
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
class Boot
|
37
|
-
def run
|
38
|
-
load_initializer
|
39
|
-
Rails::Initializer.run(:set_load_path)
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
class VendorBoot < Boot
|
44
|
-
def load_initializer
|
45
|
-
require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
|
46
|
-
Rails::Initializer.run(:install_gem_spec_stubs)
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
class GemBoot < Boot
|
51
|
-
def load_initializer
|
52
|
-
self.class.load_rubygems
|
53
|
-
load_rails_gem
|
54
|
-
require 'initializer'
|
55
|
-
end
|
56
|
-
|
57
|
-
def load_rails_gem
|
58
|
-
if version = self.class.gem_version
|
59
|
-
gem 'rails', version
|
60
|
-
else
|
61
|
-
gem 'rails'
|
62
|
-
end
|
63
|
-
rescue Gem::LoadError => load_error
|
64
|
-
$stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
|
65
|
-
exit 1
|
66
|
-
end
|
67
|
-
|
68
|
-
class << self
|
69
|
-
def rubygems_version
|
70
|
-
Gem::RubyGemsVersion if defined? Gem::RubyGemsVersion
|
71
|
-
end
|
72
|
-
|
73
|
-
def gem_version
|
74
|
-
if defined? RAILS_GEM_VERSION
|
75
|
-
RAILS_GEM_VERSION
|
76
|
-
elsif ENV.include?('RAILS_GEM_VERSION')
|
77
|
-
ENV['RAILS_GEM_VERSION']
|
78
|
-
else
|
79
|
-
parse_gem_version(read_environment_rb)
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
def load_rubygems
|
84
|
-
require 'rubygems'
|
85
|
-
min_version = '1.1.1'
|
86
|
-
unless rubygems_version >= min_version
|
87
|
-
$stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
|
88
|
-
exit 1
|
89
|
-
end
|
90
|
-
|
91
|
-
rescue LoadError
|
92
|
-
$stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
|
93
|
-
exit 1
|
94
|
-
end
|
95
|
-
|
96
|
-
def parse_gem_version(text)
|
97
|
-
$1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
|
98
|
-
end
|
99
|
-
|
100
|
-
private
|
101
|
-
def read_environment_rb
|
102
|
-
File.read("#{RAILS_ROOT}/config/environment.rb")
|
103
|
-
end
|
104
|
-
end
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
# All that for this:
|
109
|
-
Rails.boot!
|
data/rails/config/database.yml
DELETED
data/rails/config/environment.rb
DELETED
@@ -1,71 +0,0 @@
|
|
1
|
-
# Be sure to restart your server when you modify this file
|
2
|
-
|
3
|
-
# Uncomment below to force Rails into production mode when
|
4
|
-
# you don't control web/app server and can't set it the proper way
|
5
|
-
# ENV['RAILS_ENV'] ||= 'production'
|
6
|
-
|
7
|
-
# Specifies gem version of Rails to use when vendor/rails is not present
|
8
|
-
RAILS_GEM_VERSION = '2.2.2' unless defined? RAILS_GEM_VERSION
|
9
|
-
|
10
|
-
# Bootstrap the Rails environment, frameworks, and default configuration
|
11
|
-
require File.join(File.dirname(__FILE__), 'boot')
|
12
|
-
|
13
|
-
Rails::Initializer.run do |config|
|
14
|
-
# Settings in config/environments/* take precedence over those specified here.
|
15
|
-
# Application configuration should go into files in config/initializers
|
16
|
-
# -- all .rb files in that directory are automatically loaded.
|
17
|
-
# See Rails::Configuration for more options.
|
18
|
-
|
19
|
-
# Skip frameworks you're not going to use. To use Rails without a database
|
20
|
-
# you must remove the Active Record framework.
|
21
|
-
# config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
|
22
|
-
|
23
|
-
# Specify gems that this application depends on.
|
24
|
-
# They can then be installed with "rake gems:install" on new installations.
|
25
|
-
# config.gem "bj"
|
26
|
-
# config.gem "hpricot", :version => '0.6', :source => "http://code.whytheluckystiff.net"
|
27
|
-
# config.gem "aws-s3", :lib => "aws/s3"
|
28
|
-
config.gem 'thoughtbot-shoulda', :lib => 'shoulda/rails',
|
29
|
-
:source => 'http://gems.github.com'
|
30
|
-
|
31
|
-
# Only load the plugins named here, in the order given. By default, all plugins
|
32
|
-
# in vendor/plugins are loaded in alphabetical order.
|
33
|
-
# :all can be used as a placeholder for all plugins not explicitly named
|
34
|
-
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
|
35
|
-
|
36
|
-
# Add additional load paths for your own custom dirs
|
37
|
-
# config.load_paths += %W( #{RAILS_ROOT}/extras )
|
38
|
-
|
39
|
-
# Force all environments to use the same logger level
|
40
|
-
# (by default production uses :info, the others :debug)
|
41
|
-
# config.log_level = :debug
|
42
|
-
|
43
|
-
# Make Time.zone default to the specified zone, and make Active Record store time values
|
44
|
-
# in the database in UTC, and return them converted to the specified local zone.
|
45
|
-
# Run "rake -D time" for a list of tasks for finding time zone names. Comment line to use default local time.
|
46
|
-
# config.time_zone = ENV['TZ'] = 'UTC'
|
47
|
-
|
48
|
-
# Your secret key for verifying cookie session data integrity.
|
49
|
-
# If you change this key, all old sessions will become invalid!
|
50
|
-
# Make sure the secret is at least 30 characters and all random,
|
51
|
-
# no regular words or you'll be exposed to dictionary attacks.
|
52
|
-
# config.action_controller.session = {
|
53
|
-
# :session_key => '_load_model_session',
|
54
|
-
# :secret => '731d6426b38a848657211af3650ea99dc064a830ec15fd20565d71ba62498382132872b2d9b549eeb5a016025c119eb821d8e66794cd380888120aa0b857386d'
|
55
|
-
# }
|
56
|
-
|
57
|
-
# Use the database for sessions instead of the cookie-based default,
|
58
|
-
# which shouldn't be used to store highly confidential information
|
59
|
-
# (create the session table with "rake db:sessions:create")
|
60
|
-
# config.action_controller.session_store = :active_record_store
|
61
|
-
|
62
|
-
# Use SQL instead of Active Record's schema dumper when creating the test database.
|
63
|
-
# This is necessary if your schema can't be completely dumped by the schema dumper,
|
64
|
-
# like if you have constraints or database-specific column types
|
65
|
-
# config.active_record.schema_format = :sql
|
66
|
-
|
67
|
-
# Activate observers that should always be running
|
68
|
-
# config.active_record.observers = :cacher, :garbage_collector
|
69
|
-
end
|
70
|
-
|
71
|
-
require 'ruby-debug'
|
@@ -1,19 +0,0 @@
|
|
1
|
-
# Settings specified here will take precedence over those in config/environment.rb
|
2
|
-
|
3
|
-
# The test environment is used exclusively to run your application's
|
4
|
-
# test suite. You never need to work with it otherwise. Remember that
|
5
|
-
# your test database is "scratch space" for the test suite and is wiped
|
6
|
-
# and recreated between test runs. Don't rely on the data there!
|
7
|
-
config.cache_classes = true
|
8
|
-
|
9
|
-
# Log error messages when you accidentally call methods on nil.
|
10
|
-
config.whiny_nils = true
|
11
|
-
|
12
|
-
# Show full error reports and disable caching
|
13
|
-
config.action_controller.consider_all_requests_local = true
|
14
|
-
config.action_controller.perform_caching = false
|
15
|
-
|
16
|
-
# Tell ActionMailer not to deliver emails to the real world.
|
17
|
-
# The :test delivery method accumulates sent emails in the
|
18
|
-
# ActionMailer::Base.deliveries array.
|
19
|
-
config.action_mailer.delivery_method = :test
|
data/rails/config/routes.rb
DELETED
data/rails/db/schema.rb
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
ActiveRecord::Schema.define(:version => 1) do
|
2
|
-
create_table 'users', :force => true do |t|
|
3
|
-
t.column :name, :string
|
4
|
-
end
|
5
|
-
|
6
|
-
create_table 'posts', :force => true do |t|
|
7
|
-
t.column :user_id, :integer
|
8
|
-
t.column :published, :boolean, :default => true
|
9
|
-
t.column :name, :string
|
10
|
-
end
|
11
|
-
|
12
|
-
create_table 'alternates', :force => true do |t|
|
13
|
-
t.column :alternate_id, :integer
|
14
|
-
t.column :name, :string
|
15
|
-
end
|
16
|
-
|
17
|
-
create_table 'fuzzles', :force => true do |t|
|
18
|
-
t.column :fuzzle_id, :integer
|
19
|
-
t.column :name, :string
|
20
|
-
end
|
21
|
-
end
|
@@ -1,82 +0,0 @@
|
|
1
|
-
class BasicController < ActionController::Base
|
2
|
-
load_model :user
|
3
|
-
load_model :alternate, :parameter_key => :alternate_id,
|
4
|
-
:foreign_key => :alternate_id
|
5
|
-
load_model :chameleon, :class => :user
|
6
|
-
load_model :flamingo, :class => User
|
7
|
-
load_model :tucan, :class => :alternate, :parameter_key => :alternate_id,
|
8
|
-
:foreign_key => :alternate_id
|
9
|
-
|
10
|
-
def index; render :text => 'hello'; end
|
11
|
-
end
|
12
|
-
class BasicController; def rescue_action(e) raise e end; end
|
13
|
-
|
14
|
-
# Has strings for values
|
15
|
-
|
16
|
-
class StringKeyController < ActionController::Base
|
17
|
-
load_model 'user'
|
18
|
-
load_model 'alternate', :parameter_key => 'alternate_id',
|
19
|
-
:foreign_key => 'alternate_id'
|
20
|
-
load_model 'chameleon', :class => 'user'
|
21
|
-
|
22
|
-
def index; render :text => 'goodbye'; end
|
23
|
-
end
|
24
|
-
class StringKeyController; def rescue_action(e) raise e end; end
|
25
|
-
|
26
|
-
# Requires values
|
27
|
-
|
28
|
-
class RequireModelController < ActionController::Base
|
29
|
-
load_model :stuser, :class => :alternate, :parameter_key => :alternate_id,
|
30
|
-
:foreign_key => :alternate_id, :require => nil # never required
|
31
|
-
load_model :fuzzle, :parameter_key => :fuzzle_id, :foreign_key => :fuzzle_id,
|
32
|
-
:require => [:newer] # required for newer action
|
33
|
-
load_model :user, :require => true # required for all actions
|
34
|
-
|
35
|
-
def index; render :text => 'whatever'; end
|
36
|
-
def new; render :text => 'whatever 2'; end
|
37
|
-
def newer; render :text => 'whatever 3'; end
|
38
|
-
end
|
39
|
-
class RequireModelController; def rescue_action(e) raise e end; end
|
40
|
-
|
41
|
-
# Restriction options
|
42
|
-
|
43
|
-
class RestrictOptionsController < ActionController::Base
|
44
|
-
load_model :user, :only => [:index]
|
45
|
-
load_model :alternate, :except => [:index], :parameter_key => :alternate_id,
|
46
|
-
:foreign_key => :alternate_id
|
47
|
-
|
48
|
-
def index; render :text => 'ran index'; end
|
49
|
-
def show; render :text => 'ran show'; end
|
50
|
-
end
|
51
|
-
class RequireModelController; def rescue_action(e) raise e end; end
|
52
|
-
|
53
|
-
class KeysController < ActionController::Base
|
54
|
-
# Expects to use fuzzle_id as parameter key against User class with FK of :id
|
55
|
-
load_model :user, :parameter_key => :fuzzle_id
|
56
|
-
|
57
|
-
# Expects to use :fuzzle_id as FK and parameter key against Fuzzle class
|
58
|
-
# (normal operation)
|
59
|
-
load_model :fuzzler, :parameter_key => :fuzzle_id, :foreign_key => :fuzzle_id,
|
60
|
-
:class => :fuzzle
|
61
|
-
|
62
|
-
def index; render :text => 'hello'; end
|
63
|
-
end
|
64
|
-
class KeysController; def rescue_action(e) raise e end; end
|
65
|
-
|
66
|
-
# Load model through existing model
|
67
|
-
class ThroughController < ActionController::Base
|
68
|
-
load_model :user, :parameter_key => :user_id
|
69
|
-
load_model :post, :through => :user, :except => [:show]
|
70
|
-
# proving that except and only work
|
71
|
-
load_model :post, :through => :user, :parameter_key => 'weird_id',
|
72
|
-
:require => true, :only => [:show]
|
73
|
-
|
74
|
-
load_model :post, :through => :user, :association => :unpublished_posts,
|
75
|
-
:require => true, :only => [:show_unpublished]
|
76
|
-
|
77
|
-
def index; render :text => 'index'; end
|
78
|
-
def show; render :text => 'show'; end
|
79
|
-
def show_unpublished; render :text => 'unpublished'; end
|
80
|
-
|
81
|
-
end
|
82
|
-
class ThroughController; def rescue_action(e) raise e end; end
|
@@ -1,85 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
-
|
3
|
-
class LoadModelTest < Test::Unit::TestCase
|
4
|
-
|
5
|
-
def setup
|
6
|
-
super
|
7
|
-
@request = ActionController::TestRequest.new
|
8
|
-
@response = ActionController::TestResponse.new
|
9
|
-
@controller = BasicController.new
|
10
|
-
@foo = User.create!(:name => 'Foo')
|
11
|
-
end
|
12
|
-
|
13
|
-
def teardown
|
14
|
-
Alternate.delete_all
|
15
|
-
User.delete_all
|
16
|
-
end
|
17
|
-
|
18
|
-
context "when parameter" do
|
19
|
-
context "is provided" do
|
20
|
-
setup { get :index, :id => @foo.id }
|
21
|
-
should("find record") { assert_equal @foo.id, assigns(:user).id }
|
22
|
-
end # is provided
|
23
|
-
|
24
|
-
context "is not provided" do
|
25
|
-
setup { get :index }
|
26
|
-
should("not assign any record") { assert_nil assigns(:user) }
|
27
|
-
end # is not provided
|
28
|
-
|
29
|
-
context "does not match existing record" do
|
30
|
-
setup { get :index, :id => (@foo.id + 1) }
|
31
|
-
should("not assign any record") { assert_nil assigns(:user) }
|
32
|
-
end # does not match existing record
|
33
|
-
end # when parameter
|
34
|
-
|
35
|
-
def test_should_find_record_with_alternate_id_as_expected_param_key
|
36
|
-
alt = Alternate.create!(:name => 'Alternate', :alternate_id => 100)
|
37
|
-
get :index, :alternate_id => alt.alternate_id
|
38
|
-
assert_equal alt.id, assigns(:alternate).id
|
39
|
-
assert_equal alt.alternate_id, assigns(:alternate).alternate_id
|
40
|
-
end
|
41
|
-
|
42
|
-
def test_should_find_nothing_when_alternate_id_does_not_match_record
|
43
|
-
alt = Alternate.create!(:name => 'Alternate', :alternate_id => 99)
|
44
|
-
get :index, :alternate_id => 100
|
45
|
-
assert_nil assigns(:alternate)
|
46
|
-
end
|
47
|
-
|
48
|
-
def test_should_find_chameleon_in_user_table
|
49
|
-
get :index, :id => @foo.id
|
50
|
-
assert_equal @foo.id, assigns(:chameleon).id
|
51
|
-
end
|
52
|
-
|
53
|
-
def test_should_not_find_chameleon_in_user_table_with_nonexistent_id
|
54
|
-
get :index, :id => (@foo.id + 1)
|
55
|
-
assert_nil assigns(:chameleon)
|
56
|
-
end
|
57
|
-
|
58
|
-
def test_should_find_flamingo_in_user_table_even_when_class_name_is_constant
|
59
|
-
get :index, :id => @foo.id
|
60
|
-
assert_equal @foo.id, assigns(:flamingo).id
|
61
|
-
end
|
62
|
-
|
63
|
-
def test_should_not_find_flamingo_in_user_table_when_class_name_is_constant
|
64
|
-
get :index, :id => (@foo.id + 1)
|
65
|
-
assert_nil assigns(:flamingo)
|
66
|
-
end
|
67
|
-
|
68
|
-
def test_should_find_tucan_in_users_with_alternate_class_and_key
|
69
|
-
alt = Alternate.create!(:name => 'Alternate', :alternate_id => 100)
|
70
|
-
get :index, :alternate_id => alt.alternate_id
|
71
|
-
assert_equal alt.id, assigns(:tucan).id
|
72
|
-
end
|
73
|
-
|
74
|
-
def test_should_not_find_tucan_in_users_with_alternate_class_and_key
|
75
|
-
alt = Alternate.create!(:name => 'Alternate', :alternate_id => 100)
|
76
|
-
get :index, :alternate_id => (alt.alternate_id + 1)
|
77
|
-
assert_nil assigns(:tucan)
|
78
|
-
end
|
79
|
-
|
80
|
-
def test_should_not_find_record_if_key_value_is_not_an_integer
|
81
|
-
get :index, :id => 'abc'
|
82
|
-
assert_nil assigns(:user)
|
83
|
-
end
|
84
|
-
|
85
|
-
end
|
@@ -1,52 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
-
require File.dirname(__FILE__) + '/controller_helper'
|
3
|
-
|
4
|
-
class StringKeyLoadModelTest < Test::Unit::TestCase
|
5
|
-
|
6
|
-
def setup
|
7
|
-
super
|
8
|
-
@request = ActionController::TestRequest.new
|
9
|
-
@response = ActionController::TestResponse.new
|
10
|
-
@controller = StringKeyController.new
|
11
|
-
@foo = User.create!(:name => 'Foo')
|
12
|
-
end
|
13
|
-
|
14
|
-
def test_should_find_record_and_assign_to_instance_variable_if_param_provided
|
15
|
-
get :index, :id => @foo.id
|
16
|
-
assert_equal @foo.id, assigns(:user).id
|
17
|
-
end
|
18
|
-
|
19
|
-
def test_should_return_nil_if_expected_param_not_provided
|
20
|
-
get :index
|
21
|
-
assert_nil assigns(:user)
|
22
|
-
end
|
23
|
-
|
24
|
-
def test_should_return_nil_if_expected_param_does_not_match_record
|
25
|
-
get :index, :id => (@foo.id + 1) # Should not belong to an existing user
|
26
|
-
assert_nil assigns(:user)
|
27
|
-
end
|
28
|
-
|
29
|
-
def test_should_find_record_with_alternate_id_as_expected_param_key
|
30
|
-
alt = Alternate.create!(:name => 'Alternate', :alternate_id => 100)
|
31
|
-
get :index, :alternate_id => alt.alternate_id
|
32
|
-
assert_equal alt.id, assigns(:alternate).id
|
33
|
-
assert_equal alt.alternate_id, assigns(:alternate).alternate_id
|
34
|
-
end
|
35
|
-
|
36
|
-
def test_should_find_nothing_when_alternate_id_does_not_match_record
|
37
|
-
alt = Alternate.create!(:name => 'Alternate', :alternate_id => 99)
|
38
|
-
get :index, :alternate_id => 100
|
39
|
-
assert_nil assigns(:alternate)
|
40
|
-
end
|
41
|
-
|
42
|
-
def test_should_find_chameleon_in_user_table
|
43
|
-
get :index, :id => @foo.id
|
44
|
-
assert_equal @foo.id, assigns(:chameleon).id
|
45
|
-
end
|
46
|
-
|
47
|
-
def test_should_not_find_chameleon_in_user_table_with_nonexistent_id
|
48
|
-
get :index, :id => (@foo.id + 1)
|
49
|
-
assert_nil assigns(:chameleon)
|
50
|
-
end
|
51
|
-
|
52
|
-
end
|