Consul 1.3.0 lets you override generated controller methods
When you use the :as
option to
map a power to a controller method
Show archive.org snapshot
you can now override the generated method. The original implementation can be accessed with super
.
This is useful to chain additional conditions to a scope:
class NotesController < ApplicationController
power :notes, as: :note_scope
# ...
private
def note_scope
super.where(trashed: false)
end
end
Related cards:
Consul 0.9 lets you optimize records checks
Consul 0.9 comes with many new features to optimize powers that only check access to a given record. e.g. Power.current.post?(Post.last)
. See below for details.
Powers that only check a given object
-------...
Think twice before using application names in your variables and methods
I recently had fun with replacing a custom video plattform ("mycustomtv") in one of our applications. I learned a lot about naming stuff.
youtube_mycustomtv_chooser
is a bad name for a directive.
Why?
- if you add, for example, other vide...
A simpler default controller implementation
Rails has always included a scaffold
script that generates a default controller implementation for you. Unfortunately that generated controller is unnecessarily verbose.
When we take over Rails projects from other teams, we often find that cont...
Consul 0.3.0 has a shortcut to quickly create an action map for CRUD controllers
In moderately complex authorization scenarios you will often find yourself writing a map like this:
class NotesController < ApplicationController
power :notes, :map => {
[:edit, :update] => :updatable_notes
[:new, :creat...
CSS that lets your alarm bells ring
Harry Roberts, a youngster from the UK, wrote a comprehensive article telling you how to smell CSS rot early.
Examples:
- Undoing styles
- Magic numbers
- Qualified selectors
- Dangerous selectors
- Reactive !important
… and more.
Saving application objects in your session will come back to haunt you
If you save a non-standard object (not a String or Fixnum, etc) like the AwesomeClass
from your application in the session of visitors be prepared that some time you will get this exception:
ActionController::SessionRestoreError: Session c...
Exclude your staging site from Google with robots.txt and not shoot yourself in the foot
If you want to exclude your staging site from Google using robots.txt
without running the risk to forget deleting the file once you go live, name the file robots.exclude.txt
instead.
In your Apache Vhost config, rewrite requests for the stagi...
Rails: Accessing helper methods from a controller
In Rails 5+ you can access a helper from a controller using the helpers
method:
# Inside a controller action
helpers.link_to 'Foo', foo_path
In older Rails versions you can use view_context
instead:
# Inside a controller...
ActiveRecord::Relation#merge overwrites existing conditions on the same column
In Ruby on Rails ActiveRecord::Relation#merge
overwrites existing conditions on the same column. This may cause the relation to select more records than expected:
authorized_users = User.where(id: [1, 2])
filtered_users = User.where(i...
Gem development: When your specs don't see dependencies from your Gemfile
When you develop a gem and you have a Gemfile
in your project directory, you might be surprised that your gem dependencies aren't already required in your specs. Here is some info that should help you out:
- Bundler actually doesn't automatical...