class DoodadPolicy
  attr_reader :user, :doodad

  def initialize(user, doodad)
    @user = user || User.new
    @doodad = doodad
  end

  class Scope
    def initialize(user, scope)
      @user  = user
      @scope = scope
    end

    def resolve
      if user.admin?
        scope.all
      else
        scope.where(is_public: true)
      end
    end

    private

    attr_reader :user, :scope
  end

  def index?
    true
  end

  def show?
    @user.admin? || @user == @doodad.user || @doodad.is_public?
  end

  def create?
    @user.admin? || true
  end

  def new?
    @user.present? || true
  end

  def edit?
   @user.admin? || @user == @doodad.user || (@doodad.is_public? && @doodad.user.nil?)
  end

  def update?
    @user.admin? || @user == @doodad.user || (@doodad.is_public? && @doodad.user.nil?)
  end

  def destroy?
    @user.admin? || @user == @doodad.user || @doodad.user.nil?
  end

end
