Sha256: 225f20a0f98547699962ada1ef78057f43567fd28229065a21cf48d2f188886a

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

# Author: Stephen Sykes

module SlimScrooge
  module FindBySql
    def self.included(base)
      ActiveRecord::Base.extend ClassMethods
      class << base
        alias_method_chain :find_by_sql, :slim_scrooge
      end
    end

    module ClassMethods
      def find_by_sql_with_slim_scrooge(sql)
        return find_by_sql_without_slim_scrooge(sql) if sql.is_a?(Array) # don't mess with user's custom query
        callsite_key = SlimScrooge::Callsites.callsite_key(callsite_hash, sql)
        if SlimScrooge::Callsites.has_key?(callsite_key)      # seen before
          if callsite = SlimScrooge::Callsites[callsite_key]  # and is scroogeable
            seen_columns = callsite.seen_columns.dup          # dup so cols aren't changed underneath us
            rows = connection.select_all(callsite.scrooged_sql(seen_columns, sql), "#{name} Load SlimScrooged")
            rows.collect! {|row| MonitoredHash[{}, row, callsite]}
            result_set = SlimScrooge::ResultSet.new(rows.dup, callsite_key, seen_columns)
            rows.collect! do |row|
              row.result_set = result_set
              instantiate(row)
            end
          else
            find_by_sql_without_slim_scrooge(sql)
          end
        elsif callsite = SlimScrooge::Callsites.create(sql, callsite_key, name)  # new site that is scroogeable
          rows = connection.select_all(sql, "#{name} Load SlimScrooged 1st time")
          rows.collect! {|row| instantiate(MonitoredHash[row, {}, callsite])}
        else
          find_by_sql_without_slim_scrooge(sql)
        end
      end
    end
  end
end

ActiveRecord::Base.send(:include, SlimScrooge::FindBySql)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
slim_scrooge-1.0.0 lib/slim_scrooge/slim_scrooge.rb