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
  | 
# -*- coding: utf-8 -*-
class Referential < ActiveRecord::Base
  validates_presence_of :name 
  validates_presence_of :slug
  validates_presence_of :prefix
  validates_presence_of :time_zone
  validates_uniqueness_of :slug
  validates_uniqueness_of :name
  validates_format_of :slug, :with => %r{\A[0-9a-z_]+\Z}
  validates_format_of :prefix, :with => %r{\A[0-9a-zA-Z_]+\Z}
  has_many :imports, :dependent => :destroy
  has_many :exports, :dependent => :destroy
  after_initialize :init_time_zone
  
  def init_time_zone
    if time_zone.nil?
      self.time_zone = "Paris"
    end
  end
  def human_attribute_name(*args)
    self.class.human_attribute_name(*args)
  end
  def lines
    Chouette::Line.scoped
  end
  def networks
    Chouette::Network.scoped
  end
  def companies
    Chouette::Company.scoped
  end
  def stop_areas
    Chouette::StopArea.scoped
  end
  def time_tables
    Chouette::TimeTable.scoped
  end
  
  def connection_links
    Chouette::ConnectionLink.scoped
  end
  def vehicle_journeys
    Chouette::VehicleJourney.scoped
  end
  after_initialize :define_default_attributes
  def define_default_attributes
    self.time_zone ||= Time.zone.name
  end
  def switch
    raise "Referential not created" if new_record?
    Apartment::Database.switch(slug)
    self
  end
  def self.available_srids
    [
     [ "NTF Lambert Zone 1 (27561)", 27561 ],
     [ "NTF Lambert Zone 2 (27562)", 27562 ],
     [ "NTF Lambert Zone 3 (27563)", 27563 ],
     [ "NTF Lambert Zone 4 (27564)", 27564 ],
     [ "NTF Lambert 2 étendu (27572)", 27582 ],
     ["RGF 93 Lambert 93 (2154)",  2154 ]
    ]
  end
  before_create :create_schema
  before_destroy :destroy_schema
  after_create :import_resources
  attr_accessor :resources
  def import_resources
    imports.create(:resources => resources) if resources
  end
  def create_schema
    Apartment::Database.create slug
  end
  def destroy_schema
    Apartment::Database.drop slug
  end
  
end
Rails.application.config.after_initialize do
  Chouette::ActiveRecord
  class Chouette::ActiveRecord
    def referential
      @referential ||= Referential.where(:slug => Apartment::Database.current_database).first!
    end
  end
end
  |