Sha256: 90619e12b2593071ae9c1714845b3d6621aafe4b4cb76aba931c484b4a5ea025

Contents?: true

Size: 1.01 KB

Versions: 3

Compression:

Stored size: 1.01 KB

Contents

require 'will_paginate/array'

class DataController < ApplicationController

  def index
    range =
      case params['search']
      when 'secondhalf' then (51..100)
      else (1..100)
      end
    paginated_table('data', data(range), :defaults => { :sort_column => 'id' })
  end

  private

  class Datum < Struct.new(:id, :name)
    extend ActiveModel::Naming

    def to_key
      [object_id]
    end

    def self.name
      "Data"
    end
  end

  module OrderableData
    def order(arg)
      column, direction = arg.split
      sorted_data = case column
      when 'name' then sort_by { |datum| datum[1] }
      when 'id' then sort_by { |datum| datum[0] }
      else raise "Invalid column: #{column}"
      end
      case direction
      when 'asc' then sorted_data
      when 'desc' then sorted_data.reverse
      else raise "Invalid direction: #{direction}"
      end
    end
  end
    
  def data(ids)
    data = ids.map { |i| Datum.new(i, "Name #{i}") }
    data.shuffle!
    data.extend(OrderableData)
    data
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
paginated_table-0.0.9 test/dummy/app/controllers/data_controller.rb
paginated_table-0.0.8 test/dummy/app/controllers/data_controller.rb
paginated_table-0.0.7 test/dummy/app/controllers/data_controller.rb