Skip to content

Instantly share code, notes, and snippets.

View andreaseriksson's full-sized avatar

Andreas Eriksson andreaseriksson

View GitHub Profile
#INNER JOIN
#An 'inner join' is the most common join operation used in applications
#and can be regarded as the default join-type. Inner join creates a new result table
#by combining column values of two tables (A and B) based upon the join-predicate.
#The query compares each row of A with each row of B to find all pairs of rows which satisfy the join-predicate.
SELECT * FROM employee
INNER JOIN department
ON employee.DepartmentID = department.DepartmentID;
#LEFT JOIN
@andreaseriksson
andreaseriksson / gist:5655982
Created May 27, 2013 09:04
RAILS Counter Cache
#Migration
add_column :projects, :tasks_count, :integer, :default => 0
Project.reset_column_information
Project.find(:all).each do |p|
Project.update_counters p.id, :tasks_count => p.tasks.length
end
#Model
belongs_to :project, :counter_cache => true
@andreaseriksson
andreaseriksson / gist:5655873
Created May 27, 2013 08:36
BOOTSTRAP Media querys
/* Large desktop */
@media (min-width: 1200px) {
}
/* Portrait tablet to landscape and desktop */
@media (min-width: 768px) and (max-width: 979px) {
}
@andreaseriksson
andreaseriksson / gist:5648267
Last active December 17, 2015 17:49
RAILS Activerecord querys
#Querys
Foo.order("published_at desc").limit(10)
Foo.where("published_at <= ?", Time.now).includes(:comments)
Foo.order("published_at").last
#Group by
Foo.order("DATE(start_at)").group("DATE(start_at)").count
Team.where(name: 'Justice League').first_or_create!
@andreaseriksson
andreaseriksson / gist:5620840
Created May 21, 2013 15:45
RSPEC Capybara Matchers
#Navigating
visit('/projects')
visit(post_comments_path(post))
#Clicking links and buttons
click_link('id-of-link')
click_link('Link Text')
click_button('Save')
@andreaseriksson
andreaseriksson / gist:5615035
Last active December 17, 2015 13:10
RUBY Array functions
empty? #True if the array is empty
first #The first value in the array
include? #True if the array includes the given value
index #The index number of a given value
inspect #The array in a printable format
last #The last value in the array
length #The number of items in the array
reverse #The array in reverse order
sort #The array sorted
uniq #The unique values in the array
@andreaseriksson
andreaseriksson / gist:5604439
Created May 18, 2013 13:44
RSPEC Controller spec #Testing non-HTML controller output
# link_to 'Export', messages_path(format: :csv)
describe 'CSVoutput' do
it "returns a CSV file" do
get :index, format: :csv
expect(response.headers['Content-Type']).to have_content 'text/csv'
end
it 'returns content' do
create(:message,
name: 'Aaron',
@andreaseriksson
andreaseriksson / gist:5604436
Created May 18, 2013 13:43
RSPEC Controller spec #NESTED ROUTE
describe 'GET#show' do
it "renders the :show template for the appointment" do
contact = create(:contact)
appointment = create(:appointment, contact: contact)
get :show, id: appointment, contact_id: contact.id
expect(response).to render_template :show
end
end
@andreaseriksson
andreaseriksson / gist:5604434
Created May 18, 2013 13:42
RSPEC Controller spec #CUSTOM ACTION
describe "PUT flag_as_inappropriate" do
before :each do
@message = create(:message)
end
it "marks the message as inappropriate" do
put :flag_as_inappropriate, id: @message
@message.reload.is_inappropriate?.should be_true
expect(@message.reload.is_inappropriate?).to be_true
end
@andreaseriksson
andreaseriksson / gist:5604430
Created May 18, 2013 13:41
RSPEC Controller spec #DESTROY
describe 'DELETEdestroy' do
before :each do
@message = create(:message)
end
it "deletes the message" do
expect{
delete :destroy, id: @message
}.to change(Message,:count).by(-1)
end