Skip to content

Commit

Permalink
not using partials anymore - use more flexible helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
kristianmandrup committed Sep 7, 2012
1 parent 314025f commit 5f0f951
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 43 deletions.
17 changes: 0 additions & 17 deletions app/views/gmaps4rails/_gmaps4rails.html.erb

This file was deleted.

3 changes: 2 additions & 1 deletion app/views/gmaps4rails/_gmaps4rails_libs.html.erb
@@ -1,2 +1,3 @@
<% content_for :scripts do %>
<%= javascript_include_tag *js_dependencies %>
<%= javascript_include_tag *js_dependencies %>
<% end %>
2 changes: 2 additions & 0 deletions app/views/gmaps4rails/_gmaps4rails_libs.html.haml
@@ -0,0 +1,2 @@
- content_for :scripts do
= javascript_include_tag *js_dependencies
15 changes: 9 additions & 6 deletions lib/generators/gmaps4rails/install_generator.rb
Expand Up @@ -5,7 +5,7 @@ class InstallGenerator < Rails::Generators::Base

desc 'Creates a Gmaps4rails initializer and copies the assets to the public folder.'

class_option :views, :type => :boolean, :default => false, :desc => 'copy partials to app/views of Rails app'
class_option :views, :type => :string, :default => nil, :desc => 'copy partials to app/views of Rails app'

def copy_locale
if Rails::VERSION::MINOR >= 1
Expand All @@ -27,15 +27,18 @@ def copy_locale
end

def copy_views
return unless copy_views?
copy_file File.join(source_views_path, "_gmaps4rails_libs.html.erb"), File.join(destination_views_path, "_gmaps4rails_lib.html.erb")
copy_file File.join(source_views_path, "_gmaps4rails.html.erb"), File.join(destination_views_path, "_gmaps4rails.html.erb")
return unless valid_template_type?
copy_file File.join(source_views_path, "_gmaps4rails_libs.html.#{template_type}"), File.join(destination_views_path, "_gmaps4rails_lib.html.#{template_type}")
end

protected

def copy_views?
options[:views]
def template_type
options[:views].to_s.to_sym
end

def valid_template_type?
[:erb, :haml].include? template_type
end

def source_views_path
Expand Down
67 changes: 49 additions & 18 deletions lib/gmaps4rails/helper/gmaps4rails_helper.rb
Expand Up @@ -12,29 +12,60 @@ def gmaps4rails(builder)

# full helper to pass all variables and their options
# @params [Hash] options is a Hash containing data and options. Example: { markers:{ data: @json, options: { do_clustering: true } } }
def gmaps(options)
gmaps_libs(options)
render :partial => '/gmaps4rails/gmaps4rails',
:locals => {
:options => options_with_indifferent_access,
:dom => view_helper.dom_attributes
}
def gmaps(options = {})
gmaps_libs(options) if options[:with_libs]

options_with_indifferent_access = options.with_indifferent_access
view_helper = Gmaps4rails::ViewHelper.new(options_with_indifferent_access)
gmaps4rails_map view_helper.dom_attributes, options_with_indifferent_access
end

def gmaps_libs(options)
def gmaps_libs(options = {})
options_with_indifferent_access = options.with_indifferent_access
view_helper = Gmaps4rails::ViewHelper.new(options_with_indifferent_access)

js_dependencies = if Gmaps4rails.escape_js_url
view_helper.js_dependencies_array
else
view_helper.js_dependencies_array.map(&:html_safe)
end

render 'gmaps4rails/gmaps4rails_libs',
:locals => {
:js_dependencies => js_dependencies
}
js_dependencies = Gmaps4rails.escape_js_url ? view_helper.js_dependencies_array : view_helper.js_dependencies_array.map(&:html_safe)

content_for :scripts do
javascript_include_tag(*js_dependencies)
end

# render partial: '/gmaps4rails/gmaps4rails_libs',
# :locals => {
# :js_dependencies => js_dependencies,
# :options => options_with_indifferent_access
# }
end

def gmaps_libs_now(options = {})
options_with_indifferent_access = options.with_indifferent_access
view_helper = Gmaps4rails::ViewHelper.new(options_with_indifferent_access)

js_dependencies = Gmaps4rails.escape_js_url ? view_helper.js_dependencies_array : view_helper.js_dependencies_array.map(&:html_safe)

javascript_include_tag(*js_dependencies)
end

protected

def gmaps4rails_map dom, options = {}
options.reverse_merge! :style => "width:750px; height:475px;"

map_script << content_tag(:script, :type => "text/javascript") do
raw(options.to_gmaps4rails).html_safe
end

map = case dom.map_provider.to_sym
when :mapquest
content_tag :div, nil, :id => dom.map_id, :style => options[:style]
when :bing
content_tag :div, nil, :id => dom.map_id, :class => dom.map_class
else
content_tag :div, :class => dom.container_class do
content_tag :div, nil, :id => dom.map_id, :class => dom.map_class
end
end

(map_script + map).html_safe
end
end
3 changes: 2 additions & 1 deletion lib/gmaps4rails/js_builder.rb
Expand Up @@ -21,6 +21,7 @@ def initialize(option_hash)
end

def create_js
# @js << "$(function() {"
@js << "#{gmap_id} = new #{ map_constructor };"
@js << "Gmaps.#{js_function_name} = function() {"

Expand All @@ -34,7 +35,7 @@ def create_js
@js << "#{gmap_id}.callback();"
@js << "};"
@js << "Gmaps.oldOnload = window.onload;\n window.onload = function() { Gmaps.triggerOldOnload(); Gmaps.loadMaps(); };" if load_map?

# @js << "}); // ready"
@js * ("\n")
end

Expand Down

0 comments on commit 5f0f951

Please sign in to comment.