diff options
| author | Alban Peignier | 2012-06-07 13:39:35 +0200 | 
|---|---|---|
| committer | Alban Peignier | 2012-06-07 13:39:35 +0200 | 
| commit | 7c0ae629a2ea0079c8b5efbd4c41b6ca661eae91 (patch) | |
| tree | 26195fe0d4eb8e77a89a9dadaf25626fc3f7fc50 /app/models/export.rb | |
| parent | 1d72c64b77c20d0d3b0bf4b05c40ba99a97493bd (diff) | |
| download | chouette-core-7c0ae629a2ea0079c8b5efbd4c41b6ca661eae91.tar.bz2 | |
Add support for Exports. Refs #41
Diffstat (limited to 'app/models/export.rb')
| -rw-r--r-- | app/models/export.rb | 84 | 
1 files changed, 84 insertions, 0 deletions
diff --git a/app/models/export.rb b/app/models/export.rb new file mode 100644 index 000000000..a202e77ef --- /dev/null +++ b/app/models/export.rb @@ -0,0 +1,84 @@ +class Export < ActiveRecord::Base + +  belongs_to :referential +  validates_presence_of :referential_id + +  validates_inclusion_of :status, :in => %w{ pending completed failed } + +  has_many :log_messages, :class_name => "ExportLogMessage", :order => :position, :dependent => :destroy + +  serialize :options + +  def self.option(name) +    name = name.to_s + +    define_method(name) do +      self.options[name] +    end + +    define_method("#{name}=") do |prefix| +      self.options[name] = prefix +    end +  end + +  def options +    read_attribute(:options) || write_attribute(:options, {}) +  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, :output_file => file } +  end + +  before_validation :define_default_attributes, :on => :create +  def define_default_attributes +    self.status ||= "pending" +  end + +  after_create :delayed_export +  def delayed_export +    delay.export +  end + +  def export +    FileUtils.mkdir_p root + +    begin +      log_messages.create :key => :started + +      # TODO +      # Make real export here + +      update_attribute :status, "completed" +    rescue => e +      Rails.logger.error "Export #{id} failed : #{e}, #{e.backtrace}" +      update_attribute :status, "failed" +    end + +    log_messages.create :key => status +  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  | 
