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

Pundit for authorization only #129

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Move business logic out of Pundit policies
  • Loading branch information
joemasilotti committed Dec 2, 2021
commit 53d4a93b1a71a411981b54789390b84e37a1319b
@@ -1,15 +1,12 @@
class BusinessesController < ApplicationController
before_action :authenticate_user!, only: %i[new create]
before_action :redirect_to_edit_if_already_exists, only: %i[new create]

def new
authorize current_user.business, policy_class: BusinessPolicy
@business = current_user.build_business
rescue ApplicationPolicy::AlreadyExists
redirect_to edit_business_path(current_user.business)
end

def create
authorize current_user.business, policy_class: BusinessPolicy
@business = current_user.build_business(business_params)

if @business.save
@@ -38,6 +35,12 @@ def update

private

def redirect_to_edit_if_already_exists
if current_user.business.present?
redirect_to edit_business_path(current_user.business)
end
end

def business_params
params.require(:business).permit(
:name,
@@ -2,20 +2,17 @@ class DevelopersController < ApplicationController
include Pagy::Backend

before_action :authenticate_user!, only: %i[new create edit update]
before_action :redirect_to_edit_if_already_exists, only: %i[new create]

def index
@pagy, @developers = pagy(Developer.most_recently_added.with_attached_avatar)
end

def new
authorize current_user.developer, policy_class: DeveloperPolicy
@developer = current_user.build_developer
rescue ApplicationPolicy::AlreadyExists
redirect_to edit_developer_path(current_user.developer)
end

def create
authorize current_user.developer, policy_class: DeveloperPolicy
@developer = current_user.build_developer(developer_params)

if @developer.save
@@ -48,6 +45,12 @@ def show

private

def redirect_to_edit_if_already_exists
if current_user.developer.present?
redirect_to edit_developer_path(current_user.developer)
end
end

def developer_params
params.require(:developer).permit(
:name,
@@ -1,14 +1,4 @@
class BusinessPolicy < ApplicationPolicy
def new?
raise AlreadyExists unless create?

true
end

def create?
record.nil?
end

def update?
user == record.user
end
@@ -1,14 +1,4 @@
class DeveloperPolicy < ApplicationPolicy
def new?
raise AlreadyExists unless create?

true
end

def create?
record.nil?
end

def update?
user == record.user
end
@@ -12,26 +12,4 @@ class BusinessPolicyTest < ActiveSupport::TestCase

refute BusinessPolicy.new(user, business).update?
end

test "can create a business profile if they do not already have one" do
user = users(:empty)
business = user.business

assert BusinessPolicy.new(user, business).create?
end

test "cannot create a business profile if they already have one" do
user = users(:with_business)
business = user.business

refute BusinessPolicy.new(user, business).create?
end

test "raises when instantiating a new business when one exists" do
user = users(:with_business)

assert_raises(ApplicationPolicy::AlreadyExists) do
BusinessPolicy.new(user, Business.new).new?
end
end
end
@@ -12,26 +12,4 @@ class DeveloperPolicyTest < ActiveSupport::TestCase

refute DeveloperPolicy.new(user, developer).update?
end

test "can create a developer profile if they do not already have one" do
user = users(:without_profile)
developer = user.developer

assert DeveloperPolicy.new(user, developer).create?
end

test "cannot create a developer profile if they already have one" do
user = users(:with_available_profile)
developer = user.developer

refute DeveloperPolicy.new(user, developer).create?
end

test "raises when instantiating a new developer when one exists" do
user = users(:with_available_profile)

assert_raises(ApplicationPolicy::AlreadyExists) do
DeveloperPolicy.new(user, Developer.new).new?
end
end
end