Sha256: 1b98ae69446713dbc90ef490a4dd1c6756030fb72f8aab8b9e0d5f5cbf1e8f5e

Contents?: true

Size: 1.42 KB

Versions: 16

Compression:

Stored size: 1.42 KB

Contents

module Typus

  module Format

    protected

    def generate_html

      items_count = @resource[:class].count(:joins => @joins, :conditions => @conditions)
      items_per_page = @resource[:class].typus_options_for(:per_page).to_i

      @pager = ::Paginator.new(items_count, items_per_page) do |offset, per_page|
        data(:limit => per_page, :offset => offset)
      end

      @items = @pager.page(params[:page])

      select_template :index

    end

    def generate_csv

      require 'fastercsv'

      fields = @resource[:class].typus_fields_for(:csv).collect { |i| i.first }
      csv_string = FasterCSV.generate do |csv|
        csv << fields.map { |f| _(f.humanize) }
        data.each do |item|
          csv << fields.map { |f| item.send(f) }
        end
      end

      filename = "#{Time.now.strftime("%Y%m%d%H%M%S")}_#{@resource[:self]}.csv"
      send_data(csv_string,
               :type => 'text/csv; charset=utf-8; header=present',
               :filename => filename)

    rescue LoadError
      render :text => _("FasterCSV is not installed.")
    end

    def generate_xml
      fields = @resource[:class].typus_fields_for(:xml).collect { |i| i.first }
      render :xml => data.to_xml(:only => fields)
    end

    def data(*args)
      options = { :joins => @joins, :conditions => @conditions, :order => @order }
      options.merge!(args.extract_options!)
      @resource[:class].find(:all, options)
    end

  end

end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
fesplugas-typus-0.9.0 lib/typus/format.rb
fesplugas-typus-0.9.1 lib/typus/format.rb
fesplugas-typus-0.9.10 lib/typus/format.rb
fesplugas-typus-0.9.11 lib/typus/format.rb
fesplugas-typus-0.9.12 lib/typus/format.rb
fesplugas-typus-0.9.13 lib/typus/format.rb
fesplugas-typus-0.9.14 lib/typus/format.rb
fesplugas-typus-0.9.15 lib/typus/format.rb
fesplugas-typus-0.9.2 lib/typus/format.rb
fesplugas-typus-0.9.3 lib/typus/format.rb
fesplugas-typus-0.9.4 lib/typus/format.rb
fesplugas-typus-0.9.5 lib/typus/format.rb
fesplugas-typus-0.9.6 lib/typus/format.rb
fesplugas-typus-0.9.7 lib/typus/format.rb
fesplugas-typus-0.9.8 lib/typus/format.rb
fesplugas-typus-0.9.9 lib/typus/format.rb