contactable 0.1.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. data/CHANGELOG +2 -0
  2. data/Manifest +94 -14
  3. data/README.rdoc +27 -1
  4. data/Rakefile +1 -1
  5. data/app/controllers/addresses_controller.rb +3 -0
  6. data/app/controllers/cities_controller.rb +16 -0
  7. data/app/controllers/contact_field_actions.rb +60 -0
  8. data/app/controllers/divisions_controller.rb +17 -0
  9. data/app/controllers/emails_controller.rb +3 -0
  10. data/app/controllers/identifications_controller.rb +3 -0
  11. data/app/controllers/instant_messengers_controller.rb +3 -0
  12. data/app/controllers/phones_controller.rb +3 -0
  13. data/app/controllers/websites_controller.rb +3 -0
  14. data/app/helpers/contactable_helper.rb +46 -0
  15. data/app/models/address.rb +96 -0
  16. data/app/models/address_type.rb +5 -0
  17. data/app/models/city.rb +21 -0
  18. data/app/models/country.rb +8 -0
  19. data/app/models/division.rb +26 -0
  20. data/{generators/contactable/templates → app}/models/email.rb +0 -0
  21. data/app/models/email_type.rb +5 -0
  22. data/app/models/identification.rb +7 -0
  23. data/app/models/identification_type.rb +5 -0
  24. data/{generators/contactable/templates → app}/models/instant_messenger.rb +0 -0
  25. data/{generators/contactable/templates → app}/models/instant_messenger_protocol.rb +2 -2
  26. data/{generators/contactable/templates → app}/models/instant_messenger_type.rb +3 -3
  27. data/{generators/contactable/templates → app}/models/phone.rb +0 -0
  28. data/app/models/phone_type.rb +5 -0
  29. data/{generators/contactable/templates → app}/models/province.rb +0 -0
  30. data/{generators/contactable/templates → app}/models/website.rb +0 -0
  31. data/app/models/website_type.rb +5 -0
  32. data/app/views/addresses/_address.html.erb +31 -0
  33. data/app/views/cities/index.html.erb +6 -0
  34. data/app/views/contactable/_contactable.html.erb +11 -0
  35. data/app/views/contactable/_contactable_field.html.erb +8 -0
  36. data/app/views/contactable/_fields.html.erb +49 -0
  37. data/app/views/divisions/index.html.erb +9 -0
  38. data/app/views/emails/_email.html.erb +2 -0
  39. data/app/views/identifications/_identification.html.erb +2 -0
  40. data/app/views/instant_messengers/_instant_messenger.html.erb +3 -0
  41. data/app/views/phones/_phone.html.erb +2 -0
  42. data/app/views/websites/_website.html.erb +2 -0
  43. data/config/contactable_routes.rb +4 -0
  44. data/contactable.gemspec +5 -5
  45. data/generators/contactable/USAGE +1 -10
  46. data/generators/contactable/contactable_generator.rb +35 -32
  47. data/generators/contactable/lib/insert_commands.rb +33 -0
  48. data/generators/contactable/templates/README +12 -0
  49. data/generators/contactable/templates/migrations/create_contactable_related_tables.rb +153 -26
  50. data/generators/contactable_views/USAGE +1 -0
  51. data/generators/contactable_views/contactable_views_generator.rb +24 -0
  52. data/generators/contactable_views/templates/views/formtastic/addresses/_address.html.erb +31 -0
  53. data/generators/contactable_views/templates/views/formtastic/cities/index.html.erb +6 -0
  54. data/generators/contactable_views/templates/views/formtastic/contactable/_contactable.html.erb +11 -0
  55. data/generators/contactable_views/templates/views/formtastic/contactable/_contactable_field.html.erb +8 -0
  56. data/generators/contactable_views/templates/views/formtastic/contactable/_fields.html.erb +49 -0
  57. data/generators/contactable_views/templates/views/formtastic/divisions/index.html.erb +9 -0
  58. data/generators/contactable_views/templates/views/formtastic/emails/_email.html.erb +2 -0
  59. data/generators/contactable_views/templates/views/formtastic/identifications/_identification.html.erb +2 -0
  60. data/generators/contactable_views/templates/views/formtastic/instant_messengers/_instant_messenger.html.erb +3 -0
  61. data/generators/contactable_views/templates/views/formtastic/phones/_phone.html.erb +2 -0
  62. data/generators/contactable_views/templates/views/formtastic/websites/_website.html.erb +2 -0
  63. data/generators/contactable_views/templates/views/jintastic/addresses/_address.html.erb +31 -0
  64. data/generators/contactable_views/templates/views/jintastic/addresses/_form.html.erb +9 -0
  65. data/generators/contactable_views/templates/views/jintastic/addresses/create.js.rjs +3 -0
  66. data/generators/contactable_views/templates/views/jintastic/addresses/destroy.js.rjs +3 -0
  67. data/generators/contactable_views/templates/views/jintastic/addresses/update.js.rjs +3 -0
  68. data/generators/contactable_views/templates/views/jintastic/cities/index.html.erb +6 -0
  69. data/generators/contactable_views/templates/views/jintastic/contactable/_birthday.html.erb +1 -0
  70. data/generators/contactable_views/templates/views/jintastic/contactable/_contactable.html.erb +16 -0
  71. data/generators/contactable_views/templates/views/jintastic/contactable/_contactable_field.html.erb +14 -0
  72. data/generators/contactable_views/templates/views/jintastic/contactable/_fields.html.erb +49 -0
  73. data/generators/contactable_views/templates/views/jintastic/contactable/_name.html.erb +3 -0
  74. data/generators/contactable_views/templates/views/jintastic/divisions/index.html.erb +9 -0
  75. data/generators/contactable_views/templates/views/jintastic/emails/_email.html.erb +2 -0
  76. data/generators/contactable_views/templates/views/jintastic/emails/_form.html.erb +9 -0
  77. data/generators/contactable_views/templates/views/jintastic/emails/create.js.rjs +3 -0
  78. data/generators/contactable_views/templates/views/jintastic/emails/destroy.js.rjs +3 -0
  79. data/generators/contactable_views/templates/views/jintastic/emails/update.js.rjs +3 -0
  80. data/generators/contactable_views/templates/views/jintastic/identifications/_form.html.erb +9 -0
  81. data/generators/contactable_views/templates/views/jintastic/identifications/_identification.html.erb +2 -0
  82. data/generators/contactable_views/templates/views/jintastic/identifications/create.js.rjs +3 -0
  83. data/generators/contactable_views/templates/views/jintastic/identifications/destroy.js.rjs +3 -0
  84. data/generators/contactable_views/templates/views/jintastic/identifications/update.js.rjs +3 -0
  85. data/generators/contactable_views/templates/views/jintastic/instant_messengers/_form.html.erb +9 -0
  86. data/generators/contactable_views/templates/views/jintastic/instant_messengers/_instant_messenger.html.erb +3 -0
  87. data/generators/contactable_views/templates/views/jintastic/instant_messengers/create.js.rjs +3 -0
  88. data/generators/contactable_views/templates/views/jintastic/instant_messengers/destroy.js.rjs +3 -0
  89. data/generators/contactable_views/templates/views/jintastic/instant_messengers/update.js.rjs +3 -0
  90. data/generators/contactable_views/templates/views/jintastic/phones/_form.html.erb +9 -0
  91. data/generators/contactable_views/templates/views/jintastic/phones/_phone.html.erb +2 -0
  92. data/generators/contactable_views/templates/views/jintastic/phones/create.js.rjs +3 -0
  93. data/generators/contactable_views/templates/views/jintastic/phones/destroy.js.rjs +3 -0
  94. data/generators/contactable_views/templates/views/jintastic/phones/update.js.rjs +3 -0
  95. data/generators/contactable_views/templates/views/jintastic/websites/_form.html.erb +9 -0
  96. data/generators/contactable_views/templates/views/jintastic/websites/_website.html.erb +2 -0
  97. data/generators/contactable_views/templates/views/jintastic/websites/create.js.rjs +3 -0
  98. data/generators/contactable_views/templates/views/jintastic/websites/destroy.js.rjs +3 -0
  99. data/generators/contactable_views/templates/views/jintastic/websites/update.js.rjs +3 -0
  100. data/lib/contactable.rb +6 -2
  101. data/lib/contactable/extensions/routes.rb +22 -0
  102. data/rails/init.rb +1 -0
  103. metadata +109 -19
  104. data/generators/contactable/templates/models/address.rb +0 -33
  105. data/generators/contactable/templates/models/address_type.rb +0 -5
  106. data/generators/contactable/templates/models/city.rb +0 -4
  107. data/generators/contactable/templates/models/country.rb +0 -4
  108. data/generators/contactable/templates/models/email_type.rb +0 -5
  109. data/generators/contactable/templates/models/phone_type.rb +0 -5
  110. data/generators/contactable/templates/models/website_type.rb +0 -5
