diff options
| author | Zog | 2018-03-26 10:35:19 +0200 |
|---|---|---|
| committer | Zog | 2018-03-26 10:47:25 +0200 |
| commit | 3d370c032c3fa06fd03643263e29b6eded5fe6c1 (patch) | |
| tree | 533ded9a19bd9c5fe703d5c20e195d419420abab /app/models | |
| parent | c2a288a9d9db99ce06eb963246fc5e760cfb831d (diff) | |
| download | chouette-core-3d370c032c3fa06fd03643263e29b6eded5fe6c1.tar.bz2 | |
Refs #6218; Link exports to referentials
Diffstat (limited to 'app/models')
| -rw-r--r-- | app/models/export/base.rb | 4 | ||||
| -rw-r--r-- | app/models/export/simple_exporter/base.rb | 94 |
2 files changed, 97 insertions, 1 deletions
diff --git a/app/models/export/base.rb b/app/models/export/base.rb index 6a1eb791d..6cf4c6b02 100644 --- a/app/models/export/base.rb +++ b/app/models/export/base.rb @@ -1,7 +1,9 @@ class Export::Base < ActiveRecord::Base self.table_name = "exports" - validates :type, presence: true + belongs_to :referential + + validates :type, :referential_id, presence: true def self.messages_class_name "Export::Message" diff --git a/app/models/export/simple_exporter/base.rb b/app/models/export/simple_exporter/base.rb new file mode 100644 index 000000000..4e6e8eba4 --- /dev/null +++ b/app/models/export/simple_exporter/base.rb @@ -0,0 +1,94 @@ +class Export::SimpleExporter::Base < Export::Base + after_commit :call_exporter_async, on: :create + + def self.user_visible? + false + end + + def self.inherited child + super child + child.options = @options + child.instance_eval do + def self.user_visible? + true + end + end + end + + def call_exporter_async + SimpleExportWorker.perform_async(id) + end + + def simple_exporter_configuration_name + + end + + def exporter + @exporter ||= begin + if options[:_exporter_id] + exporter = SimpleJsonExporter.find options[:_exporter_id] + else + exporter = SimpleJsonExporter.create configuration_name: simple_exporter_configuration_name + options[:_exporter_id] = exporter.id + end + exporter + end + end + + def configure_exporter config + end + + def call_exporter + tmp = Tempfile.new [simple_exporter_configuration_name.to_s, ".json"] + referential.switch + exporter.configure do |config| + configure_exporter config + 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.to_s == "error" + self.status = :failed + elsif exporter.status.to_s == "success" + self.status = :successful + else + self.status = :warning + end + end + + def convert_exporter_journal_to_messages + self.messages.destroy_all + exporter.journal.each do |journal_item| + journal_item.symbolize_keys! + vals = {} + + if journal_item[:kind].to_s == "warning" + vals[:criticity] = :warning + elsif journal_item[:kind].to_s == "error" + vals[:criticity] = :error + else + vals[:criticity] = :info + if journal_item[:event].to_s == "success" + vals[:message_key] = :success + end + end + vals[:resource_attributes] = journal_item[:row] + + if journal_item[:message].present? + vals[:message_key] = :full_text + vals[:message_attributes] = { + text: journal_item[:message] + } + end + vals[:message_attributes] ||= {} + vals[:message_attributes][:line] = journal_item[:line] + self.messages.build vals + end + end +end |
