diff options
| author | Zog | 2018-03-08 15:58:34 +0100 | 
|---|---|---|
| committer | Zog | 2018-03-12 12:00:15 +0100 | 
| commit | 9b1da47adecbfb8d5d148fce06ad1aece18b41d0 (patch) | |
| tree | 3d10008a4a05f3a1a9de762d61fd849031aec609 /app/models/export | |
| parent | 132db2e97fd6e28105e472ee562062b7ebc07b39 (diff) | |
| download | chouette-core-9b1da47adecbfb8d5d148fce06ad1aece18b41d0.tar.bz2 | |
Refs #6133: Async exports
Diffstat (limited to 'app/models/export')
| -rw-r--r-- | app/models/export/base.rb | 4 | ||||
| -rw-r--r-- | app/models/export/referential_companies.rb | 39 | 
2 files changed, 37 insertions, 6 deletions
| diff --git a/app/models/export/base.rb b/app/models/export/base.rb index ae34be0d0..b078da273 100644 --- a/app/models/export/base.rb +++ b/app/models/export/base.rb @@ -61,6 +61,10 @@ class Export::Base < ActiveRecord::Base      true    end +  def visible_options +    options.select{|k, v| ! k.match  /^_/} +  end +    def display_option_value option_name, context      option = self.class.options[option_name.to_sym]      val = self.options[option_name.to_s] diff --git a/app/models/export/referential_companies.rb b/app/models/export/referential_companies.rb index 7a796fb48..0b6187060 100644 --- a/app/models/export/referential_companies.rb +++ b/app/models/export/referential_companies.rb @@ -5,35 +5,62 @@ class Export::ReferentialCompanies < Export::Base      required: true,      display: ->(val){r = Referential.find(val); link_to(r.name, [r])} -  after_commit :call_exporter, on: :create +  after_create :call_exporter_async    def referential      Referential.find referential_id    end +  def call_exporter_async +    SimpleExportWorker.perform_async(id) +  end + +  def exporter +    SimpleExporter.define :referential_companies do |config| +      config.separator = ";" +      config.encoding = 'ISO-8859-1' +      config.add_column :name +      config.add_column :registration_number +    end + +    @exporter ||= begin +      if options[:_exporter_id] +        exporter = SimpleExporter.find options[:exporter_id] +      else +        exporter = SimpleExporter.create configuration_name: :referential_companies +        options[:_exporter_id] = exporter.id +      end +      exporter +    end +  end +    def call_exporter      tmp = Tempfile.new ["referential_companies", ".csv"] -    exporter =  SimpleExporter.create configuration_name: :referential_companies, filepath: tmp.path      referential.switch      exporter.configure do |config|        config.collection = referential.companies.order(:name)      end +    exporter.filepath = tmp.path      exporter.export +    set_status_from_exporter +    convert_exporter_journal_to_messages +    self.file = tmp +    self.save! +  end + +  def set_status_from_exporter      if exporter.status == :error        self.status = :failed      else -      self.file = tmp        if exporter.status == :success          self.status = :successful        else          self.status = :warning        end      end -    convert_exporter_journal_to_messages exporter -    self.save!    end -  def convert_exporter_journal_to_messages exporter +  def convert_exporter_journal_to_messages      self.messages.destroy_all      exporter.journal.each do |journal_item|        journal_item.symbolize_keys! | 
