Skip to content

Instantly share code, notes, and snippets.

View 8parth's full-sized avatar

Parth Modi 8parth

View GitHub Profile
@8parth
8parth / index.html
Created October 6, 2016 16:40
Random Quote Machine
<body>
<div class="outer-block" style="">
<div class="inner-block text-center">
<blockquote class="font-weight-bold font-italic text-style">
<div class="row">
<div class="col-md-12 quote">
The quote will appear here.
</div>
<div class="col-md-12 text-center author font-italic text-muted text-style">
@8parth
8parth / let_example.rb
Created October 17, 2016 16:53
explaining usage for let block in rspec tests
RSpec.describe UsersController, type: :controller do
# let can be used as instance variables, instead of @user
let(:create_user) { FactoryGirl.create(:user) }
let(:new_path){ new_user_path }
let(:edit_path){ edit_user_path }
# let can be used as parameters that can be shared across examples
let(:create_params){
{
name: "NAME",
age: "20"
@8parth
8parth / as_json_example_old.rb
Last active October 20, 2016 17:06
old approach of overriding as_json
# override as_json method of User class
def as_json(options={})
h = {
name: self.name,
picture: self.picture.url(:medium),
age: self.age
}
h
end
# put helper methods, such as creating full name from given first_name and last_name attribute of user
@8parth
8parth / as_json_example.rb
Created October 22, 2016 04:25
to show how as_json can be overridden and used effectively
# override as_json method of User class
def as_json(options={})
super(only: [:picture, :age],
methods: [:name])
end
# put helper methods, such as creating full name from given first_name and last_name attribute of user
def name
self.first_name +" " +self.last_name
end
# example response, note that timestamps, authentication token and password digest fields are not present
@8parth
8parth / as_json_example_enhanced.rb
Last active October 22, 2016 12:06
to show how as_json can be used with super
def as_json(options={})
# options hash accepts four keys for better customization :only, :methods, :include, :except
# so whenever such keys are found, we call super with those keys to provide response consisting only those keys
if options.key?(:only) or options.key?(:methods) or options.key?(:include) or options.key?(:except)
h = super(options)
else
h = super(only: [:picture, :age],
methods: [:name],
include: {:emails => { :only => [:id, :email] })
end
def index
@users = User.all
render json: @users
end
@8parth
8parth / fetch_users_optimized.rb
Created November 12, 2016 14:01
example with nested includes
def index
@users = User.all.includes(:posts => [:comments])
render json: @users
end
@8parth
8parth / models.rb
Last active November 13, 2016 04:32
model structure
class User < ApplicationRecord
has_many :posts
has_many :comments
end
class Post < ApplicationRecord
has_many :comments
belongs_to :user
end
@8parth
8parth / treat_swagger_documentation_as_comments.rb
Created December 8, 2016 06:00
Ignoring swagger-docs lines by treating them as comments for simplecov gem
module SimpleCov
class SourceFile
class Line
def reinitialize_line(coverage)
raise ArgumentError, "Only Integer and nil accepted for coverage" unless coverage.is_a?(Integer) || coverage.nil?
@coverage = coverage
@skipped = false
end
end
end
@8parth
8parth / revert_destroy_all.rb
Last active February 2, 2024 09:04
revert back accidently deleted Model.destroy_all in rails console
Model.destroy_all
# if you haven't performed any other action after accidently deleting all records from Model,
# then _ would grab last response generated by Model.destroy_all
destroyed_records = _
destroyed_records.each do |destroyed_record|
Model.create(destroyed_record.attributes)
end