Skip to content

Commit

Permalink
Refactor handling of belongs to owner struct
Browse files Browse the repository at this point in the history
  • Loading branch information
danschultzer committed Mar 26, 2018
1 parent 1b481b8 commit d3cef0b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ defmodule ExOauth2Provider.OauthAccessGrants.OauthAccessGrant do

use ExOauth2Provider.Schema
alias ExOauth2Provider.OauthApplications.OauthApplication
alias ExOauth2Provider.{Config, Utils}

@resource_owner_struct Config.resource_owner_struct()
@resource_owner_belongs_to_opts Utils.schema_belongs_to_opts(@resource_owner_struct)

schema "oauth_access_grants" do
belongs_to :resource_owner, ExOauth2Provider.Config.resource_owner_struct(), type: ExOauth2Provider.Config.resource_owner_struct().__schema__(:type, :id)
belongs_to :resource_owner, @resource_owner_struct, @resource_owner_belongs_to_opts
belongs_to :application, OauthApplication

field :token, :string, null: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ defmodule ExOauth2Provider.OauthAccessTokens.OauthAccessToken do

use ExOauth2Provider.Schema
alias ExOauth2Provider.OauthApplications.OauthApplication
alias ExOauth2Provider.{Config, Utils}

@resource_owner_struct Config.resource_owner_struct()
@resource_owner_belongs_to_opts Utils.schema_belongs_to_opts(@resource_owner_struct)

schema "oauth_access_tokens" do
belongs_to :application, OauthApplication, on_replace: :nilify
belongs_to :resource_owner, ExOauth2Provider.Config.resource_owner_struct(), type: ExOauth2Provider.Config.resource_owner_struct().__schema__(:type, :id)
belongs_to :resource_owner, @resource_owner_struct, @resource_owner_belongs_to_opts

field :token, :string, null: false
field :refresh_token, :string
Expand Down
11 changes: 6 additions & 5 deletions lib/ex_oauth2_provider/oauth_applications/oauth_application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ defmodule ExOauth2Provider.OauthApplications.OauthApplication do

use ExOauth2Provider.Schema
require Logger
alias ExOauth2Provider.{Config, Utils}

# For Phoenix integrations
if Code.ensure_loaded?(Phoenix.Param) do
@derive {Phoenix.Param, key: :uid}
end

schema "oauth_applications" do
if is_nil(ExOauth2Provider.Config.application_owner_struct()) do
Logger.error("You need to set a resource_owner or application_owner in your config and recompile ex_oauth2_provider!")
end
@owner_struct Config.application_owner_struct()
@owner_belongs_to_opts Utils.schema_belongs_to_opts(@owner_struct)
if is_nil(@owner_struct), do: Logger.error("You need to set a resource_owner or application_owner in your config and recompile ex_oauth2_provider!")

belongs_to :owner, ExOauth2Provider.Config.application_owner_struct(), type: ExOauth2Provider.Config.resource_owner_struct().__schema__(:type, :id)
schema "oauth_applications" do
belongs_to :owner, @owner_struct, @owner_belongs_to_opts

field :name, :string, null: false
field :uid, :string, null: false
Expand Down
4 changes: 4 additions & 0 deletions lib/ex_oauth2_provider/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ defmodule ExOauth2Provider.Utils do
def schema_association(module, association) do
module.__schema__(:association, association)
end

@spec schema_belongs_to_opts(Ecto.Schema | nil) :: Keyword.t
def schema_belongs_to_opts(nil), do: []
def schema_belongs_to_opts(module), do: [type: module.__schema__(:type, :id)]
end

0 comments on commit d3cef0b

Please sign in to comment.