Geocoder rails, check for correctness

Below is my class with which I use geocoder. I am having trouble checking if the address that is being transmitted through the geocoder is valid. This code works great when I add a company / edit a company that has addresses that, when transmitted through a geocoder, give latitude and longitude. But I need a code to work, so when a user tries to put an address that does not give latitude and longitude, neither when editing nor in creating a company, an error message appears and informs the user that the address is invalid.

Model

class Company < ActiveRecord::Base
    validates :name, presence: true, length: { maximum: 30 }
    validates :website, presence: true
    validates :address, presence: true
    validates :description, presence: true
    validates :primary_field, presence: true

    geocoded_by :address
    before_save :geocode, if: ->(obj){ obj.address.present? and obj.address_changed? }

    after_save :set_popup_value

    private

     ...

end

View

<% provide(:title, 'Add Company') %>
<h1>Add Company</h1>
<div class="row">
  <div class="span6 offset3">
    <%= form_for(@company) do |f| %>
      <%= render 'shared/error_messages' %>

      <%= f.label :name %>
      <%= f.text_field :name %>

      <%= f.label :website %>
      <%= f.text_field :website %>

      <%= f.label :primary_field %>
      <%= f.select :primary_field, @primary_field %>

      <%= f.label :address %>
      <%= f.text_field :address, :placeholder => '123 Test St, City State Zip'%>

      <%= f.label :description %>
      <%= f.text_area :description, :size => "30x5" %>

      <%= f.submit "Add Company", class: "btn btn-large btn-primary" %>
    <% end %>
  </div>
</div>
+4
source share
5 answers

, , . , , . , ! , .

.

geocoded_by :address
after_validation :geocode, if: ->(obj){ obj.address.present? and obj.address_changed? }
after_validation :lat_changed?

after_save :set_popup_value

private

# This is used to make sure that our address is actually parsed properly and we
# get a valuable lat/long
def lat_changed?
    if (self.address_changed?)
        if !(self.latitude_changed?)
            self.errors.add(:address, "is not valid")
            return false
        end
    end
    return true
end

after_validation , , , , , .

, , , , lat/long. , , false, .

, !

+3
+1

Geocoder, . " " github. , :

geocoded_by :address do |obj,results|
  ....
end

, , / /.

, , -, , - , .

+1

You can set in the model, as this example below, to check it address geocodewill be called only once, when the address has changed. In the method, geocoded_bywe set to explicitly write latitudeand longitudeso that than when the address was not found, these columns will be set to nil.

class Company < ActiveRecord::Base
   geocoded_by :address do |object, results|
    if results.present?
     object.latitude = results.first.latitude
     object.longitude = results.first.longitude
    else
     object.latitude = nil
     object.longitude = nil
    end
  end

  before_validation :geocode, if: :address_changed?

  validates :address, presence: true
  validates :found_address_presence

  def found_address_presence
    if latitude.blank? || longitude.blank?
      errors.add(:address, "We couldn't find the address")
    end
  end
end
+1
source

What Sean Callahan said is all right. You can improve the code:

def lat_changed?
  if address_changed? && !latitude_changed?
    self.errors.add(:address, "is not valid")
  end
end

No need to return true / false

0
source

Source: https://habr.com/ru/post/1536637/


All Articles