Skip to content

Commit

Permalink
Do not hold AR class objects in callsites because class reloading in …
Browse files Browse the repository at this point in the history
…dev mode will be hampered (fixes issue 4)
  • Loading branch information
sdsykes committed Dec 26, 2009
1 parent f0b79dd commit 5486662
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
16 changes: 10 additions & 6 deletions lib/slim_scrooge/callsite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Callsite
ScroogeRegexJoin = /(?:LEFT|INNER|OUTER|CROSS)*\s*(?:STRAIGHT_JOIN|JOIN)/i

attr_accessor :seen_columns
attr_reader :columns_hash, :primary_key, :model_class
attr_reader :columns_hash, :primary_key, :model_class_name

class << self
# Make a callsite if the query is of the right type for us to optimise
Expand Down Expand Up @@ -40,19 +40,19 @@ def select_regexp(table_name)

def initialize(model_class)
@all_columns = SimpleSet.new(model_class.column_names)
@model_class = model_class
@model_class_name = model_class.to_s
@quoted_table_name = model_class.quoted_table_name
@primary_key = model_class.primary_key
@quoted_primary_key = model_class.connection.quote_column_name(@primary_key)
@columns_hash = model_class.columns_hash
@select_regexp = self.class.select_regexp(model_class.table_name)
@seen_columns = SimpleSet.new(essential_columns)
@seen_columns = SimpleSet.new(essential_columns(model_class))
end

# List of columns that should always be fetched no matter what
#
def essential_columns
@model_class.reflect_on_all_associations.inject([@primary_key]) do |arr, assoc|
def essential_columns(model_class)
model_class.reflect_on_all_associations.inject([@primary_key]) do |arr, assoc|
if assoc.options[:dependent] && assoc.macro == :belongs_to
arr << assoc.association_foreign_key
end
Expand Down Expand Up @@ -81,11 +81,15 @@ def reload_sql(primary_keys, fetched_columns)
"SELECT #{cols} FROM #{@quoted_table_name} WHERE #{@quoted_primary_key} IN (#{sql_keys})"
end

def connection
@model_class_name.constantize.connection
end

# Change a set of columns into a correctly quoted comma separated list
#
def scrooge_select_sql(set)
set.collect do |name|
"#{@quoted_table_name}.#{@model_class.connection.quote_column_name(name)}"
"#{@quoted_table_name}.#{connection.quote_column_name(name)}"
end.join(ScroogeComma)
end
end
Expand Down
3 changes: 1 addition & 2 deletions lib/slim_scrooge/result_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ def reload!
callsite = Callsites[@callsite_key]
rows_hash = rows_by_key(callsite.primary_key)
sql = callsite.reload_sql(rows_hash.keys, @fetched_columns)
model_class = callsite.model_class
new_rows = model_class.connection.send(:select, sql, "#{model_class.name} Reload SlimScrooged")
new_rows = callsite.connection.send(:select, sql, "#{callsite.model_class_name} Reload SlimScrooged")
new_rows.each do |row|
if old_row = rows_hash[row[callsite.primary_key]]
old_row.result_set = nil
Expand Down
4 changes: 2 additions & 2 deletions slim_scrooge.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

Gem::Specification.new do |s|
s.name = %q{slim_scrooge}
s.version = "1.0.3"
s.version = "1.0.4"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Stephen Sykes"]
s.date = %q{2009-11-26}
s.date = %q{2009-12-26}
s.description = %q{Slim scrooge boosts speed in Rails ActiveRecord Models by only querying the database for what is needed.}
s.email = %q{sdsykes@gmail.com}
s.extensions = ["ext/extconf.rb"]
Expand Down

0 comments on commit 5486662

Please sign in to comment.