Skip to content

Commit

Permalink
Store associated resource in config
Browse files Browse the repository at this point in the history
  • Loading branch information
djsun committed Oct 14, 2009
1 parent 5cf1ba6 commit 3c41b2c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/rest-sinatra.rb
Expand Up @@ -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
Expand All @@ -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 => []
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down
8 changes: 8 additions & 0 deletions spec/unit/posts_and_comments_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions spec/unit/sources_spec.rb
Expand Up @@ -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 == []
Expand Down

0 comments on commit 3c41b2c

Please sign in to comment.