resource_controller 0.5.2 → 0.5.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/resource_controller/helpers/current_objects.rb +63 -59
- data/lib/resource_controller/helpers/internal.rb +69 -65
- data/lib/resource_controller/helpers/nested.rb +54 -50
- data/lib/resource_controller/helpers/singleton_customizations.rb +50 -46
- data/lib/resource_controller/helpers/urls.rb +73 -69
- data/lib/resource_controller/helpers.rb +6 -0
- data/lib/resource_controller/version.rb +1 -1
- data/lib/resource_controller.rb +13 -0
- data/test/config/environment.rb +3 -18
- data/test/log/development.log +264 -0
- data/test/log/test.log +29568 -0
- metadata +4 -2
@@ -1,69 +1,73 @@
|
|
1
|
-
module ResourceController
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
1
|
+
module ResourceController
|
2
|
+
module Helpers
|
3
|
+
module CurrentObjects
|
4
|
+
protected
|
5
|
+
# Used internally to return the model for your resource.
|
6
|
+
#
|
7
|
+
def model
|
8
|
+
model_name.to_s.camelize.constantize
|
9
|
+
end
|
8
10
|
|
9
11
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
12
|
+
# Used to fetch the collection for the index method
|
13
|
+
#
|
14
|
+
# In order to customize the way the collection is fetched, to add something like pagination, for example, override this method.
|
15
|
+
#
|
16
|
+
def collection
|
17
|
+
end_of_association_chain.find(:all)
|
18
|
+
end
|
17
19
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
20
|
+
# Returns the current param.
|
21
|
+
#
|
22
|
+
# Defaults to params[:id].
|
23
|
+
#
|
24
|
+
# Override this method if you'd like to use an alternate param name.
|
25
|
+
#
|
26
|
+
def param
|
27
|
+
params[:id]
|
28
|
+
end
|
27
29
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
30
|
+
# Used to fetch the current member object in all of the singular methods that operate on an existing member.
|
31
|
+
#
|
32
|
+
# Override this method if you'd like to fetch your objects in some alternate way, like using a permalink.
|
33
|
+
#
|
34
|
+
# class PostsController < ResourceController::Base
|
35
|
+
# private
|
36
|
+
# def object
|
37
|
+
# @object ||= end_of_association_chain.find_by_permalink(param)
|
38
|
+
# end
|
39
|
+
# end
|
40
|
+
#
|
41
|
+
def object
|
42
|
+
@object ||= end_of_association_chain.find(param) unless param.nil?
|
43
|
+
@object
|
44
|
+
end
|
43
45
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
46
|
+
# Used internally to load the member object in to an instance variable @#{model_name} (i.e. @post)
|
47
|
+
#
|
48
|
+
def load_object
|
49
|
+
instance_variable_set "@#{parent_type}", parent_object if parent?
|
50
|
+
instance_variable_set "@#{object_name}", object
|
51
|
+
end
|
50
52
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
53
|
+
# Used internally to load the collection in to an instance variable @#{model_name.pluralize} (i.e. @posts)
|
54
|
+
#
|
55
|
+
def load_collection
|
56
|
+
instance_variable_set "@#{parent_type}", parent_object if parent?
|
57
|
+
instance_variable_set "@#{object_name.to_s.pluralize}", collection
|
58
|
+
end
|
57
59
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
60
|
+
# Returns the form params. Defaults to params[model_name] (i.e. params["post"])
|
61
|
+
#
|
62
|
+
def object_params
|
63
|
+
params["#{object_name}"]
|
64
|
+
end
|
63
65
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
66
|
+
# Builds the object, but doesn't save it, during the new, and create action.
|
67
|
+
#
|
68
|
+
def build_object
|
69
|
+
@object ||= end_of_association_chain.send parent? ? :build : :new, object_params
|
70
|
+
end
|
68
71
|
end
|
72
|
+
end
|
69
73
|
end
|
@@ -2,75 +2,79 @@
|
|
2
2
|
#
|
3
3
|
# All of these methods are used internally to execute the options, set by the user in ActionOptions and FailableActionOptions
|
4
4
|
#
|
5
|
-
module ResourceController
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
5
|
+
module ResourceController
|
6
|
+
module Helpers
|
7
|
+
module Internal
|
8
|
+
protected
|
9
|
+
# Used to actually pass the responses along to the controller's respond_to method.
|
10
|
+
#
|
11
|
+
def response_for(action)
|
12
|
+
respond_to do |wants|
|
13
|
+
options_for(action).response.each do |method, block|
|
14
|
+
if block.nil?
|
15
|
+
wants.send(method)
|
16
|
+
else
|
17
|
+
wants.send(method) { instance_eval(&block) }
|
18
|
+
end
|
19
|
+
end
|
16
20
|
end
|
17
21
|
end
|
18
|
-
|
19
|
-
|
22
|
+
|
23
|
+
# Calls the after callbacks for the action, if one is present.
|
24
|
+
#
|
25
|
+
def after(action)
|
26
|
+
invoke_callbacks *options_for(action).after
|
27
|
+
end
|
28
|
+
|
29
|
+
# Calls the before block for the action, if one is present.
|
30
|
+
#
|
31
|
+
def before(action)
|
32
|
+
invoke_callbacks *self.class.send(action).before
|
33
|
+
end
|
20
34
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
35
|
+
# Sets the flash and flash_now for the action, if it is present.
|
36
|
+
#
|
37
|
+
def set_flash(action)
|
38
|
+
set_normal_flash(action)
|
39
|
+
set_flash_now(action)
|
40
|
+
end
|
26
41
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
# Manages splitting things like :create_fails.
|
59
|
-
#
|
60
|
-
def options_for(action)
|
61
|
-
action = action == :new_action ? [action] : "#{action}".split('_').map(&:to_sym)
|
62
|
-
options = self.class.send(action.first)
|
63
|
-
options = options.send(action.last == :fails ? :fails : :success) if ResourceController::FAILABLE_ACTIONS.include? action.first
|
42
|
+
# Sets the regular flash (i.e. flash[:notice] = '...')
|
43
|
+
#
|
44
|
+
def set_normal_flash(action)
|
45
|
+
if f = options_for(action).flash
|
46
|
+
flash[:notice] = f.is_a?(Proc) ? instance_eval(&f) : options_for(action).flash
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# Sets the flash.now (i.e. flash.now[:notice] = '...')
|
51
|
+
#
|
52
|
+
def set_flash_now(action)
|
53
|
+
if f = options_for(action).flash_now
|
54
|
+
flash.now[:notice] = f.is_a?(Proc) ? instance_eval(&f) : options_for(action).flash_now
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# Returns the options for an action, which is a symbol.
|
59
|
+
#
|
60
|
+
# Manages splitting things like :create_fails.
|
61
|
+
#
|
62
|
+
def options_for(action)
|
63
|
+
action = action == :new_action ? [action] : "#{action}".split('_').map(&:to_sym)
|
64
|
+
options = self.class.send(action.first)
|
65
|
+
options = options.send(action.last == :fails ? :fails : :success) if ResourceController::FAILABLE_ACTIONS.include? action.first
|
66
|
+
|
67
|
+
options
|
68
|
+
end
|
69
|
+
|
70
|
+
def invoke_callbacks(*callbacks)
|
71
|
+
unless callbacks.empty?
|
72
|
+
callbacks.select { |callback| callback.is_a? Symbol }.each { |symbol| send(symbol) }
|
64
73
|
|
65
|
-
|
74
|
+
block = callbacks.detect { |callback| callback.is_a? Proc }
|
75
|
+
instance_eval &block unless block.nil?
|
76
|
+
end
|
77
|
+
end
|
66
78
|
end
|
67
|
-
|
68
|
-
def invoke_callbacks(*callbacks)
|
69
|
-
unless callbacks.empty?
|
70
|
-
callbacks.select { |callback| callback.is_a? Symbol }.each { |symbol| send(symbol) }
|
71
|
-
|
72
|
-
block = callbacks.detect { |callback| callback.is_a? Proc }
|
73
|
-
instance_eval &block unless block.nil?
|
74
|
-
end
|
75
|
-
end
|
79
|
+
end
|
76
80
|
end
|
@@ -1,63 +1,67 @@
|
|
1
1
|
# Nested and Polymorphic Resource Helpers
|
2
2
|
#
|
3
|
-
module ResourceController
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
3
|
+
module ResourceController
|
4
|
+
module Helpers
|
5
|
+
module Nested
|
6
|
+
protected
|
7
|
+
# Returns the relevant association proxy of the parent. (i.e. /posts/1/comments # => @post.comments)
|
8
|
+
#
|
9
|
+
def parent_association
|
10
|
+
@parent_association ||= parent_object.send(model_name.to_s.pluralize.to_sym)
|
11
|
+
end
|
10
12
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
# Returns the type of the current parent
|
14
|
+
#
|
15
|
+
def parent_type
|
16
|
+
@parent_type ||= parent_type_from_params || parent_type_from_request
|
17
|
+
end
|
16
18
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
19
|
+
# Returns the type of the current parent extracted from params
|
20
|
+
#
|
21
|
+
def parent_type_from_params
|
22
|
+
[*belongs_to].find { |parent| !params["#{parent}_id".to_sym].nil? }
|
23
|
+
end
|
22
24
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
25
|
+
# Returns the type of the current parent extracted form a request path
|
26
|
+
#
|
27
|
+
def parent_type_from_request
|
28
|
+
[*belongs_to].find { |parent| request.path.split('/').include? parent.to_s }
|
29
|
+
end
|
28
30
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
31
|
+
# Returns true/false based on whether or not a parent is present.
|
32
|
+
#
|
33
|
+
def parent?
|
34
|
+
!parent_type.nil?
|
35
|
+
end
|
34
36
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
37
|
+
# Returns true/false based on whether or not a parent is a singleton.
|
38
|
+
#
|
39
|
+
def parent_singleton?
|
40
|
+
!parent_type_from_request.nil?
|
41
|
+
end
|
40
42
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
43
|
+
# Returns the current parent param, if there is a parent. (i.e. params[:post_id])
|
44
|
+
def parent_param
|
45
|
+
params["#{parent_type}_id".to_sym]
|
46
|
+
end
|
45
47
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
48
|
+
# Like the model method, but for a parent relationship.
|
49
|
+
#
|
50
|
+
def parent_model
|
51
|
+
parent_type.to_s.camelize.constantize
|
52
|
+
end
|
51
53
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
54
|
+
# Returns the current parent object if a parent object is present.
|
55
|
+
#
|
56
|
+
def parent_object
|
57
|
+
parent? && !parent_singleton? ? parent_model.find(parent_param) : nil
|
58
|
+
end
|
57
59
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
60
|
+
# If there is a parent, returns the relevant association proxy. Otherwise returns model.
|
61
|
+
#
|
62
|
+
def end_of_association_chain
|
63
|
+
parent? ? parent_association : model
|
64
|
+
end
|
62
65
|
end
|
66
|
+
end
|
63
67
|
end
|
@@ -2,59 +2,63 @@
|
|
2
2
|
#
|
3
3
|
# Used internally to transform a plural RESTful controller into a singleton
|
4
4
|
#
|
5
|
-
module ResourceController
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
5
|
+
module ResourceController
|
6
|
+
module Helpers
|
7
|
+
module SingletonCustomizations
|
8
|
+
def self.included(subclass)
|
9
|
+
subclass.class_eval do
|
10
|
+
methods_to_undefine = [:param, :index, :collection, :load_collection, :collection_url,
|
11
|
+
:collection_path, :hash_for_collection_url, :hash_for_collection_path]
|
12
|
+
methods_to_undefine.each { |method| undef_method(method) if method_defined? method }
|
11
13
|
|
12
|
-
|
13
|
-
|
14
|
-
|
14
|
+
class << self
|
15
|
+
def singleton?
|
16
|
+
true
|
17
|
+
end
|
18
|
+
end
|
15
19
|
end
|
16
20
|
end
|
17
|
-
end
|
18
|
-
end
|
19
21
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
22
|
+
protected
|
23
|
+
# Used to fetch the current object in a singleton controller.
|
24
|
+
#
|
25
|
+
# By defult this method is able to fetch the current object for resources nested with the :has_one association only. (i.e. /users/1/image # => @user.image)
|
26
|
+
# In other cases you should override this method and provide your custom code to fetch a singleton resource object, like using a session hash.
|
27
|
+
#
|
28
|
+
# class AccountsController < ResourceController::Singleton
|
29
|
+
# private
|
30
|
+
# def object
|
31
|
+
# @object ||= Account.find(session[:account_id])
|
32
|
+
# end
|
33
|
+
# end
|
34
|
+
#
|
35
|
+
def object
|
36
|
+
@object ||= parent? ? end_of_association_chain : nil
|
37
|
+
end
|
36
38
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
39
|
+
# Returns the :has_one association proxy of the parent. (i.e. /users/1/image # => @user.image)
|
40
|
+
#
|
41
|
+
def parent_association
|
42
|
+
@parent_association ||= parent_object.send(model_name.to_sym)
|
43
|
+
end
|
42
44
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
45
|
+
# Used internally to provide the options to smart_url in a singleton controller.
|
46
|
+
#
|
47
|
+
def object_url_options(action_prefix = nil, alternate_object = nil)
|
48
|
+
[action_prefix] + namespaces + [parent_url_options, route_name.to_sym]
|
49
|
+
end
|
48
50
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
51
|
+
# Builds the object, but doesn't save it, during the new, and create action.
|
52
|
+
#
|
53
|
+
def build_object
|
54
|
+
@object ||= singleton_build_object_base.send parent? ? "build_#{model_name}".to_sym : :new, object_params
|
55
|
+
end
|
54
56
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
57
|
+
# Singleton controllers don't build off of association proxy, so we can't use end_of_association_chain here
|
58
|
+
#
|
59
|
+
def singleton_build_object_base
|
60
|
+
parent? ? parent_object : model
|
61
|
+
end
|
59
62
|
end
|
63
|
+
end
|
60
64
|
end
|