From 3c41b2c89a1fac58fefcf0034cac9d6b9c6211b3 Mon Sep 17 00:00:00 2001 From: David James Date: Tue, 13 Oct 2009 22:03:12 -0400 Subject: [PATCH] Store associated resource in config --- lib/rest-sinatra.rb | 5 +++++ spec/unit/posts_and_comments_spec.rb | 8 ++++++++ spec/unit/sources_spec.rb | 4 ++++ 3 files changed, 17 insertions(+) diff --git a/lib/rest-sinatra.rb b/lib/rest-sinatra.rb index 707eb96..1fe244d 100644 --- a/lib/rest-sinatra.rb +++ b/lib/rest-sinatra.rb @@ -20,6 +20,7 @@ def nestable_resource(name, &block) def _resource(name, resource_type, &block) config = evaluate_block(name, resource_type, &block) + config[:resource] = self validate(config) build_resource(config) config @@ -34,6 +35,7 @@ def evaluate_block(name, resource_type, &block) :resource_type => resource_type, :model => nil, :read_only => [], + :resource => nil, :permission => nil, :callbacks => {}, :nested_resources => [] @@ -70,6 +72,7 @@ def build_parent_resource(config) model = config[:model] name = config[:name] read_only = config[:read_only] + resource = config[:resource] get '/?' do require_at_least(:basic) @@ -154,11 +157,13 @@ def build_nested_resource(klass, association, parent_config, child_config) callbacks = child_config[:callbacks] child_model = child_config[:model] child_name = child_config[:name] + child_resource = child_config[:resource] permission = child_config[:permission] read_only = child_config[:read_only] parent_model = parent_config[:model] parent_name = parent_config[:name] + parent_resource = parent_config[:resource] get "/:parent_id/#{child_name}/?" do parent_id = params.delete("parent_id") diff --git a/spec/unit/posts_and_comments_spec.rb b/spec/unit/posts_and_comments_spec.rb index 37da1f7..2f2b882 100644 --- a/spec/unit/posts_and_comments_spec.rb +++ b/spec/unit/posts_and_comments_spec.rb @@ -17,6 +17,10 @@ @config[:model].should == Post end + it "resource should be Posts" do + @config[:resource].should == Posts + end + it "read_only should be correct" do @config[:read_only].should == [:created_at, :updated_at] end @@ -133,6 +137,10 @@ @config[:model].should == Comment end + it "model should be Comments" do + @config[:resource].should == Comments + end + it "read_only should be correct" do @config[:read_only].should == [:created_at] end diff --git a/spec/unit/sources_spec.rb b/spec/unit/sources_spec.rb index 373f32a..fe328c4 100644 --- a/spec/unit/sources_spec.rb +++ b/spec/unit/sources_spec.rb @@ -15,6 +15,10 @@ it "model should be Source" do @config[:model].should == Source end + + it "resource should be Sources" do + @config[:resource].should == Sources + end it "read_only should be empty" do @config[:read_only].should == []