Sha256: e0a5242904daf907f89df83d0fc6a5d77f31ac2ca08161327130823d2bbcfc91

Contents?: true

Size: 917 Bytes

Versions: 11

Compression:

Stored size: 917 Bytes

Contents

require "enju_export/engine"

module EnjuExport
  module ActsAsMethods
    def self.included(base)
      base.extend ClassMethods
    end

    module ClassMethods
      def enju_export
        include ExportCSV
      end
    end

    module ExportCSV
      def self.included(base)
        base.extend ClassMethods
      end

      module ClassMethods
        def generate_csv(ids, attrs, export_file, role = nil)
          export_file.sm_start!
          file = Tempfile.new("#{self.name.downcase}_")
          self.where('required_role_id >= ?', role.try(:id).to_i).find_each(:conditions => {:id => ids}) do |record|
            file.write(attrs.map{|attr| record.send(attr)}.join(',') + "\n")
          end
          file.close
          export_file.export = File.new(file.path)
          export_file.sm_complete!
        end
      end
    end
  end
end

ActiveRecord::Base.send :include, EnjuExport::ActsAsMethods

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
enju_export-0.1.1.pre6 lib/enju_export.rb
enju_export-0.1.1.pre5 lib/enju_export.rb
enju_export-0.1.1.pre4 lib/enju_export.rb
enju_export-0.1.1.pre3 lib/enju_export.rb
enju_export-0.1.1.pre2 lib/enju_export.rb
enju_export-0.1.1.pre lib/enju_export.rb
enju_export-0.1.0.pre lib/enju_export.rb
enju_export-0.1.0 lib/enju_export.rb
enju_export-0.0.3 lib/enju_export.rb
enju_export-0.0.2 lib/enju_export.rb
enju_export-0.0.1 lib/enju_export.rb