2015-03-10 09:27:32 -04:00
|
|
|
require "pundit"
|
|
|
|
require "pundit/rspec"
|
2014-07-18 10:20:48 -04:00
|
|
|
|
2015-03-27 05:14:09 -04:00
|
|
|
require "rack"
|
|
|
|
require "rack/test"
|
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"
|
2015-03-27 05:14:09 -04:00
|
|
|
require "action_controller/metal/strong_parameters"
|
2014-04-23 22:58:38 -04:00
|
|
|
|
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)
|
2016-01-14 09:15:30 -05:00
|
|
|
class Scope < Struct.new(:user, :scope)
|
|
|
|
def resolve
|
|
|
|
scope.published
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-23 22:58:38 -04:00
|
|
|
def update?
|
|
|
|
post.user == user
|
|
|
|
end
|
2016-01-14 09:15:30 -05:00
|
|
|
|
2014-04-23 22:58:38 -04:00
|
|
|
def destroy?
|
|
|
|
false
|
|
|
|
end
|
2016-01-14 09:15:30 -05:00
|
|
|
|
2014-04-23 22:58:38 -04:00
|
|
|
def show?
|
|
|
|
true
|
|
|
|
end
|
2016-01-14 09:15:30 -05:00
|
|
|
|
2015-03-27 05:14:09 -04:00
|
|
|
def permitted_attributes
|
|
|
|
if post.user == user
|
|
|
|
[:title, :votes]
|
|
|
|
else
|
|
|
|
[:votes]
|
|
|
|
end
|
|
|
|
end
|
2016-01-14 08:43:51 -05:00
|
|
|
|
|
|
|
def permitted_attributes_for_revise
|
|
|
|
[:body]
|
|
|
|
end
|
2014-04-23 22:58:38 -04:00
|
|
|
end
|
2016-01-14 09:15:30 -05:00
|
|
|
|
2014-04-23 22:58:38 -04:00
|
|
|
class Post < Struct.new(:user)
|
|
|
|
def self.published
|
|
|
|
:published
|
|
|
|
end
|
2016-01-14 09:15:30 -05:00
|
|
|
|
|
|
|
def to_s
|
|
|
|
"Post"
|
|
|
|
end
|
|
|
|
|
|
|
|
def inspect
|
|
|
|
"#<Post>"
|
|
|
|
end
|
2014-04-23 22:58:38 -04:00
|
|
|
end
|
|
|
|
|
2015-07-13 09:56:55 -04:00
|
|
|
module Customer
|
|
|
|
class Post < Post
|
2015-07-15 05:58:54 -04:00
|
|
|
def model_name
|
2016-01-14 09:15:30 -05:00
|
|
|
OpenStruct.new(param_key: "customer_post")
|
2015-07-13 09:56:55 -04:00
|
|
|
end
|
2016-01-14 09:15:30 -05:00
|
|
|
|
2015-07-13 09:56:55 -04:00
|
|
|
def policy_class
|
|
|
|
PostPolicy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-01-14 09:15:30 -05:00
|
|
|
|
|
|
|
class CommentPolicy < Struct.new(:user, :comment)
|
|
|
|
class Scope < Struct.new(:user, :scope)
|
|
|
|
def resolve
|
|
|
|
scope
|
|
|
|
end
|
2014-04-23 22:58:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-14 09:15:30 -05:00
|
|
|
class Comment
|
|
|
|
extend ActiveModel::Naming
|
|
|
|
end
|
|
|
|
|
2015-03-27 13:09:26 -04:00
|
|
|
class CommentsRelation
|
2016-01-14 09:15:30 -05:00
|
|
|
def initialize(empty = false)
|
|
|
|
@empty = empty
|
|
|
|
end
|
|
|
|
|
|
|
|
def blank?
|
|
|
|
@empty
|
|
|
|
end
|
|
|
|
|
|
|
|
def model_name
|
|
|
|
Comment.model_name
|
|
|
|
end
|
2015-03-27 13:09:26 -04:00
|
|
|
end
|
|
|
|
|
2014-04-23 22:58:38 -04:00
|
|
|
class Article; end
|
|
|
|
|
|
|
|
class BlogPolicy < Struct.new(:user, :blog); end
|
2016-01-14 09:15:30 -05:00
|
|
|
|
2014-04-23 22:58:38 -04:00
|
|
|
class Blog; end
|
2016-01-14 09:15:30 -05:00
|
|
|
|
2014-04-23 22:58:38 -04:00
|
|
|
class ArtificialBlog < Blog
|
|
|
|
def self.policy_class
|
|
|
|
BlogPolicy
|
|
|
|
end
|
|
|
|
end
|
2016-01-14 09:15:30 -05:00
|
|
|
|
|
|
|
class ArticleTagOtherNamePolicy < Struct.new(:user, :tag)
|
|
|
|
def show?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-23 22:58:38 -04:00
|
|
|
class ArticleTag
|
|
|
|
def self.policy_class
|
2016-01-14 09:15:30 -05:00
|
|
|
ArticleTagOtherNamePolicy
|
2014-04-23 22:58:38 -04:00
|
|
|
end
|
|
|
|
end
|
2014-05-21 22:09:17 -04:00
|
|
|
|
2015-05-08 20:43:48 -04:00
|
|
|
class CriteriaPolicy < Struct.new(:user, :criteria); end
|
2014-07-13 06:34:09 -04:00
|
|
|
|
2014-10-11 09:01:45 -04:00
|
|
|
module Project
|
2015-11-20 22:02:56 -05:00
|
|
|
class CommentPolicy < Struct.new(:user, :post); end
|
2015-05-08 20:43:48 -04:00
|
|
|
class CriteriaPolicy < Struct.new(:user, :criteria); end
|
2015-11-20 22:02:56 -05:00
|
|
|
class PostPolicy < Struct.new(:user, :post); end
|
2014-10-11 09:01:45 -04:00
|
|
|
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
|
2016-02-02 14:31:43 -05:00
|
|
|
# Mark protected methods public so they may be called in test
|
|
|
|
public(*Pundit.protected_instance_methods)
|
2014-05-21 22:09:17 -04:00
|
|
|
|
|
|
|
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
|
2015-11-20 22:02:56 -05:00
|
|
|
|
|
|
|
class PostFourFiveSix < Struct.new(:user); end
|
2016-01-14 09:15:30 -05:00
|
|
|
|
2015-11-20 22:02:56 -05:00
|
|
|
class CommentFourFiveSix; extend ActiveModel::Naming; end
|
|
|
|
|
|
|
|
module ProjectOneTwoThree
|
|
|
|
class CommentFourFiveSixPolicy < Struct.new(:user, :post); end
|
2016-01-14 09:15:30 -05:00
|
|
|
|
2015-11-20 22:02:56 -05:00
|
|
|
class CriteriaFourFiveSixPolicy < Struct.new(:user, :criteria); end
|
2016-01-14 09:15:30 -05:00
|
|
|
|
2015-11-20 22:02:56 -05:00
|
|
|
class PostFourFiveSixPolicy < Struct.new(:user, :post); end
|
2016-01-14 09:15:30 -05:00
|
|
|
|
2015-11-20 22:02:56 -05:00
|
|
|
class TagFourFiveSix < Struct.new(:user); end
|
2016-01-14 09:15:30 -05:00
|
|
|
|
2015-11-20 22:02:56 -05:00
|
|
|
class TagFourFiveSixPolicy < Struct.new(:user, :tag); end
|
2016-01-14 09:15:30 -05:00
|
|
|
|
2015-11-20 22:02:56 -05:00
|
|
|
class AvatarFourFiveSix; extend ActiveModel::Naming; end
|
2016-01-14 09:15:30 -05:00
|
|
|
|
2015-11-20 22:02:56 -05:00
|
|
|
class AvatarFourFiveSixPolicy < Struct.new(:user, :avatar); end
|
|
|
|
end
|