Skip to content

Commit

Permalink
Make custom_urls visible at the class level
Browse files Browse the repository at this point in the history
And a test for class/instance URL visibility
  • Loading branch information
clayallsopp committed May 20, 2012
1 parent 283a31a commit 61ac785
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/remote_model.rb
Expand Up @@ -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),
Expand Down
35 changes: 35 additions & 0 deletions spec/remote_model_spec.rb
@@ -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.