Skip to content

Commit

Permalink
Refactor big helper to partial and smaller helper. [#82]
Browse files Browse the repository at this point in the history
  • Loading branch information
marnen committed Aug 3, 2013
1 parent 9eecba5 commit a31ac57
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 68 deletions.
24 changes: 0 additions & 24 deletions app/helpers/events_helper.rb
Expand Up @@ -54,23 +54,6 @@ def edit_link(event)
link_to h(_("edit")), url_for(:controller => 'events', :action => 'edit', :id => event.id), :class => :edit
end

# Generates a <div> element with a map for #Event, using the Google API key for <em>host</em>.
# TODO: figure out how to make this html_safe! And fix the architecture!
def event_map(event, hostname)
return nil if event.nil?

content_for(:javascript) do
google_maps_header(hostname) + javascript_include_tag('events/map')
end

result = ''.html_safe
result << info(event)
result << content_tag(:div, event.latitude, id: :lat, class: :hidden)
result << content_tag(:div, event.longitude, id: :lng, class: :hidden)
result << tag(:div, id: :map)
result
end

# Escapes characters in <em>string</em> that would be illegal in iCalendar format.
def ical_escape(string)
string.gsub(%r{[\\,;]}) {|c| '\\' + c}.gsub("\n", '\\n')
Expand Down Expand Up @@ -146,11 +129,4 @@ def sort_link(title, field, direction = :asc, options = {})
def status_strings
@status_strings ||= {yes: _('attending'), no: _('not attending'), maybe: _('uncommitted')}
end

private

def google_maps_header(hostname)
api_key = GMAPS_API_KEY.kind_of?(Array) ? GMAPS_API_KEY[hostname] : GMAPS_API_KEY
javascript_include_tag "http://maps.google.com/maps?file=api&v=2&sensor=false&key=#{api_key}"
end
end
5 changes: 5 additions & 0 deletions app/helpers/gmaps_helper.rb
@@ -0,0 +1,5 @@
module GmapsHelper
def gmaps_api_key
GMAPS_API_KEY.kind_of?(Hash) ? GMAPS_API_KEY[controller.request.host_with_port] : GMAPS_API_KEY
end
end
9 changes: 8 additions & 1 deletion app/views/events/map.html.haml
@@ -1 +1,8 @@
= event_map @event, @host
- content_for(:javascript) do
= javascript_include_tag "http://maps.google.com/maps?file=api&v=2&sensor=false&key=#{gmaps_api_key}"
= javascript_include_tag('events/map')

= info(@event)
#lat.hidden= @event.latitude
#lng.hidden= @event.longitude
#map
43 changes: 0 additions & 43 deletions spec/helpers/events_helper_spec.rb
Expand Up @@ -87,49 +87,6 @@
end
end

describe EventsHelper, "event_map" do
before(:each) do
User.stub!(:current_user).and_return(FactoryGirl.create :user)
end

it "should return a safe string" do
helper.event_map(Factory(:event), DOMAIN).should be_html_safe
end

it "should set up a Google map" do
event = FactoryGirl.create :event

# TODO: since this code is now in events/map.js , translate these specs into JavaScript!
=begin
marker = GMarker.new([1.0, 2.0])
gmap_header = "[Stubbed header for #{DOMAIN}]"
GMap.should_receive(:header).with(:host => DOMAIN).at_least(:once).and_return(gmap_header)
GMarker.stub!(:new).and_return(marker)
gmap.should_receive(:center_zoom_init)
gmap.should_receive(:overlay_init).with(marker)
marker.should_receive(:open_info_window).with(EventsHelper::ElementVar.new(helper.info(event)))
gmap.should_receive(:control_init) do |opts|
opts.should be_a_kind_of(Hash)
opts.should have_key(:large_map)
opts[:large_map].should == true
opts.should have_key(:map_type)
opts[:map_type].should == true
end
gmap.should_receive(:to_html).at_least(:once)
=end

map = helper.event_map(event, DOMAIN)
{'#map' => nil, '#info' => nil, '#lat' => ERB::Util::h(event.latitude), '#lng' => ERB::Util::h(event.longitude)}.each do |k, v|
map.should have_selector(k, :content => v)
end

helper.content_for(:javascript).should_not be_nil
api_key = GMAPS_API_KEY.kind_of?(Array) ? GMAPS_API_KEY[DOMAIN] : GMAPS_API_KEY
helper.content_for(:javascript).should include(javascript_include_tag "http://maps.google.com/maps?file=api&v=2&sensor=false&key=#{api_key}")
helper.content_for(:javascript).should include(javascript_include_tag 'events/map')
end
end

describe EventsHelper, "ical_escape" do
it "should make newlines into '\n'" do
helper.ical_escape("a\na").should == 'a\\na'
Expand Down
24 changes: 24 additions & 0 deletions spec/helpers/gmaps_helper_spec.rb
@@ -0,0 +1,24 @@
require 'spec_helper.rb'

describe GmapsHelper do
describe '.gmaps_api_key' do
let(:key) { Faker::Lorem.sentence.gsub /\s/, '_' }

context 'API key is a string' do
it 'should return the string' do
stub_const 'GMAPS_API_KEY', key
helper.gmaps_api_key.should == key
end
end

context 'API key is a hash' do
it 'should use the hostname as a key into the hash' do
hostname = "#{Faker::Internet.domain_name}:#{rand 10000}"
controller.stub_chain(:request, :host_with_port).and_return hostname
stub_const 'GMAPS_API_KEY', {hostname => key, 'other-host.com' => 'some_other_key'}

helper.gmaps_api_key.should == key
end
end
end
end

0 comments on commit a31ac57

Please sign in to comment.