@@ -0,0 +1,2 @@
1
+ v1.0.1. Added contactable_views generator
2
+ v1.0. Initial public release
data/Manifest CHANGED
@@ -1,23 +1,103 @@
1
+ CHANGELOG
1
2
  LICENSE
2
3
  Manifest
3
4
  README.rdoc
4
5
  Rakefile
6
+ app/controllers/addresses_controller.rb
7
+ app/controllers/cities_controller.rb
8
+ app/controllers/contact_field_actions.rb
9
+ app/controllers/divisions_controller.rb
10
+ app/controllers/emails_controller.rb
11
+ app/controllers/identifications_controller.rb
12
+ app/controllers/instant_messengers_controller.rb
13
+ app/controllers/phones_controller.rb
14
+ app/controllers/websites_controller.rb
15
+ app/helpers/contactable_helper.rb
16
+ app/models/address.rb
17
+ app/models/address_type.rb
18
+ app/models/city.rb
19
+ app/models/country.rb
20
+ app/models/division.rb
21
+ app/models/email.rb
22
+ app/models/email_type.rb
23
+ app/models/identification.rb
24
+ app/models/identification_type.rb
25
+ app/models/instant_messenger.rb
26
+ app/models/instant_messenger_protocol.rb
27
+ app/models/instant_messenger_type.rb
28
+ app/models/phone.rb
29
+ app/models/phone_type.rb
30
+ app/models/province.rb
31
+ app/models/website.rb
32
+ app/models/website_type.rb
33
+ app/views/addresses/_address.html.erb
34
+ app/views/cities/index.html.erb
35
+ app/views/contactable/_contactable.html.erb
36
+ app/views/contactable/_contactable_field.html.erb
37
+ app/views/contactable/_fields.html.erb
38
+ app/views/divisions/index.html.erb
39
+ app/views/emails/_email.html.erb
40
+ app/views/identifications/_identification.html.erb
41
+ app/views/instant_messengers/_instant_messenger.html.erb
42
+ app/views/phones/_phone.html.erb
43
+ app/views/websites/_website.html.erb
44
+ config/contactable_routes.rb
5
45
  generators/contactable/USAGE
