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
module Abilities | |
class MyModel < Base | |
def initialize(user) | |
super(user) | |
return unless user.present? | |
can :manage, ::MyModel, my_other_model: { user_id: user.id } | |
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
# ... | |
def base_scope | |
super.select("ON (my_table.id) my_table.*") | |
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
# spec/helpers/rails_helper.rb | |
RSpec.shared_context "Authorization helpers" do | |
let(:current_ability){ | |
Abilities::Base.new(user).tap do |ability| | |
ability.can(:manage, :all) | |
end | |
} | |
let(:user){ |
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/models/resources/application_resource.rb | |
class ApplicationResource < Graphiti::Resource | |
# ... | |
## | |
# Cancancan | |
## | |
def base_scope | |
model.accessible_by(context.current_ability) | |
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/models/abilities/base.rb | |
module Abilities | |
class Base | |
include CanCan::Ability | |
def initialize(user) | |
end | |
end | |
end | |
# app/models/abilities/my_model.rb | |
module Abilities |
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
def current_ability | |
@current_ability ||= Abilities::Factory.ability_for(self.class, current_user) | |
end | |
rescue_from CanCan::AccessDenied do |exception| | |
respond_to do |format| | |
format.jsonapi { render jsonapi: { meta: { error: exception.message } }, status: :forbidden } | |
format.json { head :forbidden, content_type: 'text/html' } | |
format.html { redirect_to main_app.root_url, notice: exception.message } | |
format.js { head :forbidden, content_type: 'text/html' } |
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/bash | |
set -ex | |
mkdir -p test_generators | |
cd test_generators | |
rm -rf graphiti | |
git clone https://github.com/sideshowbandana/graphiti.git --single-branch --branch factory_bot_in_generator | |
curl https://gist.githubusercontent.com/sideshowbandana/a341bf8e0ede4c739c8abbd49809b908/raw/782cdad24dece3893b188d1a1107e6a2927b408c/generator_template.rb > generator_template.rb | |
rm -rf blog && rails new blog --api -m generator_template.rb |
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
Thor::Base.shell = Thor::Shell::Color | |
require 'yaml' | |
def truthy?(statement) | |
val = ask(statement) | |
['y', 'yes', ''].include?(val) | |
end | |
def eval_template(name) | |
instance_eval(File.read(File.dirname(__FILE__) + "/#{name}.rb")) |
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/bash | |
set -e | |
PROJECT_ID=[GCLOUD_PROJECT_ID] | |
gcloud auth print-access-token | docker login -u oauth2accesstoken --password-stdin https://gcr.io | |
docker build ${DOCKER_BUILD_ARGS} -t ${IMAGE} . | |
set -x |
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
build: | |
<<: *deploy_image | |
environment: | |
- IMAGE: "MY_IMAGE_NAME" | |
steps: | |
- setup_remote_docker | |
- checkout | |
- run: | |
name: Build | |
command: | |
NewerOlder