Sha256: 54b05653f52761207916d160c5ee026c109184738f627d2fa80206c2d9056d95

Contents?: true

Size: 762 Bytes

Versions: 93

Compression:

Stored size: 762 Bytes

Contents

# frozen_string_literal: true

module Motor
  module ApiQuery
    module ApplyScope
      module_function

      def call(rel, scope)
        return rel if scope.blank?

        scope_symbol = scope.to_sym

        if rel.klass.defined_scopes.include?(scope_symbol)
          rel.public_send(scope_symbol)
        else
          apply_filter_scope(rel, scope)
        end
      end

      def apply_filter_scope(rel, scope)
        configs = Motor::Resource.find_by(name: rel.klass.name.underscore)

        return rel unless configs

        scope_configs = configs.preferences[:scopes].find { |s| s[:name] == scope }

        return rel unless scope_configs

        ApiQuery::Filter.call(rel, scope_configs[:preferences][:filter])
      end
    end
  end
end

Version data entries

93 entries across 93 versions & 1 rubygems

Version Path
motor-admin-0.1.61 lib/motor/api_query/apply_scope.rb
motor-admin-0.1.60 lib/motor/api_query/apply_scope.rb
motor-admin-0.1.58 lib/motor/api_query/apply_scope.rb
motor-admin-0.1.57 lib/motor/api_query/apply_scope.rb
motor-admin-0.1.56 lib/motor/api_query/apply_scope.rb
motor-admin-0.1.55 lib/motor/api_query/apply_scope.rb
motor-admin-0.1.54 lib/motor/api_query/apply_scope.rb
motor-admin-0.1.53 lib/motor/api_query/apply_scope.rb
motor-admin-0.1.52 lib/motor/api_query/apply_scope.rb
motor-admin-0.1.51 lib/motor/api_query/apply_scope.rb
motor-admin-0.1.50 lib/motor/api_query/apply_scope.rb
motor-admin-0.1.49 lib/motor/api_query/apply_scope.rb
motor-admin-0.1.48 lib/motor/api_query/apply_scope.rb