Skip to content

Instantly share code, notes, and snippets.

@kstevens715
Last active March 20, 2017 18:56
Show Gist options
  • Save kstevens715/61aac85e65a90c330c0ae7b248552f65 to your computer and use it in GitHub Desktop.
Save kstevens715/61aac85e65a90c330c0ae7b248552f65 to your computer and use it in GitHub Desktop.
class ActiveRecordComparer
def initialize(original, new)
@original = original
@new = new
end
def method_missing(m, *arg)
puts arg
@original = @original.send(m, *arg)
@new = @new.send(m, *arg)
if @original.class != @new.class
fail "classes don't match"
end
if @original.class != ActiveRecord::Relation
if @original != @new
fail "results don't match. Original: #{@original.inspect}, New: #{@new.inspect}"
end
return @original
end
self
end
end
# Use:
#
# In Organization user identity class:
# def applicants
# ActiveRecordComparer(self.old_applicants_method, self.new_applicants_method)
# end
#
# In client code:
# applicants.application_status_of("In Progress")
#
# I think it would raise a failure because they don't match. Which would make regression testing more likely to catch problems.
@therealjasonkenney
Copy link

therealjasonkenney commented Mar 20, 2017

def method_missing(arg)
   @original = @original.send(arg)
   @new = @new.send(arg)
   if [@original, @new].all? { |k| k.kind_of?(ActiveRecord::Relation) }
    new(@original, @new)
  elsif @original == @new
    @new
  else
    raise 'The results do not match'
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment