How to split config/routes.rb in Rails 4
A word of caution
There should rarely be a reason for you to split up config/routes.rb
. If you need to, probably your whole application should be split up.
Split it anyway
Rails::Engine
looks at config.paths['config/routes.rb']
and registers its value with app.routes_reloader
. This means you could put routing files anywhere and then require them. However, I recommend to put any routing files into config/routes/
:
# config/routes/example.rb
Rails.application.routes.draw do
resources :example
end
After creating y...
marco-polo improves your Rails console prompt
MarcoPolo shows your app name and environment in your console prompt so you don't accidentally break production
Officially supporting IRB (standard rails console) and pry (via pry-rails gem).
Example:
$ rails console
Loading development environment (Rails 4.2.1)
agencyapp(dev)>
Get the root class of an ActiveRecord STI hierarchy
Use base_class
.
This traverses up the hierarchy until it encounters either
- a class inheriting from
ActiveRecord::Base
or - a class inheriting from an abstract class (inheriting from
ActiveRecord::Base
)
How to enable SSL in development with Passenger standalone
Here is how to start your Rails application to accept both HTTP and HTTPS in development.
-
gem install passenger
-
Create a self-signed SSL certificate. Store the generated files in config/passenger-standalone-ssl.
-
Create a Passengerfile.json at the project root with this content (or save the attached file):
{ "ssl": true, "ssl_port": 3001, "ssl_certificate": "config/passenger-standalone-ssl/server.crt",
...
Getting Sidekiq error "delay is defined by Active Record"
Reason: You very likely have a model that has a delay
attribute.
You can configure Sidekiq to remove its delay
method by adding this to your Sidekiq initializer:
Sidekiq.remove_delay!
If you need to keep Sidekiqs delay
features, add Sidekiq.hook_rails!
before the option above. The sidekiq methods will be prefixed with sidekiq_
then.
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.
List of Helpful RubyMine Shortcuts
Navigation
CTRL + SHIFT + ALT + N
: Search for any symbol in your application, like CSS classes, Ruby classes, methods, helpers etc.
CTRL + SHIFT + N
: Search for filename in your application (also dependencies)
CTRL + E
: Open a list of recently opened files
ALT + POS1
: Open a the navigation bar as a context menu. Allows you to quickly navigate between files.
CTRL + G
: Go to line
Actions
CTRL + SHIFT + A
: Find ...
PostgreSQL: Be careful when creating records with specific ids
In tests, it is sometimes useful to create records with specific ids. On PostgreSQL this can cause problems:
Usually, PostgreSQL uses an "autoincrement" sequences to provide sequential ids for new database rows. However, these sequences will not increment if you insert a record "by hand". This will cause an error:
record = Record.create!
record.id # => 100, next automatic id will be 101
Record.create!(id: record.id + 1) # okay, but next automatic id will still be 101
Record.create! ...
Non-blocking Asynchronous JSON.parse Using The Fetch API
Interesting hack to move expensive JSON.parse
calls out of the main thread.
Openstack: nova resize
To change RAM size, VDISK size or VCPU count of an openstack instance you have to use nova resize
. You can't change for e.g. just the RAM size with a parameter, you have to assign a new/other flavor. If there is no suitable flavor for the new properties of the VM, create a new one.
nova resize [--poll] <server> <flavor>
The resize could take a while, after it is finished, the VM boots up with the new specifications. SSH into the VM and check if everything is alright...
Jasmine: Testing complex types for equality
Jasmine comes with two matchers that test for equality. The first is toBe
:
expect(first).toBe(second)
toBe
passes when first === second
. Unfortunately this is useless for non-primitive values because JavaScript is a horrible language.
However, Jasmine comes with another matcher toEqual
:
expect(first).toEqual(second)
This matcher behaves as a human would expect for types like the following:
- Arrays
- Objects
- Nested array/object constructs
- Regular expressions...
Mocking time in Jasmine specs
The easiest way to freeze or travel through time in a Jasmine spec is to use the built-in jasmine.clock()
.
- After
jasmine.clock().install()
you can use it to controlsetTimeout
andsetInterval
. - Using
jasmine.clock().mockDate()
you can mocknew Date()
(which returns the current time in Javascript)
While you can use SinonJS Fake timers, using the built-in Jasmine clock will save you an extra dependency.
Pierce through Javascript closures and access private symbols
If you are writing any amount of Javascript, you are probably using closures to hide local state, e.g. to have private methods.
In tests you may find it necessary to inspect a variable that is hidden behind a closure, or to mock a private method using Jasmine spies.
You can use the attached Knife
helper to punch a hole into your closure, through which you can read, write or mock local symbols:
klass = (->
privateVariable = 0
privateMethod = ->
...
Detecting N+1 queries with Bullet
The Bullet gem is designed to help you increase your application's
performance by reducing the number of queries it makes. It will watch
your queries while you develop your application and notify you when
you should add eager loading (N+1 queries), when you're using eager
loading that isn't necessary and when you should use counter cache.
Rails 4.2 Foreign Key Support
The migration DSL now supports adding and removing foreign keys. They are dumped to schema.rb as well. At this time, only the mysql, mysql2 and postgresql adapters support foreign keys. @rubyonrails
Workings
add_foreign_key(:comments, :users)
adds a database constraint
ALTER TABLE "comments" ADD CONSTRAINT comments_user_id_fk FOREIGN KEY ("user_id") REFERENCES "user" ("id")
Disadvantage
Foreign key constraints double the validatio...
Spreewald 1.2.11 fixes frequent "no alert open" errors on Chrome
When running capybara with Chrome you might start seeing frequent "no alert open" errors when trying to interact with browser dialogs. This seems to be triggered by a recent Chrome update.
Apparently these dialogs no longer appear instantly. Spreewald 1.2.11 fixes the "When I confirm the browser dialog" and similar steps by waiting for the dialog to appear.
How to fix a corrupt (zsh) history file
When you the following error:
zsh: corrupt history file /home/marc/.zsh_history
You can try and fix your history file by extracting only the valid strings:
cd ~
mv .zsh_history .zsh_history_bad
strings .zsh_history_bad > .zsh_history
fc -R .zsh_history # reloads the history
Capistrano 3 has slightly changed its symlink implementation
In Capistrano 2, directories in shared_children
used to be symlinked to the shared
directory during the finalize_update
task.
# <capistrano>/lib/capistrano/recipes/deploy.rb
_cset :shared_children, %w(public/system log tmp/pids)
# ...
task :finalize_update, :except => { :no_release => true } do
# ...
shared_children.map do |d|
run "ln -s #{shared_path}/#{d.split('/').last} #{latest_release}/#{d}" # <-- symlinks only the last s...
JavaScript events: target vs currentTarget
tl;dr: Use event.currentTarget
unless you are absolutely certain that you need event.target
.
Since it hasn't been written down in this deck before, here it goes:
When working with JavaScript Event
objects, the DOM element that triggered the event is attached to them. [1]
However, there are 2 "opinions" on which element that would be:
- The element that the user interacted with (
event.target
), - or the element that the event listener is bound to (
event.currentTarget
).
Note that both can be, but not...
A commitment is not a guarantee - Pivotal Tracker
The team is responsible for building great software—that’s it. It’s the only thing the team is responsible for and it’s the only thing that they actually control. In other words, the development team is committed to quality software.
How to capture changes in after_commit
Your after_commit
callbacks will not know about changes, as Rails discards them when committing.
The linked article shows a clever trick to work around that: It uses an after_save
method that looks at changes
and writes its decision to an instance variable. That instance variable can then be used in the after_commit
method.
Note that while callbacks like after_save
are not affected, there are valid reasons for using only after_commit
, and not after_save
. Enqueueing a Sidekiq job is just one of them.
Rails 5+
You can use ...
How to fix: iPad does not trigger click event on some elements
iPads will not trigger click events for all elements. You can fix that, but you don't want to know why.
Example: Capturing clicks on table rows (use case would be clicking the 1st link inside it for better UI). Consider this setup:
<table>
<tr>
<td>hello</td>
</tr>
</table>
^
$(document).on('click', 'tr', function () {
alert('row clicked')
});
While this works on a desktop browser, clicking the row/cell on an iPad will just do nothing.
Turns out, the iPad will only trigger such events for ...
24 little known CSS facts
This blew my mind today:
Please make sure to check browser support for CSS features on Can I Use and the Mozilla Development Network before using.
Favorites
-
outline-offset
: specify how far away from the element an outline is rendered -
::first-letter
: matches the first letter insid...