Skip to content

Commit

Permalink
Implemented Cards Controller
Browse files Browse the repository at this point in the history
  • Loading branch information
amirci committed Jul 19, 2011
1 parent 370e19b commit e3fda6b
Show file tree
Hide file tree
Showing 19 changed files with 128 additions and 24 deletions.
3 changes: 3 additions & 0 deletions app/assets/javascripts/cards.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
7 changes: 7 additions & 0 deletions app/assets/stylesheets/cards.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Place all the styles related to the Cards controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/

body.cards {
// Place scoped styles here
}
13 changes: 13 additions & 0 deletions app/controllers/cards_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CardsController < ApplicationController
respond_to :json

def index
project = Project.find(params[:project_id])
if project
respond_with({cards: project.cards}, :status => :ok)
else
respond_with(@project.to_json, :status => 404)
end
end

end
2 changes: 2 additions & 0 deletions app/helpers/cards_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module CardsHelper
end
2 changes: 2 additions & 0 deletions app/models/card.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Card < ActiveRecord::Base
end
1 change: 1 addition & 0 deletions app/models/project.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
class Project < ActiveRecord::Base
has_many :cards, :dependent => :destroy
end
6 changes: 5 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Kanbanfu::Application.routes.draw do
resources :projects, :defaults => { :format => :json }
get "cards/index"

resources :projects, :defaults => { :format => :json } do
resources :cards
end

# The priority is based upon order of creation:
# first created -> highest priority.
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20110719214707_create_cards.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateCards < ActiveRecord::Migration
def change
create_table :cards do |t|
t.string :title
t.text :description
t.references :project

t.timestamps
end
end
end
10 changes: 9 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20110712145406) do
ActiveRecord::Schema.define(:version => 20110719214707) do

create_table "cards", :force => true do |t|
t.string "title"
t.text "description"
t.integer "project_id"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "projects", :force => true do |t|
t.string "name"
Expand Down
23 changes: 12 additions & 11 deletions features/cards.feature
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ Feature: Cards API
I want to use the API to manage cards
to administrate cards operations

@wip
Scenario: Listing cards for a project
Given I have the cards for project "1" named "Blazing Saddles":
| id | name | phase | description |
| 1 | Find a Sheriff | Analysis | Get a Sheriff everybody will hate |
| 2 | Kill the Sheriff | Working | Make the town kill the Sheriff |
| 3 | Take the town | Backlog | Make everybody sell their houses |
When I call API "/projects/1"
Then the JSON response should have 3 cards
And the JSON should have the following:
| 0 | {"id": 1, "name": "Blazing Saddles", "description": "To ruin a western town, a corrupt political boss appoints a black sheriff, who promptly becomes his most formidable adversary."} |
| 1 | {"id": 2, "name": "Spaceballs", "description": "Planet Spaceball's President Skroob sends Lord Dark Helmet to steal Planet Druidia's abundant supply of air to replenish their own, and only Lone Starr can stop them."} |
| 2 | {"id": 3, "name": "Young Frankenstein", "description": "Dr. Frankenstein's grandson, after years of living down the family reputation, inherits granddad's castle and repeats the experiments."} |
Given I have the cards for project "1":
| id | title | description |
| 1 | Find a Sheriff | Get a Sheriff everybody will hate |
| 2 | Kill the Sheriff | Make the town kill the Sheriff |
| 3 | Take the town | Make everybody sell their houses |
When I call API "/projects/1/cards"
Then the response should be a collection of "cards" with:
| id | title | description |
| 1 | Find a Sheriff | Get a Sheriff everybody will hate |
| 2 | Kill the Sheriff | Make the town kill the Sheriff |
| 3 | Take the town | Make everybody sell their houses |
1 change: 0 additions & 1 deletion features/projects.feature
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ Feature: Projects API
I want to call /projects
to get all the projects for account

@wip
Scenario: Listing projects
Given I have the projects:
| id | name | description |
Expand Down
7 changes: 7 additions & 0 deletions features/step_definitions/card_steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Given /^I have the cards for project "([^"]*)":$/ do |project_id, table|
project = Project.create(id: project_id, name: "Blazing Saddles", description: "Good movie")
table.hashes.each do |card|
project.cards.create(card)
end
end

7 changes: 7 additions & 0 deletions features/step_definitions/json_steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Then /^the response should be a collection of "(.+)" with:$/ do |root, table|
table.hashes.each do |row|
json = row.to_json
path = "#{root}/#{row['id'].to_i - 1}"
Then(%Q{the JSON at "#{path}" should be #{json}})
end
end
8 changes: 1 addition & 7 deletions features/step_definitions/projects_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,5 @@
projects = table.hashes.collect { |project| Project.create project }
end

Then /^the response should be a collection of "(.+)" with:$/ do |root, table|
table.hashes.each do |row|
json = row.to_json
path = "#{root}/#{row['id'].to_i - 1}"
Then(%Q{the JSON at "#{path}" should be #{json}})
end
end


25 changes: 25 additions & 0 deletions spec/controllers/cards_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'spec_helper'

describe CardsController do

before(:each) do
@project = stub_model(Project, id: 1, name: "Blazing Saddles", description: "Movie")
@cards = (1..10).collect {|i| stub_model(Card, :project_id => @project.id, :id => i, :title => "card_#{i}", :description => "description_#{i}") }
@project.stub(:cards).and_return(@cards)
end

describe "GET on 'index' via JSON" do
it "returns the collection of cards" do
Project.stub(:find).with(@project.id).and_return(@project)
get :index, :format => :json, :project_id => @project.id
response.body.should eq({cards: @cards}.to_json)
end

it "returns an error when the project does not exist" do
Project.stub(:find).with(@project.id).and_return(nil)
get :index, :format => :json, :project_id => @project.id
should respond_with 404
end
end

end
4 changes: 2 additions & 2 deletions spec/controllers/projects_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
end

describe 'GET on index via JSON' do
it 'renders the projects page' do
it 'returns the collection of projects' do
Project.stub(:all).and_return(@projects)
get :index, :format => :json
response.body.should eq(@projects.to_json)
response.body.should eq({projects: @projects}.to_json)
end
end

Expand Down
15 changes: 15 additions & 0 deletions spec/helpers/cards_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'spec_helper'

# Specs in this file have access to a helper object that includes
# the CardsHelper. For example:
#
# describe CardsHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# helper.concat_strings("this","that").should == "this that"
# end
# end
# end
describe CardsHelper do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions spec/models/card_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'spec_helper'

describe Card do
pending "add some examples to (or delete) #{__FILE__}"
end
2 changes: 1 addition & 1 deletion spec/models/project_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'spec_helper'

describe Project do
pending "add some examples to (or delete) #{__FILE__}"
it { should have_many(:cards).dependent(:destroy) }
end

0 comments on commit e3fda6b

Please sign in to comment.