Class: RademadeAdmin::SortableService

Inherits:
Object
  • Object
show all
Defined in:
app/services/sortable_service.rb

Instance Method Summary collapse

Constructor Details

#initialize(model_info, params) ⇒ SortableService

Returns a new instance of SortableService.



5
6
7
8
# File 'app/services/sortable_service.rb', line 5

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

Instance Method Details

#can_reset?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'app/services/sortable_service.rb', line 18

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

#has_position?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'app/services/sortable_service.rb', line 30

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

#sort_itemsObject



10
11
12
13
14
15
16
# File 'app/services/sortable_service.rb', line 10

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

#sorting_sign(field) ⇒ Object



22
23
24
25
26
27
28
# File 'app/services/sortable_service.rb', line 22

def sorting_sign(field)
  if field.order_column.to_s == @params[:sort]
    return '' if @params[:direction] == 'asc'
    return '' if @params[:direction] == 'desc'
  end
  ''
end