Sha256: 0e1a6522b5fb3789c18bd8889ce1e9063d01b28824f8096e91cbaec5f51e1feb

Contents?: true

Size: 1.99 KB

Versions: 3

Compression:

Stored size: 1.99 KB

Contents

require 'singleton'
require 'praxis/extensions/attribute_filtering/filtering_params'

module Praxis
  module Plugins
    module MapperPlugin
      include Praxis::PluginConcern

      class Plugin < Praxis::Plugin
        include Singleton

        def config_key
          :mapper
        end

        def load_config!
          {} # override the default one, since we don't necessarily want to configure it via a yaml file.
        end

        def prepare_config!(node)
          node.attributes do
            attribute :debug_queries, Attributor::Boolean, default: false,
              description: 'Weather or not to log debug information about queries executed in the build_query automation module'
          end
        end
      end

      module Controller
        extend ActiveSupport::Concern

        included do
          include Praxis::Extensions::FieldExpansion
        end

        def set_selectors
          return unless self.media_type.respond_to?(:domain_model) &&
            self.media_type.domain_model < Praxis::Mapper::Resource

          resolved = Praxis::MediaType::FieldResolver.resolve(self.media_type, self.expanded_fields)
          selector_generator.add(self.media_type.domain_model, resolved)
        end

        def build_query(base_query) # rubocop:disable Metrics/AbcSize
          domain_model = self.media_type&.domain_model
          raise "No domain model defined for #{self.name}. Cannot use the attribute filtering helpers without it" unless domain_model
          
          filters = request.params.filters if request.params&.respond_to?(:filters)
          base_query = domain_model.craft_filter_query( base_query , filters: filters )

          base_query = domain_model.craft_field_selection_query(base_query, selectors: selector_generator.selectors)

          # TODO: handle pagination and ordering
          base_query
        end

        def selector_generator
          @selector_generator ||= Praxis::Mapper::SelectorGenerator.new
        end

      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
praxis-2.0.pre.5 lib/praxis/plugins/mapper_plugin.rb
praxis-2.0.pre.4 lib/praxis/plugins/mapper_plugin.rb
praxis-2.0.pre.3 lib/praxis/plugins/mapper_plugin.rb