Check if an object is an ActiveRecord scope
Don't say is_a?(ActiveRecord::NamedScope::Scope)
because that is no longer true in Rails 3 and also doesn't match unscoped ActiveRecord classes themselves (which we consider scopes for all practical purposes).
A good way is to say this instead:
object.respond_to?(:scoped)
Boolean fields in migrations
If you want to update some records with boolean fields in a migration, always remember to set your values with field=#{quoted_true}
and field=#{quoted_false}
. The Rails methods quoted_false
and quoted_true
return the correct boolean representations for your database.
Cucumber does not find neither env.rb nor step definitions when running features in nested directories
Usually, Cucumber feature files live in features/
. When you group them in sub directories, make sure to add -r features
to the standard Cucumber options.
In Rails apps, Cucumber options are likely to be stored in config/cucumber.yml
.
Why your all.js is empty on staging or production
When you include a non-existing Javascript file, you probably won't notice it during development. But with caching active (on production or staging) Rails will write an empty all.js
file without complaining.
Pattern: Disabling a certain feature in tests
There is a kind of features in web applications that hinder automated integration tests. Examples include cookie consent banners or form captchas. Clearly, these should be disabled so you do not have to explicitly deal with them in each and every test (like, every test starting with accepting the cookies notice). On the other hand, they must be tested as well.
A good feature disabling solution should therefore meet these requirements:
-
The feature is generally disabled in tests. A test does not need to do anything manually.
-
It is *...
Bootswatch: Paper
Free Bootstrap theme resembling Material Design.
Bootswatch offers Sass and Less files, so the theme can easily be integrated into your usual Rails application.
Implements only Bootstrap features which means that some Material stuff is missing, but also that you can easily use or replace the theme.
Does not come with extra JavaScript; some effects like button click ripples are implemented via CSS.
Also check out their other themes which can be used in a similar fashion.
travisliu/traim: Resource-oriented microframework for RESTful APIs
Use Traim to build a RESTful API for your ActiveRecord models with very little code.
Traim assumes your API resources will map 1:1 to your ActiveRecord models and database tables. This assumption usually falls apart after a few months into a project, so be ready to replace your Traim API with something more expressive afterwards.
Traim outputs a Rack application which you can either serve standalone or mount into your Rails app.
RSpec 2.6 supports "any_instance" now
This finally works:
User.any_instance.should_receive(...)
as does
User.any_instance.stub(...)
Note: You won't have RSpec 2.6 if you're still working on Rails 2.
Sending raw JSON data to a member action in a controller spec
This is what worked for me in a Rails 4:
# JSON data as first argument, then parameters
patch :update, { some: 'data' }.to_json, id: id, format: :json
Don't use Ruby 1.9.2
Ruby 1.9.2 is very slow when loading files, especially starting Rails servers or running specs takes forever.
Do yourself a favor and upgrade to 1.9.3.
Ruby: Find the most common string from an array
This will give you the string that appears most often in an array:
names = %w[ foo foo bar bar bar baz ]
names.group_by(&:to_s).values.max_by(&:size).try(:first)
=> "bar"
This is very similar to the linked StackOverflow thread, but does not break on empty arrays.
Note that try
is provided by ActiveSupport (Rails). You could explicitly load activesupport
or use andand
on plain Ruby.
How to stub class constants in RSpec
Hint: There's another card with this helper for Cucumber features.
Sometimes you feel like you need to stub some CONSTANT you have defined in an other class. Since actually constants are called constants because they're constant, there's no way to easily stub a constant.
Here are three solutions for you.
Easiest solution
Rethink! Do you really need CONSTANT = %w[foo bar]
to be constant? In many cases, setting it as a...
Run multiple local webricks at the same time using different ports
If you specify different ports, you can run multiple local webricks with rails server --port=300X
at the same time.
We are no longer maintaining Dusen
If you were using Dusen for its query parsing and LIKE queries, we recommend to migrate to Minidusen, which extracts those parts from Dusen. Minidusen is compatible with MySQL, PostgreSQL and Rails 3.2, 4.2 and 5.0.
If you are looking for a full text indexing solution, we recommend to use PostgreSQL with pg_search.
Always, always declare your associations with symbols
Never ever declare your associations with a string, especially when doing metaprogramming. A common mistake is something like
# WRONG
class Page < ActiveRecord::Base
%w[main sub].each do |type|
belongs_to "#{type}_title"
end
end
# RIGHT
class Page < ActiveRecord::Base
%w[main sub].each do |type|
belongs_to :"#{type}_title"
end
end
Always convert to a symbol, otherwise you'll have all [kinds](/m...
Git: Issues with Gemfile.lock
When there's a Gemfile.lock
in your working directory that you cannot remove by either checkout
, reset [--hard]
, stash
, probably Rails' Spring is the culprit and not Bundler itself.
Fix
spring stop
The author of the linked Stackoverflow post supposes Spring re-writes the Gemfile.lock
on change to ensure all Spring processes are using the same gem versions. Meh.
danialfarid/angular-file-upload
Lightweight Angular JS directive to upload files
Includes polyfills for old IEs. Unfortunately, their auto-loading mechanism may not work properly on your Rails application due to the asset pipeline. They use FileAPI, so you could just include it manually for old browsers, or when you want to use FileAPI's toolkit anyway.
How much should I refactor?
The Rails community has been abuzz with object-oriented programming, SOLID principles, laws, design patterns, and other principles, practices, and patterns. We’ve (re)discovered new tools and techniques to separate and reuse logic, making code easier to test, understand, and maintain. Now that we’ve learned about all these new tools, when do we use them?
Incredibly fast web apps // Speaker Deck
Presentation about optimizing Ruby on Rails apps.
From Nico Hagenburger (homify's lead frontend developer).
Databound
Databound provides Javascript a simple API to the Ruby on Rails CRUD.
Tries to expose a full model CRUD as an API. Not sure if this is useful for more refined requirements.
ActiveRecord's where.not
Rails 4.0 introduced a helpful new method for ActiveRecord queries: where.not
. It can make clunky queries easier to read.