Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Workflow associations and remove Task #22

Merged
merged 7 commits into from
May 30, 2016
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/pco_api/people/person.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
defmodule PcoApi.People.Person do
use PcoApi.Actions
import PcoApi.RecordAssociation

endpoint "people/v2/people/"

import PcoApi.RecordAssociation
linked_association :addresses
linked_association :apps
linked_association :connnected_people
Expand Down
3 changes: 1 addition & 2 deletions lib/pco_api/people/workflow.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
defmodule PcoApi.People.Workflow do
use PcoApi.Actions
import PcoApi.RecordAssociation

endpoint "people/v2/workflows/"

import PcoApi.RecordAssociation
linked_association :cards
linked_association :steps
end
11 changes: 6 additions & 5 deletions lib/pco_api/people/workflow/card.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ defmodule PcoApi.People.Workflow.Card do
"""

use PcoApi.Actions
import PcoApi.RecordAssociation
endpoint "people/v2/workflows/"
endpoint "people/v2/"

import PcoApi.RecordAssociation
linked_association :activities
linked_association :assignee
linked_association :notes
linked_association :tasks
linked_association :person

@doc """
Gets associated WorkflowCard records from a Workflow Record from links.
Expand All @@ -38,7 +39,7 @@ defmodule PcoApi.People.Workflow.Card do
%PcoApi.Record{type: "WorkflowCard", id: 1, ...}

"""
def get(%PcoApi.Record{type: "Workflow", id: id}), do: get("#{id}/cards")
def get(%PcoApi.Record{type: "Workflow", id: id}), do: get("workflows/#{id}/cards")

@doc """
Gets a single WorkflowCard for a Workflow.
Expand All @@ -51,5 +52,5 @@ defmodule PcoApi.People.Workflow.Card do
%PcoApi.Record{type: "WorkflowCard", id: 2} # for Workflow.id == 1

"""
def get(%PcoApi.Record{type: "Workflow", id: workflow_id}, id), do: get("#{workflow_id}/cards/#{id}")
def get(%PcoApi.Record{type: "Workflow", id: workflow_id}, id), do: get("workflows/#{workflow_id}/cards/#{id}")
end
2 changes: 1 addition & 1 deletion lib/pco_api/people/workflow/card/activity.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ defmodule PcoApi.People.Workflow.Card.Activity do

## Example:

iex> %PcoApi.Record{type: "WorkflowCard", id: 1, links: %{"self" => "http://example.com"}} |> Address.get(2)
iex> %PcoApi.Record{type: "WorkflowCard", id: 1, links: %{"self" => "http://example.com"}} |> Note.get(2)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be Activity.get(2)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops!

%PcoApi.Record{type: "WorkflowCardActivity", id: 2} # for Person.id == 1

"""
Expand Down
2 changes: 1 addition & 1 deletion lib/pco_api/people/workflow/card/note.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ defmodule PcoApi.People.Workflow.Card.Note do

## Example:

iex> %PcoApi.Record{type: "WorkflowCard", id: 1, links: %{"self" => "http://example.com"}} |> Address.get(2)
iex> %PcoApi.Record{type: "WorkflowCard", id: 1, links: %{"self" => "http://example.com"}} |> Note.get(2)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above: Workflow.Card.Note

%PcoApi.Record{type: "WorkflowCardNote", id: 2} # for Person.id == 1

"""
Expand Down
64 changes: 0 additions & 64 deletions lib/pco_api/people/workflow/card/task.ex

This file was deleted.

9 changes: 6 additions & 3 deletions lib/pco_api/people/workflow/step.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ defmodule PcoApi.People.Workflow.Step do
"""

use PcoApi.Actions
endpoint "people/v2/workflows/"
endpoint "people/v2/"

import PcoApi.RecordAssociation
linked_association :default_assignee

@doc """
Gets associated WorkflowStep records from a Workflow Record from links.
Expand All @@ -33,7 +36,7 @@ defmodule PcoApi.People.Workflow.Step do
%PcoApi.Record{type: "WorkflowStep", id: 1, ...}

"""
def get(%PcoApi.Record{type: "Workflow", id: id}), do: get("#{id}/steps")
def get(%PcoApi.Record{type: "Workflow", id: id}), do: get("workflows/#{id}/steps")

