Skip to content

Instantly share code, notes, and snippets.

@Olgagr
Olgagr / bash_profile
Created November 2, 2013 07:01
Terminal: Create mate command to open Textmate
export EDITOR="mate -w"
@Olgagr
Olgagr / companies_controller_spec.rb
Created September 6, 2013 08:45
send nested attributes in controller tests
recruiters_attributes: {'1234567' => attributes_for(:recruiter)})
@Olgagr
Olgagr / redirect_with_params
Created September 6, 2013 07:52
Redirect to eg. edit action with additional parameters
redirect_to action: :edit, id: @company.id, params: {tab: 'locations' }
@Olgagr
Olgagr / get_content_of_email.rb
Created August 22, 2013 13:13
Get content of the mail
UserMailer.job_apply_reject(job_apply).deliver
expect(ActionMailer::Base.deliveries.last.encoded).to include 'Company Lorem ipsum'
@Olgagr
Olgagr / match_but_dont_capture
Created July 14, 2013 16:01
Construct to match something but don't count it as a capture
str = 'abc def ghi'
m = /(abc) (?:def) (ghi)/.match str
# m[0] -> abc
# m[2] -> ghi
@Olgagr
Olgagr / lookahead_lookbehind_assertions
Created July 14, 2013 15:57
Lookahead and Lookbehind Assertions
# Lookahead assertion
str = "123 456. 789"
m = /\d+(?=\.)/.match(str) # m[0] = '456'. Period doesn't count as a part of the match
/\d+(?!\.)/ # negative lookahead
# Lookbehind assertion
str = "David BLACK"
m = /(?<=David )BLACK/ # only match BLACK if it's preceded by 'David'
/(?<!David) BLACK/ # negative lookbehind
@Olgagr
Olgagr / launchy_open_page.rb
Created June 23, 2013 14:35
Using launchy gem in integration tests
# integration tests...some code
save_and_open_page
# integration tests...some code
@Olgagr
Olgagr / set_partial_path_in_class.rb
Created June 15, 2013 16:43
Set partial path in the class which does not inherit fro active model
class Timeline
extend ActiveModel::Naming # it's better approach
def initialize(user)
@user = user
end
def to_partial_path
'path/to/partial' # but there is a better way(see above)
@Olgagr
Olgagr / controller_private_setter_for_local_variable.rb
Created June 15, 2013 15:48
Controller private setter for local variable
class FollowingRelationshipsController < ApplicationController
def create
current_user.follow user
redirect_to user
end
def destroy
current_user.unfollow user
redirect_to user
@Olgagr
Olgagr / short_syntax_factory_girl.rb
Created June 2, 2013 07:09
shorter syntax for factory_girl
# in spec_helper
RSpec.configure do |config| do
#other code
config.include FactoryGirl::Syntax::Methods
end