app/controllers/observations_controller.rb
# Tries to create a new observation from the specified Flickr photo ID and
# update the existing @observation with the new properties, without saving
Tries to create a new observation from the specified Flickr photo ID and
update the existing @observation with the new properties, without saving
Tries to create a new observation from the specified Flickr photo ID and update the existing @observation with the new properties without saving
def sync_flickr_photo
    flickr = get_flickraw
    begin
      fp = flickr.photos.getInfo(:photo_id => params[:flickr_photo_id])
      @flickr_photo = FlickrPhoto.new_from_flickraw(fp, :user => current_user)
    rescue FlickRaw::FailedResponse => e
      Rails.logger.debug "[DEBUG] FlickRaw failed to find photo " +
        "#{params[:flickr_photo_id]}: #{e}\n#{e.backtrace.join("\n")}"
      @flickr_photo = nil
    rescue Timeout::Error => e
      flash.now[:error] = t(:sorry_flickr_isnt_responding_at_the_moment)
      Rails.logger.error "[ERROR #{Time.now}] Timeout: #{e}"
      Airbrake.notify(e, :request => request, :session => session)
      return
    end
    if fp && @flickr_photo && @flickr_photo.valid?
      @flickr_observation = @flickr_photo.to_observation
      sync_attrs = %w(description species_guess taxon_id observed_on 
        observed_on_string latitude longitude place_guess map_scale)
      unless params[:flickr_sync_attrs].blank?
        sync_attrs = sync_attrs & params[:flickr_sync_attrs]
      end
      sync_attrs.each do |sync_attr|
        val = @flickr_observation.send(sync_attr)
        @observation.send("#{sync_attr}=", val) unless val.blank?
      end
      unless @observation.observation_photos.detect {|op| op.photo.native_photo_id == @flickr_photo.native_photo_id}
        @observation.observation_photos.build(:photo => @flickr_photo)
      end
      unless @observation.new_record?
        flash.now[:notice] = t(:preview_of_synced_observation, :url => url_for)
      end
      if (@existing_photo = Photo.find_by_native_photo_id(@flickr_photo.native_photo_id)) && 
          (@existing_photo_observation = @existing_photo.observations.first) && @existing_photo_observation.id != @observation.id
        msg = t(:heads_up_this_photo_is_already_associated_with, :url => url_for(@existing_photo_observation))
        flash.now[:notice] = flash.now[:notice].blank? ? msg : "#{flash.now[:notice]}<br/>#{msg}"
      end
    else
      flash.now[:error] = t(:sorry_we_didnt_find_that_photo)
    end
  end
def sync_flickr_photo flickr = get_flickraw begin fp = flickr . photos . getInfo ( :photo_id => params [ :flickr_photo_id ] ) @flickr_photo = FlickrPhoto . new_from_flickraw ( fp , :user => current_user ) rescue FlickRaw :: FailedResponse => e Rails . logger . debug "[DEBUG] FlickRaw failed to find photo " + "#{params[:flickr_photo_id]}: #{e}\n#{e.backtrace.join("\n")}" @flickr_photo = nil rescue Timeout :: Error => e flash . now [ :error ] = t ( :sorry_flickr_isnt_responding_at_the_moment ) Rails . logger . error "[ERROR #{Time.now}] Timeout: #{e}" Airbrake . notify ( e , :request => request , :session => session ) return end if fp && @flickr_photo && @flickr_photo . valid? @flickr_observation = @flickr_photo . to_observation sync_attrs = %w( description species_guess taxon_id observed_on observed_on_string latitude longitude place_guess map_scale ) unless params [ :flickr_sync_attrs ] . blank? sync_attrs = sync_attrs & params [ :flickr_sync_attrs ] end sync_attrs . each do | sync_attr | val = @flickr_observation . send ( sync_attr ) @observation . send ( "#{sync_attr}=" , val ) unless val . blank? end unless @observation . observation_photos . detect { | op | op . photo . native_photo_id == @flickr_photo . native_photo_id } @observation . observation_photos . build ( :photo => @flickr_photo ) end unless @observation . new_record? flash . now [ :notice ] = t ( :preview_of_synced_observation , :url => url_for ) end if ( @existing_photo = Photo . find_by_native_photo_id ( @flickr_photo . native_photo_id ) ) && ( @existing_photo_observation = @existing_photo . observations . first ) && @existing_photo_observation . id != @observation . id msg = t ( :heads_up_this_photo_is_already_associated_with , :url => url_for ( @existing_photo_observation ) ) flash . now [ :notice ] = flash . now [ :notice ] . blank? ? msg : "#{flash.now[:notice]}<br/>#{msg}" end else flash . now [ :error ] = t ( :sorry_we_didnt_find_that_photo ) end end
Tries to create a new observation from the specified Flickr photo ID and
update the existing @observation with the new properties, without saving
Tries to create a new observation from the specified Flickr photo ID and update the existing @observation with the new properties without saving
# merge flickr_observation with existing observation # Note: the following is sort of a hacky alternative to build().  We # need to append a new photo object without saving it, but build() won't # work here b/c Photo and its descedents use STI, and type is a # protected attributes that can't be mass-assigned.