Creating direction from source to destination in Google Map using Rails

Google map in Rails application is implemented by YM4R  gem/ ym4r_gm plugin. Suppose anyone one to see direction in Google Map From his address to destination address by clicking his city point in Map. The following code in Ruby on Rails do that with a bit of JavaScript function...........
def initiate_map fromadd , toadd
@map = GMap.new("map_div");
@map.control_init(:large_map  => true,:map_type => true)
@map.center_zoom_init([38.957101, -95.251469],4)
@map.overlay_init(GMarker.new(fromadd))
@map.event_init(@map, :dblclick,"function(marker,address){ if (marker)
{map.removeOverlay(marker); } else {
var gdir;
gdir  = new GDirections(map, document.getElementById('directions'));
gdir.load('from: ' + '#{fromadd}' + ' to: ' + '#{toadd}');
}}" )
end
Now for dynamically updating the direction do the following code….

def initiate_map1 fromadd , toadd
@map=GMap.new('map_div')
@js_str = "var geocoder = new GClientGeocoder(); \n"+
"geocoder.getLatLng( '#{MappingObject.javascriptify_variable(fromadd)}',function(point) {
map.setCenter(point, 13);
var marker = new GMarker(point);
map.addOverlay(marker);
marker.openInfoWindowHtml('#{MappingObject.javascriptify_variable(fromadd)}');
}) ; \n" +
"GEvent.addListener(map, \"click\", function(address) { var gdir  = new GDirections(map, document.getElementById('directions'));
gdir.load('from: ' + '#{MappingObject.javascriptify_variable(fromadd)}' + ' to: ' + '#{MappingObject.javascriptify_variable(toadd)}');});"
end

After that you have to write  page << @js_str in a js tempalte. In the view write this code

=GMap.header
=@map.to_html
=@map.div(:width => 600, :height => 400)
.directions

All is done. 

Advertisements

My reading blog list…..

SM Sohan’s blog for some interesting things of Rails =>  http://smsohan.blogspot.com/

Samiron’s blog for creative talks => http://samironpaul.blogspot.com/

How to create procedure and passing argument as code block ??

In Ruby a Proc is an object that holds a chunk of code. We can create Proc by ” lambda( )” method. To call the code block we have to use call method of Proc.

And if we want this Proc object to pass as code block argument then we have to append an ‘&’ with Proc name.

One thing to remember that we have to associate an ‘&’  with the parametername to pick up any code block in the method definition. The whole thing is as follows —-

class Report

def output_report (&Outputblock)
Outputblock.call(  )
end

end

# code-block here with Proc

hello = lambda do

puts('Hello')
puts('I am inside a proc')
end

report = Report .new

report.output_report( &hello)

————————————————————-

 

In the above code “hello” is the created procedure.

Auto-escaping HTML with Rails in the model level for protecting script injection

We can sanitize an input field data easily before saving or before displaying it in Rails. In this case we don’t need to h function in the view level. We can do it at each model by using plugin.

xss-terminate is a plugin  that makes stripping and sanitizing HTML stupid-simple, => http://github.com/look/xss_terminate , here we can find plugin. But currently it can’t be installed directly, so we have to download it and unpack its contents in a folder named “xss_terminate” under vendor/plugins folder.

Suppose we want to sanitize any script before saving an user name in user model so we have to just write the following code……

class User < ActiveRecord::Base
xss_terminate  :sanitize=>[:user_name]

...

.....
end

We can also avoid any field from sanitizing by “:except” option as follows..

xss_terminate  :except => [:address]

More on…..

http://railspikes.com/2008/1/28/auto-escaping-html-with-rails

How we do A/B testing in Rails

Configuration

For A/B testing in Rails there is a gem named vanity. At first we have to install the vanity gem by the command=> gem install vanity. After that we have to just configure the test.rb file under the “/environments” folder as follow…

config.after_initialize do
require "vanity"
Vanity.playground.test!
end

Be careful this code must be written at the end of the file. Here we used Vanity.playground.test!  for not accessing the redis for storing data. But if we want to access redis then we have to configure as follows…

Vanity.playground.redis = "::15" # Different DB
Vanity.playground.redis.flushdb # Do this before running tests

Add a new controller for Vanity:

class VanityController < ApplicationController
include Vanity::Rails::Dashboard
end

Lets write a test code  ===>

Now our A/B test file should be under the “experiments/” folder. Lets see a sample code

ab_test "Customer Message without Pricing" do
 description "Testing to see if messages without pricing increases number of signups."
 metrics :trial_activation
end

Here the name of the metric parameter is same as the name of the file where our actual experiment is done under the “experiments/metrics/” directory. The following code is contained in the trial_activation.rb file.

metric "Signup for trial" do
 description "Measures how many people signed up to try our awesome service."
 model Account.in_trial
end

Quick helper links…………….

Important links……

Basic about Object Oriented JavaScript

JavaScript has nice and special object-oriented features. Discussion about some of these features are…….

(1) JavaScript Object

Object in JavaScript is not same as C#, Java. JavaScript object is called a name-value pair i.e just like {“name” : “value”}. A function in JavaScript is basically an object function. Example…..

  • var motor = { “Name” : “Honda” };
  • alert( motor.Name ); // output is Honda.

(2)Instantiating Object

In JavaScript an object is instantiated by calling function with the new operator. This is done by following way…..

  • function Car(name) {
  • this.name=name;
  • }
  • var motor = new Car(“Honda”);

(3) Prototype object

An object is a copy of existing example (prototype) object. Any properties and method of the prototype object also appear in the created object. That means created object inherits the properties and method of the prototype object. Every constructor function/object has property name “prototype” and this prototype object has a property called “constructor”. Constructor property indicates the base constructor function. One thing need to remember that “constructor” is not the own property of created object. In JavaScript “Object.prototype” is the ultimate base prototype of  all prototypes.

(4)Static members

A member declared in a function object  associated with the function name is called static member of the object. The calling of the static member/function is same as Java.

  • function Car() {
  • Car.name= function(){
  • return “Motor”;
  • };
  • }
  • Car.name();

(5)Concept of closure

Anonymous inner function is treated as closure in JavaSript because the inner function closes over the environment (the arguments and local variables of the outer function) in which it is defined. Closure in JavaScript is used for simulating private members. See the following….

  • function Check (upperbound) {
  • return function (lowerbound ) {
  • return (upperbound>lowerbound) ? true : false;
  • };
  • }
  • var bigger =  Check (100);
  • alert(bigger(90));  // return true.


(6) Accessing Private Members

Normally local variables of a function can’t be accessed outside the function. But in JavaScript local variable can be accessed through inner function’s closure.

(7) Inheriting classes

Properties of one class(prototype) is inherited by another class(prototype) only by constructor function. Lets see it in following…..

  • function vehicle {
  • //
  • }
  • function car {
  • //
  • }
  • car.prototype = new vehicle();

Here, car inherits the vehicle class.

(8) Namespaces

Namespaces in JavaScript is used for creating JavaScript library. In the following way we can build Namespaces for prototype classes…..

  • var namespace={ };
  • namespace.ClassPrototype = function() {
  • //
  • };

Read the rest of this entry »