Skip to content

Instantly share code, notes, and snippets.

@brooksreese
Last active August 29, 2015 14:01
Show Gist options
  • Save brooksreese/a7533da01f410ed623df to your computer and use it in GitHub Desktop.
Save brooksreese/a7533da01f410ed623df to your computer and use it in GitHub Desktop.
refactoring example
def set_gmap_markers
markers = {
:ddtcs => ActionController::Base.helpers.asset_path('marker_DDTCS.png'),
:chms => ActionController::Base.helpers.asset_path('marker_CHMS.png'),
:all => ActionController::Base.helpers.asset_path('marker_CHMS-DDTCS.png')
}
@hash = Gmaps4rails.build_markers(@locations) do |location, marker|
marker.lat location.latitude
marker.lng location.longitude
##################################
# code before refactoring
##################################
# if location.services.length == 2
# marker.picture({ :url => markers[:all], :width => 32, :height => 32 })
# end
# if location.services.length == 1 && location.services[0].name == 'CHMS'
# marker.picture({ :url => markers[:chms], :width => 32, :height => 32 })
# end
# if location.services.length == 1 && location.services[0].name == 'DDTCS'
# marker.picture({ :url => markers[:ddtcs], :width => 32, :height => 32 })
# end
##################################
# code after refactoring
##################################
case location.services.length
when 2
marker.picture({ :url => markers[:all], :width => 32, :height => 32 })
when 1
if location.services[0].name == 'CHMS'
marker.picture({ :url => markers[:chms], :width => 32, :height => 32 })
else
marker.picture({ :url => markers[:ddtcs], :width => 32, :height => 32 })
end
end
marker.infowindow "<b>#{location.name}</b>
<br/><i>#{location.address}</i>
<br/><i>#{location.city} #{location.state}., #{location.zip}</i>
<p/>#{location.services.map{|service| service.name }.join(', ') }
<p/><a href=\"#{location_path(location)}\">Location Details</a>"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment