From 03d6c246e0a9a8bce72b306c3bd8bffc750df1b3 Mon Sep 17 00:00:00 2001 From: Ben Biddington Date: Mon, 30 Jan 2012 16:37:45 +0800 Subject: [PATCH] how to use boards, can close a board --- lib/trello/board.rb | 30 +++++--- spec/board_spec.rb | 81 +++++++++++++++++++++- spec/integration/how_to_use_boards_spec.rb | 10 ++- 3 files changed, 106 insertions(+), 15 deletions(-) diff --git a/lib/trello/board.rb b/lib/trello/board.rb index 37b33a8a..584dc32b 100644 --- a/lib/trello/board.rb +++ b/lib/trello/board.rb @@ -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 diff --git a/spec/board_spec.rb b/spec/board_spec.rb index fc08ca10..47b5ef42 100644 --- a/spec/board_spec.rb +++ b/spec/board_spec.rb @@ -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 @@ -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 diff --git a/spec/integration/how_to_use_boards_spec.rb b/spec/integration/how_to_use_boards_spec.rb index 11900af5..8c26878f 100644 --- a/spec/integration/how_to_use_boards_spec.rb +++ b/spec/integration/how_to_use_boards_spec.rb @@ -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 @@ -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