thumblemonks-load_model 0.2.0 → 0.2.2
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/lib/load_model.rb +6 -78
- data/lib/thumblemonks/model_loader.rb +90 -0
- data/load_model.gemspec +14 -15
- data/test/functional/basic_controller_test.rb +90 -0
- data/test/functional/from_controller_test.rb +26 -0
- data/test/functional/string_key_controller_test.rb +54 -0
- data/test/functional/through_controller_test.rb +10 -0
- data/test/rails/app/controllers/application.rb +3 -0
- data/test/rails/config/boot.rb +109 -0
- data/test/rails/config/database.yml +3 -0
- data/test/rails/config/environment.rb +15 -0
- data/test/rails/config/environments/test.rb +19 -0
- data/test/rails/config/routes.rb +5 -0
- data/test/rails/db/schema.rb +21 -0
- data/test/test_helper.rb +4 -0
- metadata +14 -14
data/lib/load_model.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'thumblemonks/model_loader'
|
2
|
+
|
1
3
|
module ThumbleMonks #:nodoc:
|
2
4
|
module LoadModel
|
3
5
|
|
@@ -151,93 +153,19 @@ module ThumbleMonks #:nodoc:
|
|
151
153
|
end
|
152
154
|
|
153
155
|
def loaders; self.read_inheritable_attribute(:loaders); end
|
154
|
-
|
155
|
-
class ModelLoader #:nodoc
|
156
|
-
attr_reader :assigns_to, :load_through, :parameter_key, :foreign_key,
|
157
|
-
:except, :only, :requires
|
158
|
-
|
159
|
-
def initialize(name, opts={})
|
160
|
-
config = {:require => false, :parameter_key => :id,
|
161
|
-
:foreign_key => :id, :class => name}.merge(opts)
|
162
|
-
@assigns_to = "@#{name}".to_sym
|
163
|
-
@load_through = config[:class].to_s.classify.constantize
|
164
|
-
@parameter_key = config[:parameter_key].to_s
|
165
|
-
@foreign_key = config[:foreign_key].to_s
|
166
|
-
@requires = parse_required_actions(config[:require])
|
167
|
-
@except = stringify_array(config[:except])
|
168
|
-
@only = stringify_array(config[:only])
|
169
|
-
end
|
170
|
-
|
171
|
-
def action_allowed?(action)
|
172
|
-
return false if except.include?(action)
|
173
|
-
only.empty? ? true : only.include?(action)
|
174
|
-
end
|
175
|
-
|
176
|
-
def action_required?(action)
|
177
|
-
requires == true || requires.include?(action)
|
178
|
-
end
|
179
|
-
|
180
|
-
def load_model(controller)
|
181
|
-
begin
|
182
|
-
lookup = parameter_value(controller)
|
183
|
-
source(controller).send("find_by_#{foreign_key}", lookup)
|
184
|
-
rescue ActiveRecord::StatementInvalid
|
185
|
-
nil
|
186
|
-
end
|
187
|
-
end
|
188
|
-
private
|
189
|
-
def source(controller) load_through; end
|
190
|
-
|
191
|
-
def parse_required_actions(actions)
|
192
|
-
actions == true ? true : stringify_array(actions)
|
193
|
-
end
|
194
|
-
|
195
|
-
def parameter_value(controller) controller.params[parameter_key]; end
|
196
|
-
|
197
|
-
def stringify_array(value) Array(value).map(&:to_s); end
|
198
|
-
end # ModelLoader
|
199
|
-
|
200
|
-
class ThroughModelLoader < ModelLoader #:nodoc
|
201
|
-
attr_reader :load_through, :association
|
202
|
-
def initialize(name, opts={})
|
203
|
-
super(name, opts)
|
204
|
-
@load_through = "@#{opts[:through]}".to_sym
|
205
|
-
@association = opts[:association] || name.to_s.pluralize
|
206
|
-
end
|
207
|
-
private
|
208
|
-
def source(controller)
|
209
|
-
controller.instance_variable_get(load_through).send(association)
|
210
|
-
end
|
211
|
-
end # ThroughModelLoader
|
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
156
|
private
|
226
157
|
def loader_class(opts)
|
227
|
-
return ThroughModelLoader if opts[:through]
|
228
|
-
return FromModelLoader if opts[:from]
|
229
|
-
|
158
|
+
return ThumbleMonks::ThroughModelLoader if opts[:through]
|
159
|
+
return ThumbleMonks::FromModelLoader if opts[:from]
|
160
|
+
ThumbleMonks::AssociativeModelLoader
|
230
161
|
end
|
231
162
|
end # ClassMethods
|
232
|
-
|
233
163
|
private
|
234
164
|
def load_specified_models
|
235
165
|
self.class.loaders.each do |loader|
|
236
166
|
if loader.action_allowed?(action_name)
|
237
167
|
obj = loader.load_model(self)
|
238
|
-
if obj.nil? && loader.action_required?(action_name)
|
239
|
-
raise RequiredRecordNotFound
|
240
|
-
end
|
168
|
+
raise RequiredRecordNotFound if obj.nil? && loader.action_required?(action_name)
|
241
169
|
instance_variable_set(loader.assigns_to, obj)
|
242
170
|
end
|
243
171
|
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
module ThumbleMonks #:nodoc:
|
2
|
+
class NoModelLoaderFound < Exception; end
|
3
|
+
|
4
|
+
class ModelLoader #:nodoc
|
5
|
+
attr_reader :assigns_to, :except, :only, :requires
|
6
|
+
|
7
|
+
def initialize(name, opts={})
|
8
|
+
config = {:require => false}.merge(opts)
|
9
|
+
@assigns_to = "@#{name}".to_sym
|
10
|
+
@requires = parse_required_actions(config[:require])
|
11
|
+
@except = stringify_array(config[:except])
|
12
|
+
@only = stringify_array(config[:only])
|
13
|
+
end
|
14
|
+
|
15
|
+
def action_allowed?(action)
|
16
|
+
return false if except.include?(action)
|
17
|
+
only.empty? ? true : only.include?(action)
|
18
|
+
end
|
19
|
+
|
20
|
+
def action_required?(action)
|
21
|
+
requires == true || requires.include?(action)
|
22
|
+
end
|
23
|
+
|
24
|
+
def load_model(controller)
|
25
|
+
raise NoModelLoaderFound
|
26
|
+
end
|
27
|
+
private
|
28
|
+
def parse_required_actions(actions)
|
29
|
+
actions == true ? true : stringify_array(actions)
|
30
|
+
end
|
31
|
+
|
32
|
+
def parameter_value(controller) controller.params[parameter_key]; end
|
33
|
+
|
34
|
+
def stringify_array(value) Array(value).map(&:to_s); end
|
35
|
+
|
36
|
+
def retrieve_association_through(controller, instance_variable_name)
|
37
|
+
controller.instance_variable_get(instance_variable_name).send(@association)
|
38
|
+
end
|
39
|
+
end # ModelLoader
|
40
|
+
|
41
|
+
class AssociativeModelLoader < ModelLoader #:nodoc
|
42
|
+
attr_reader :load_through, :parameter_key, :foreign_key
|
43
|
+
|
44
|
+
def initialize(name, opts={})
|
45
|
+
super
|
46
|
+
config = {:parameter_key => :id, :foreign_key => :id, :class => name}.merge(opts)
|
47
|
+
@load_through = config[:class].to_s.classify.constantize
|
48
|
+
@parameter_key = config[:parameter_key].to_s
|
49
|
+
@foreign_key = config[:foreign_key].to_s
|
50
|
+
end
|
51
|
+
|
52
|
+
def load_model(controller)
|
53
|
+
begin
|
54
|
+
lookup = parameter_value(controller)
|
55
|
+
source(controller).send("find_by_#{foreign_key}", lookup) unless lookup.blank?
|
56
|
+
rescue ActiveRecord::StatementInvalid
|
57
|
+
nil
|
58
|
+
end
|
59
|
+
end
|
60
|
+
private
|
61
|
+
def source(controller)
|
62
|
+
load_through
|
63
|
+
end
|
64
|
+
end # AssociativeModelLoader
|
65
|
+
|
66
|
+
class ThroughModelLoader < AssociativeModelLoader #:nodoc
|
67
|
+
def initialize(name, opts={})
|
68
|
+
super
|
69
|
+
@load_through = "@#{opts[:through]}".to_sym
|
70
|
+
@association = opts[:association] || name.to_s.pluralize
|
71
|
+
end
|
72
|
+
private
|
73
|
+
def source(controller)
|
74
|
+
retrieve_association_through(controller, load_through)
|
75
|
+
end
|
76
|
+
end # ThroughModelLoader
|
77
|
+
|
78
|
+
class FromModelLoader < ModelLoader #:nodoc
|
79
|
+
def initialize(name, opts={})
|
80
|
+
super
|
81
|
+
@load_from = "@#{opts[:from]}".to_sym
|
82
|
+
@association = name.to_s
|
83
|
+
end
|
84
|
+
|
85
|
+
def load_model(controller)
|
86
|
+
retrieve_association_through(controller, @load_from)
|
87
|
+
end
|
88
|
+
end # FromModelLoader
|
89
|
+
|
90
|
+
end # ThumbleMonks
|
data/load_model.gemspec
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "load_model"
|
3
|
-
s.version = "0.2.
|
4
|
-
s.date = "2009-03
|
3
|
+
s.version = "0.2.2"
|
4
|
+
s.date = "2009-06-03"
|
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"
|
8
8
|
s.description = "Rails Controller plugin that provides easy and useful macros for tying models and requests together"
|
9
|
-
s.authors = %w[Justin\ Knowlden Gabriel\ Gironda]
|
9
|
+
s.authors = %w[Justin\ Knowlden Gabriel\ Gironda Dan\ Hodos]
|
10
10
|
|
11
11
|
s.rubyforge_project = %q{load_model}
|
12
12
|
|
@@ -25,26 +25,25 @@ Gem::Specification.new do |s|
|
|
25
25
|
README.markdown
|
26
26
|
Rakefile
|
27
27
|
lib/load_model.rb
|
28
|
+
lib/thumblemonks/model_loader.rb
|
28
29
|
load_model.gemspec
|
29
30
|
]
|
30
31
|
|
31
32
|
s.test_files = %w[
|
32
|
-
|
33
|
-
|
34
|
-
rails/config/database.yml
|
35
|
-
rails/config/environment.rb
|
36
|
-
rails/config/environments/test.rb
|
37
|
-
rails/config/routes.rb
|
38
|
-
rails/db/schema.rb
|
39
|
-
rails/db/test.db
|
40
|
-
rails/log/test.log
|
41
|
-
test/functional/controller_helper.rb
|
33
|
+
test/functional/basic_controller_test.rb
|
34
|
+
test/functional/from_controller_test.rb
|
42
35
|
test/functional/keys_controller_test.rb
|
43
|
-
test/functional/load_model_test.rb
|
44
36
|
test/functional/require_model_controller_test.rb
|
45
37
|
test/functional/restrict_options_controller_test.rb
|
46
|
-
test/functional/
|
38
|
+
test/functional/string_key_controller_test.rb
|
47
39
|
test/functional/through_controller_test.rb
|
40
|
+
test/rails/app/controllers/application.rb
|
41
|
+
test/rails/config/boot.rb
|
42
|
+
test/rails/config/database.yml
|
43
|
+
test/rails/config/environment.rb
|
44
|
+
test/rails/config/environments/test.rb
|
45
|
+
test/rails/config/routes.rb
|
46
|
+
test/rails/db/schema.rb
|
48
47
|
test/test_helper.rb
|
49
48
|
]
|
50
49
|
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class BasicController < ActionController::Base
|
4
|
+
load_model :user
|
5
|
+
load_model :alternate, :parameter_key => :alternate_id,
|
6
|
+
:foreign_key => :alternate_id
|
7
|
+
load_model :chameleon, :class => :user
|
8
|
+
load_model :flamingo, :class => User
|
9
|
+
load_model :tucan, :class => :alternate, :parameter_key => :alternate_id,
|
10
|
+
:foreign_key => :alternate_id
|
11
|
+
|
12
|
+
def index; render :text => 'hello'; end
|
13
|
+
end
|
14
|
+
|
15
|
+
class BasicControllerTest < ActionController::TestCase
|
16
|
+
def setup
|
17
|
+
@foo = User.create!(:name => 'Foo')
|
18
|
+
end
|
19
|
+
|
20
|
+
context "when parameter" do
|
21
|
+
context "is provided" do
|
22
|
+
setup { get :index, :id => @foo.id }
|
23
|
+
should("find record") { assert_equal @foo.id, assigns(:user).id }
|
24
|
+
end # is provided
|
25
|
+
|
26
|
+
context "is not provided" do
|
27
|
+
setup { get :index }
|
28
|
+
should("not assign any record") { assert_nil assigns(:user) }
|
29
|
+
end # is not provided
|
30
|
+
|
31
|
+
context "does not match existing record" do
|
32
|
+
setup { get :index, :id => (@foo.id + 1) }
|
33
|
+
should("not assign any record") { assert_nil assigns(:user) }
|
34
|
+
end # does not match existing record
|
35
|
+
end # when parameter
|
36
|
+
|
37
|
+
should "find chameleon in users" do
|
38
|
+
get :index, :id => @foo.id
|
39
|
+
assert_equal @foo.id, assigns(:chameleon).id
|
40
|
+
end
|
41
|
+
|
42
|
+
should "not find chameleon in users with nonexistent id" do
|
43
|
+
get :index, :id => (@foo.id + 1)
|
44
|
+
assert_nil assigns(:chameleon)
|
45
|
+
end
|
46
|
+
|
47
|
+
should "not find record if key value is not an integer" do
|
48
|
+
get :index, :id => 'abc'
|
49
|
+
assert_nil assigns(:user)
|
50
|
+
end
|
51
|
+
|
52
|
+
context "when class name is constant" do
|
53
|
+
should "find flamingo in user table" do
|
54
|
+
get :index, :id => @foo.id
|
55
|
+
assert_equal @foo.id, assigns(:flamingo).id
|
56
|
+
end
|
57
|
+
|
58
|
+
should "not find flamingo in user table" do
|
59
|
+
get :index, :id => (@foo.id + 1)
|
60
|
+
assert_nil assigns(:flamingo)
|
61
|
+
end
|
62
|
+
end # when class name is constant
|
63
|
+
|
64
|
+
context "for alternate" do
|
65
|
+
context "with existing id" do
|
66
|
+
setup do
|
67
|
+
@alt = Alternate.create!(:name => 'Alternate', :alternate_id => 100)
|
68
|
+
get :index, :alternate_id => @alt.alternate_id
|
69
|
+
end
|
70
|
+
|
71
|
+
should_assign_to(:alternate) { @alt }
|
72
|
+
should_assign_to(:tucan) { @alt }
|
73
|
+
end
|
74
|
+
|
75
|
+
context "with non-existant id" do
|
76
|
+
setup { get :index, :alternate_id => 99 }
|
77
|
+
should_not_assign_to(:alternate)
|
78
|
+
should_not_assign_to(:tucan)
|
79
|
+
end
|
80
|
+
end # with alternate class and key
|
81
|
+
|
82
|
+
context "when id is nil for users" do
|
83
|
+
setup do
|
84
|
+
User.expects(:find_by_id).never
|
85
|
+
get :index, :id => ''
|
86
|
+
end
|
87
|
+
|
88
|
+
should_not_assign_to(:user)
|
89
|
+
end # when parameter value is nil
|
90
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class FromController < ActionController::Base
|
4
|
+
load_model :post
|
5
|
+
load_model :user, :from => :post
|
6
|
+
load_model :foo, :from => :post
|
7
|
+
|
8
|
+
def index; render :text => 'index'; end
|
9
|
+
end
|
10
|
+
|
11
|
+
class FromControllerTest < ActionController::TestCase
|
12
|
+
def setup
|
13
|
+
@user = User.create!(:name => 'Foo')
|
14
|
+
@post = @user.posts.create!(:name => 'Foo post')
|
15
|
+
end
|
16
|
+
|
17
|
+
context "loading from post model" do
|
18
|
+
setup do
|
19
|
+
get :index, :id => @post.id
|
20
|
+
end
|
21
|
+
|
22
|
+
should_assign_to(:post) { @post }
|
23
|
+
should_assign_to(:user) { @user }
|
24
|
+
should_assign_to(:foo) { "bar" }
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class StringKeyController < ActionController::Base
|
4
|
+
load_model 'user'
|
5
|
+
load_model 'alternate', :parameter_key => 'alternate_id',
|
6
|
+
:foreign_key => 'alternate_id'
|
7
|
+
load_model 'chameleon', :class => 'user'
|
8
|
+
|
9
|
+
def index; render :text => 'goodbye'; end
|
10
|
+
end
|
11
|
+
|
12
|
+
class StringKeyControllerTest < ActionController::TestCase
|
13
|
+
def setup
|
14
|
+
@foo = User.create!(:name => 'Foo')
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_should_find_record_and_assign_to_instance_variable_if_param_provided
|
18
|
+
get :index, :id => @foo.id
|
19
|
+
assert_equal @foo.id, assigns(:user).id
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_should_return_nil_if_expected_param_not_provided
|
23
|
+
get :index
|
24
|
+
assert_nil assigns(:user)
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_should_return_nil_if_expected_param_does_not_match_record
|
28
|
+
get :index, :id => (@foo.id + 1) # Should not belong to an existing user
|
29
|
+
assert_nil assigns(:user)
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_should_find_record_with_alternate_id_as_expected_param_key
|
33
|
+
alt = Alternate.create!(:name => 'Alternate', :alternate_id => 100)
|
34
|
+
get :index, :alternate_id => alt.alternate_id
|
35
|
+
assert_equal alt.id, assigns(:alternate).id
|
36
|
+
assert_equal alt.alternate_id, assigns(:alternate).alternate_id
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_should_find_nothing_when_alternate_id_does_not_match_record
|
40
|
+
alt = Alternate.create!(:name => 'Alternate', :alternate_id => 99)
|
41
|
+
get :index, :alternate_id => 100
|
42
|
+
assert_nil assigns(:alternate)
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_should_find_chameleon_in_user_table
|
46
|
+
get :index, :id => @foo.id
|
47
|
+
assert_equal @foo.id, assigns(:chameleon).id
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_should_not_find_chameleon_in_user_table_with_nonexistent_id
|
51
|
+
get :index, :id => (@foo.id + 1)
|
52
|
+
assert_nil assigns(:chameleon)
|
53
|
+
end
|
54
|
+
end
|
@@ -67,4 +67,14 @@ class ThroughControllerTest < ActionController::TestCase
|
|
67
67
|
end
|
68
68
|
end # has nonexistant records for required action
|
69
69
|
end # show with alternative post via weird_id
|
70
|
+
|
71
|
+
context "when parameter value is nil" do
|
72
|
+
setup do
|
73
|
+
Post.expects(:find_by_id).never
|
74
|
+
get :index, :user_id => @user.id, :id => ''
|
75
|
+
end
|
76
|
+
|
77
|
+
should_assign_to(:user) { @user }
|
78
|
+
should_not_assign_to(:post)
|
79
|
+
end # when parameter value is nil
|
70
80
|
end
|
@@ -0,0 +1,109 @@
|
|
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 rescue nil
|
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.3.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!
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# Specifies gem version of Rails to use when vendor/rails is not present
|
2
|
+
RAILS_GEM_VERSION = '2.3.2' unless defined? RAILS_GEM_VERSION
|
3
|
+
|
4
|
+
# Bootstrap the Rails environment, frameworks, and default configuration
|
5
|
+
require File.join(File.dirname(__FILE__), 'boot')
|
6
|
+
|
7
|
+
Rails::Initializer.run do |config|
|
8
|
+
config.gem 'thoughtbot-shoulda', :lib => 'shoulda/rails', :source => 'http://gems.github.com'
|
9
|
+
config.gem 'mocha'
|
10
|
+
|
11
|
+
config.action_controller.session = {
|
12
|
+
:session_key => '_load_model_session',
|
13
|
+
:secret => '9908cd9908cd9908cd9908cd9908cd9908cd9908cd9908cd9908cd9908cd9908cd9908cd9908cd'
|
14
|
+
}
|
15
|
+
end
|
@@ -0,0 +1,19 @@
|
|
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
|
@@ -0,0 +1,21 @@
|
|
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
|
data/test/test_helper.rb
CHANGED
@@ -4,9 +4,11 @@ end
|
|
4
4
|
|
5
5
|
ENV["RAILS_ENV"] = "test"
|
6
6
|
ENV["RAILS_ROOT"] = File.expand_path(File.join(File.dirname(__FILE__), '..', 'test', 'rails'))
|
7
|
+
|
7
8
|
require File.expand_path(File.join(ENV["RAILS_ROOT"], 'config', 'environment'))
|
8
9
|
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'load_model'))
|
9
10
|
require 'test_help'
|
11
|
+
|
10
12
|
load(File.join(ENV["RAILS_ROOT"], "db", "schema.rb"))
|
11
13
|
|
12
14
|
# Models
|
@@ -17,6 +19,8 @@ class User < ActiveRecord::Base
|
|
17
19
|
end
|
18
20
|
class Post < ActiveRecord::Base
|
19
21
|
belongs_to :user
|
22
|
+
|
23
|
+
def foo; "bar"; end
|
20
24
|
end
|
21
25
|
class Alternate < ActiveRecord::Base; end
|
22
26
|
class Fuzzle < ActiveRecord::Base; end
|
metadata
CHANGED
@@ -1,16 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: thumblemonks-load_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Justin Knowlden
|
8
8
|
- Gabriel Gironda
|
9
|
+
- Dan Hodos
|
9
10
|
autorequire:
|
10
11
|
bindir: bin
|
11
12
|
cert_chain: []
|
12
13
|
|
13
|
-
date: 2009-03
|
14
|
+
date: 2009-06-03 00:00:00 -07:00
|
14
15
|
default_executable:
|
15
16
|
dependencies: []
|
16
17
|
|
@@ -31,6 +32,7 @@ files:
|
|
31
32
|
- README.markdown
|
32
33
|
- Rakefile
|
33
34
|
- lib/load_model.rb
|
35
|
+
- lib/thumblemonks/model_loader.rb
|
34
36
|
- load_model.gemspec
|
35
37
|
has_rdoc: true
|
36
38
|
homepage: http://github.com/thumblemonks/load_model
|
@@ -64,20 +66,18 @@ signing_key:
|
|
64
66
|
specification_version: 2
|
65
67
|
summary: Rails Controller plugin that provides easy and useful macros for tying models and requests together
|
66
68
|
test_files:
|
67
|
-
-
|
68
|
-
-
|
69
|
-
- rails/config/database.yml
|
70
|
-
- rails/config/environment.rb
|
71
|
-
- rails/config/environments/test.rb
|
72
|
-
- rails/config/routes.rb
|
73
|
-
- rails/db/schema.rb
|
74
|
-
- rails/db/test.db
|
75
|
-
- rails/log/test.log
|
76
|
-
- test/functional/controller_helper.rb
|
69
|
+
- test/functional/basic_controller_test.rb
|
70
|
+
- test/functional/from_controller_test.rb
|
77
71
|
- test/functional/keys_controller_test.rb
|
78
|
-
- test/functional/load_model_test.rb
|
79
72
|
- test/functional/require_model_controller_test.rb
|
80
73
|
- test/functional/restrict_options_controller_test.rb
|
81
|
-
- test/functional/
|
74
|
+
- test/functional/string_key_controller_test.rb
|
82
75
|
- test/functional/through_controller_test.rb
|
76
|
+
- test/rails/app/controllers/application.rb
|
77
|
+
- test/rails/config/boot.rb
|
78
|
+
- test/rails/config/database.yml
|
79
|
+
- test/rails/config/environment.rb
|
80
|
+
- test/rails/config/environments/test.rb
|
81
|
+
- test/rails/config/routes.rb
|
82
|
+
- test/rails/db/schema.rb
|
83
83
|
- test/test_helper.rb
|