Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
42 changed files
with
605 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class GrumblersController < ApplicationController | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
class GrumblesController < ApplicationController | ||
before_filter :load_target | ||
|
||
def index | ||
grumbles = @target.grumbles.map { |grumble| grumble_attributes(grumble) } | ||
render_json({:grumbles => grumbles}, :callback => 'getGrumbles') | ||
end | ||
|
||
def create | ||
grumble = @target.grumbles.build(params[:grumble].slice(:subject, :body, :anon_grumbler_name)) | ||
if grumble.save | ||
#status(201) | ||
render_json({:grumble => grumble_attributes(grumble)}, :callback => 'grumbleCreated', :status => :created) | ||
else | ||
throw(:halt, [406, [:invalid_record, grumble]]) | ||
end | ||
end | ||
|
||
private | ||
|
||
def grumble_attributes(grumble) | ||
grumble_data = {:id => grumble.uuid, :grumble_url => grumble_url(:id => grumble, :target_id => grumble.target), | ||
:grumbler_name => grumble.grumbler_name, :subject => grumble.subject, :body => grumble.body, | ||
:created_at => grumble.created_at} | ||
grumble_data[:grumbler_url] = grumbler_url(grumble.grumbler) if grumble.grumbler | ||
grumble_data | ||
end | ||
|
||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class TargetsController < ApplicationController | ||
before_filter :load_target | ||
rescue_from ActiveRecord::RecordNotFound, :with => :target_not_found | ||
rescue_from ActiveRecord::RecordInvalid, :with => :target_invalid | ||
|
||
def show | ||
render_json({:target => {:grumble_count => @target.grumbles.count.to_i, | ||
:new_grumble_url => new_grumble_url(:target_id => @target), | ||
:grumble_index_url => grumbles_url(:target_id => @target)}}, | ||
:callback => 'getTarget') | ||
end | ||
|
||
private | ||
|
||
def target_not_found | ||
render :nothing => true, :status => :not_found | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module GrumblersHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module GrumblesHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module TargetsHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,10 @@ def registered_grumbler? | |
!grumbler.nil? | ||
end | ||
|
||
def to_param | ||
uuid | ||
end | ||
|
||
private | ||
|
||
def set_uuid | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require 'ruby-debug' if Rails.env.test? || Rails.env.development? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#ActionController::Routing::SEPARATORS.delete('.') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
require 'test_helper' | ||
|
||
class GrumblersControllerTest < ActionController::TestCase | ||
# Replace this with your real tests. | ||
test "the truth" do | ||
assert true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
require 'test_helper' | ||
|
||
class GrumblesControllerTest < ActionController::TestCase | ||
|
||
should "call a callback function if callback=true parameter is given" do | ||
get :index, :target_id => "http://www.example.com/posts/12/", :callback => "true" | ||
assert_match %r[Grumble.getGrumbles\(.*\)], @response.body | ||
end | ||
|
||
context "GET request with valid target" do | ||
setup do | ||
@target = Factory(:target) | ||
json_response { get :index, :target_id => @target.uri } | ||
end | ||
|
||
should "return a JSON list of grumbles" do | ||
assert_equal @target.grumbles.size, @json_response['grumbles'].size | ||
end | ||
|
||
context "returned grumble" do | ||
context "by a non registered grumbler" do | ||
setup do | ||
grumble = @target.grumbles.detect { |g| !g.registered_grumbler? } | ||
@json_grumble = @json_response['grumbles'].detect { |g| g['id'] == grumble.uuid } | ||
end | ||
|
||
should "have a grumble_url" do | ||
deny @json_grumble['grumble_url'].blank? | ||
end | ||
|
||
should "have a grumbler_name" do | ||
deny @json_grumble['grumbler_name'].blank? | ||
end | ||
|
||
should "not have a grumbler_url" do | ||
assert @json_grumble['grumbler_url'].blank? | ||
end | ||
|
||
should "have a subject" do | ||
deny @json_grumble['subject'].blank? | ||
end | ||
|
||
should "have a body" do | ||
deny @json_grumble['body'].blank? | ||
end | ||
|
||
should "have a created_at" do | ||
deny @json_grumble['created_at'].blank? | ||
end | ||
|
||
end # by a non registered grumbler | ||
|
||
context "by a registered grumbler" do | ||
setup do | ||
grumble = @target.grumbles.detect(&:registered_grumbler?) | ||
@json_grumble = @json_response['grumbles'].detect { |g| g['id'] == grumble.uuid } | ||
end | ||
|
||
should "have a grumble_url" do | ||
deny @json_grumble['grumble_url'].blank? | ||
end | ||
|
||
should "have a grumbler_name" do | ||
deny @json_grumble['grumbler_name'].blank? | ||
end | ||
|
||
should "have a grumbler_url" do | ||
deny @json_grumble['grumbler_url'].blank? | ||
end | ||
|
||
should "have a subject" do | ||
deny @json_grumble['subject'].blank? | ||
end | ||
|
||
should "have a body" do | ||
deny @json_grumble['body'].blank? | ||
end | ||
|
||
should "have a created_at" do | ||
deny @json_grumble['created_at'].blank? | ||
end | ||
|
||
end # by a registered grumbler | ||
|
||
end # returned grumbles | ||
|
||
end # GET | ||
|
||
context "POST request with valid target" do | ||
|
||
context "with valid grumble attributes and a brand new target" do | ||
setup do | ||
@target = Factory.build(:target) | ||
grumble_attrs = Factory.attributes_for(:grumble) | ||
json_response { post :create, :target_id => @target.uri, :grumble => grumble_attrs } | ||
@json_grumble = @json_response['grumble'] | ||
end | ||
|
||
should_change "Target.count", :by => 1 | ||
should_change "Grumble.count", :by => 1 | ||
|
||
should "create a target with the URI specified" do | ||
assert Target.find_by_uri(@target.uri) | ||
end | ||
|
||
end # with valid grumble attributes and a brand new target | ||
|
||
setup do | ||
@target = Factory(:target) | ||
end | ||
|
||
context "by a non registered user" do | ||
|
||
should "call a callback function if callback=true parameter is given" do | ||
grumble_attrs = Factory.attributes_for(:grumble) | ||
post :create, :target_id => @target.uri, :grumble => grumble_attrs, :callback => 'true' | ||
assert_match %r[Grumble.grumbleCreated\(.*\)], @response.body | ||
end | ||
|
||
context "with valid grumble attributes" do | ||
setup do | ||
grumble_attrs = Factory.attributes_for(:grumble) | ||
json_response { post :create, :target_id => @target.uri, :grumble => grumble_attrs } | ||
@json_grumble = @json_response['grumble'] | ||
end | ||
|
||
should "respond with 201 created" do | ||
assert_response :created | ||
end | ||
|
||
should "have a grumble_url" do | ||
deny @json_grumble['grumble_url'].blank? | ||
end | ||
|
||
should "have a grumbler_name" do | ||
deny @json_grumble['grumbler_name'].blank? | ||
end | ||
|
||
should "not have a grumbler_url" do | ||
assert @json_grumble['grumbler_url'].blank? | ||
end | ||
|
||
should "have a subject" do | ||
deny @json_grumble['subject'].blank? | ||
end | ||
|
||
should "have a body" do | ||
deny @json_grumble['body'].blank? | ||
end | ||
|
||
should "have a created_at" do | ||
deny @json_grumble['created_at'].blank? | ||
end | ||
end # with valid grumble attributes | ||
|
||
end # by a non registered user | ||
|
||
end # POST request with valid target | ||
|
||
end |
Oops, something went wrong.