Ruby : Public , Private, Protected simplified

My Daily Technical Notes [ Ruby, Rails, Gems, Server Ops]

class A
  privatedef pri
    'hey I am private of A'
  endprotecteddef prot
    'Hey I am protected of A'
  endendclass B < A
  def call_pri_of_a
    pri
  enddef call_prot_of_a
    prot
  endend

Now calling the accessors within B

 > b = B.new=> #<B:0x00000002b56ad8> 
 > b.call_pri_of_a
 => "hey I am private of A" 
 > b.call_prot_of_a
 => "Hey I am protected of A"

Up to here; there seem to be no difference

bb = B.new=> #<B:0x00000002b1a0b0>
def bb.access_private(child_of_B)
  child_of_B.pri
end
def bb.access_protected(child_of_B)
  child_of_B.prot
end

Now calling the accessor

> bb.access_private(b)
# => NoMethodError: private method `pri' called for #<B:0x00000002b56ad8>
> bb.access_protected(b)
# => "Hey I am protected of A"

View original post

Processes to run Whenever gem in remote server.

These are the processes to run whenever gem in remote server:

  1. Install the whenever gem
  2. Write your scheduler.rb file
  3. Push to the remote server
  4. Login to the remote server (for example with ssh)
  5. See if whenever is good uploaded by running in terminal: whenever . You can see the following type result if whenever is uploaded.

Screen Shot 2015-10-04 at 9.40.12 PM

  1. Update whenever crontab by running: whenever –update-crontab
  2. Restart the server crontab (for example in Ubuntu server): sudo service cron restart
  3. Check if crontab is good implemented on the server: crontab -l.  You can see the following type result after whenever implemented.

Screen Shot 2015-10-04 at 9.48.08 PM

Creating User and Admin Model using Devise Rails 4.0

Ruby on Rails Help

In this tutorial I will show you how to install Devise. I decided to make this tutorial because it is hard to find a good resource for installing Devise on Rails 4.0. This tutorial will NOT teach you the basics of Ruby on Rails. It is intended to help those who have a basic understanding of Ruby on Rails.https://rubyonrailshelp.wordpress.com/wp-admin/post.php?post=12&action=edit&message=10

If you would like a good place to start learning Ruby on Rails, read and complete http://ruby.railstutorial.org/ruby-on-rails-tutorial-book by Michael Hartl

Other good resources are:
Devise Github page https://github.com/plataformatec/devise
Railscasts http://railscasts.com/

In this tutorial I will be using the following gems

  • Rails 4.0
  • Devise 3.2
  • PostgresSQL 9.1

Using PostgreSQL is optional. If you would like to use it then you can follow the railscast to learn how to set it up. http://railscasts.com/episodes/342-migrating-to-postgresql

This tutorial will show you how to create two models within devise. We will create a User and Admin model…

View original post 572 more words

Google Integration Using Devise and Omniauth In Rails App.

GOOGLE INTEGRATION USING DEVISE AND OMNIAUTH IN RAILS APP.

Ruby on Rails

ruby-on-rails-logowwAdd118003-matte-blue-and-white-square-icon-social-media-logos-google-g-logo

In this blog i’ll show how to integrate Google authentication using devise.To install devise you can refer to my blog here.

Step:1
Add the gems in your gem file

gem ‘devise’gem 'omniauth'gem 'omniauth-google-oauth2' 

Run the “bundle install” command to install the gem.

Step:2
You need two more columns to store provider type and userid given from google

rails g migration AddProviderToUsers provider:string uid:string

Runt rake db:migrate to insert the columns in users table.

Step:3
Go the user model “user.rb” and add the following line

devise : omniauthable

Step:4
First of all you need to create an app in google to get “Client key” and “Client Secret key”

https://code.google.com/apis/console/

Create an app and get the Client id and secret key.

Step:5
Now you need to declare the provider name and client id and key.Go to the file config/initializers/devise.rb and the following line

require 'omniauth-google-oauth2' config.omniauth :google_oauth2, "APP_ID", "APP_SECRET"…

View original post 195 more words

minimum maximum validation between two attributes

No GUI Just CLI

There have been many instances when working with rails application, I had to put the minimum, maximum custom validator or active record callbacks check if the maximum value is valid compared to minimum value.

Few such examples are:

1. Comparing minimum and maximum salary.
Maximum salary should not be less than the minimum salary. It can only be equal or greater than the minimum salary.
Minimum salary or Maximum salary can be blank because we might have constraint on minimum salary and no cap on maximum salary or vice a versa.

2. Comparing the age.
Comparing the father son age. Son’s age should not be more than father’s age

3. Comparing Minimum, maximum points to avail an offer.

Though writing a custom validator is a matter of 5 minutes. But for each of the case in different application you have to repeat the code in one of the module or…

View original post 152 more words

Export data to CSV and Excel in your Rails

Cloud Computing | #CloudFoundry | Ruby | Go | OpenStack | Ansible | Docker | Jenkins

A common requirement from customers is the ability to export tabular data to a CSV file that can be imported into Excel. Ruby on Rails uses the standard ruby CSV library to import test fixtures that are in CSV format.

Below are the steps we need to follow.

1. require the CSV library to controller, Where we need to write import code

require 'csv'

2. Emport code in controller

def export_to_csv       
    @users = User.find(:all)
    csv_string = CSV.generate do |csv|
         csv << ["Id", "Name", "Email","Role"]
         @users.each do |user|
           csv << [user.id, user.name, user.name, user.role]
         end
    end         
  
   send_data csv_string,
   :type => 'text/csv; charset=iso-8859-1; header=present',
   :disposition => "attachment; filename=users.csv" 
end @users = User.find(:all)
Fetching all user details and assigned on @users
csv_string = CSV.generate do |csv|
Using CSV class generate method to create csv file
csv << ["Id", "Name"…

View original post 40 more words

Using Google Apps Scripts as a back end

Using Google App’s Script as a back end

Reflections

I recently built a quick internal web app for mobiles using Sencha Touch, and I needed a back end system for very simple content management. As this was a lightweight project without a need for ongoing support and with no expectation of high use, I didn’t want to build a whole back end, or pay for an out of the box solution that would feel like overkill – and not having control of the server was a factor too.

I decided to use Google Spreadsheets for the ad hoc back end, knowing they had an API that supports JSON, but not having explored it before. Sencha Touch comes with handy AJAX and JSONP handlers that I wanted to use as well; the JSON needs to be clean and in a useful format for this to work out of the box – unfortunately what the Spreadsheets API gives you is very…

View original post 836 more words