Avoid png file uploads with RSpec + Paperclip + Imagemagick
File uploads with .png are +10 times slower than using .jpeg or .gif.
# Bad
post :create, {
:property_id => property.id,
:photo => {
:image => File.open("#{Rails.root}/public/images/foo.png")
}
}
# Good
post :create, {
:property_id => property.id,
:photo => {
:image => File.open("#{Rails.root}/public/images/foo.jpeg")
}
}
Related cards:
Profiling Ruby with ruby-prof
require 'ruby-prof'
# you don't need this if you have ruby-prof in your Gemfile
You can set one of the things you want to measure by using (default is PROCESS_TIME
most useful ones are PROCESS_TIME, MEMORY, CPU_TIME):
RubyProf.me...
Keeping your specs DRY
I often see long before
blocks with lots of should_receive ... and_return
inside.
Remember that before
blocks are about setting up the "stage" (the context of your test), not declaring your expectations!
Also, they get run for every spec (e...
Add indexes on foreign keys when you create a migration (with foreign key)
Whenever you make a migration to add a foreign key, you should also add an index for it
def self.up
add_column :comments, :user_id, :integer
add_index :comments, :user_id
end
Cleaner Rspec
When simply checking equality or truthiness then
Instead of:
it "should have role set to admin" do
@user.role.should eql('admin')
end
it "should be valid" do
@user.valid?.should be_true
end
Do:
it { @user.role.shou...
To avoid using bundle exec or creating rvm gemsets
- Add to the end your .bash_profile
export PATH="./vendor/bundle/bin:$PATH"
- Also add
alias bi="bundle install --path vendor/bundle --binstubs=vendor/bundle/bin"
- Then to bundle install next time just use
bi
Now no more ```...
Creating a gem in lib folder
Go to lib folder and use bundler to generate main files for a gem:
$ bundle gem test_gem
create test_gem/Gemfile
create test_gem/Rakefile
create test_gem/LICENSE
create test_gem/README.md
...
Database: Scopes, migrations, and indices
Wether you modify an existing named scope or add a new one, or when you write a new query, make sure you have the proper indices.
This particularly applies if you're going to run non-trivial queries of course (admin backends, analytics, etc).
##...
If you GROUP BY, make sure you ORDER BY NULL
TL;DR: If using :group => :some_field
you might want to :order => 'NULL'
.
According to the man
By default, MySQL sorts all GROUP BY col1, col2, ... queries as if you ...
Rebase your feature branches
Regularly, but at least before merging your feature branches, rebase them to get rid of "fix typo" and "wip" commits.
Getting rid of unnecessary commits
Let's say you do a git rebase -i HEAD~~~~
and have this...
When you set date attributes, you should not pass times
As you know, time zones make stuff a bit more difficult but are necessary.
A time-zoned record is converted to UTC using to_s(:db)
to be st...