Skip to content

Instantly share code, notes, and snippets.

@cpjk
Last active November 25, 2015 04:34
Show Gist options
  • Save cpjk/958eeae3e130648ef47f to your computer and use it in GitHub Desktop.
Save cpjk/958eeae3e130648ef47f to your computer and use it in GitHub Desktop.
defmodule Blog.Abilities do
defimpl Canada.Can, for: Blog.User do
def can?(%Blog.User{}, :index, Blog.User), do: true
def can?(%Blog.User{}, _, _), do: true
def can?(nil, _, _), do: false
end
defimpl Canada.Can, for: nil do
def can?(nil, _, _), do: false
end
end end
@cpjk
Copy link
Author

cpjk commented Nov 25, 2015

pry(2)> can? %Blog.User{}, :index, Blog.User
** (FunctionClauseError) no function clause matching in Canada.Can.can?/3
(blog) lib/abilities.ex:9: Canada.Can.can?(%Blog.User{meta: #Ecto.Schema.Metadata<:built>, email: nil, encrypted_password: nil, first_name: nil, id: nil, inserted_at: nil, last_name: nil, password: nil, updated_at: nil}, :index, Blog.User)
(stdlib) erl_eval.erl:669: :erl_eval.do_apply/6
(iex) lib/iex/evaluator.ex:117: IEx.Evaluator.handle_eval/5
(iex) lib/iex/evaluator.ex:110: IEx.Evaluator.do_eval/3
(iex) lib/iex/evaluator.ex:90: IEx.Evaluator.eval/3
(iex) lib/iex/evaluator.ex:27: IEx.Evaluator.loop/2
(iex) lib/iex/evaluator.ex:18: IEx.Evaluator.start/2
(iex) lib/iex.ex:411: IEx.pry/3
lib/canary/plugs.ex:162: Canary.Plugs._authorize_resource/2
lib/canary/plugs.ex:219: Canary.Plugs._load_and_authorize_resource/2
(blog) web/controllers/user_controller.ex:1: Blog.UserController.phoenix_controller_pipeline/2
(blog) lib/phoenix/router.ex:255: Blog.Router.dispatch/2
(blog) web/router.ex:1: Blog.Router.do_call/2
(blog) lib/blog/endpoint.ex:1: Blog.Endpoint.phoenix_pipeline/1
(blog) lib/plug/debugger.ex:90: Blog.Endpoint."call (overridable 3)"/2
(blog) lib/phoenix/endpoint/render_errors.ex:34: Blog.Endpoint.call/2
(plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
(cowboy) src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4

@cpjk
Copy link
Author

cpjk commented Nov 25, 2015

When defimpl Canada.Can, for: nil... is removed, the protocol resolved fine for normal users, but not for nil

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