Skip to content

Model Customization

map7 edited this page Jun 4, 2012 · 14 revisions

Declaration in model

Basic

The easiest way to declare gmaps4rails in your model is:

acts_as_gmappable

The following default config will be assumed:

  • :process_geocoding => true : the geocode will be performed each time the model is run through a validate method

  • :validation => true : if the position of the address provided isn't found by google, an error will be displayed

  • :lat => "latitude", :lng => "longitude" : latitude and longitude will be saved in the columns latitude and longitude

  • :check_process => true, :checker => "gmaps" : the status of the geocode, will be checked in a method or in db column boolean called gmaps. Note: If :check_process is set to false then lat & long will be updated when the model is updated.

  • :address =>"gmaps4rails_address": will look for the default gmaps4rails_address

Custom

All or part of these default behaviors can be customized with the following arguments:

  • :lat : string

  • :lng : string

  • :process_geocoding : true/false/Proc/String/symbol

These are only used if process_geocoding returns true:

  • :check_process : Either a Boolean (most obvious case), Symbol (in this case, it should refer to an instance method's name), or Proc. This is not taken into account when process_geocoding is a Proc or a method name.

  • :checker : string (only if check_process is true), could be a method or a db column boolean. This is not taken into account when process_geocoding is a Proc or a method name.

  • :validation : Either a Boolean (most obvious case), Symbol (in this case, it should refer to an instance method's name), or Proc.

  • :msg : string, to customize the error message in validation (if activated)

  • :address : string it could be a method or even a db column name.

  • normalized_address : string, you can retrieve directly the address matched by google. Set here in which db column you want it to be saved.

  • language : string, the language in which the results will be returned (Useful in combination with normalized_adress).

  • protocol : string, "http" by default

  • callback : string, if set, this will be called with the most specific result as a parameter. You can then do whatever you want with it. For example:

acts_as_gmappable :callback => :save_country
def save_country(data)
  data["address_components"].each do |c|
    if c["types"].include? "country"
      self.country = c["long_name"]
    end
  end
end

Example

You could do the following:

acts_as_gmappable :process_geocoding => false
Clone this wiki locally