Skip to content

Instantly share code, notes, and snippets.

View aruprakshit's full-sized avatar
🏠
Working from home

Arup Rakshit aruprakshit

🏠
Working from home
View GitHub Profile
@aruprakshit
aruprakshit / dream_spec.rb
Last active August 29, 2015 14:02
Rspec to test my dream
describe "#dream" do
let(:user1) { User.create }
let(:user2) { User.create }
before do
# below update is on the hand of God
user1.update(:luck => true)
user2.update(:luck => false)
end
describe "#users" do
let(:sub_reporting_group) { FactoryGirl.create(:reporting_group) }
let(:reporting_group) { FactoryGirl.create(:reporting_group, reporting_group_ids: [sub_reporting_group.id]) }
let(:user) { FactoryGirl.create(:user) }
let(:team) { FactoryGirl.create(:team) }
let(:workplace) { FactoryGirl.create(:workplace) }
context "when user belongs to either team_id or workplace_id of a parent reporting group" do
it "shouldn't include user when workplace_id not present" do
user.update(team_id: team.id)
Arup-iMac:yelloday shreyas$ git add -p
diff --git a/app/interactors/reporting_groups/list_colleagues.rb b/app/interactors/reporting_groups/list_colleagues.rb
index adc28af..f46f5e3 100644
--- a/app/interactors/reporting_groups/list_colleagues.rb
+++ b/app/interactors/reporting_groups/list_colleagues.rb
@@ -14,7 +14,4 @@ module ReportingGroups
reporting_group.employees_from_team_sub_reporting_groups
else
reporting_group.users
- end
@aruprakshit
aruprakshit / belongs_to.rb
Created June 12, 2014 10:07
Confusion with Rails belongs_to association
#------model definition-----
class Book < ActiveRecord::Base
end
class Page < ActiveRecord::Base
belongs_to :book
end
#------migration----------
class CreateBooks < ActiveRecord::Migration
def change
# This gist is the correct one taken from Book Beginning Rails 4. The gist they mentioned in the book accidentally contains wrong
# code. Thus I created this gist, so that you can copy the code directly and move on.
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
def new
@user = User.new
end
2.1.2 :008 > Employee.pluck(:age)
(0.4ms) SELECT "employees"."age" FROM "employees"
=> [19, 29, 20, 17, 25, 19, 27, nil]
2.1.2 :009 > Employee.pluck('age)
2.1.2 :010'> ^C
2.1.2 :010 > Employee.pluck(:age)
(0.4ms) SELECT "employees"."age" FROM "employees"
=> [19, 29, 20, 17, 25, 19, 27, nil]
2.1.2 :011 > Employee.pluck('age')
(0.7ms) SELECT "employees"."age" FROM "employees"
@aruprakshit
aruprakshit / respond_to.rb
Last active August 29, 2015 14:03
respond_to? vs respond_to_missing?
# First check is always #respond_to? , if it returns false, then #respond_to_missing? will be called, otherwise #respond_to_missing?
# will never be called. For all regular methods( which has been created by def, define_method, in C-implementation etc), thus we get
# false, while we pass the method name to the #respond_to_missing?. Because, first check has been done inside #respond_to?, and it
# returns true. That's why second output is [true, false]. Because, #foo is a regular method.
# Now why the second output is [true, true] ? The reason, I already explained, again #baz is not a regular method, so first check in
# #respond_to? return false, thus check goes to #respond_to_missing?. As I implemented it, so as per the implementations it is giving
# true, which in turn caused #respond_to? method to returns to true.
@aruprakshit
aruprakshit / to_a.md
Last active August 29, 2015 14:03
Playing with Enumerable#to_a

Enumerable#to_a says, that it can take arguments also. Lets try

(1..4).to_a(1,2)
#ArgumentError: wrong number of arguments (2 for 0)

Oops! Why then error ? Because Enumerable#to_a called actually Range#each which don't accept any arguments. Now look the below code :-

class Wrong
def method_missing(m, *)
if m =~ /\Ahello_(.+)\z/
puts "Hello, #{$1.capitalize}"
else
super
end
end
end
@aruprakshit
aruprakshit / Enumrable.md
Created July 7, 2014 04:05
Enumerable vs Enumerator

Enumerable is a module, and Enumerator is a class. So that should tell you a lot out the the box. Enumerable is a set of behaviors and functionality you'd want to add to a class. The Enumerator class is something you get objects from itself.