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.
@@ -1,69 +1,73 @@
1
- module ResourceController::Helpers::CurrentObjects
2
- protected
3
- # Used internally to return the model for your resource.
4
- #
5
- def model
6
- model_name.to_s.camelize.constantize
7
- end
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
- # Used to fetch the collection for the index method
11
- #
12
- # In order to customize the way the collection is fetched, to add something like pagination, for example, override this method.
13
- #
14
- def collection
15
- end_of_association_chain.find(:all)
16
- end
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
- # Returns the current param.
19
- #
20
- # Defaults to params[:id].
21
- #
22
- # Override this method if you'd like to use an alternate param name.
23
- #
24
- def param
25
- params[:id]
26
- end
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
- # Used to fetch the current member object in all of the singular methods that operate on an existing member.
29
- #
30
- # Override this method if you'd like to fetch your objects in some alternate way, like using a permalink.
31
- #
32
- # class PostsController < ResourceController::Base
33
- # private
34
- # def object
35
- # @object ||= end_of_association_chain.find_by_permalink(param)
36
- # end
37
- # end
38
- #
39
- def object
40
- @object ||= end_of_association_chain.find(param) unless param.nil?
41
- @object
42
- end
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
- # Used internally to load the member object in to an instance variable @#{model_name} (i.e. @post)
45
- #
46
- def load_object
47
- instance_variable_set "@#{parent_type}", parent_object if parent?
48
- instance_variable_set "@#{object_name}", object
49
- end
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
- # Used internally to load the collection in to an instance variable @#{model_name.pluralize} (i.e. @posts)
52
- #
53
- def load_collection
54
- instance_variable_set "@#{parent_type}", parent_object if parent?
55
- instance_variable_set "@#{object_name.to_s.pluralize}", collection
56
- end
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
- # Returns the form params. Defaults to params[model_name] (i.e. params["post"])
59
- #
60
- def object_params
61
- params["#{object_name}"]
62
- end
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
- # Builds the object, but doesn't save it, during the new, and create action.
65
- #
66
- def build_object
67
- @object ||= end_of_association_chain.send parent? ? :build : :new, object_params
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::Helpers::Internal
6
- protected
7
- # Used to actually pass the responses along to the controller's respond_to method.
8
- #
9
- def response_for(action)
10
- respond_to do |wants|
11
- options_for(action).response.each do |method, block|
12
- if block.nil?
13
- wants.send(method)
14
- else
15
- wants.send(method) { instance_eval(&block) }
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
- end
19
- end
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
- # Calls the after callbacks for the action, if one is present.
22
- #
23
- def after(action)
24
- invoke_callbacks *options_for(action).after
25
- end
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
- # Calls the before block for the action, if one is present.
28
- #
29
- def before(action)
30
- invoke_callbacks *self.class.send(action).before
31
- end
32
-
33
- # Sets the flash and flash_now for the action, if it is present.
34
- #
35
- def set_flash(action)
36
- set_normal_flash(action)
37
- set_flash_now(action)
38
- end
39
-
40
- # Sets the regular flash (i.e. flash[:notice] = '...')
41
- #
42
- def set_normal_flash(action)
43
- if f = options_for(action).flash
44
- flash[:notice] = f.is_a?(Proc) ? instance_eval(&f) : options_for(action).flash
45
- end
46
- end
47
-
48
- # Sets the flash.now (i.e. flash.now[:notice] = '...')
49
- #
50
- def set_flash_now(action)
51
- if f = options_for(action).flash_now
52
- flash.now[:notice] = f.is_a?(Proc) ? instance_eval(&f) : options_for(action).flash_now
53
- end
54
- end
55
-
56
- # Returns the options for an action, which is a symbol.
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
- options
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::Helpers::Nested
4
- protected
5
- # Returns the relevant association proxy of the parent. (i.e. /posts/1/comments # => @post.comments)
6
- #
7
- def parent_association
8
- @parent_association ||= parent_object.send(model_name.to_s.pluralize.to_sym)
9
- end
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
- # Returns the type of the current parent
12
- #
13
- def parent_type
14
- @parent_type ||= parent_type_from_params || parent_type_from_request
15
- end
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
- # Returns the type of the current parent extracted from params
18
- #
19
- def parent_type_from_params
20
- [*belongs_to].find { |parent| !params["#{parent}_id".to_sym].nil? }
21
- end
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
- # Returns the type of the current parent extracted form a request path
24
- #
25
- def parent_type_from_request
26
- [*belongs_to].find { |parent| request.path.split('/').include? parent.to_s }
27
- end
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
- # Returns true/false based on whether or not a parent is present.
30
- #
31
- def parent?
32
- !parent_type.nil?
33
- end
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
- # Returns true/false based on whether or not a parent is a singleton.
36
- #
37
- def parent_singleton?
38
- !parent_type_from_request.nil?
39
- end
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
- # Returns the current parent param, if there is a parent. (i.e. params[:post_id])
42
- def parent_param
43
- params["#{parent_type}_id".to_sym]
44
- end
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
- # Like the model method, but for a parent relationship.
47
- #
48
- def parent_model
49
- parent_type.to_s.camelize.constantize
50
- end
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
- # Returns the current parent object if a parent object is present.
53
- #
54
- def parent_object
55
- parent? && !parent_singleton? ? parent_model.find(parent_param) : nil
56
- end
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
- # If there is a parent, returns the relevant association proxy. Otherwise returns model.
59
- #
60
- def end_of_association_chain
61
- parent? ? parent_association : model
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::Helpers::SingletonCustomizations
6
- def self.included(subclass)
7
- subclass.class_eval do
8
- methods_to_undefine = [:param, :index, :collection, :load_collection, :collection_url,
9
- :collection_path, :hash_for_collection_url, :hash_for_collection_path]
10
- methods_to_undefine.each { |method| undef_method(method) if method_defined? method }
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
- class << self
13
- def singleton?
14
- true
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
- protected
21
- # Used to fetch the current object in a singleton controller.
22
- #
23
- # 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)
24
- # In other cases you should override this method and provide your custom code to fetch a singleton resource object, like using a session hash.
25
- #
26
- # class AccountsController < ResourceController::Singleton
27
- # private
28
- # def object
29
- # @object ||= Account.find(session[:account_id])
30
- # end
31
- # end
32
- #
33
- def object
34
- @object ||= parent? ? end_of_association_chain : nil
35
- end
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
- # Returns the :has_one association proxy of the parent. (i.e. /users/1/image # => @user.image)
38
- #
39
- def parent_association
40
- @parent_association ||= parent_object.send(model_name.to_sym)
41
- end
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
- # Used internally to provide the options to smart_url in a singleton controller.
44
- #
45
- def object_url_options(action_prefix = nil, alternate_object = nil)
46
- [action_prefix] + namespaces + [parent_url_options, route_name.to_sym]
47
- end
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
- # Builds the object, but doesn't save it, during the new, and create action.
50
- #
51
- def build_object
52
- @object ||= singleton_build_object_base.send parent? ? "build_#{model_name}".to_sym : :new, object_params
53
- end
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
- # Singleton controllers don't build off of association proxy, so we can't use end_of_association_chain here
56
- #
57
- def singleton_build_object_base
58
- parent? ? parent_object : model
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