Module: ConstantTableSaver::ActiveRecord5ClassMethods

Defined in:
lib/constant_table_saver.rb

Instance Method Summary collapse

Instance Method Details

#find_by_sql(sql, binds = [], preparable: nil, &block) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/constant_table_saver.rb', line 81

def find_by_sql(sql, binds = [], preparable: nil, &block)
  @find_by_sql ||= {
    :all   => relation.to_sql,
    :id    => relation.where(relation.table[primary_key].eq(Arel::Nodes::BindParam.new)).limit(1).arel,
    :first => relation.order(relation.table[primary_key].asc).limit(1).arel,
    :last  => relation.order(relation.table[primary_key].desc).limit(1).arel,
  }

  @limit_one ||= ActiveRecord::Attribute.with_cast_value("LIMIT", 1, ActiveRecord::Type::Value.new)

  _sql = _to_sql_with_binds(sql, binds)

  if binds.empty?
    if _sql == @find_by_sql[:all]
      return @cached_records ||= super(relation.to_sql).each(&:freeze)
    end

  elsif binds.size == 1 &&
        binds.last == @limit_one
    if _sql == _to_sql_with_binds(@find_by_sql[:first], binds)
      return [relation.to_a.first].compact
    elsif _sql == _to_sql_with_binds(@find_by_sql[:last], binds)
      return [relation.to_a.last].compact
    end

  elsif binds.size == 2 &&
        binds.last == @limit_one &&
        binds.first.is_a?(ActiveRecord::Relation::QueryAttribute) &&
        binds.first.name == primary_key &&
        _sql == _to_sql_with_binds(@find_by_sql[:id], binds) # we have to late-render the find(id) SQL because mysql2 on 4.1 and later requires the bind variables to render the SQL, and errors out with a nil dereference otherwise
    @cached_records_by_id ||= relation.to_a.index_by {|record| record.id.to_param}
    return [@cached_records_by_id[binds.first.value.to_param]].compact
  end

  super(sql, binds, preparable: preparable, &block)
end