-
-
Save afn/441dfcf4ddc751b82f09af11bb356ba2 to your computer and use it in GitHub Desktop.
CanCanCan Issue
This file contains hidden or 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
begin | |
require 'bundler/inline' | |
rescue LoadError => e | |
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler' | |
raise e | |
end | |
gemfile(true) do | |
source 'https://rubygems.org' | |
gem 'rails', '7.1' # use correct rails version | |
gem 'cancancan', '3.5.0', require: false # use correct cancancan version | |
gem 'sqlite3', '~> 1.4' # use another DB if necessary | |
end | |
require 'active_record' | |
require 'action_controller' | |
require 'cancancan' | |
require 'cancan/model_adapters/active_record_adapter' | |
require 'cancan/model_adapters/active_record_4_adapter' | |
require 'minitest/autorun' | |
require 'logger' | |
# This connection will do for database-independent bug reports. | |
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:') | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
# create your tables here | |
ActiveRecord::Schema.define do | |
create_table :books, force: true do |t| | |
t.integer :user_id | |
t.string :title | |
t.string :secret_attribute | |
end | |
create_table :users, force: true do |t| | |
t.string :name | |
end | |
end | |
class Book < ActiveRecord::Base | |
belongs_to :user | |
end | |
class User < ActiveRecord::Base | |
has_many :books | |
end | |
class Ability | |
include CanCan::Ability | |
def initialize(user) | |
can :read, Book, [:id, :title], user: user | |
end | |
end | |
class TestApp < Rails::Application | |
config.root = File.dirname(__FILE__) | |
config.session_store :cookie_store, key: "cookie_store_key" | |
secrets.secret_token = "secret_token" | |
secrets.secret_key_base = "secret_key_base" | |
config.logger = Logger.new($stdout) | |
Rails.logger = config.logger | |
routes.draw do | |
resources :books | |
end | |
end | |
class BooksController < ActionController::Base | |
include CanCan::ControllerAdditions | |
load_and_authorize_resource :book | |
def index | |
render json: @books | |
end | |
private | |
def current_user | |
User.first | |
end | |
end | |
class ExampleControllerTest < ActionController::TestCase | |
setup do | |
@controller = BooksController.new | |
@routes = Rails.application.routes | |
end | |
def test_action | |
user_1 = User.create(name: 'John Doe') | |
user_2 = User.create(name: 'Jane Roe') | |
book_1 = user_1.books.create!(title: 'Harry Potter and the Chamber of Secrets', secret_attribute: 'Tom Riddle') | |
book_2 = user_2.books.create!(title: 'Lord of the Rings', secret_attribute: 'My Precious') | |
get :index | |
assert_equal [{ 'id' => book_1.id, 'title' => 'Harry Potter and the Chamber of Secrets' }], response.parsed_body | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment