blob: 041d30eb9564e2fb30b8145eba47ed0f17359353 (
plain)
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
  | 
# -*- coding: utf-8 -*-
class StopAreaCopy
  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend  ActiveModel::Naming
  attr_accessor :source_id, :hierarchy, :area_type, :source, :copy
  
  validates_presence_of :source_id, :hierarchy, :area_type
  
  validates :hierarchy, inclusion: { in: %w(child parent) }
    
  def initialize(attributes = {})    
    attributes.each { |name, value| send("#{name}=", value) } if attributes
    if self.area_type.blank? && self.source != nil
      self.source_id = self.source.id
      if self.hierarchy == "child"
        if self.source.area_type.underscore == "stop_place"
          self.area_type="commercial_stop_point"
        else
          self.area_type="boarding_position"
        end
      else
        if self.source.area_type.underscore == "stop_place" || self.source.area_type.underscore == "commercial_stop_point"
          self.area_type="stop_place"
        else
          self.area_type="commercial_stop_point"
        end
      end
    end
  end
  
  def persisted?
    false
  end
  
  def save
    begin
      if self.valid?
        self.source ||= Chouette::StopArea.find self.source_id
        self.copy = source.duplicate
        self.copy.name = source.name
        self.copy.area_type = self.area_type.camelcase
        Chouette::StopArea.transaction do
          if self.hierarchy == "child"
            self.copy.parent_id = source.id
          end
          self.copy.save!
          if self.hierarchy == "parent"
            self.source.parent_id = copy.id
            self.source.save!
          end
        end
        true
      else
        false  
      end
    rescue Exception => exception
      Rails.logger.error(exception.message)
      errors.add :base, I18n.t("stop_area_copies.errors.exception")
      false
    end
  end   
  
  
end
  |