Class: Presentation::Grid::Field
- Inherits:
-
Presenting::Attribute
- Object
- Presenting::Attribute
- Presentation::Grid::Field
- Defined in:
- lib/presentation/grid.rb
Instance Attribute Summary collapse
-
#sort_name ⇒ Object
readonly
Returns the value of attribute sort_name.
Attributes inherited from Presenting::Attribute
Instance Method Summary collapse
-
#is_sorted?(request) ⇒ Boolean
is this field sorted in the given request?.
-
#sortable=(val) ⇒ Object
Defines how this field sorts.
-
#sortable? ⇒ Boolean
if the field is sortable at all.
-
#sorted_url(request) ⇒ Object
for the view – modifies the current request such that it would sort this field.
Methods inherited from Presenting::Attribute
#id, #id=, #sanitize?, #value_from
Methods included from Presenting::Configurable
Instance Attribute Details
#sort_name ⇒ Object (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?
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
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 |