Showing posts from 2013

Capybara cheat sheet

=Navigating= visit('/projects') visit(post_comments_path(post)) =Clicking links and buttons= click_link('id-of-link') click_link('Link Text') click_button('Save') click('Link Text') # Click either a link or a button click('Button Value') =Interacting with forms= fill_in('First Name', :with => 'John') fill_in('Password', :with => 'Seekrit') fill_in('Description', :with => 'Really Long Text…') choose('A Radio Button') check('A Checkbox') uncheck('A Checkbox') attach_file('Image', '/path/to/image.jpg') select('Option', :from => 'Select Box') =scoping= within("//li[@id='employee']") do fill_in 'Name', :with => 'Jimmy' end within(:css, "li#employee") do fill_in 'Name', :with => 'Jim…

Scroll to div using JQuery Animate

Here is the small piece of code, which you can use
$("#scroll-btn").click(function (){ $('html, body').animate({ scrollTop: $(".area").offset().top }, 800); return false; });

Browser Testing from Rails console using Capybara and selenium

To begin with you need to install the Capybara gem
gem install capybara Once the gem is installed you can follow the below mentioned procedure.
nishant@Nishant-Lap:~/Desktop/capybara$ rails cLoading development environment (Rails 3.2.13) 1.9.3p392 :001 > require 'capybara/dsl' => true 1.9.3p392 :002 > include Capybara::DSL => Object 1.9.3p392 :003 > Capybara.default_driver = :selenium => :selenium 1.9.3p392 :004 > visit "" => "" 1.9.3p392 :005 > page.fill_in('q', :with => 'nishant nigam')

Remotipart Gem : Response not executing ( create.js.erb )

Cause of this issue is the javascript goes in to a textarea tag and when these contents are extracted from the textarea double quotes(") gets converted to (&quot)

Solution ( create.js.erb ):
<% if remotipart_submitted? %> $('.form-wrapper').html("<%= j "#{render('form')}" %>"); <% else %> $('.form-wrapper').html("<%= j render('form') %>"); <% end %>
More details here
Issue on Github

How to create Safe mailto links using JQuery

Here is a small piece of code that you can use to create safe mailto (spam emails free) links in your web application,

Firstly you need to create a link which is in the example below

<a href="info[at]example[dot]com"></a><a href="info[at]example[dot]com">Custom Text</a> As you can see, we have replaced @ with [at] and . with [dot], Now we will use JQuery to loop through all the links and do the exact opposite.

$(document).ready(function() { $('a[href*="[at]"][href*="[dot]"]').each(function() { var addr = $(this).attr('href').split('[at]').join('@').split('[dot]').join('.'); $(this).attr('href', 'mailto:' + addr.toLowerCase()); if ($(this).text().length == 0) $(this).text(addr); }); }); This JQuery code will generate the following links.

<a href=""></a>   <a href="mailto…

Git Visual Cheat Sheet


Gems for Development Group

Here are the list of gems which you can use in your development machine or inside the development group in your gemfile, Basically these gems helps to make things faster and bring simplicity to the development process.
gem 'debugger' gem "better_errors" gem "binding_of_caller" gem 'annotate' gem 'rails-footnotes' gem 'rails_best_practices' gem 'bullet' gem 'flay' gem 'pry' gem 'pry-doc' gem 'quiet_assets' gem 'rack-mini-profiler' gem 'railroady' gem 'reek' gem 'request-log-analyzer' gem 'smusher' gem 'hirb' gem 'localtunnel' gem 'lol_dba' gem 'mailcatcher' gem 'meta_request','0.2.1' Other then the above mentioned gems there is one gem which you must have
gem install zeus You don't need to mention this inside your Gemfile, This really helps to improve the develo…

How to Handle OAuth::Unauthorized error for Omniauth provider?

Add this piece of code in your initializer file
Rails.application.config.middleware.use OmniAuth::Builder do provider :twitter, twitter_consumer_key, twitter_consumer_secret end OmniAuth.config.on_failure = { |env| } This will redirect user to the failure page, instead of showing an error.

Rake Routes for a Single Controller

You can list routes in your Rails application with:
rake routes If in-case you were looking for some specific route and grep doesn't solve your problems
rake routes | grep 'users' Here is a tip which you can try
rake routes CONTROLLER=controller_name

Handling JavaScript Confirm in Selenium and Capybara

page.driver.browser.switch_to.alert.accept This will accept the confirm box.

Public wildcard domain name that resolves to IP address

These are the various options for the public wildcard domain, It's good for testing sub-domains on localhost. So we don't need to make any sort of changes in hosts file.

Passing command line arguments to a rake task

Creating a rake task
desc "Description of what this task does" task :myraketask => :environment do .. .. end Once you have created a rake file(File with extension as .rake) and placed in /lib/task folder in your application.You can check rake -Tfor all the avaliable tasks, Your custom task will also be listed here with the description you have mentioned.
So this is the general way of using your rake task.
rake myraketask And if you wanted to pass any sort of arguments to your rake task , you can do this by
rake myraketask foo=bar So the argument foo can be accessed by
ENV['foo']in your task.

Better Git Log in browser window

Better Git Log

For all the Git users try this command , it will generate a better logging for git. It also shows your recent commits and the code difference in your browser window. You can also mention a port number using -p option.

git instaweb --httpd webrick Enjoy !! Play with it :)

Rails Error Messages on Fields and other Customizations

Rails Error Messages on Fields and other Customizations

If you wanted to show an error message with the field and also you wanted to highlight that particular field. Here you have to create a new file inside  config/initializers folder of your application, make sure to restart your application if you are making any sort of changes inside the config folder.

Here we are basically adding a class "error" to the field and showing a span with the error message.

ActionView::Base.field_error_proc = do |html_tag, instance| class_attr_index = html_tag.index 'class="' errors = Array(instance.error_message).join(',') if !class_attr_index.nil? html_tag.insert class_attr_index+7, 'error ' else html_tag.insert html_tag.index('>'), ' class="error"' end %(#{html_tag} #{errors}).html_safe end You can add your css for example.