# frozen_string_literal: true

module SolidusSubscriptions
  module PermissionSets
    class DefaultCustomer < ::Spree::PermissionSets::Base
      def activate!
        can :manage, Subscription, Subscription.where(user: user) do |subscription, guest_token|
          (subscription.guest_token.present? && subscription.guest_token == guest_token) ||
            (subscription.user && subscription.user == user)
        end

        can :manage, LineItem do |line_item, guest_token|
          (line_item.subscription&.guest_token.present? && line_item.subscription.guest_token == guest_token) ||
            (line_item.subscription&.user && line_item.subscription.user == user)
        end
      end
    end
  end
end

# frozen_string_literal: true

module SolidusSubscriptions
  module PermissionSets
    class DefaultCustomer < ::Spree::PermissionSets::Base
      def activate!
        can :manage, Subscription, ['user_id = ?', user.id] do |subscription, guest_token|
          (subscription.guest_token.present? && subscription.guest_token == guest_token) ||
            (subscription.user && subscription.user == user)
        end

        can :manage, LineItem do |line_item, guest_token|
          (line_item.subscription&.guest_token.present? && line_item.subscription.guest_token == guest_token) ||
            (line_item.subscription&.user && line_item.subscription.user == user)
        end
      end
    end
  end
end

Fix DefaultCustomer permission set with CanCan 2
Fix DefaultCustomer permission set with CanCan 2

CanCan 2 complains about not being able to merge abilities that use
an ActiveRecord scope, so we need to use an SQL string instead.
