module Ropenlayer module ActsAs # acts_as_localizable_on: give to the model facilities to store and draw object in a map. # # class Team < ActiveRecord::Base # acts_as_map :default_layer => :google_streets, :nodes => [ :offices ] # end # # class Office < ActiveRecord::Base # belongs_to :team # acts_as_localizable_on :team # end # # Avalaible options: # # All the options can be mapped to a Proc object that will bind self or a Symbol which perform send(:symbol) method. # - :name, :Name of the point. By default, Just another Element # - :popup_content:Content of the popup, if set, element will render a popup in click with this xhtml content. by default, is false # - :icon_url: Icon url to show, by default, /images/ropenlayer/default_marker.png # - :color: Background color, by default transparent # # Examples # # Build a Map of the team offices with custom icons # # # on app/models/team.rb # class Team < ActiveRecord::Base # has_many :offices # acts_as_map :default_layer => :google_streets, :latitude => 40000.23, :longitude => 54555.233, :zoom => 3 # :nodes => [ :offices ] # end # # # on app/models/office.rb # class Office < ActiveRecord::Base # belongs_to :team # acts_as_localizable_on :team, :icon_url => Proc.new{|o| o.main ? '/images/main_office.png' : '/images/office.png' }, # :color => Proc.new{|o| o.main ? 'red' : 'blue' }, # :popup_content => :full_description # # def full_description # %( #{ name } - #{ address } ) # end # end # # # on your view, just # # apps/views/offices/index.html # #
# <%= raw @team.ropenlayer_map(:id => 'offices_for_team_map').to_js %> module Nodeable class << self def included(base) base.class_eval do has_one :node, :as => :nodeable, :dependent => :destroy, :include => :localizations, :class_name => "Ropenlayer::Node" accepts_nested_attributes_for :node base.send :include, InstanceMethods end end module InstanceMethods def node_data attributes_config = self.class.ropenlayer_node_config[:attributes] || {} #[ :name, :popup_content, :icon_url, :color ].each do |attribute| node_map_attributes = Ropenlayer::Openlayer::Feature.all_attributes.inject({}) do |hash, attribute| hash[attribute] = eval_node_attribute(attributes_config[attribute]) unless attributes_config[attribute].nil? hash end if self.node.localizations.any? node_map_attributes[:id] = self.node.id node_map_attributes[:geometry] = self.node.geometry node_map_attributes[:longitude] = self.node.localizations.first.longitude node_map_attributes[:latitude] = self.node.localizations.first.latitude node_map_attributes[:localizations] = self.node.localizations.map{|l| { :latitude => l.latitude, :longitude => l.longitude }} else node_map_attributes = nil end node_map_attributes end def ensure_nodeable_model raise "Model `#{ self.class }` doesnt acts as nodeable" unless self.class.methods.include?("acts_as_nodeable") and self.class.acts_as_nodeable end private def eval_node_attribute(attribute) case attribute when Symbol return self.send(attribute) when Proc return attribute.call(self) else return attribute end end end end end end end