Permalink
Browse files

Make custom_urls visible at the class level

And a test for class/instance URL visibility
  • Loading branch information...
1 parent 283a31a commit 61ac7855ea975aeee14b51d650852e1ae762d3d6 @clayallsopp committed May 20, 2012
Showing with 43 additions and 0 deletions.
  1. +8 −0 lib/remote_model.rb
  2. +35 −0 spec/remote_model_spec.rb
View
@@ -36,6 +36,14 @@ def pluralize
self.to_s.downcase + "s"
end
+ def method_missing(method, *args, &block)
+ if self.custom_urls.has_key? method
+ return self.custom_urls[method].format(args && args[0], self)
+ end
+
+ super
+ end
+
private
# This is kind of neat.
# Because models can be mutually dependent (User has a Question, Question has a User),
View
@@ -0,0 +1,35 @@
+class CustomUrlModel < RemoteModule::RemoteModel
+ collection_url "collection"
+ member_url "collection/:id"
+
+ custom_urls :a_url => "custom", :format_url => "custom/:var",
+ :method_url => "custom/:a_method"
+
+ def id
+ 8
+ end
+
+ def a_method
+ 10
+ end
+end
+
+describe "URLs" do
+ it "should make visible urls at class and instance level" do
+ CustomUrlModel.a_url.should == "custom"
+ CustomUrlModel.collection_url.should == "collection"
+ CustomUrlModel.member_url.should == "collection/:id"
+
+ # NOTE that Class.member_url(params) won't work (it's the setter).
+ CustomUrlModel.member_url.format(:id => 9).should == "collection/9"
+
+ c = CustomUrlModel.new
+ c.collection_url.should == "collection"
+ c.member_url.should == "collection/8"
+ c.a_url.should == "custom"
+
+ CustomUrlModel.format_url.should == "custom/:var"
+ c.format_url(:var => 3).should == "custom/3"
+ c.method_url.should == "custom/10"
+ end
+end

0 comments on commit 61ac785

Please sign in to comment.