Created
November 10, 2021 08:59
-
-
Save julianrubisch/57237a9f87e016647f04ff0b40a2631d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationController < ActionController::Base | |
include Pundit | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/reflexes/application_reflex.rb | |
class ApplicationReflex < StimulusReflex::Reflex | |
include Pundit | |
rescue_from Pundit::NotAuthorizedError do |exception| | |
cable_ready | |
.inner_html(selector: "#alert", html: exception.message) | |
.remove_css_class(name: "hidden") | |
.broadcast | |
end | |
delegate :current_user, to: :connection | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationReflex < StimulusReflex::Reflex | |
include Pundit | |
delegate :current_user, to: :connection | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ bundle add pundit | |
$ bin/rails g pundit:install |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ApplicationController from "./application_controller"; | |
export default class extends ApplicationController { | |
connect() { | |
super.connect(); | |
} | |
destroyHalted() { | |
alert("You are not allowed to destroy this embed!"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/policies/embed_policy.rb | |
class EmbedPolicy < ApplicationPolicy | |
attr_reader :user, :embed | |
def initialize(user, embed) | |
@user = user | |
@embed = embed | |
end | |
def destroy? | |
embed.board.owner == user | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/reflexes/embed_reflex.rb | |
class EmbedReflex < ApplicationReflex | |
# ... | |
before_reflex only: [:destroy, :update] do | |
@embed = element.signed[:sgid] | |
throw :abort unless policy(@embed).send("#{action_name}?") | |
end | |
# ... | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/reflexes/embed_reflex.rb | |
class EmbedReflex < ApplicationReflex | |
# ... | |
before_reflex only: :destroy do | |
@embed = element.signed[:sgid] | |
throw :abort unless policy(@embed).destroy? | |
end | |
# ... | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/reflexes/embed_reflex.rb | |
class EmbedReflex < ApplicationReflex | |
# ... | |
def destroy | |
@embed = element.signed[:sgid] | |
@embed.destroy | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ bin/rails g pundit:policy embed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment