Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Arup Rakshit aruprakshit

🏠
Working from home
View GitHub Profile
@aruprakshit
aruprakshit / dream_spec.rb
Last active Aug 29, 2015
Rspec to test my dream
View dream_spec.rb
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
View user.rb
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)
View Interactive Staging
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 Jun 12, 2014
Confusion with Rails belongs_to association
View belongs_to.rb
#------model definition-----
class Book < ActiveRecord::Base
end
class Page < ActiveRecord::Base
belongs_to :book
end
#------migration----------
class CreateBooks < ActiveRecord::Migration
def change
View users_controller.rb
# 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
View pluck.rb
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 Aug 29, 2015
respond_to? vs respond_to_missing?
View respond_to.rb
# 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 Aug 29, 2015
Playing with Enumerable#to_a
View to_a.md

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 :-

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

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.