Ruby where refactor

Viewed 46

I'm refactoring a method and I'm having trouble in refactoring the following code to a simple .where

def check_hp_repayments
  include = [:finance_detail]
  @finances = Finance.legacy_find(:all,
      :include => include,
      :conditions => ["finance_details.next_repayment_date < ? and finance_details.loan_term > 0", Time.now]
    )
end

legacy_find is a piece of custom code we have in the app, why I don't know so I'm trying to replace these with actual .where's and .find's

EDIT:

Here is the legacy_find method

def self.legacy_find(type, args=nil)
  # need to add capability to handle array of id's
  if type.kind_in?([Numeric, String, Array]) && !args
    result =  self.find(type)
  elsif !args
    result =  self.send(type.to_s)
  else
    result = self

    if type.kind_of?(Array)
      if !args[:conditions]
         args[:conditions] = ["1 = 1"]
      elsif args[:conditions].kind_of?(String)
        args[:conditions] = [args[:conditions]]
      end

      args[:conditions][0] = '(' + args[:conditions][0] + ')'

      args[:conditions][0] += " AND `" + self.table_name + "`.`id` in(" + type.join(',') + ")"
    elsif type && !type.kind_of?(Symbol)
      if !args[:conditions]
         args[:conditions] = ["1 = 1"]
      elsif args[:conditions].kind_of?(String)
        args[:conditions] = [args[:conditions]]
      end

      args[:conditions][0] = '(' + args[:conditions][0] + ')'

      args[:conditions][0] += " AND `" + self.table_name + "`.`id` = ?"
      args[:conditions].push(type)
    end

    result      = self.legacy_conditions(args)

    if type && ((type.kind_of?(String) && type.to_i.to_s == type) || type.kind_of?(Numeric))
      result = result.first
    elsif type && !type.kind_of?(Symbol)
      result = result.to_a
    else
      result =  type ? result.send((type == :all ? 'to_a' : type).to_s) : result
    end
  end

  new_result = result
  return new_result
end

Here is the legacy_conditions

def self.legacy_conditions(args)
          return self if !args

            args[:conditions] = [] if args[:conditions] && args[:conditions][0].kind_of?(String) && args[:conditions][0].size == 0

            result = self
            result = result.where(args[:conditions]) if (args.has_key?(:conditions) && args[:conditions] && args[:conditions].size > 0)
            result = result.select(args[:select]) if args.has_key?(:select) && args[:select]
            result = result.includes(args[:include]) if args.has_key?(:include) && args[:include]
            result = result.includes(args[:include_without_references]) if args.has_key?(:include_without_references) && args[:include_without_references]
            result = result.references(args[:include]) if args.has_key?(:include) && args[:include]
            result = result.joins(args[:joins]) if args.has_key?(:joins) && args[:joins]
            result = result.order(args[:order]) if args.has_key?(:order) && args[:order]
            result = result.group(args[:group]) if args.has_key?(:group) && args[:group]
            result = result.limit(args[:limit]) if args.has_key?(:limit) && args[:limit]
            result = result.offset(args[:offset]) if args.has_key?(:offset) && args[:offset]
            result = result.from(args[:from]) if args.has_key?(:from) && args[:from]
            result = result.lock(args[:lock]) if args.has_key?(:lock) && args[:lock]
            result = result.readonly(args[:readonly]) if args.has_key?(:readonly) && args[:readonly]
            result
        end

        def human_attribute_name(attr, opts = {})
          self.class.human_attribute_name(attr, opts)
        end
    end
end

Rails version: 6.1.3.2

0 Answers
Related