1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
class Export < ActiveRecord::Base
belongs_to :referential
validates_presence_of :referential_id
validates_inclusion_of :status, :in => %w{ pending processing completed failed }
has_many :log_messages, :class_name => "ExportLogMessage", :order => :position, :dependent => :delete_all
serialize :options
include ::TypeIdsModelable
def self.option(name)
name = name.to_s
define_method(name) do
self.options and self.options[name]
end
define_method("#{name}=") do |prefix|
(self.options ||= {})[name] = prefix
end
end
def exporter
exporter ||= ::Chouette::Exporter.new(referential.slug)
end
@@root = "#{Rails.root}/tmp/exports"
cattr_accessor :root
after_destroy :destroy_file
def destroy_file
FileUtils.rm file if File.exists? file
end
def file
"#{root}/#{id}.zip"
end
def name
"#{self.class.model_name.human} #{id}"
end
def export_options
{ :export_id => self.id, :o => export_object_type }.tap do |options|
options[:id] = reference_ids.join(',') if reference_ids.present?
end
end
def export_object_type
references_relation ? references_relation.singularize : "line"
end
before_validation :define_default_attributes, :on => :create
def define_default_attributes
self.status ||= "pending"
self.options ||= {}
end
after_create :delayed_export
def delayed_export
delay.export
end
def export
result_severity = "ok"
FileUtils.mkdir_p root
begin
# delayed job may repeat call
ExportLogMessage.where(:export_id => self.id).delete_all
log_messages.create :severity => "ok", :key => :started
exporter.export file, export_options
update_attribute :status, "completed"
rescue => e
Rails.logger.error "Export #{id} failed : #{e}, #{e.backtrace}"
update_attribute :status, "failed"
result_severity = "error"
end
log_messages.create :severity => result_severity, :key => status
end
def self.all_references_types
[ Chouette::Line, Chouette::Network, Chouette::Company , Chouette::StopArea]
end
def references_types
[ Chouette::Line, Chouette::Network, Chouette::Company ]
end
# @@references_types = [ Chouette::Line, Chouette::Network, Chouette::Company ]
# cattr_reader :references_types
# validates_inclusion_of :references_type, :in => references_types.map(&:to_s), :allow_blank => true, :allow_nil => true
def self.format_name(format)
name_by_format = {
"NeptuneExport" => "Neptune",
"CsvExport" => "CSV",
"GtfsExport" => "GTFS",
"NetexExport" => "NeTEx",
"KmlExport" => "KML",
"HubExport" => "HUB"
}
name_by_format[format]
end
def self.format_label(format)
I18n.t 'exchange.format.'+format.sub("Export",'').downcase
end
def self.types
unless Rails.env.development?
subclasses.map(&:to_s)
else
# FIXME
%w{NeptuneExport CsvExport GtfsExport NetexExport KmlExport HubExport}
end
end
def self.new(attributes = {}, options = {}, &block)
if self == Export
Object.const_get(attributes.delete(:type) || "NeptuneExport").new(attributes, options)
else
super
end
end
end
|