6
46
  generators/contactable/contactable_generator.rb
47
+ generators/contactable/lib/insert_commands.rb
48
+ generators/contactable/templates/README
7
49
  generators/contactable/templates/migrations/create_contactable_related_tables.rb
8
- generators/contactable/templates/models/address.rb
9
- generators/contactable/templates/models/address_type.rb
10
- generators/contactable/templates/models/city.rb
11
- generators/contactable/templates/models/country.rb
12
- generators/contactable/templates/models/email.rb
13
- generators/contactable/templates/models/email_type.rb
14
- generators/contactable/templates/models/instant_messenger.rb
15
- generators/contactable/templates/models/instant_messenger_protocol.rb
16
- generators/contactable/templates/models/instant_messenger_type.rb
17
- generators/contactable/templates/models/phone.rb
18
- generators/contactable/templates/models/phone_type.rb
19
- generators/contactable/templates/models/province.rb
20
- generators/contactable/templates/models/website.rb
21
- generators/contactable/templates/models/website_type.rb
50
+ generators/contactable_views/USAGE
51
+ generators/contactable_views/contactable_views_generator.rb
52
+ generators/contactable_views/templates/views/formtastic/addresses/_address.html.erb
53
+ generators/contactable_views/templates/views/formtastic/cities/index.html.erb
54
+ generators/contactable_views/templates/views/formtastic/contactable/_contactable.html.erb
55
+ generators/contactable_views/templates/views/formtastic/contactable/_contactable_field.html.erb
56
+ generators/contactable_views/templates/views/formtastic/contactable/_fields.html.erb
57
+ generators/contactable_views/templates/views/formtastic/divisions/index.html.erb
58
+ generators/contactable_views/templates/views/formtastic/emails/_email.html.erb
59
+ generators/contactable_views/templates/views/formtastic/identifications/_identification.html.erb
60
+ generators/contactable_views/templates/views/formtastic/instant_messengers/_instant_messenger.html.erb
61
+ generators/contactable_views/templates/views/formtastic/phones/_phone.html.erb
62
+ generators/contactable_views/templates/views/formtastic/websites/_website.html.erb
63
+ generators/contactable_views/templates/views/jintastic/addresses/_address.html.erb
64
+ generators/contactable_views/templates/views/jintastic/addresses/_form.html.erb
65
+ generators/contactable_views/templates/views/jintastic/addresses/create.js.rjs
66
+ generators/contactable_views/templates/views/jintastic/addresses/destroy.js.rjs
67
+ generators/contactable_views/templates/views/jintastic/addresses/update.js.rjs
68
+ generators/contactable_views/templates/views/jintastic/cities/index.html.erb
69
+ generators/contactable_views/templates/views/jintastic/contactable/_birthday.html.erb
70
+ generators/contactable_views/templates/views/jintastic/contactable/_contactable.html.erb
71
+ generators/contactable_views/templates/views/jintastic/contactable/_contactable_field.html.erb
72
+ generators/contactable_views/templates/views/jintastic/contactable/_fields.html.erb
73
+ generators/contactable_views/templates/views/jintastic/contactable/_name.html.erb
74
+ generators/contactable_views/templates/views/jintastic/divisions/index.html.erb
75
+ generators/contactable_views/templates/views/jintastic/emails/_email.html.erb
76
+ generators/contactable_views/templates/views/jintastic/emails/_form.html.erb
77
+ generators/contactable_views/templates/views/jintastic/emails/create.js.rjs
78
+ generators/contactable_views/templates/views/jintastic/emails/destroy.js.rjs
79
+ generators/contactable_views/templates/views/jintastic/emails/update.js.rjs
80
+ generators/contactable_views/templates/views/jintastic/identifications/_form.html.erb
81
+ generators/contactable_views/templates/views/jintastic/identifications/_identification.html.erb
82
+ generators/contactable_views/templates/views/jintastic/identifications/create.js.rjs
83
+ generators/contactable_views/templates/views/jintastic/identifications/destroy.js.rjs
84
+ generators/contactable_views/templates/views/jintastic/identifications/update.js.rjs
85
+ generators/contactable_views/templates/views/jintastic/instant_messengers/_form.html.erb
86
+ generators/contactable_views/templates/views/jintastic/instant_messengers/_instant_messenger.html.erb
87
+ generators/contactable_views/templates/views/jintastic/instant_messengers/create.js.rjs
88
+ generators/contactable_views/templates/views/jintastic/instant_messengers/destroy.js.rjs
89
+ generators/contactable_views/templates/views/jintastic/instant_messengers/update.js.rjs
90
+ generators/contactable_views/templates/views/jintastic/phones/_form.html.erb
91
+ generators/contactable_views/templates/views/jintastic/phones/_phone.html.erb
92
+ generators/contactable_views/templates/views/jintastic/phones/create.js.rjs
93
+ generators/contactable_views/templates/views/jintastic/phones/destroy.js.rjs
94
+ generators/contactable_views/templates/views/jintastic/phones/update.js.rjs
95
+ generators/contactable_views/templates/views/jintastic/websites/_form.html.erb
96
+ generators/contactable_views/templates/views/jintastic/websites/_website.html.erb
97
+ generators/contactable_views/templates/views/jintastic/websites/create.js.rjs
98
+ generators/contactable_views/templates/views/jintastic/websites/destroy.js.rjs
99
+ generators/contactable_views/templates/views/jintastic/websites/update.js.rjs
22
100
  lib/contactable.rb
