slim_scrooge 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -30,7 +30,9 @@ Requirements: Rails 2.2 or above, Ruby 1.8.6 or above. (Ruby 1.9 to be tested)
30
30
  sudo gem install slim_scrooge
31
31
  </pre>
32
32
 
33
- Then add this to your Rails::Initializer section in environment.rb:
33
+ If the C extension does not build (perhaps you are on windows and do not have a C compiler), then do not worry - SlimScrooge has a second way of generating callsite information. Ignore the error and carry on with the next step.
34
+
35
+ Next add this to your Rails::Initializer section in environment.rb:
34
36
 
35
37
  <pre>
36
38
  config.gem 'slim_scrooge'
@@ -70,10 +72,20 @@ In fact for efficiency, column accesses are only recorded when they are for newl
70
72
 
71
73
  h2. Caveats
72
74
 
73
- It is possible to delete an object and then to try to use its attributes to access another object that perhaps must be also deleted (like the option :dependent=>:destroy in Rails associations).
75
+ * It is possible to delete an object and then to try to use its attributes to access another object that perhaps must be also deleted (like the option :dependent=>:destroy in Rails associations).
74
76
 
75
77
  This situation is likely to be found rarely because SlimScrooge particularly checks for columns used by :dependent=>:destroy, but if you are doing something similar manually in your code then you may run into problems. Your attempt to access the key of the dependent object could cause a reload if the column is not already noted by SlimScrooge, and the reload will fail if you have already destroyed the parent object.
76
78
 
79
+ * Some users have complained that running their test suite is slower when SlimScrooge is enabled. This is expected - it's doing a little more work for each query, and most queries in tests are only executed once.
80
+
81
+ If it proves to be a problem, you can try this in environment.rb:
82
+
83
+ <pre>
84
+ config.gem 'slim_scrooge' unless Rails.env.test?
85
+ </pre>
86
+
87
+ I do recommend you test with SlimScrooge enabled at some point though, to make sure that everything is working as expected.
88
+
77
89
  h2. Tests
78
90
 
79
91
  SlimScrooge performs the full activerecord test suite without errors, except for a couple of tests that check the number of queries performed.
@@ -1,5 +1,5 @@
1
1
  ---
2
- :patch: 3
2
+ :patch: 4
3
3
  :major: 1
4
- :minor: 0
5
4
  :build:
5
+ :minor: 0
@@ -9,7 +9,7 @@ module SlimScrooge
9
9
  ScroogeRegexJoin = /(?:LEFT|INNER|OUTER|CROSS)*\s*(?:STRAIGHT_JOIN|JOIN)/i
10
10
 
11
11
  attr_accessor :seen_columns
12
- attr_reader :columns_hash, :primary_key, :model_class
12
+ attr_reader :columns_hash, :primary_key, :model_class_name
13
13
 
14
14
  class << self
15
15
  # Make a callsite if the query is of the right type for us to optimise
@@ -40,19 +40,19 @@ module SlimScrooge
40
40
 
41
41
  def initialize(model_class)
42
42
  @all_columns = SimpleSet.new(model_class.column_names)
43
- @model_class = model_class
43
+ @model_class_name = model_class.to_s
44
44
  @quoted_table_name = model_class.quoted_table_name
45
45
  @primary_key = model_class.primary_key
46
46
  @quoted_primary_key = model_class.connection.quote_column_name(@primary_key)
47
47
  @columns_hash = model_class.columns_hash
48
48
  @select_regexp = self.class.select_regexp(model_class.table_name)
49
- @seen_columns = SimpleSet.new(essential_columns)
49
+ @seen_columns = SimpleSet.new(essential_columns(model_class))
50
50
  end
51
51
 
52
52
  # List of columns that should always be fetched no matter what
53
53
  #
54
- def essential_columns
55
- @model_class.reflect_on_all_associations.inject([@primary_key]) do |arr, assoc|
54
+ def essential_columns(model_class)
55
+ model_class.reflect_on_all_associations.inject([@primary_key]) do |arr, assoc|
56
56
  if assoc.options[:dependent] && assoc.macro == :belongs_to
57
57
  arr << assoc.association_foreign_key
58
58
  end
@@ -81,11 +81,15 @@ module SlimScrooge
81
81
  "SELECT #{cols} FROM #{@quoted_table_name} WHERE #{@quoted_primary_key} IN (#{sql_keys})"
82
82
  end
83
83
 
84
+ def connection
85
+ @model_class_name.constantize.connection
86
+ end
87
+
84
88
  # Change a set of columns into a correctly quoted comma separated list
85
89
  #
86
90
  def scrooge_select_sql(set)
87
91
  set.collect do |name|
88
- "#{@quoted_table_name}.#{@model_class.connection.quote_column_name(name)}"
92
+ "#{@quoted_table_name}.#{connection.quote_column_name(name)}"
89
93
  end.join(ScroogeComma)
90
94
  end
91
95
  end
@@ -26,8 +26,7 @@ module SlimScrooge
26
26
  callsite = Callsites[@callsite_key]
27
27
  rows_hash = rows_by_key(callsite.primary_key)
28
28
  sql = callsite.reload_sql(rows_hash.keys, @fetched_columns)
29
- model_class = callsite.model_class
30
- new_rows = model_class.connection.send(:select, sql, "#{model_class.name} Reload SlimScrooged")
29
+ new_rows = callsite.connection.send(:select, sql, "#{callsite.model_class_name} Reload SlimScrooged")
31
30
  new_rows.each do |row|
32
31
  if old_row = rows_hash[row[callsite.primary_key]]
33
32
  old_row.result_set = nil
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slim_scrooge
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen Sykes
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-26 00:00:00 +02:00
12
+ date: 2009-12-26 00:00:00 +02:00
13
13
  default_executable:
14
14
  dependencies: []
15
15