Skip to content

Instantly share code, notes, and snippets.

@jlsync
Created February 26, 2009 20:51
Show Gist options
  • Save jlsync/71099 to your computer and use it in GitHub Desktop.
Save jlsync/71099 to your computer and use it in GitHub Desktop.
diff --git a/vendor/plugins/resources_controller/lib/ardes/resources_controller.rb b/vendor/plugins/resources_controller/lib/ardes/resources_co
index 7bd2b61..06affe6 100644
--- a/vendor/plugins/resources_controller/lib/ardes/resources_controller.rb
+++ b/vendor/plugins/resources_controller/lib/ardes/resources_controller.rb
@@ -110,11 +110,11 @@ module Ardes#:nodoc:
#
# You may have a named route that maps a url to a particular controller and action,
# this causes resources_controller problems as it relies on the route to load the
- # resources. You can get around this by specifying :erp as a param in routes.rb
+ # resources. You can get around this by specifying :resource_path as a param in routes.rb
#
- # map.home '', :controller => :forums, :action => :index, :resource_route => '/forums'
+ # map.root :controller => :forums, :action => :index, :resource_path => '/forums'
#
- # When the controller is invoked via the '' url, rc will use :erp to recognize the
+ # When the controller is invoked via the '' url, rc will use :resource_path to recognize the
# route.
#
# ==== Putting it all together
@@ -138,7 +138,7 @@ module Ardes#:nodoc:
# forum.resource :image
# end
#
- # map.home '', :controller => :forums, :action => :index, :erp => '/forums'
+ # map.root :controller => :forums, :action => :index, :resource_path => '/forums'
#
# app/controllers:
#
@@ -484,25 +484,12 @@ module Ardes#:nodoc:
map_enclosing_resource(*args, &block)
end
- # Include the specified module, optionally specifying which public methods to include
- #
- # eg
- #
- # include_actions ActionMixin, :only => :index
- # include_actions ActionMixin, :except => [:create, :new]
+ # Include the specified module, optionally specifying which public methods to include, for example:
+ # include_actions ActionMixin, :only => :index
+ # include_actions ActionMixin, :except => [:create, :new]
def include_actions(mixin, options = {})
- options.assert_valid_keys(:only, :except)
- raise ArgumentError, "you can only specify either :except or :only, not both" if options[:only] && options[:except]
-
- mixin = mixin.dup
- if only = options[:only]
- only = Array(options[:only]).collect(&:to_s)
- mixin.instance_methods.each {|m| mixin.send(:undef_method, m) unless only.include?(m)}
- elsif except = options[:except]
- except = Array(options[:except]).collect(&:to_s)
- mixin.instance_methods.each {|m| mixin.send(:undef_method, m) if except.include?(m)}
- end
- include mixin
+ mixin.extend(IncludeActions) unless mixin.respond_to?(:include_actions)
+ mixin.include_actions(self, options)
end
private
@@ -641,7 +628,7 @@ module Ardes#:nodoc:
# returns the name of the immediately enclosing resource
def enclosing_resource_name
- enclosing_resource.class.name.underscore
+ @enclosing_resource_name
end
# returns the resource service for the controller - this will be lazilly created
@@ -665,15 +652,28 @@ module Ardes#:nodoc:
@enclosing_collection_resources ||= []
end
- # Returns self.resource.save and caches the result for future calls.
- # This is useful when you want to know outside of an action whether the resource was saved.
+ # NOTE: This method is overly complicated and unecessary. It's much clearer just to keep
+ # track of record saves yourself, this is here for BC. For an example of how it should be
+ # done look at the actions module in http://github.com/ianwhite/response_for_rc
+ #
+ # Has the resource been saved successfully?, if no save has been attempted, save the
+ # record and return the result
#
- # Pass true to ignore the cached value
- def resource_saved?(reload = false)
- save_resource if reload || @resource_saved.nil?
+ # This method uses the @resource_saved tracking var, or the model's state itself if
+ # that is not available (which means if you do resource.update_attributes, then this
+ # method will return the correct result)
+ def resource_saved?
+ save_resource if @resource_saved.nil? && !resource.validation_attempted?
+ @resource_saved = resource.saved? if @resource_saved.nil?
@resource_saved
end
+ # NOTE: it's clearer to just keep track of record saves yourself, this is here for BC
+ # See the comment on #resource_saved?
+ #
+ # @resource_saved = resource.update_attributes(params[resource_name])
+ #
+ # Save the resource, and keep track of the result
def save_resource
@resource_saved = resource.save
end
@@ -733,6 +733,9 @@ module Ardes#:nodoc:
end
end
end
+ rescue MissingSegment
+ # fallback: construct enclosing names from param ids
+ @route_enclosing_names = params.keys.select{|k| k.to_s =~ /_id$/}.map{|id| [id.sub('_id','').pluralize, false]}
end
# this is the before_filter that loads all specified and wildcard resources
@@ -784,14 +787,20 @@ module Ardes#:nodoc:
def load_enclosing_resource_from_specification(spec)
spec.segment == route_enclosing_names[enclosing_resources.size].first or ResourcesController.raise_resource_mismatch(self)
returning spec.find_from(self) do |resource|
- update_name_prefix(spec.name_prefix)
- enclosing_resources << resource
- enclosing_collection_resources << resource unless spec.singleton?
- instance_variable_set("@#{spec.name}", resource)
- instance_variable_set("@#{spec.as}", resource) if spec.as
+ add_enclosing_resource(resource, :name => spec.name, :name_prefix => spec.name_prefix, :is_singleton => spec.singleton?, :as => spec
end
end
-
+
+ def add_enclosing_resource(resource, options = {})
+ name = options[:name] || resource.class.name.underscore
+ update_name_prefix(options[:name_prefix] || (options[:name_prefix] == false ? '' : "#{name}_"))
+ enclosing_resources << resource
+ enclosing_collection_resources << resource unless options[:is_singleton]
+ instance_variable_set("@enclosing_resource_name", options[:name])
+ instance_variable_set("@#{name}", resource)
+ instance_variable_set("@#{options[:as]}", resource) if options[:as]
+ end
+
# The name prefix is used for forwarding urls and will be different depending on
# which route the controller was invoked by. The resource specifications build
# up the name prefix as the resources are loaded.
@@ -818,7 +827,7 @@ module Ardes#:nodoc:
resource_specification.find ? resource_specification.find_custom(controller) : super
end
- def respond_to?(method)
+ def respond_to?(method, include_private = false)
super || service.respond_to?(method)
end
diff --git a/vendor/plugins/resources_controller/lib/ardes/resources_controller/actions.rb b/vendor/plugins/resources_controller/lib/ardes/reso
index 24364d3..a9729e8 100644
--- a/vendor/plugins/resources_controller/lib/ardes/resources_controller/actions.rb
+++ b/vendor/plugins/resources_controller/lib/ardes/resources_controller/actions.rb
@@ -93,9 +93,9 @@ module Ardes#:nodoc:
# POST /events.xml
def create
self.resource = new_resource
-
+
respond_to do |format|
- if resource_saved?
+ if resource.save
format.html do
flash[:notice] = "#{resource_name.humanize} was successfully created."
redirect_to resource_url
@@ -114,10 +114,9 @@ module Ardes#:nodoc:
# PUT /events/1.xml
def update
self.resource = find_resource
- resource.attributes = params[resource_name]
-
+
respond_to do |format|
- if resource_saved?
+ if resource.update_attributes(params[resource_name])
format.html do
flash[:notice] = "#{resource_name.humanize} was successfully updated."
redirect_to resource_url
diff --git a/vendor/plugins/resources_controller/lib/ardes/resources_controller/helper.rb b/vendor/plugins/resources_controller/lib/ardes/resou
index 69e03f5..a49edb6 100644
--- a/vendor/plugins/resources_controller/lib/ardes/resources_controller/helper.rb
+++ b/vendor/plugins/resources_controller/lib/ardes/resources_controller/helper.rb
@@ -101,8 +101,8 @@ module Ardes#:nodoc:
end
# delegate url help method creation to the controller
- def respond_to_with_named_route_helper?(method)
- respond_to_without_named_route_helper?(method) || controller.resource_named_route_helper_method?(method)
+ def respond_to_with_named_route_helper?(*args)
+ respond_to_without_named_route_helper?(*args) || controller.resource_named_route_helper_method?(args.first)
end
private
diff --git a/vendor/plugins/resources_controller/lib/ardes/resources_controller/named_route_helper.rb b/vendor/plugins/resources_controller/lib
index d5f4172..1ac070a 100644
--- a/vendor/plugins/resources_controller/lib/ardes/resources_controller/named_route_helper.rb
+++ b/vendor/plugins/resources_controller/lib/ardes/resources_controller/named_route_helper.rb
@@ -38,7 +38,7 @@ module Ardes#:nodoc:
alias_method_chain :respond_to?, :named_route_helper
end
base.hide_action *instance_methods
- base.hide_action :method_missing_without_named_route_helper, :respond_to_without_named_route_helper?
+ base.hide_action :method_missing_without_named_route_helper, :respond_to_without_named_route_helper?, :respond_to?
end
def method_missing_with_named_route_helper(method, *args, &block)
@@ -54,8 +54,8 @@ module Ardes#:nodoc:
end
end
- def respond_to_with_named_route_helper?(method)
- respond_to_without_named_route_helper?(method) || resource_named_route_helper_method?(method)
+ def respond_to_with_named_route_helper?(*args)
+ respond_to_without_named_route_helper?(*args) || resource_named_route_helper_method?(args.first)
end
# return true if the passed method (e.g. 'resources_path') corresponds to a defined
@@ -124,7 +124,7 @@ Currently:
name_prefix = method.to_s.sub(/^.*_for_/,'')
if resource_method =~ /enclosing_resource/
route, route_method = *route_and_method_from_enclosing_resource_method_and_name_prefix(resource_method, name_prefix)
- required_args = route.significant_keys.reject{|k| [:controller, :action].include?(k)}.size
+ required_args = (route.significant_keys - [:controller, :action, :format]).size
self.class.send :module_eval, <<-end_eval, __FILE__, __LINE__
def #{method}(*args)
@@ -137,7 +137,7 @@ Currently:
else
route, route_method = *route_and_method_from_resource_method_and_name_prefix(resource_method, name_prefix)
- required_args = route.significant_keys.reject{|k| [:controller, :action].include?(k)}.size
+ required_args = (route.significant_keys - [:controller, :action, :format]).size
self.class.send :module_eval, <<-end_eval, __FILE__, __LINE__
def #{method}(*args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment