Skip to content

Commit

Permalink
how to use boards, can close a board
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-biddington authored and jeremytregunna committed Jan 31, 2012
1 parent adcbc3e commit 03d6c24
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 15 deletions.
30 changes: 19 additions & 11 deletions lib/trello/board.rb
Expand Up @@ -14,21 +14,29 @@ def create(attributes)
end
end

# Update the fields of a board.
#
# Supply a hash of string keyed data retrieved from the Trello API representing
# a board.
def save!
fail "Cannot save new instance." unless self.id

Client.put("/boards/#{self.id}/", {
:name => @name,
:description => @description,
:closed => @closed,
:url => @url,
:organisation_id => @organisation_id
}).json_into(self)
end

def update_fields(fields)
@id = fields['id']
@name = fields['name']
@description = fields['desc']
@closed = fields['closed']
@url = fields['url']
@organization_id = fields['idOrganization']
@id = fields['id'] if fields['id']
@name = fields['name'] if fields['name']
@description = fields['desc'] if fields['desc']
@closed = fields['closed'] if fields.has_key?('closed')
@url = fields['url'] if fields['url']
@organization_id = fields['idOrganization'] if fields['idOrganization']

self
end

# Check if the board is active.
def closed?
@closed
end
Expand Down
81 changes: 80 additions & 1 deletion spec/board_spec.rb
Expand Up @@ -82,6 +82,85 @@ module Trello
@board.closed?.should_not be_true
end
end

describe "#update_fields" do
it "does not set any fields when the fields argument is empty" do
expected = {
'id' => "id",
'name' => "name",
'desc' => "desc",
'closed' => false,
'url' => "url",
'idOrganization' => "org_id"
}

board = Board.new(expected)

board.update_fields({})

expected.each_pair do |key, value|
if board.respond_to?(key)
board.send(key).should == value
end
end

board.description.should == expected['desc']
board.organization_id.should == expected['idOrganization']
end

it "sets any attributes supplied in the fields argument"
end

describe "#save!" do
include Helpers

let(:any_board_json) do
JSON.generate(boards_details.first)
end

it "cannot currently save a new instance" do
Client.should_not_receive :put

the_new_board = Board.new
lambda{the_new_board.save!}.should raise_error "Cannot save new instance."
end

it "puts all fields except id" do
expected_fields = %w{name description closed url organisation_id}.map{|s| s.to_sym}

Client.should_receive(:put) do |anything, body|
body.keys.should =~ expected_fields
any_board_json
end

the_new_board = Board.new 'id' => "xxx"
the_new_board.save!
end

it "mutates the current instance" do
Client.stub(:put).and_return any_board_json

board = Board.new 'id' => "xxx"

the_result_of_save = board.save!

the_result_of_save.should equal board
end

it "uses the correct resource" do
expected_resource_id = "xxx_board_id_xxx"

Client.should_receive(:put) do |path, anything|
path.should =~ /#{expected_resource_id}\/$/
any_board_json
end

the_new_board = Board.new 'id' => expected_resource_id
the_new_board.save!
end

it "saves OR updates depending on whether or not it has an id set"
end

describe "Repository" do
include Helpers
Expand Down Expand Up @@ -110,7 +189,7 @@ module Trello
the_new_board = Board.create :xxx => ""
the_new_board.should be_a Board
end

it "at least name is required"
end
end
10 changes: 7 additions & 3 deletions spec/integration/how_to_use_boards_spec.rb
Expand Up @@ -13,7 +13,8 @@

after do
if @new_board and false == @new_board.closed?
Client.put "/boards/#{@new_board.id }/closed", { :value => true }
@new_board.update_fields 'closed' => true
@new_board.save!
end
end

Expand All @@ -32,9 +33,12 @@
end

it "can close a board" do
@new_board = Board.create(:name => "An example")
@new_board = Board.create(:name => "[#{Time.now}, CLOSED] An example")

Client.put "/boards/#{@new_board.id}/closed", { :value => true }
@new_board.update_fields 'closed' => true
@new_board.save!

Board.find(@new_board.id).should be_closed
end
end
end

0 comments on commit 03d6c24

Please sign in to comment.