Sha256: 43eeef4f924164c19282c6c10c0a57856a83714d4e48d3109819300b52a1e12d

Contents?: true

Size: 1.52 KB

Versions: 21

Compression:

Stored size: 1.52 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 { |i| csv << fields.map { |f| i.send(f) } }
      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)
      eager_loading = @resource[:class].reflect_on_all_associations(:belongs_to).map { |i| i.name }
      options = { :joins => @joins, :conditions => @conditions, :order => @order, :include => eager_loading }
      options.merge!(args.extract_options!)
      @resource[:class].find(:all, options)
    end

  end

end

Version data entries

21 entries across 21 versions & 2 rubygems

Version Path
fesplugas-typus-0.9.16 lib/typus/format.rb
fesplugas-typus-0.9.17 lib/typus/format.rb
typus-0.9.35 lib/typus/format.rb
typus-0.9.34 lib/typus/format.rb
typus-0.9.33 lib/typus/format.rb
typus-0.9.32 lib/typus/format.rb
typus-0.9.31 lib/typus/format.rb
typus-0.9.30 lib/typus/format.rb
typus-0.9.29 lib/typus/format.rb
typus-0.9.28 lib/typus/format.rb
typus-0.9.27 lib/typus/format.rb
typus-0.9.26 lib/typus/format.rb
typus-0.9.25 lib/typus/format.rb
typus-0.9.24 lib/typus/format.rb
typus-0.9.23 lib/typus/format.rb
typus-0.9.22 lib/typus/format.rb
typus-0.9.21 lib/typus/format.rb
typus-0.9.20 lib/typus/format.rb
typus-0.9.19 lib/typus/format.rb
typus-0.9.18 lib/typus/format.rb