SM Sohan’s blog for some interesting things of Rails => http://smsohan.blogspot.com/
Samiron’s blog for creative talks => http://samironpaul.blogspot.com/
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.
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]
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