@doc """
Gets a single WorkflowStep for a Workflow.
Expand All @@ -46,5 +49,5 @@ defmodule PcoApi.People.Workflow.Step do
%PcoApi.Record{type: "WorkflowStep", id: 2} # for Workflow.id == 1

"""
def get(%PcoApi.Record{type: "Workflow", id: workflow_id}, id), do: get("#{workflow_id}/steps/#{id}")
def get(%PcoApi.Record{type: "Workflow", id: workflow_id}, id), do: get("workflows/#{workflow_id}/steps/#{id}")
end
11 changes: 7 additions & 4 deletions lib/pco_api/record_association.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ defmodule PcoApi.RecordAssociation do
def unquote(:"#{name}")([]), do: []
def unquote(:"#{name}")(%PcoApi.Record{} = record) do
# TODO: error handling when the association link doesn't exist
request(:get, record.links[Atom.to_string(unquote(name))], [])
|> Enum.map(fn(%{"attributes" => attrs, "id" => id, "links" => links, "type" => type}) ->
%PcoApi.Record{attributes: attrs, id: id, links: links, type: type}
end)
unquote(:"#{name}")(request(:get, record.links[Atom.to_string(unquote(name))], []))
end
def unquote(:"#{name}")([%PcoApi.Record{} = first | rest]) do
[unquote(:"#{name}")(first) | unquote(:"#{name}")(rest)]
|> List.flatten
|> Enum.dedup
end
def unquote(:"#{name}")(%{"attributes" => attrs, "id" => id, "links" => links, "type" => type}) do
%PcoApi.Record{attributes: attrs, id: id, links: links, type: type}
end
def unquote(:"#{name}")(list) when is_list(list) do
list |> Enum.map(&(unquote(:"#{name}")(&1)))
end
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still a little hard to read the metaprogramming in Elixir, but are you doing anything in this file other than just breaking the function into smaller functions?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The line 5 get was expecting request/3 to return a list which it would iterate through and map each item inside. Some requests return a single record and the Enum.map would fail. So I tore out the "assigning %<"attributes"...> to a record" logic and made it it's own function (line 14) and then moved the Enum.map part of the function out so that it only runs when a list is returned, otherwise it goes straight to assign the attributes to a record.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. 👍

end
end
end
23 changes: 14 additions & 9 deletions test/fixtures/workflow_card_notes.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
{
"data": {
"type": "WorkflowCardNote",
"id": "1",
"attributes": {
"note": "A note."
},
"links": {
"self": "https:\/\/api.planningcenteronline.com\/people\/v2\/workflows\/1\/cards\/1\/notes\/1"
}
"links": {
"self": "https:\/\/api.planningcenteronline.com\/people\/v2\/workflows\/1\/cards\/1\/notes"
},
"data": [
{
"type": "WorkflowCardNote",
"id": "1",
"attributes": {
"note": "A note."
},
"links": {
"self": "https:\/\/api.planningcenteronline.com\/people\/v2\/workflows\/1\/cards\/1\/notes\/1"
}
}
],
"included": [

],
Expand Down
36 changes: 0 additions & 36 deletions test/fixtures/workflow_card_task.json

This file was deleted.

53 changes: 0 additions & 53 deletions test/fixtures/workflow_card_tasks.json

This file was deleted.

7 changes: 0 additions & 7 deletions test/pco_api/people/workflow/card/activity_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,4 @@ defmodule PcoApi.People.Workflow.Card.ActivityTest do
type: "WorkflowCard"
}
end

defp record_without_link do
%PcoApi.Record{
id: "1",
type: "WorkflowCard"
}
end
end
7 changes: 0 additions & 7 deletions test/pco_api/people/workflow/card/note_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,4 @@ defmodule PcoApi.People.Workflow.Card.NoteTest do
type: "WorkflowCard"
}
end

defp record_without_link do
%PcoApi.Record{
id: "1",
type: "WorkflowCard"
}
end
end
Loading