Thursday, 10 September 2015

Recapture gem integration steps:


1, add recaptcha gem to gemfile.
gem 'recaptcha', :require => 'recaptcha/rails'
2, the run bundle
bundle install
3, register an app in google recaptcha with following link
https://www.google.com/recaptcha/admin
4, add recaptch.rb with following code in instializers
Recaptcha.configure do |config|
  config.public_key  = 'xxxxxxxxxxxxxxxxxxxc' #site key
  config.private_key = 'xxxxxxxxxxxxxxxy' #secreat key
  config.api_version = 'v2'
end
5, add below tag in sign up form
<%= recaptha_tags %>
6, add below code in regitsration create method
def create
if verify_recaptcha
super
else
build_resource(sign_up_params)
flash.now[:alert] = 'There was an error with the recaptcha code below. Please re-enter the code.'
render :new
end
end

that's it..

Friday, 4 September 2015

Validates Email Field From Controller



class User
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

if valid_email?(user.email)
user.save
redirect_to root_url, :notice => 'Good email'
else
flash[:error] = 'Bad email!'
render :new
end

private
def valid_email?(email)
email.present? &&
(email =~ VALID_EMAIL_REGEX) &&
User.find_by(email: email).empty?
end
end

Thursday, 3 September 2015

Sublime text2 package control install:



Go to link
https://packagecontrol.io/installation
Copy python code for sublime text2/3

ctrl+`  --> To open console.

Paste python code, then press enter

Restart sublime.

Ex: To install trailing spaces.

Then install package Trailing spaces(like package control)

ctrl+shift+p search for package control press enter.

ctrl+shift+p search for trailing space press enter.

Monday, 31 August 2015

Better Errors To Open In Sublime:


group :development, :test do
gem 'better_errors'
gem 'binding_of_caller'
  gem 'meta_request'
end

$bundle

add better_errors.rb in /config/initializers then add bellow code:

if defined? BetterErrors
  BetterErrors.editor = proc { |full_path, line|
    full_path = full_path.sub(Rails.root.to_s, "/home/saritha/sites/testproj")
    "subl://open?url=file://#{full_path}&line=#{line}"
  }
end

To handle with firefox:

1. First of all download and install sublime url handler patch to handle the url with line number. Download sublime-url-handler.
from https://github.com/algorich/sublime-url-handler/archive/master.zip
2. Goto to development.rb file and add BetterErrors.editor = :sublime

3. Goto firefox and type about:config in url and hit enter

4. Right click and create new property with boolean type with name " network.protocol-handler.expose.subl" set "False".

5. Restart Firefox.

6. Run your rails app and get the error link page, click on error link it will ask open open with if sublime-url-hanlder is shown here well done choose and cheers if not shown than give your sublime executable path here and done.

you can add https://chrome.google.com/webstore/detail/railspanel/gjpfobpafnhjhbajcjgccbbdofdckggg to chrome..this will help in detail work flow..

Create custom rake tasks:

Rake Tasks:

Rake is Ruby Make, a standalone Ruby utility that replaces the Unix utility 'make', and uses a 'Rakefile' and .rake files to build up a list of tasks. In Rails, Rake is used for common administration tasks, especially sophisticated ones that build off of each other.

It is most often used for administration level tasks that can be scripted. The benefit to using Rake over Make or similar, is that it is a Ruby tool and can interface with your RoR app natively, so Models, data constraints and business rules are all available for use.

1. create a rake file

say lib/tasks/product.rake

another way to create a rake file:

$rails g task my_namespace my_task1 my_task2
$ create lib/tasks/my_namespace.rake
It will generate scaffold for our new rake task: >lib/tasks/my_namespace.rake

namespace :my_namespace do
  desc "TODO"
  task :my_task1 => :environment do
  end

  desc "TODO"
  task :my_task2 => :environment do
  end
end

It is awesome! And now you can write here your code for new rake tasks.

Let’s make sure these rake tasks are exist and we are able to use them:

$ rake -T | grep my_namespace
rake my_namespace:my_task1  # TODO
rake my_namespace:my_task2  # TODO

To upload products from csv using rake task:
require 'rubygems'
require 'csv'
namespace :product do
desc "To Upload products"
task :upload , [:file] => :environment do |task, args|
CSV.foreach(args[:file]) do |row|
puts row[0] + "creating.."
if row[0].present?
Product.create(row[0])
else
puts "Here is an empty row!"
end
end
end
end

$rake --tasks
$rake product:upload[file] #To Upload products

rake product:upload['/home/saritha/Downloads/270815 New Registrations.csv']

To run rake tasks in production mode:

$rake product:upload['/home/saritha/Downloads/270815 New Registrations.csv'] RAILS_ENV=production

Friday, 21 August 2015

How to get rails process id


ps aux|grep rails

ps --> process status

a --> show processes for all users
u -->  display the process's user/owner
x --> also show processes not attached to a term

By the way, "man ps" is a good resource.

What is the parent class of all classes in Ruby?



2.2.2 :001 >  Class.superclass
 => Module
2.2.2 :002 > Module.superclass
 => Object
2.2.2 :003 > Object.superclass
 => BasicObject
2.2.2 :004 > BasicObject.superclass
 => nil

===> "BasicObject" is the parent class of all classes in Ruby.

2.2.2 :001 >  BasicObject.class
 => Class
2.2.2 :002 > Object.class
 => Class
2.2.2 :003 > Module.class
 => Class
2.2.2 :004 > Class.class
 => Class