101
+ lib/contactable/extensions/routes.rb
102
+ rails/init.rb
23
103
  test/contactable_test.rb
@@ -1,2 +1,28 @@
1
1
  = Contactable
2
- A rails gem that adds contact information to an active record model
2
+ A rails gem that adds contact information to an active record model
3
+
4
+ == Installation
5
+
6
+ To install the Formtastic gem run:
7
+ sudo gem install contactable
8
+
9
+ And add it to your environment.rb configuration as a gem dependency:
10
+ config.gem 'contactable'
11
+
12
+ Now you can use it in your existing models by running:
13
+ ./script/generate contactable contact
14
+
15
+ Remember you need to add the new contactable routes:
16
+ map.resources :contacts do |contact|
17
+ contact.contactable_resources
18
+ end
19
+
20
+ You can now use the default contactable partial in your show view
21
+ <%= render :partial => 'contactable/contactable', :locals => {:contactable => @contact} %>
22
+
23
+ And in your formtastic form
24
+ <%= render :partial => 'contactable/fields', :locals => {:f => f}
25
+
26
+ Optionally, run
27
+ ./script/generate contactable_views formtastic
28
+
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('contactable', '0.1.0') do |p|
5
+ Echoe.new('contactable', '1.0.1') do |p|
6
6
  p.description = "Adds contact information to an active record model"
