Class: Trestle::SortHelper::SortLink

Inherits:
Object
  • Object
show all
Defined in:
app/helpers/trestle/sort_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field, params, options) ⇒ SortLink

Returns a new instance of SortLink.


11
12
13
# File 'app/helpers/trestle/sort_helper.rb', line 11

def initialize(field, params, options)
  @field, @params, @options = field, params, options
end

Instance Attribute Details

#fieldObject (readonly)

Returns the value of attribute field


9
10
11
# File 'app/helpers/trestle/sort_helper.rb', line 9

def field
  @field
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)

15
16
17
18
# File 'app/helpers/trestle/sort_helper.rb', line 15

def active?
  @params[:sort] == field.to_s ||
    (@options[:default] && !@params.key?(:sort))
end

#classesObject


40
41
42
43
44
45
46
47
48
49
# File 'app/helpers/trestle/sort_helper.rb', line 40

def classes
  classes = ["sort"]

  if active?
    classes << "sort-#{current_order}"
    classes << "active"
  end

  classes
end

#current_orderObject


32
33
34
# File 'app/helpers/trestle/sort_helper.rb', line 32

def current_order
  @params[:order] || default_order
end

#default_orderObject


36
37
38
# File 'app/helpers/trestle/sort_helper.rb', line 36

def default_order
  @options.fetch(:default_order, "asc").to_s
end

#orderObject


24
25
26
27
28
29
30
# File 'app/helpers/trestle/sort_helper.rb', line 24

def order
  if active?
    reverse_order(current_order)
  else
    default_order
  end
end

#paramsObject


20
21
22
# File 'app/helpers/trestle/sort_helper.rb', line 20

def params
  @params.merge(sort: field, order: order)
end

#reverse_order(order) ⇒ Object


51
52
53
# File 'app/helpers/trestle/sort_helper.rb', line 51

def reverse_order(order)
  order == "asc" ? "desc" : "asc"
end