Wednesday, January 11, 2017

Spawnling Gem in rails

Spawnling gem:

This gem provides a 'Spawnling' class to easily fork OR thread long-running sections of code so that your application can return results to your users more quickly. It works by creating new database connections in ActiveRecord::Base for the spawned block so that you don't have to worry about database connections working, they just do.

Step 1: Gem add in Gemfile:

            gem 'spawnling'

Step 2: Add in database.yml file

            production/development:
       ..............
       ..............
       reconnect: true

Step 3: Usages:

      Spawnling.new do

       @calendar.send_notification_to_team # method name

      end

Tuesday, August 30, 2016

Login with facebook

Step 1:

Add gem into gemfile
gem 'omniauth-facebook'

Step 2:

Facebook App setup
https://developers.facebook.com/

Step 3:

run command on terminal
bundle install
Step 4:
Need to add facebook app id and facebook secret key

config/initializers/devise.rb
config.omniauth :facebook, 'facebook app id', 'facebook app secret',{:scope => 'email'}


Step 5:

we will need to create a User model to store our information that Facebook returns. Run the commands below to create the user model.
rails g model user provider uid name oauth_token oauth_expires_at:datetime
rake db:migrate

Step 6:
create omniauth_callbacks_controller

devise/omniauth_callbacks_controller.rb
class Devise::OmniauthCallbacksController < ApplicationController
  skip_before_filter :authenticate_user!
  def facebook
    begin
      @user = User.from_facebook_omniauth(request.env['omniauth.auth'])
      session[:user_id] = @user.id
      sign_in @user
      flash[:success] = "Welcome, #{@user.name}!"
    rescue
      flash[:warning] = "There was an error while trying to authenticate you..."
    end
    redirect_to users_path
  end
  def failure
    redirect_to root_path
  end
end

Step 7:

  config/routes.rb
devise_for :users, :controllers => {:omniauth_callbacks => "devise/omniauth_callbacks",registrations:"devise/registrations"}
Step 8:

Write a method in user model
app/model/user.rb
 def self.from_facebook_omniauth(auth)
    email = auth.info.email.present? ? auth.info.email : "#{auth.uid}@facebook.com"
     user_exist = User.find_by_email(email)
     if user_exist.nil?
       where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
         user.provider = auth.provider
         user.uid = auth.uid
         user.email = email
         user.first_name = auth.info.name.split(' ')[0]
         user.last_name = auth.info.name.split(' ')[1]
         user.username = auth.info.name.split(' ')[0].downcase
         user.password = Devise.friendly_token[0,20]
       end
     else
       user_exist.update(provider: auth.provider, access_token: auth.credentials.token, uid: auth.uid, first_name: auth.info.name, last_name: nil)
     end
     user_exist = User.find_by_email(email)
   end



About us


Saturday, August 27, 2016

Ckeditor Gem

CKEditor:
CKEditor is a WYSIWYG text editor designed to simplify web content creation. It brings common word processing features directly to your web pages. Enhance your website experience with our community maintained editor.

Step 1:

add gem to gemfile 

gemfile

gem 'ckeditor_rails'

Step 2:

In Terminal

$ bundle install

Step 3:

 Include CKEditor javascript assets

app/assets/javascripts/user.js

//= require ckeditor-jquery

Step 4:
 Configure plugins, languages, and skins of CKEditor assets

config/initializers/assets.rb

Ckeditor::Rails.configure do |config|
  # default is nil for all languages, or set as %w[en zh]
  config.assets_languages = nil
  # default is nil for all plugins,
  # or set as white list: %w[image link liststyle table tabletools]
  # or set as black list: config.default_plugins - %w[about a11yhelp]
  config.assets_plugins = nil
  # default is nil for all skins, or set as %w[moono]
  config.assets_skins = nil
end

Friday, August 26, 2016

Export Data in xls format

Step 1:

Add Gem into Gemfile

gem 'to_xls'


Step 2:


app/admin/view/stories/index.html.haml


Give link on button:
%a.btn.btn-primary.create_new{:href => admin_export_stories_path(:release => @release, format: "xls")}
   %i.fa.fa-plus
   Export


Step 3:


Create a action in controller


app/controller/admin/stories_controller.rb


def export_stories_report
if params[:release].present?
@release = Release.find(params[:release])
@stories = @release.stories
respond_to do |format|
   format.xls{send_data @stories.to_xls(:only => [:id, :project_id, :release_id, :release_name, :title, :if, :when, :then, :estimated_hours, :tags, :status]) }
end
end
end


Step 4:


Add Mime type
config/initializer/mime_type.rb

Mime::Type.register "application/xls", :xls


Thursday, August 25, 2016

Brakeman Gem


Brakeman Gem:
          A security analysis tool for your Rails applications. It scans through your application and outputs a nicely formatted table of possible vulnerabilities. Security warnings are grouped according to their severity (High, Medium and Low).

Step 1:

In Terminal

$ gem install brakeman 

Step 2:

In Terminal

$ brakeman


Traceroute Gem

Traceroute Gem:

       Traceroute is a route cleaning tool for Rails applications. It provides a simple rake task for checking which routes are mapped to non existing controller actions, and finds out which controller actions are not reachable.

Step 1:

Add gem into Gemfile

config/gemfile

gem 'traceroute'

Step 2:

Then, run below command on console

$ rake traceroute