2015-03-10 09:27:32 -04:00
|
|
|
require "pundit"
|
|
|
|
require "pundit/rspec"
|
2014-07-18 10:20:48 -04:00
|
|
|
|
2014-04-23 22:58:38 -04:00
|
|
|
require "pry"
|
2015-03-10 09:27:32 -04:00
|
|
|
require "active_support"
|
2014-04-23 22:58:38 -04:00
|
|
|
require "active_support/core_ext"
|
|
|
|
require "active_model/naming"
|
|
|
|
|
2014-07-18 10:20:48 -04:00
|
|
|
I18n.enforce_available_locales = false
|
|
|
|
|
|
|
|
module PunditSpecHelper
|
|
|
|
extend RSpec::Matchers::DSL
|
|
|
|
|
|
|
|
matcher :be_truthy do
|
|
|
|
match do |actual|
|
|
|
|
actual
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
RSpec.configure do |config|
|
|
|
|
config.include PunditSpecHelper
|
|
|
|
end
|
|
|
|
|
2014-04-23 22:58:38 -04:00
|
|
|
class PostPolicy < Struct.new(:user, :post)
|
|
|
|
def update?
|
|
|
|
post.user == user
|
|
|
|
end
|
|
|
|
def destroy?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
def show?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
class PostPolicy::Scope < Struct.new(:user, :scope)
|
|
|
|
def resolve
|
|
|
|
scope.published
|
|
|
|
end
|
|
|
|
end
|
|
|
|
class Post < Struct.new(:user)
|
|
|
|
def self.published
|
|
|
|
:published
|
|
|
|
end
|
2015-03-27 05:22:30 -04:00
|
|
|
def to_s; "Post"; end
|
|
|
|
def inspect; "#<Post>"; end
|
2014-04-23 22:58:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class CommentPolicy < Struct.new(:user, :comment); end
|
|
|
|
class CommentPolicy::Scope < Struct.new(:user, :scope)
|
|
|
|
def resolve
|
|
|
|
scope
|
|
|
|
end
|
|
|
|
end
|
|
|
|
class Comment; extend ActiveModel::Naming; end
|
|
|
|
|
2015-03-27 13:09:26 -04:00
|
|
|
# minimum mock for an ActiveRecord Relation returning comments
|
|
|
|
class CommentsRelation
|
|
|
|
def initialize(empty=false); @empty=empty; end
|
|
|
|
def blank?; @empty; end
|
|
|
|
def model_name; Comment.model_name; end
|
|
|
|
end
|
|
|
|
|
2014-04-23 22:58:38 -04:00
|
|
|
class Article; end
|
|
|
|
|
|
|
|
class BlogPolicy < Struct.new(:user, :blog); end
|
|
|
|
class Blog; end
|
|
|
|
class ArtificialBlog < Blog
|
|
|
|
def self.policy_class
|
|
|
|
BlogPolicy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
class ArticleTag
|
|
|
|
def self.policy_class
|
|
|
|
Struct.new(:user, :tag) do
|
|
|
|
def show?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
def destroy?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-05-21 22:09:17 -04:00
|
|
|
|
2014-07-13 06:34:09 -04:00
|
|
|
class DashboardPolicy < Struct.new(:user, :dashboard); end
|
|
|
|
|
2014-10-11 09:01:45 -04:00
|
|
|
module Project
|
|
|
|
class DashboardPolicy < Struct.new(:user, :dashboard); end
|
|
|
|
end
|
|
|
|
|
2015-03-26 05:32:20 -04:00
|
|
|
class DenierPolicy < Struct.new(:user, :record)
|
|
|
|
def update?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-21 22:09:17 -04:00
|
|
|
class Controller
|
|
|
|
include Pundit
|
|
|
|
|
|
|
|
attr_reader :current_user, :params
|
|
|
|
|
|
|
|
def initialize(current_user, params)
|
|
|
|
@current_user = current_user
|
|
|
|
@params = params
|
|
|
|
end
|
|
|
|
end
|
2015-03-26 05:25:21 -04:00
|
|
|
|
|
|
|
class NilClassPolicy
|
|
|
|
class Scope
|
|
|
|
def initialize(*)
|
|
|
|
raise "I'm only here to be annoying!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(*)
|
|
|
|
raise "I'm only here to be annoying!"
|
|
|
|
end
|
|
|
|
end
|