module SortableHelper

Constants

DEFAULT_GROUP

Public Instance Methods

sortable_filter_tag(column_name, options_list, opts = {}) click to toggle source
# File lib/sortable_skima/helpers/sortable_helper.rb, line 6
def sortable_filter_tag(column_name, options_list,  opts = {})
  opts = {:raw_sql=>false, :group=>DEFAULT_GROUP}.merge(opts)

  srtbl = get_sortable opts

  selected_value = opts.delete(:selected_value) || opts.delete(:selected)
  selected_value_id = nil
  opts[:class] = "#{opts[:class]} sortable-filter"
  filter_id = srtbl.filters.length
  options = []
  new_options_list = []
  options_list.each_with_index do |val,key|
    unless val.is_a?(Array)
      val = [val,val]
    end
    key_val = val[1]
    selected_value_id = options.length if !selected_value.nil? && selected_value == key_val
    new_options_list<<[val[0],options.length]
    options<<key_val
  end
  options_list = new_options_list

  filter = {:id=>filter_id,:column_name=>column_name,:options=>options,:raw_sql=>opts.delete(:raw_sql)}
  srtbl.add_filter filter

  opts['data-filter-key'] = filter_id
  opts['data-group-id'] = opts.delete(:group);
  name = "filters[#{column_name}]"


  option_tags = options_for_select(options_list, selected_value) #TODO build default list from column maybe ...IE User.all.map{username, id}
  select_tag(name, option_tags, opts)
end
sortable_table_tag(base_query, columns=[], opts={}) click to toggle source

table options

- items_per_page
- disable_backtrace
- group

col options

- sort_field
- display_method
- path_method
- title
- no_display
- td_class
- link_item
- disabled
# File lib/sortable_skima/helpers/sortable_helper.rb, line 57
def sortable_table_tag base_query, columns=[], opts={}

  default_opts = {
      :default_order=>nil,
      :class =>'sortable-list',
      :items_per_page=>10,
      :disable_backtrace=>false,
      :paginate=>true,
      :group=>DEFAULT_GROUP
  }
  opts ||= {}
  default_opts[:class] = "#{opts[:class]} #{default_opts[:class]}" #any parameter added classes plus 'sortableList'
  opts = default_opts.merge opts

  do_paginate = opts.delete(:paginate) #opts[:items_per_page]
  disable_backtrace = opts.delete(:disable_backtrace)

  srtbl = get_sortable opts
  srtbl.base_query = base_query
  srtbl.add_option :default_order, opts.delete(:default_order)
  srtbl.add_option :per_page, opts[:items_per_page] if opts[:items_per_page]
  srtbl.add_option :disable_backtrace, disable_backtrace
  srtbl.add_option :paginate, do_paginate

  out = ''
  thead = ''

  columns.each do |col|
    sort_field =  col.delete(:sort_field)
    display_method = col.delete(:display_method) || sort_field
    col_title = (col.delete(:title) || sort_field).html_safe
    path_method = col.delete(:path_method)

    col['sort-field'] = srtbl.add_column(sort_field,display_method, path_method)
    unless col[:no_display] #column is not shown on front table but is still passed to js
      col['col-title']=col_title
      col['data-class']=col.delete(:td_class)
      col['data-link-item']=col.delete(:link_item)
      thead += content_tag( 'th', col_title, col).html_safe
    end
  end

  thead = content_tag('thead',content_tag('tr',thead.html_safe).html_safe)
  tbody = content_tag('tbody','')
  out = (thead + tbody).html_safe

  srtbl.save
  opts['data-sortable-query'] = srtbl.id


  opts['data-per-page'] = opts.delete(:items_per_page)
  opts['data-table-token'] = srtbl.token
  opts['data-group-id'] = opts.delete(:group)
  opts['data-initialize-on-client'] = disable_backtrace

  out = content_tag 'table', out, opts
  if do_paginate
    out += content_tag 'div','',:class=>'paginate-container','data-group-id'=>opts['data-group-id']
  end
  out
end

Private Instance Methods

get_sortable(opts) click to toggle source
# File lib/sortable_skima/helpers/sortable_helper.rb, line 120
def get_sortable opts
  @sortable_groups ||= {}
  srtbl = @sortable_groups[opts[:group]]
  unless srtbl
    srtbl = Sortable.new
    srtbl.token = "#{controller_name}_#{action_name}"
    @sortable_groups[opts[:group]] = srtbl
  end
  srtbl
end