Sha256: 2e51085c8e91cf50d0a3544b9e4c290286ff32c02a09baac0b799bc9b383a90f

Contents?: true

Size: 1.3 KB

Versions: 3

Compression:

Stored size: 1.3 KB

Contents

# -*- encoding : utf-8 -*-
require 'search/conditions/abstract'

module RademadeAdmin
  module Search
    module Conditions
      class RelatedList < Abstract

        attr_reader :item

        protected

        def initialize(item, params, data_items)
          @item = item
          super(params, data_items)
        end

        def where
          where_conditions = RademadeAdmin::Search::Part::Where.new(:and)
          where_conditions.add(:id, related_item_ids)
          @params.slice(*@data_items.origin_fields).each do |field, value|
            where_conditions.add(field, value)
          end
          where_conditions
        end

        def order
          order_conditions = super
          field = @params[:sort] || default_order_field
          order_conditions.unshift(field, @params[:direction])
          order_conditions
        end

        def page
          @params[:page] || 1
        end

        def per_page
          @params[:paginate] || 20
        end

        private

        def default_order_field
          @data_items.has_field?(:position) ? :position : :id
        end

        def related_item_ids
          related_items = @item.send(params[:relation])
          related_items.map do |related_item|
            related_item.id.to_s
          end
        end

      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rademade_admin-0.1.3 app/services/search/conditions/related_list.rb
rademade_admin-0.1.2 app/services/search/conditions/related_list.rb
rademade_admin-0.1.1 app/services/search/conditions/related_list.rb