Skip to content

Instantly share code, notes, and snippets.

View ParthivPatel-BTC's full-sized avatar

ParthivPatel-BTC ParthivPatel-BTC

View GitHub Profile
@ParthivPatel-BTC
ParthivPatel-BTC / tasks_controller.rb
Last active September 10, 2019 02:35
Create new task in the google calendar
require "google/apis/calendar_v3"
require "google/api_client/client_secrets.rb"
class TasksController < ApplicationController
CALENDAR_ID = 'primary'
# GET /tasks/new
def new
@task = Task.new
end
@ParthivPatel-BTC
ParthivPatel-BTC / new.html.erb
Last active September 11, 2019 08:44
Create new task form
<%= form_with(model: @task, local: true) do |form| %>
<% if task.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(task.errors.count, "error") %> prohibited this task from being saved:</h2>
<ul>
<% task.errors.full_messages.each do |message| %>
<li><%= massage %></li>
<% end %>
</ul>
@ParthivPatel-BTC
ParthivPatel-BTC / 20190906090110_create_tasks.rb
Created September 10, 2019 02:06
Migrations - Add tasks table
class CreateTasks < ActiveRecord::Migration[5.2]
def change
create_table :tasks do |t|
t.string :title
t.string :description
t.datetime :start_date
t.datetime :end_date
t.string :event
t.string :members
t.references :user, foreign_key: true
@ParthivPatel-BTC
ParthivPatel-BTC / 20190906103341_add_columns_to_users.rb
Created September 10, 2019 02:05
Migrations - AddColumnsToUsers
class AddColumnsToUsers < ActiveRecord::Migration[5.2]
def change
add_column :users, :name, :string
add_column :users, :access_token, :string
add_column :users, :expires_at, :datetime
add_column :users, :refresh_token, :string
end
end
@ParthivPatel-BTC
ParthivPatel-BTC / user.rb
Created September 10, 2019 02:01
OAuthUserModel
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :omniauthable, :omniauth_providers => [:google_oauth2]
has_many :tasks
def self.from_omniauth(access_token)
data = access_token.info
user = User.where(:email => data["email"]).first
@ParthivPatel-BTC
ParthivPatel-BTC / omniauth_callbacks_controller.rb
Created September 10, 2019 01:58
OmniauthCallbacksController
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def google_oauth2
@user = User.from_omniauth(request.env["omniauth.auth"])
if @user.persisted?
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Google"
auth = request.env["omniauth.auth"]
@user.access_token = auth.credentials.token
@user.expires_at = auth.credentials.expires_at
@user.refresh_token = auth.credentials.refresh_token
@user.save!
class Msg91MessageService
def send_sms(to_number, message)
require 'msg91ruby'
begin
api = Msg91ruby::API.new(ENV['MSG91_AUTH_KEY'], ENV['MSG91_SENDER_ID'])
api.send(to_number, message, 4)
rescue => e
end
end
end
2.2.2 :015 > array = Array.new
#=> []
2.2.2 :016 > array = Array.new(5)
#=> [nil, nil, nil, nil, nil]
2.2.2 :017 > array = [ '1', '2', '3' ]
#=> ["1", "2", "3"]
2.2.2 :018 > array = ('1'..'3').to_a
#=> ["1", "2", "3"]
2.2.2 :019 > array = *('1'..'3')
#=> ["1", "2", "3"]
1.DRY - Dont' Repeat Yourself
2.Two spaces, no tabs
3.Boolean Tests: don't use "and" and "or", always use "&&" and "||"
Comments
--------
1.Remove old commented code
2."How to" comments - If you add/change any method/variables etc.., always add detailed description in just above line. So, anybody can understand that code.
Camels for Classes, Snakes Everywhere Else
1.DRY - Dont' Repeat Yourself
2.Two spaces, no tabs
3.Boolean Tests: don't use "and" and "or", always use "&&" and "||"
Comments
--------
1.Remove old commented code
2."How to" comments - If you add/change any method/variables etc.., always add detailed description in just above line. So, anybody can understand that code.
Camels for Classes, Snakes Everywhere Else