Skip to content

Commit

Permalink
Story#transition! to change state
Browse files Browse the repository at this point in the history
  • Loading branch information
tpope committed Nov 24, 2008
1 parent 7ec53df commit 1209a20
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions lib/pickler/tracker/story.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@ def initialize(project, attributes = {})
super(attributes) super(attributes)
end end


def transition!(state)
raise Pickler::Tracker::Error, "Invalid state #{state}", caller unless STATES.include?(state)
self.current_state = state
if id
xml = "<story><current-state>#{state}</current-state></story>"
error = tracker.request_xml(:put, resource_url, xml).fetch("errors",{})["error"] || true
else
error = save
end
raise Pickler::Tracker::Error, Array(error).join("\n"), caller unless error == true
end

def complete?
%w(finished delivered accepted).include?(current_state)
end

def tracker def tracker
project.tracker project.tracker
end end
Expand Down Expand Up @@ -64,24 +80,24 @@ def to_xml


def destroy def destroy
if id if id
request = tracker.request_xml(:delete, "/projects/#{project.id}/stories/#{id}", to_xml) response = tracker.request_xml(:delete, "/projects/#{project.id}/stories/#{id}", to_xml)
raise Error, request["message"], caller if request["success"] != "true" raise Error, response["message"], caller if response["success"] != "true"
@attributes["id"] = nil @attributes["id"] = nil
self self
end end
end end


def resource_url
["/projects/#{project.id}/stories",id].compact.join("/")
end

def save def save
if id response = tracker.request_xml(id ? :put : :post, resource_url, to_xml)
request = tracker.request_xml(:put, "/projects/#{project.id}/stories/#{id}", to_xml) if response["success"] == "true"
else initialize(project, response["story"])
request = tracker.request_xml(:post, "/projects/#{project.id}/stories", to_xml)
end
if request["success"] == "true"
initialize(project, request["story"])
true true
else else
Array(request["errors"]["error"]) Array(response["errors"]["error"])
end end
end end


Expand Down

0 comments on commit 1209a20

Please sign in to comment.