Created
September 5, 2015 17:01
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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