Sha256: 5f3bd002a0b598ecb3df83f68ff1bfda2188fa4fea6f65894edbe79aff8b28b2

Contents?: true

Size: 815 Bytes

Versions: 4

Compression:

Stored size: 815 Bytes

Contents

# -*- encoding : utf-8 -*-
module RademadeAdmin
  class SortableService

    def initialize(model_info, params)
      @model_info = model_info
      @params = params
    end

    def sort_items
      @params[:sorted].each do |_, sorted_data|
        item = @model_info.query_adapter.find(sorted_data[:id])
        item.position = sorted_data[:position].to_i
        item.save
      end
    end

    def can_reset?
      @params[:sort].present?
    end

    #
    # @return [RademadeAdmin::Model::Info::DataItem]
    #
    def sorting_sign(field)
      if field.name.to_s == @params[:sort]
        return '⬇' if @params[:direction] == 'asc'
        return '⬆' if @params[:direction] == 'desc'
      end
      ''
    end

    def has_position?
      @model_info.data_items.has_field? :position
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rademade_admin-0.1.3 app/services/sortable_service.rb
rademade_admin-0.1.2 app/services/sortable_service.rb
rademade_admin-0.1.1 app/services/sortable_service.rb
rademade_admin-0.1.0 app/services/sortable_service.rb