7
7
  p.url = "http://github.com/lperichon/contactable"
8
8
  p.author = "Luis Perichon"
@@ -0,0 +1,3 @@
1
+ class AddressesController < ApplicationController
2
+ include ContactFieldActions
3
+ end
@@ -0,0 +1,16 @@
1
+ class CitiesController < ApplicationController
2
+ layout nil
3
+
4
+ def index
5
+ search_options = {
6
+ }.merge(params[:search])
7
+ search_options[:country_id] = 0 if search_options[:country_id].blank?
8
+ @field_dom_id = params['field_dom_id']
9
+ @search = City.search(search_options)
10
+ if @search
11
+ @cities = @search.all
12
+ else
13
+ @cities = []
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,60 @@
1
+ module ContactFieldActions
2
+ def self.included(base)
3
+ base.class_eval do
4
+ before_filter :load_contactable
5
+ helper ContactableHelper
6
+ end
7
+ end
8
+
9
+ def load_contactable
10
+ path_component = request.path.split('/').reject(&:blank?).first.singularize
11
+ class_name = path_component.capitalize
12
+ @contactable = Kernel.const_get(class_name).find(params["#{path_component}_id"])
13
+ end
14
+
15
+ def association_name
16
+ self.field_name.pluralize
17
+ end
18
+
19
+ def field_name
20
+ self.controller_class_name.chomp("Controller").singularize.underscore
21
+ end
22
+
23
+ def create
24
+ @field = @contactable.send("#{self.association_name}").new(params[self.field_name.to_sym])
25
+
26
+ if @field.save
27
+ flash[:notice] = t(self.association_name + '.create.notice')
28
+ respond_to do |format|
29
+ format.js {}
30
+ end
31
+ else
32
+ # TODO: handle errors via js
33
+ end
34
+ end
35
+
36
+ def update
37
+ @field = @contactable.send("#{self.association_name}").find(params[:id])
38
+
39
+ @field.attributes = params[self.field_name.to_sym]
40
+ if @field.save
41
+ flash[:notice] = t(self.association_name + '.update.notice')
42
+
43
+ respond_to do |format|
44
+ format.js {}
45
+ end
46
+ end
47
+ end
48
+
49
+ def destroy
50
+ @field = @contactable.send("#{self.association_name}").find(params[:id])
51
+ @field_dom_id = dom_id @field
52
+ if @field.destroy
53
+ flash[:notice] = t(self.association_name + '.destroy.notice')
54
+
55
+ respond_to do |format|
56
+ format.js {}
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,17 @@
1
+ class DivisionsController < ApplicationController
2
+ layout nil
3
+ def index
4
+ search_options = {
5
+ 'level' => 1,
6
+ }.merge(params[:search])
7
+ search_options[:country_id] = 0 if search_options[:country_id].blank?
8
+ @field_dom_id = params['field_dom_id']
9
+ @level = Integer(search_options['level'])
10
+ @search = Division.search(search_options)
11
+ if params[:search]
12
+ @divisions = @search.all
13
+ else
14
+ @divisions = []
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ class EmailsController < ApplicationController
2
+ include ContactFieldActions
3
+ end
@@ -0,0 +1,3 @@
1
+ class IdentificationsController < ApplicationController
2
+ include ContactFieldActions
3
+ end
@@ -0,0 +1,3 @@
1
+ class InstantMessengersController < ApplicationController
2
+ include ContactFieldActions
3
+ end
@@ -0,0 +1,3 @@
1
+ class PhonesController < ApplicationController
2
+ include ContactFieldActions
3
+ end
@@ -0,0 +1,3 @@
1
+ class WebsitesController < ApplicationController
2
+ include ContactFieldActions
3
+ end
@@ -0,0 +1,46 @@
1
+ module ContactableHelper
2
+ def identification_label(identification)
3
+ "#{identification.type ? identification.type.name : nil} - #{identification.code}"
4
+ end
5
+
6
+ def phone_label(phone)
7
+ "#{phone.type ? phone.type.name : nil} - #{phone.number}"
8
+ end
9
+
10
+ def address_label(address)
11
+ "#{address.type ? address.type.name : nil} - #{address.full_address}"
12
+ end
13
+
14
+ def email_label(email)
15
+ "#{email.type ? email.type.name : nil} - #{email.address}"
16
+ end
17
+
18
+ def website_label(website)
19
+ "#{website.type ? website.type.name : nil} - #{website.address}"
20
+ end
21
+
22
+ def instant_messenger_label(instant_messenger)
23
+ "#{instant_messenger.type ? instant_messenger.type.name : nil} - #{instant_messenger.protocol ? instant_messenger.protocol.name : nil} - #{instant_messenger.nick}"
24
+ end
25
+
26
+ # Helpers for contact nested forms
27
+ def add_new_record_link(record, name, form_builder)
28
+ name = name.to_s
29
+ plural_name = name.pluralize
30
+ link_to_function "add a new #{name}", :class => 'add' do |page|
31
+ new_record = record.send(plural_name).build
32
+ form_builder.semantic_fields_for plural_name, new_record, :child_index => 'NEW_RECORD' do |p|
33
+ html = "<fieldset class='inputs'><ol>#{render(:partial => plural_name + '/' + name, :locals => { :f => p, name.to_sym => new_record })}</ol></fieldset>"
34
+ page << "$('##{plural_name}').append('#{escape_javascript(html)}'.replace(/NEW_RECORD/g, new Date().getTime()));"
35
+ end
36
+ end
37
+ end
38
+
39
+ # remove link
40
+ def remove_link_unless_new_record(fields)
41
+ out = ''
42
+ out << fields.hidden_field(:_delete) unless fields.object.new_record?
43
+ out << link_to_function("remove", "$(this).parent().parent().hide(); $(this).siblings('input[type=hidden]')[0].value = 1;", :class => 'remove')
44
+ out
45
+ end
46
+ end
@@ -0,0 +1,96 @@
1
+ class Address < ActiveRecord::Base
2
+
3
+ belongs_to :owner, :polymorphic => true
4
+ belongs_to :type, :class_name => 'AddressType'
5
+ belongs_to :city
6
+ belongs_to :country
7
+ belongs_to :division1, :class_name => 'Division'
8
+ belongs_to :division2, :class_name => 'Division'
9
+ belongs_to :division3, :class_name => 'Division'
10
+ belongs_to :division4, :class_name => 'Division'
11
+
12
+ #acts_as_mappable :default_units => :kms
13
+ before_save :cleanup_address
14
+ #before_validation :geocode_address
15
+
16
+ def full_address
17
+ returning address_str = "" do
18
+ if self.address.present?
19
+ address_str << self.address
20
+ end
21
+ if self.city.present?
22
+ address_str << ", #{self.city.name}"
23
+ end
24
+ if self.zip.present?
25
+ address_str << " (#{self.zip})"
26
+ end
27
+ %w(division4 division3 division2 division1).each do |division|
28
+ if self.send(division).present?
29
+ address_str << ", #{self.send(division).name}"
30
+ end
31
+ end
32
+ if self.country.present?
33
+ address_str << ", #{self.country.name}"
34
+ end
35
+ end
36
+ end
37
+
38
+ def mapped?
39
+ lat && lng
40
+ end
41
+
42
+ def coordinates
43
+ [lat, lng]
44
+ end
45
+
46
+ def cities
47
+ if self.division4
48
+ self.division4.cities
49
+ elsif self.division3 && self.division3.children.empty?
50
+ self.division3.cities
51
+ elsif self.division2 && self.division2.children.empty?
52
+ self.division2.cities
53
+ elsif self.division1 && self.division1.children.empty?
54
+ self.division1.cities
55
+ elsif self.country && self.country.first_level_divisions.empty?
56
+ self.country.cities
57
+ else
58
+ []
59
+ end
60
+ end
61
+
62
+ private
63
+
64
+ #
65
+ # def geocode_address
66
+ # geo=Geokit::Geocoders::MultiGeocoder.geocode(self.full_address)
67
+ # errors.add(:address, I18n.t('address.geocode_error')) if !geo.success
68
+ # self.lat, self.lng = geo.lat,geo.lng if geo.success
69
+ # end
70
+
71
+ def cleanup_address
72
+ (2..4).to_a.reverse.each do |idx|
73
+ # if division[idx-1] is not parent of division[idx]
74
+ # or division[idx] is not present
75
+ # or division[idx-1] is not present
76
+ if(self.send("division#{idx}").present? && self.send("division#{idx - 1}").present? && self.send("division#{idx - 1}") != self.send("division#{idx}").parent ||
77
+ self.send("division#{idx}").blank? ||
78
+ self.send("division#{idx - 1}").blank?)
79
+ (idx..4).each do |invalid_idx|
80
+ # cleanup all divisions below
81
+ self.send("division#{invalid_idx}=", nil)
82
+ end
83
+ end
84
+
85
+ if(self.division1.present? && self.country.present? && self.country != self.division1.country ||
86
+ self.country.blank?)
87
+ (1..4).each do |invalid_idx|
88
+ # cleanup all divisions below
89
+ self.send("division#{invalid_idx}=", nil)
90
+ end
91
+ end
92
+
93
+ self.city = nil unless self.cities.include?(self.city)
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,5 @@
1
+ class AddressType < ActiveRecord::Base
2
+ translates :name
3
+
4
+ validates_presence_of :name
5
+ end
@@ -0,0 +1,21 @@
1
+ class City < ActiveRecord::Base
2
+ belongs_to :country
3
+ belongs_to :division
4
+
5
+ # Returns an array with all the parents of this city
6
+ #
7
+ # The first position in the array is the +country+ and up to 4 more positions
8
+ # can contain the ADM1, ADM2, ADM3 and ADM4 divisions containing the city
9
+ def containers
10
+ return @containers unless @containers.nil?
11
+ container_codes = []
12
+ codes = code.split('|')
13
+ container_codes << (codes = codes[0..-2]).join('|') while codes.size > 1
14
+ # first is country code
15
+ @containers = [country]
16
+ # second, third can be parent administrative division if not nil
17
+ # NOTE container codes is like: ["ES|58|PO", "ES|58", "ES"] (country is last)
18
+ @containers += Division.find_all_by_code container_codes[0..-2], :order => :level if container_codes.size > 1
19
+ return @containers
20
+ end
21
+ end