Skip to content

Instantly share code, notes, and snippets.

View andresf's full-sized avatar

Andres Freyria andresf

View GitHub Profile
# This is a simple refactoring exercise.
#
# What to do?
#
# 1. Look at the code of the class CorrectAnswerBehavior
# 2. Try to see what it does by running `ruby refactoring_example.rb`
# 3. Record characterisation tests by running `ruby refactoring_example.rb --record`
# 4. Make the code beautiful;)
# 5. You are allowed to modify only the code between markers (REFACTORING START/REFACTORING END).
# 6. Test must pass! You can run them with command `ruby refactoring_example.rb --test`
@andresf
andresf / active_model_errors_extension.rb
Last active April 28, 2017 17:37
Expose more information about validation errors in Rails
module ActiveModel
class Errors
def error_types
@_error_types ||= Hash.new { |hash, key| hash[key] = [] }
end
def add_with_save_names(attribute, message = nil, options = {})
message ||= :invalid
message = message.call if message.is_a?(Proc)
error_types[attribute] << message
@andresf
andresf / gist:3490864
Created August 27, 2012 17:58
Skipping mongoid instantiation
For Mongoid 2.4.x:
collection = Mongoid.master.collections("libraries")
collection.find({ user_id: current_user.id }, fields: { documents: 1 }).entries.to_json
For Mongoid 3.x:
Library.collection.find({ user_id: current_user.id }).select(documents: 1).to_json