class NonProductionMailInterceptor | |
def self.delivering_email(message) | |
message.subject = "#{message.to}: #{message.subject}" | |
message.to = '[email protected], [email protected]' | |
message.cc = nil | |
message.bcc = nil | |
end | |
end | |
unless Rails.env.production? |
Intention revealing method is simple and yet I see it frequently slip through programmers' code. Developers don't like lengthy methods, or find it inconvenient to read through chubby if-else branches, and if they are nice enough they'll leave comments like those.
If we change spaces by underscores in the comments, delete the comment characters, and define the resulting methods in the same file (as private helpers for example), we get code that explains itself, instead of through verbose long methods, or human code comments which get stale.
Intention revealing methods is the most basic, no brain-teaser, easiest rule that I know. Combine it with Sandi Metz's rule of a maximum of 5 lines per method and you'll get simple code that explains itself, that is a pleasure to read, improving communication and productivity of the team (even when it's only yourself).
# lib/sortable_controller_methods.rb (must then load lib files manually) | |
# Add in "sortable" controllers: | |
# include SortableControllerMethods | |
# | |
# As in http://railscasts.com/episodes/147-sortable-lists | |
module SortableControllerMethods | |
def sort | |
klass.all.each do |object| | |
object.position = params[object.class.to_s.downcase].index(object.id.to_s) + 1 | |
object.save |