What would be the best way to handle this situation?

Hi All,

I’m trying to work out an issue and a bit stuck on the best way to
handle
this, so I’m hoping someone can give me some ideas to try.

I have a form that contains some info about a particular job. Part of
this
form has some address info that once saved we gather some Geo Location
info
so we can plot the distance.

We had an issue where the Geo API was down and therefore we couldn’t get
the distance in miles from the base office to a customers location for
plotting on a map.

I was asked to raise an error and bring the user back to the form to
manually add in the distance if the API is not responding correctly.
Pretty
straight forward.

The issue I am having is that I have a before_save method in the Job
Model
that runs this geo location code. If the API is having issues I Raise an
error and handle it in the controller

So the Model has

before_save :geolocation_address

def geolocation_address
geo = APICall
errors.add(:base, “Geo Location Failed” ) if !geo.success
raise “Error” if !geo.success
end

Then in my controllers update action I have

@job = Job.find(params[:id])
  if @job.update_attributes(params[:job])
    flash[:notice] = "Successfully updated job."
    return if has_session_return_to?
    redirect_to @job.contact
  else
    render :action => 'edit'
  end
rescue
  render :action => 'edit'

This brings the user back to the form to manually edit which is what I
want. But the problem is that I’m always going to go back into the
rescue
block as long as the Geo location fails.

Should I pull that before_save out and manually call the
geolocation_address in the controller then when I hit the rescue block
pass
a variable so I know not to call it again when the user updates the form
again, or does someone have a better solution?

Thanks,
Ben

Hi,

Maybe you can just make the API call only if the distance is blank? So,
if
the user manually sets the distance, the API call is not triggered.

Btw, you should prefer unless geo.success instead of if !geo.success.

Phil.

Le mercredi 20 mars 2013 18:18:26 UTC+1, Ben Densmore a crit :

On Mar 20, 2013, at 11:18 AM, Ben Densmore wrote:

Should I pull that before_save out and manually call the geolocation_address in
the controller then when I hit the rescue block pass a variable so I know not to
call it again when the user updates the form again, or does someone have a better
solution?

If it were me, I might keep two “global” variables (not session
variables, so stored in the db or in text files), for the timestamps of
the last successful transaction with the API, and the last failed
transaction with the API. Then you could look at those and decide
whether or not to try using the API.

You might also consider some AJAXy voodoo to use the API before
submitting the whole form…


Scott R.
[email protected]
http://www.elevated-dev.com/
(303) 722-0567 voice

So what I did for now was in my rescue statement I’ve added an instance
variable called @manual_edit_allowed. If that variable exists then I
just
allow the user to update the distance manually and the Geocode Location
API
is never called. I had to get something in quick because it was stopping
users from doing certain tasks. I hope to look more deeply into it now
that
it’s at least working.

Phillipe, I do try to use unless whenever possible. This is code I
inherited and yesterday was my first time working in that area, so I
haven’t started fixing things like that, especially since it works as
is.
But I agree, it just looks cleaner using unless rather than if !

I appreciate the help on thinking this through.

Ben