Skip to content

Commit

Permalink
added support for the :if and :unless options
Browse files Browse the repository at this point in the history
  • Loading branch information
fabrik42 committed Apr 24, 2011
1 parent 3f18d34 commit 250e5eb
Show file tree
Hide file tree
Showing 5 changed files with 429 additions and 9 deletions.
21 changes: 17 additions & 4 deletions lib/acts_as_api/api_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,22 @@ def api_template_for(parent, item)

# Decides if the passed item should be added to
# the response.
def allowed_to_render?(parent, item)
# TODO implement :if, :unless options
true
def allowed_to_render?(parent, item, model)
return true unless parent.is_a? ActsAsApi::ApiTemplate
allowed = true
allowed = condition_fulfilled?(model, parent.option_for(item, :if)) if parent.option_for(item, :if)
allowed = !(condition_fulfilled?(model, parent.option_for(item, :unless))) if parent.option_for(item, :unless)
return allowed
end

def condition_fulfilled?(model, condition)
case condition
when Symbol
result = model.send(condition)
when Proc
result = condition.call(model)
end
!result.nil? && !result.is_a?(FalseClass)
end

# Generates a hash that represents the api response based on this
Expand All @@ -80,7 +93,7 @@ def to_response_hash(model)

leaf[:item].each do |k,v|

next unless allowed_to_render?(leaf[:item], k)
next unless allowed_to_render?(leaf[:item], k, model)

case v
when Symbol
Expand Down
9 changes: 4 additions & 5 deletions spec/controllers/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
get :index, :format => 'json', :api_template => :name_only
end

it "should have a root node named users" do
it "should have a root node named users" do
response_body_json.should have_key("users")
end

Expand All @@ -93,7 +93,7 @@
get :show, :format => 'json', :api_template => :name_only, :id => @luke.id
end

it "should have a root node named user" do
it "should have a root node named user" do
response_body_json.should have_key("user")
end

Expand Down Expand Up @@ -163,7 +163,7 @@
get :index, :format => 'json', :api_template => :name_only, :callback => @callback
end

it "should wrap the response in the callback" do
it "should wrap the response in the callback" do
response_body_jsonp(@callback).should_not be_nil
end

Expand All @@ -175,7 +175,7 @@
get :show, :format => 'json', :api_template => :name_only, :id => @luke.id, :callback => @callback
end

it "should wrap the response in the callback" do
it "should wrap the response in the callback" do
response_body_jsonp(@callback).should_not be_nil
end

Expand All @@ -184,5 +184,4 @@
end
end


end
178 changes: 178 additions & 0 deletions spec/models/base/conditional_if_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
require File.dirname(__FILE__) + '/../../spec_helper.rb'

describe ActsAsApi::Base do

describe "conditional statements", :orm => :active_record, :meow => true do

before(:each) do
setup_models
end

after(:each) do
clean_up
end

describe "using the :if option" do

describe "passing a symbol" do

describe "that returns false" do

before(:each) do
@response = @luke.as_api_response(:if_over_thirty)
end

it "returns a hash" do
@response.should be_kind_of(Hash)
end

it "returns the correct number of fields" do
@response.should have(1).keys
end

it "won't add the conditional field but all others" do
@response.keys.should include(:first_name)
@response.keys.should_not include(:full_name)
end

it "the other specified fields have the correct value" do
@response.values.should include(@luke.first_name)
end

end

describe "that returns nil" do

before(:each) do
@response = @luke.as_api_response(:if_returns_nil)
end

it "returns a hash" do
@response.should be_kind_of(Hash)
end

it "returns the correct number of fields" do
@response.should have(1).keys
end

it "won't add the conditional field but all others" do
@response.keys.should include(:first_name)
@response.keys.should_not include(:full_name)
end

it "the other specified fields have the correct value" do
@response.values.should include(@luke.first_name)
end

end

describe "that returns true" do

before(:each) do
@response = @han.as_api_response(:if_over_thirty)
end

it "returns a hash" do
@response.should be_kind_of(Hash)
end

it "returns the correct number of fields" do
@response.should have(2).keys
end

it "won't add the conditional field but all others" do
@response.keys.should include(:first_name)
@response.keys.should include(:last_name)
end

it "the other specified fields have the correct value" do
@response.values.should include(@han.first_name, @han.last_name)
end

end

end

end

describe "passing a proc" do

describe "that returns false" do

before(:each) do
@response = @luke.as_api_response(:if_over_thirty_proc)
end

it "returns a hash" do
@response.should be_kind_of(Hash)
end

it "returns the correct number of fields" do
@response.should have(1).keys
end

it "won't add the conditional field but all others" do
@response.keys.should include(:first_name)
@response.keys.should_not include(:full_name)
end

it "the other specified fields have the correct value" do
@response.values.should include(@luke.first_name)
end

end

describe "that returns nil" do

before(:each) do
@response = @luke.as_api_response(:if_returns_nil_proc)
end

it "returns a hash" do
@response.should be_kind_of(Hash)
end

it "returns the correct number of fields" do
@response.should have(1).keys
end

it "won't add the conditional field but all others" do
@response.keys.should include(:first_name)
@response.keys.should_not include(:full_name)
end

it "the other specified fields have the correct value" do
@response.values.should include(@luke.first_name)
end

end

describe "that returns true" do

before(:each) do
@response = @han.as_api_response(:if_over_thirty_proc)
end

it "returns a hash" do
@response.should be_kind_of(Hash)
end

it "returns the correct number of fields" do
@response.should have(2).keys
end

it "won't add the conditional field but all others" do
@response.keys.should include(:first_name)
@response.keys.should include(:last_name)
end

it "the other specified fields have the correct value" do
@response.values.should include(@han.first_name, @han.last_name)
end

end

end

end
end
Loading

0 comments on commit 250e5eb

Please sign in to comment.