Class: Presentation::Grid::Field

Inherits:
Presenting::Attribute show all
Defined in:
lib/presentation/grid.rb

Instance Attribute Summary collapse

Attributes inherited from Presenting::Attribute

#name, #sanitize, #value

Instance Method Summary collapse

Methods inherited from Presenting::Attribute

#id, #id=, #sanitize?, #value_from

Methods included from Presenting::Configurable

#initialize

Instance Attribute Details

#sort_nameObject (readonly)

Returns the value of attribute sort_name.



76
77
78
# File 'lib/presentation/grid.rb', line 76

def sort_name
  @sort_name
end

Instance Method Details

#is_sorted?(request) ⇒ Boolean

is this field sorted in the given request?

Returns:

  • (Boolean)


79
80
81
82
83
84
85
# File 'lib/presentation/grid.rb', line 79

def is_sorted?(request)
  @is_sorted ||= if sortable? and sorting = request.query_parameters["sort"] and sorting[sort_name]
    sorting[sort_name].to_s.match(/desc/i) ? 'desc' : 'asc'
  else
    false
  end
end

#sortable=(val) ⇒ Object

Defines how this field sorts. This means two things:

1. whether it sorts
2. what name it uses to sort

Examples:

# The field is sortable and assumes the sort_name of "first_name".
# This is the default.
Field.new(:sortable => true, :name => "First Name")

# The field is sortable and assumes the sort_name of "first".
Field.new(:sortable => 'first', :name => 'First Name')

# The field is unsortable.
Field.new(:sortable => false)


60
61
62
63
64
65
66
67
# File 'lib/presentation/grid.rb', line 60

def sortable=(val)
  @sort_name = case val
  when TrueClass, FalseClass, NilClass
    val
  else
    val.to_s
  end
end

#sortable?Boolean

if the field is sortable at all

Returns:

  • (Boolean)


70
71
72
73
74
# File 'lib/presentation/grid.rb', line 70

def sortable?
  self.sortable = Presenting::Defaults.grid_is_sortable unless defined? @sort_name
  self.sortable = self.id if @sort_name == true
  !@sort_name.blank?
end

#sorted_url(request) ⇒ Object

for the view – modifies the current request such that it would sort this field.



88
89
90
91
92
93
94
95
# File 'lib/presentation/grid.rb', line 88

def sorted_url(request)
  if current_direction = is_sorted?(request)
    next_direction = current_direction == 'desc' ? 'asc' : 'desc'
  else
    next_direction = 'desc'
  end
  request.path + '?' + request.query_parameters.merge("sort" => {sort_name => next_direction}).to_param
end