Skip to content

Instantly share code, notes, and snippets.

@maxjustus
Created September 5, 2015 17:01
Show Gist options
  • Save maxjustus/27e145a745555a6dfd5a to your computer and use it in GitHub Desktop.
Save maxjustus/27e145a745555a6dfd5a to your computer and use it in GitHub Desktop.
hack to allow rspec fire to play nice with active record model column methods
# Rspec fire can only mock methods that are defined on a class.
# The way active record defines accessors is incompatible with rspec-fire's
# implementation so this works around that by defining an accessor for every
# attribute as soon as each model class is loaded for the first time
#
# TODO: See if this is needed with Rails 4
class ActiveRecord::Base
class << self
alias :__ar_original_inherited__ :inherited
def inherited(subclass)
__ar_original_inherited__(subclass)
subclass.define_column_methods_explicitly
end
def define_column_methods_explicitly
return if !table_exists? || table_name.blank?
column_names.each do |name|
unless instance_methods.include?(name.to_sym)
define_method(name) do
super()
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment