Skip to content

Instantly share code, notes, and snippets.

@tute
tute / sortable_controller_methods.rb
Created June 17, 2011 03:31
Rails SortableMethods
# 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
@tute
tute / intention-revealing-message.md
Last active August 31, 2021 14:54
Notes/Patterns for Refactoring Code

Intention Revealing Methods

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).

Sample Code

@tute
tute / email_interceptor.rb
Last active August 29, 2015 14:02
config/initializers/email_interceptor.rb
class NonProductionMailInterceptor
def self.delivering_email(message)
message.subject = "#{message.to}: #{message.subject}"
message.to = 'ourselves+test@gmail.com, ourselves+test@gmail.com'
message.cc = nil
message.bcc = nil
end
end
unless Rails.env.production?