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 

Thursday 20 August 2015

How To: Create custom layouts for devise:


1, Define layouts in the ApplicationController
2, Define layouts in config (config/environment.rb for rails 2, config/application.rb in rails 3)

Define layouts in the ApplicationController:

class ApplicationController < ActionController::Base
  layout :check_layout

  protected

  def check_layout
    if devise_controller? && resource_name == :user && action_name == "new"
      "layout_for_devise"
    else
      "application"
    end
  end
end

devise_controller? --> Determine weather it's devise controller or not
resource_name ---> Dealing with roles
action_name --> To determine specific action

2, Define in config:

Below code need to write in application.rb:

#to set layout for specific controller
config.to_prepare do
  Devise::SessionsController.layout "layout_for_sessions_controller"
  Devise::RegistrationsController.layout proc { |controller| user_signed_in? ? "application" : "devise" }
end

#to set layout for devise mailer
config.to_prepare do
  Devise::Mailer.layout "email_layout" #email_layout.erb
end

If u want to write these configurations in devise.rb:
# append to end of config/initializers/devise.rb
Rails.application.config.to_prepare do
  Devise::SessionsController.layout "layout_for_sessions_controller"
  Devise::RegistrationsController.layout proc { |controller| user_signed_in? ? "application" : "layout_for_registration_controller" }
end


that's it..

To generate devise views:
rails generate devise:views

To destroy generate devise views:
rails destroy devise:views

if you want to destroy an especific set of views use:
rails destroy devise:views model
instead of model use the name of the model you want to destroy, for example user, or admin whichever your model is.

Monday 3 August 2015

Rails touch method

touch method:

Saves the record with the updated_at/on attributes set to the current time.

Product.last.touch # it will set update_at with current date and time

Product.last.touch(:last_signed_in) # it will set last_signed_in with current date and time

How to skip rails validation



Skip all validations:

validates :name, presence: true

@user.name = ""
@user.save(validate: false)

It saves the record without running any validations.

Skipping individual validations:

Skipping individual validations requires a bit more work. First, we need to create a property on our model called something like skip_name_validation:

attr_accessor :skip_name_validation, :skip_price_validation

Next we will tell Rails to check and see if that property is set to true:

validates :name, presence: true, uniqueness: true, unless: :skip_name_validation
Validates: price, presence: true, numerically: {greater_than: 0 }, unless: :skip_price_validation

Finally we will set the property to true any time we want to skip validations. For example:

def create
   @product = Product.new(product_params)
   @product.skip_name_validation = true
   if @product.save
    redirect_to products_path, notice: "#{@product.name} has been created."
  else
    render 'new'
  end
end

def update
  @product = Product.find(params[:id])
  @product.attributes = product_params
  @product.skip_price_validation = true
  if @product.save
    redirect_to products_path, notice: "The product \"#{@product.name}\" has been updated. "
  else
    render 'edit'
  end
end

Above you will see that for the create method we skip name validation; and for the update method we skip price validation. A complete listing of the model is shown below.

class Product < ActiveRecord::Base
  validates :name, presence: true, uniqueness: true, unless: :skip_name_validation
  validates :price, presence: true, numericality: { greater_than: 0 }, unless: :skip_price_validation

  attr_accessor :skip_name_validation, :skip_price_validation
end
That's it...:-)