-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic pundit setup with OrderPolicy on create
- Needs to be integrated into a controller with `authorize @order` to be useful - Add a default ApplicationPolicy using `pundit install`
- Loading branch information
Showing
5 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,3 +41,5 @@ group :test do | |
gem "selenium-webdriver" | ||
gem "webmock" | ||
end | ||
|
||
gem "pundit", "~> 2.3" |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class StockPurchaseJob < ApplicationJob | ||
queue_as :default | ||
|
||
def perform | ||
pending_orders = Order.pending | ||
|
||
pending_orders.each do |pending_order| | ||
PurchaseStock.execute(pending_order) | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
|
||
class ApplicationPolicy | ||
attr_reader :user, :record | ||
|
||
def initialize(user, record) | ||
@user = user | ||
@record = record | ||
end | ||
|
||
def index? | ||
false | ||
end | ||
|
||
def show? | ||
false | ||
end | ||
|
||
def create? | ||
false | ||
end | ||
|
||
def new? | ||
create? | ||
end | ||
|
||
def update? | ||
false | ||
end | ||
|
||
def edit? | ||
update? | ||
end | ||
|
||
def destroy? | ||
false | ||
end | ||
|
||
class Scope | ||
def initialize(user, scope) | ||
@user = user | ||
@scope = scope | ||
end | ||
|
||
def resolve | ||
raise NoMethodError, "You must define #resolve in #{self.class}" | ||
end | ||
|
||
private | ||
|
||
attr_reader :user, :scope | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class OrderPolicy < ApplicationPolicy | ||
def create? | ||
user.portfolio == resource.portfolio | ||
end | ||
end |