Class: Gmaps4rails::JsonBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/gmaps4rails/json_builder.rb

Overview

the to_gmaps4rails method accepts a block to customize:

  • infowindow

  • picture

  • title

  • sidebar

  • json

This works this way:

@json = User.all.to_gmaps4rails do |user, marker|
  marker.infowindow render_to_string(:partial => "/users/my_template", :locals => { :object => user}).gsub(/\n/, '').gsub(/"/, '\"')
  marker.picture({
                  :picture => "http://www.blankdots.com/img/github-32x32.png",
                  :width   => "32",
                  :height  => "32"
                 })
  marker.title   "i'm the title"
  marker.sidebar "i'm the sidebar"
  marker.json({ :id => user.id })
end

For backward compability, a mere string could be passed:

@json = User.all.to_gmaps4rails do |user, marker|
  "\"id\": #{user.id}"
end

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ JsonBuilder

Returns a new instance of JsonBuilder.



31
32
33
34
# File 'lib/gmaps4rails/json_builder.rb', line 31

def initialize(object)
  @object, @json_hash, @custom_json = object, Hash.new, nil
  @options = OpenStruct.new @object.gmaps4rails_options
end

Instance Method Details

#infowindow(string) ⇒ Object



46
47
48
49
# File 'lib/gmaps4rails/json_builder.rb', line 46

def infowindow(string)
  @json_hash[:description] = string
  true
end

#json(json) ⇒ Object



61
62
63
64
# File 'lib/gmaps4rails/json_builder.rb', line 61

def json(json)
  return @json_hash.merge! json if json.is_a? Hash
  true
end

#picture(hash) ⇒ Object



66
67
68
69
# File 'lib/gmaps4rails/json_builder.rb', line 66

def picture(hash)
  @json_hash.merge! hash
  true
end

#process(&block) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/gmaps4rails/json_builder.rb', line 36

def process(&block)
  if compliant?
    handle_block(&block) if block_given?
    handle_model_methods
    return_json
  else
    nil
  end
end


56
57
58
59
# File 'lib/gmaps4rails/json_builder.rb', line 56

def sidebar(string)
  @json_hash[:sidebar] = string
  true
end

#title(string) ⇒ Object



51
52
53
54
# File 'lib/gmaps4rails/json_builder.rb', line 51

def title(string)
  @json_hash[:title] = string
  true
end