Sha256: 6bbcc3e5fa9fda9510d1f8b1236ab1fa922e11b303a95a22fdbc3380f132de48

Contents?: true

Size: 1.15 KB

Versions: 4

Compression:

Stored size: 1.15 KB

Contents

module DataTable

  def self.included base
    base.send :extend, ClassMethods
    base.send :extend, Mongoid::ClassMethods
  end

  module ClassMethods
    def for_data_table controller, fields, search_fields=nil, explicit_block=nil, &implicit_block
      params = controller.params
      search_fields ||= fields
      block = (explicit_block or implicit_block)

      objects = _find_objects params, fields, search_fields

      {:sEcho                => params[:sEcho].to_i,
       :iTotalRecords        => self.count,
       :iTotalDisplayRecords => objects.total_entries,
       :aaData               => _yield_and_render_array(controller, objects, block)
      }.to_json.html_safe
    end

    private

    def _yield_and_render_array controller, objects, block
      objects.map do |object|
        block[object].map do |string|
          controller.instance_eval %{
            Rails.logger.silence do
              render_to_string :inline => %Q|#{string}|, :locals => {:#{self.name.downcase} => object}
            end
          }
        end
      end
    end

    def _page params
      params[:iDisplayStart].to_i / params[:iDisplayLength].to_i + 1
    end
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
data_table-0.1.4 lib/data_table/base.rb
data_table-0.1.3 lib/data_table/base.rb
data_table-0.1.2 lib/data_table/base.rb
data_table-0.1.1 lib/data_table/base.rb