Sha256: 847d191476f39910af5d1ffe84e8dbef2e5268450caac9cb1f6ab4fadf514a2b

Contents?: true

Size: 1.93 KB

Versions: 4

Compression:

Stored size: 1.93 KB

Contents

module Pageflow
  class AccountMemberQuery
    class Scope
      # Account whose members we scope
      # @return {Pageflow::Account}
      attr_reader :account

      # Base scope which is further scoped according to account role
      # @return [ActiveRecord::Relation<User>]
      attr_reader :scope

      # Create scope that can limit base scope to account members at
      # or above a given role
      #
      # @param {Pageflow::Account} account
      #   Required. Membership account to check.
      # @param [ActiveRecord::Relation<User>] scope
      #   Optional. Membership entity to check.
      def initialize(account, scope = User.all)
        @account = account
        @scope = scope
      end

      # Scope to those members from scope on account who have at least
      # role
      #
      # @param {String} role
      #   Required. Minimum role that we compare against.
      # @return [ActiveRecord::Relation<User>]
      def with_role_at_least(role)
        scope.joins(memberships_for_account_with_at_least_role(role))
             .where(membership_is_present)
      end

      private

      # @api private
      def memberships_for_account_with_at_least_role(role)
        options = {roles: Roles.at_least(role), account_id: account.id}

        sanitize_sql(<<-SQL, options)
          LEFT OUTER JOIN pageflow_memberships as
            pageflow_account_memberships ON
          pageflow_account_memberships.user_id = users.id AND
          pageflow_account_memberships.role IN (:roles) AND
          pageflow_account_memberships.entity_id = :account_id AND
          pageflow_account_memberships.entity_type = 'Pageflow::Account'
        SQL
      end

      # @api private
      def membership_is_present
        'pageflow_account_memberships.entity_id IS NOT NULL'
      end

      # @api private
      def sanitize_sql(sql, interpolations)
        ActiveRecord::Base.send(:sanitize_sql_array, [sql, interpolations])
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
pageflow-12.0.4 app/models/pageflow/account_member_query.rb
pageflow-12.0.3 app/models/pageflow/account_member_query.rb
pageflow-12.0.2 app/models/pageflow/account_member_query.rb
pageflow-12.0.1 app/models/pageflow/account_member_query.rb