Class: AddMagicAddresses

Inherits:
ActiveRecord::Migration
  • Object
show all
Defined in:
lib/generators/magic_addresses/templates/magic_addresses_migration.rb

Instance Method Summary collapse

Instance Method Details

#downObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/generators/magic_addresses/templates/magic_addresses_migration.rb', line 159

def down
  
  # when use postgres earthdistance
  if MagicAddresses.configuration.earthdistance
    remove_index :mgca_addresses, [:latitude,:longitude], name: "mgca_addresses_earthdistance_ix"
  end
  
  ## Addresses
  remove_index  :mgca_addresses, :subdistrict_id
  remove_index  :mgca_addresses, :district_id
  remove_index  :mgca_addresses, :city_id
  remove_index  :mgca_addresses, :state_id
  remove_index  :mgca_addresses, :country_id
  remove_index  :mgca_addresses, [:owner_type, :owner_id]
  drop_table    :mgca_addresses
  MagicAddresses::Address.drop_translation_table!
  
  
  ## Countries
  drop_table    :mgca_countries
  MagicAddresses::Country.drop_translation_table!
  
  
  ## States
  remove_index  :mgca_states, :country_id
  drop_table    :mgca_states
  MagicAddresses::State.drop_translation_table!
  
  
  ## Cities
  remove_index  :mgca_cities, :state_id
  remove_index  :mgca_cities, :country_id
  drop_table    :mgca_cities
  MagicAddresses::City.drop_translation_table!
  
  
  ## Districts
  remove_index  :mgca_districts, :city_id
  drop_table    :mgca_districts
  MagicAddresses::District.drop_translation_table!
  
  
  ## Subdistricts
  remove_index  :mgca_subdistricts, :district_id
  remove_index  :mgca_subdistricts, :city_id
  drop_table    :mgca_subdistricts
  MagicAddresses::Subdistrict.drop_translation_table!
  
  
  # => # disable extensions for distance calculation
  # => disable_extension "earthdistance"
  # => disable_extension "cube"
  
end

#upObject



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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/generators/magic_addresses/templates/magic_addresses_migration.rb', line 3

def up
  
  # if using with postgresql
  # => # add extensions for distance calculation
  # => enable_extension "cube"
  # => enable_extension "earthdistance"
  
  
  #
  ## Addresses
  create_table :mgca_addresses do |t|
    
    t.string        :name                     # => name (if needed)
    
    t.text          :fetch_address            # => default serialize field
    
    # t.string        :street
    t.string        :street_default
    t.string        :street_number
    t.string        :street_additional
    t.integer       :zipcode
    
    t.string        :status,                  default: "new"
    
    t.float         :longitude
    t.float         :latitude
    
    t.references    :subdistrict
    t.references    :district
    t.references    :city
    t.references    :state
    t.references    :country
    
    t.timestamps    null: false
  end
  
  add_index :mgca_addresses, :subdistrict_id
  add_index :mgca_addresses, :district_id
  add_index :mgca_addresses, :city_id
  add_index :mgca_addresses, :state_id
  add_index :mgca_addresses, :country_id
  
  # when use postgres earthdistance
  if MagicAddresses.configuration.earthdistance
    add_earthdistance_index :mgca_addresses, lat: 'latitude', lng: 'longitude'
  end
  
  MagicAddresses::Address.create_translation_table! :street_name => :string
  
  
  #############################################################################################################
  ##### Addressibles
  #####
  create_table :mgca_addressibles do |t|
    t.boolean       :default      # is default address ?
    t.string        :name         # address name (home | office | ..)
    t.references    :owner,       polymorphic: true
    t.references    :address
    t.timestamps    null: false
  end
  
  add_index :mgca_addressibles, [:owner_type, :owner_id]
  add_index :mgca_addressibles, :address_id
  
  
  #############################################################################################################
  ##### Country
  #####
  create_table :mgca_countries do |t|
    t.string      :default_name
    # t.string    :name
    t.string      :iso_code,       limit: 2
    t.string      :dial_code
    t.string      :fsm_state,      default: "new"
    t.timestamps
  end
  
  MagicAddresses::Country.create_translation_table! :name => :string
  
  # seed some countries in some languages
  load "#{ ::Rails.root }/db/seed_countries.rb"
  
  
  #############################################################################################################
  ##### State
  #####
  create_table :mgca_states do |t|
    t.string      :default_name
    t.string      :short_name
    # t.string      :name
    t.string      :fsm_state,      default: "new"
    t.references  :country
    t.timestamps
  end
  
  add_index :mgca_states, :country_id
  
  MagicAddresses::State.create_translation_table! :name => :string
  
  
  #############################################################################################################
  ##### City
  #####
  create_table :mgca_cities do |t|
    t.string      :default_name
    t.string      :short_name
    # t.string      :name
    t.string      :fsm_state,      default: "new"
    t.references  :state
    t.references  :country
    t.timestamps
  end
  
  add_index :mgca_cities, :state_id
  add_index :mgca_cities, :country_id
  
  MagicAddresses::City.create_translation_table! :name => :string
  
  
  #############################################################################################################
  ##### District
  #####
  create_table :mgca_districts do |t|
    t.string      :default_name
    t.string      :short_name
    # t.string      :name
    t.string      :fsm_state,      default: "new"
    t.references  :city
    t.timestamps
  end
  
  add_index :mgca_districts, :city_id
  
  MagicAddresses::District.create_translation_table! :name => :string
  
  
  #############################################################################################################
  ##### Subdistrict
  #####
  create_table :mgca_subdistricts do |t|
    t.string      :default_name
    t.string      :short_name
    # t.string    :name
    t.string      :fsm_state,      default: "new"
    t.references  :district
    t.references  :city
    t.timestamps
  end
  
  add_index :mgca_subdistricts, :district_id
  add_index :mgca_subdistricts, :city_id
  
  MagicAddresses::Subdistrict.create_translation_table! :name => :string
  
  
end