Skip to content

Instantly share code, notes, and snippets.

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
# ...
def base_scope
super.select("ON (my_table.id) my_table.*")
end
# 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){
# app/models/resources/application_resource.rb
class ApplicationResource < Graphiti::Resource
# ...
##
# Cancancan
##
def base_scope
model.accessible_by(context.current_ability)
end
# 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
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' }
@sideshowbandana
sideshowbandana / test.sh
Created February 6, 2019 12:48
test generators
#!/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
@sideshowbandana
sideshowbandana / generator_template.rb
Created February 6, 2019 12:39
generator test template
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"))
#!/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
build:
<<: *deploy_image
environment:
- IMAGE: "MY_IMAGE_NAME"
steps:
- setup_remote_docker
- checkout
- run:
name: Build
command: |