Skip to content

Instantly share code, notes, and snippets.

@lutter
Created Sep 20, 2012
Embed
What would you like to do?
Deltacloud: Correct index/show routes for collections where singular and plural name are identical
diff --git a/server/lib/deltacloud/helpers/rabbit_helper.rb b/server/lib/deltacloud/helpers/rabbit_helper.rb
index ef8080f..11dda20 100644
--- a/server/lib/deltacloud/helpers/rabbit_helper.rb
+++ b/server/lib/deltacloud/helpers/rabbit_helper.rb
@@ -64,18 +64,43 @@ module Sinatra::Rabbit
helper_method_name += '_url'
[Proc.new do
- define_method helper_method_name do |*args|
- if (opts = args.first).kind_of? Hash
- path = operation.full_path.convert_query_params(opts)
- elsif !args.empty? and (obj_id = args.first)
- path = operation.full_path.convert_query_params(:id => obj_id)
- else
- path = operation.full_path
- end
- path.slice!(root_url)
- url(path)
- end unless respond_to?(helper_method_name)
+ if ['index', 'show'].include?(operation_name)
+ define_method helper_method_name do |*args|
+ index_or_show_url_for(collection, args)
+ end
+ else
+ define_method helper_method_name do |*args|
+ operation_url_for(operation, args)
+ end
+ end
+ end unless respond_to?(helper_method_name)
end, helper_method_name]
end
+ private
+ def self.operation_url_for(operation, args)
+ if (opts = args.first).kind_of? Hash
+ path = operation.full_path.convert_query_params(opts)
+ elsif !args.empty? and (obj_id = args.first)
+ path = operation.full_path.convert_query_params(:id => obj_id)
+ else
+ path = operation.full_path
+ end
+ path.slice!(root_url)
+ url(path)
+ end
+
+ # Depending on whether +args+ is empty or not, generate the index route
+ # or the show route. This is so that for collections whose singular and
+ # plural name are identical (e.g. 'data'), we can use data_url for the
+ # index route, and data_url(id) for the show route
+ def self.index_or_show_url_for(collection, args)
+ opname = args.empty? ? :index : :show
+ path = collection.operation(opname).full_path
+ if obj_id = args.first
+ path = path.convert_query_params(:id => obj_id)
+ end
+ path.slice!(root_url)
+ url(path)
+ end
end
# server/lib/deltacloud/helpers/rabbit_helper.rb
module Sinatra::Rabbit
module URLHelper
def url_for(path); url(path); end
def root_url; settings.root_url; end
def base_uri; url_for('/').gsub(/\/$/,''); end
end
def self.URLFor(collections)
collections.each do |c|
c.operations.each do |operation|
URLHelper.instance_eval(&generate_url_helper_for(c, operation)[0])
end
end
URLHelper
end
def self.generate_url_helper_for(collection, operation)
operation_name = operation.operation_name.to_s
collection_name = collection.collection_name.to_s
# Construct OPERATION_COLLECTION_URL helper
# The :index and :create operation does not get any prefix
#
helper_method_name = case operation_name
when 'index' then collection_name
when 'show' then collection_name.singularize
else operation_name + '_' + collection_name.singularize
end
helper_method_name += '_url'
[Proc.new do
if ['index', 'show'].include?(operation_name)
define_method helper_method_name do |*args|
index_or_show_url_for(collection, args)
end
else
define_method helper_method_name do |*args|
operation_url_for(operation, args)
end
end
end unless respond_to?(helper_method_name)
end, helper_method_name]
end
private
def self.operation_url_for(operation, args)
if (opts = args.first).kind_of? Hash
path = operation.full_path.convert_query_params(opts)
elsif !args.empty? and (obj_id = args.first)
path = operation.full_path.convert_query_params(:id => obj_id)
else
path = operation.full_path
end
path.slice!(root_url)
url(path)
end
# Depending on whether +args+ is empty or not, generate the index route
# or the show route. This is so that for collections whose singular and
# plural name are identical (e.g. 'data'), we can use data_url for the
# index route, and data_url(id) for the show route
def self.index_or_show_url_for(collection, args)
opname = args.empty? ? :index : :show
path = collection.operation(opname).full_path
if obj_id = args.first
path = path.convert_query_params(:id => obj_id)
end
path.slice!(root_url)
url(path)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment