Sha256: febfc4b8bcab7b9f8d45618bcfa7ba5366d1dacdfed6201645be8adba00d214f

Contents?: true

Size: 990 Bytes

Versions: 4

Compression:

Stored size: 990 Bytes

Contents

module JeraPush
  class DeviceFilter
    include ActiveModel::Model

    attr_accessor :platform, :value, :field, :message_id

    def platform
      @platform ||= []
    end

    def search
      @scope = JeraPush::Device.joins(:resource).includes(:resource)
      if message_id
        @scope = @scope.joins(:messages).where('jera_push_messages.id = ?', message_id)
      end
      if platform.any?
        @scope = @scope.where('jera_push_devices.platform in (?)', platform)
      end
      if field.present? && value.present?
        @scope = search_with_value_field(@scope)
      end
      @scope
    end

    private

    def search_with_value_field(scope)
      if field.to_sym == :token
        return scope.where('jera_push_devices.token like ?', "%#{value}%")
      elsif JeraPush.resource_attributes.include?(field.to_sym)
        return scope.where("#{JeraPush.resource_name.constantize.table_name}.#{field} like ?", "%#{value}%")
      end
      return scope
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
jera_push-1.0.3 lib/jera_push/models/device_filter.rb
jera_push-1.0.2 lib/jera_push/models/device_filter.rb
jera_push-1.0.1.1 lib/jera_push/models/device_filter.rb
jera_push-1.0.1 lib/jera_push/models/device_filter.rb