neo neo - 3 months ago 18
Ruby Question

ruby call method based on record type

I have the following methods on my vehicle model like so:

class Vehicle < ActiveRecord::Base
def get_history(start_time, end_time)
#...
end

def gsm_get_history(start_time, end_time)
#...
end
end


get_history
is being called all over the codebase, I defined
gsm_get_history
method that's very similar, accepts the same arguments.

how can I keep calling
get_history
without changing every instance of this method call in my code base? and call
gsm_get_history
only if communication channel on the vehicle is
:gsm

Answer

Change get_history to look like this:

def get_history(start_time, end_time)
  if communication_channel == :gsm
    return gsm_get_history(start_time, end_time)
  end

  # ...
end