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

Switches category controller away from ja_resource #880

Closed

Conversation

crodriguez1a
Copy link
Contributor

@crodriguez1a crodriguez1a commented Aug 30, 2017

What's in this PR?

  • Updates Category policy methods
    - can (policy.ex)
    - update (category/policy.ex)
    - create (category/policy.ex)

  • Updates Category controller methods
    - index
    - show
    - update
    - create

Progress on: #864

joshsmith and others added 30 commits September 16, 2016 23:31
Push project categories endpoints to production
Push Stripe and tasks to production
Push analytics changes to production
Copy link
Contributor

@begedin begedin left a comment

Choose a reason for hiding this comment

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

Just some minor code style changes I'd like before we get this merged. Otherwise, done exactly as it should have been.

Awesome job and thank you!

def update?(%User{admin: true}), do: true
def update?(%User{admin: false}), do: false
def update?(%{admin: true}), do: true
def update?(%{admin: false}), do: false
Copy link
Contributor

Choose a reason for hiding this comment

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

I understand your goal here is reduction of code, but we tend to prefer using stronger type constraints unless we have to loosen them for some reason.

In fact, what we'd prefer here is to rewrite these as

@spec create?(User.t) :: boolean
def create?(%User{admin: true}), do: true
def create?(%User{}), do: false

@spec update?(User.t) :: boolean
def update?(%User{admin: true}), do: true
def update?(%User{}), do: false

defp can?(%User{} = user, :create, %Category{}, %{}), do: Policy.Category.create?(user)
defp can?(%User{} = user, :update, %Category{}, %{}), do: Policy.Category.update?(user)

@spec can?(User.t, atom, struct, map) :: boolean
Copy link
Contributor

Choose a reason for hiding this comment

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

These all have the same "prototype" (same amount of arguments, same types of arguments in the same order), so there is no need for an extra @spec, nor for keeping a blank line between them. Elixir, and Credo prefer no blank lines between the same function's (in the context of arity) different clauses.

This is something that wasn't clear since there was only a single example in the code previously, but should be clearer once we merge your PR in.

def model, do: CodeCorps.Category
@spec index(Conn.t, map) :: Conn.t
def index(%Conn{} = conn, %{} = params) do
with category <- Category |> Query.id_filter(params) |> Repo.all do
Copy link
Contributor

Choose a reason for hiding this comment

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

categories instead of category here. We don't want to imply that index loads a single record. I'm guessing a copy-paste lapse :)

{:ok, %Category{} = category} <- category |> Category.changeset(params) |> Repo.update
do
conn |> render("show.json-api", data: category)
end
end
end
Copy link
Contributor

Choose a reason for hiding this comment

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

Textbook quality controller conversion here 👍

@crodriguez1a crodriguez1a force-pushed the 864/category_controller branch from b98fb0f to 9806646 Compare August 31, 2017 12:52
@crodriguez1a crodriguez1a force-pushed the 864/category_controller branch from 9806646 to 89316ba Compare August 31, 2017 12:56
@crodriguez1a
Copy link
Contributor Author

@begedin Having a little trouble when rebasing. Is there a wiki somewhere with your git conventions?

@crodriguez1a
Copy link
Contributor Author

crodriguez1a commented Aug 31, 2017

@begedin Actually, I think I may have rebased from master(instead of develop). I may need to close this PR and open another.

@crodriguez1a
Copy link
Contributor Author

crodriguez1a commented Aug 31, 2017

@begedin Closing in favor of #881 (since I accidentally rebased from master)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants