kasket 0.5.2 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.2
1
+ 0.5.3
@@ -2,11 +2,14 @@ require 'rubygems'
2
2
  require 'active_record'
3
3
  require 'active_support'
4
4
 
5
- require 'kasket/configuration_mixin'
6
- require 'kasket/reload_association_mixin'
7
- require 'kasket/cache'
5
+ require 'kasket/active_record_patches'
8
6
 
9
7
  module Kasket
8
+ autoload :Cache, 'kasket/cache'
9
+ autoload :ConfigurationMixin, 'kasket/configuration_mixin'
10
+ autoload :ReloadAssociationMixin, 'kasket/reload_association_mixin'
11
+ autoload :RackMiddleware, 'kasket/rack_middleware'
12
+
10
13
  CONFIGURATION = {:max_collection_size => 100}
11
14
 
12
15
  module_function
@@ -23,13 +26,20 @@ module Kasket
23
26
  ActiveRecord::Associations::BelongsToPolymorphicAssociation.send(:include, Kasket::ReloadAssociationMixin)
24
27
  ActiveRecord::Associations::HasOneThroughAssociation.send(:include, Kasket::ReloadAssociationMixin)
25
28
 
26
- #sets up local cache clearing after each request
29
+ #sets up local cache clearing on rack
30
+ begin
31
+ ActionController::Dispatcher.middleware.use(Kasket::RackMiddleware)
32
+ rescue NameError => e
33
+ puts('WARNING: The kasket rack middleware is not in your rack stack')
34
+ end
35
+
36
+ #sets up local cache clearing before each request.
37
+ #this is done to make it work for non rack rails and for functional tests
27
38
  begin
28
- ApplicationController.after_filter do
39
+ ApplicationController.before_filter do
29
40
  Kasket.cache.clear_local
30
41
  end
31
42
  rescue NameError => e
32
-
33
43
  end
34
44
 
35
45
  #sets up local cache clearing after each test case
@@ -0,0 +1,56 @@
1
+ module Kasket
2
+ module FixForAssociationAccessorMethods
3
+ def association_accessor_methods(reflection, association_proxy_class)
4
+ define_method(reflection.name) do |*params|
5
+ force_reload = params.first unless params.empty?
6
+ association = association_instance_get(reflection.name)
7
+
8
+ if association.nil? || force_reload
9
+ association = association_proxy_class.new(self, reflection)
10
+ retval = force_reload ? association.reload : association.__send__(:load_target)
11
+ if retval.nil? and association_proxy_class == ActiveRecord::Associations::BelongsToAssociation
12
+ association_instance_set(reflection.name, nil)
13
+ return nil
14
+ end
15
+ association_instance_set(reflection.name, association)
16
+ end
17
+
18
+ association.target.nil? ? nil : association
19
+ end
20
+
21
+ define_method("loaded_#{reflection.name}?") do
22
+ association = association_instance_get(reflection.name)
23
+ association && association.loaded?
24
+ end
25
+
26
+ define_method("#{reflection.name}=") do |new_value|
27
+ association = association_instance_get(reflection.name)
28
+
29
+ if association.nil? || association.target != new_value
30
+ association = association_proxy_class.new(self, reflection)
31
+ end
32
+
33
+ if association_proxy_class == ActiveRecord::Associations::HasOneThroughAssociation
34
+ association.create_through_record(new_value)
35
+ if new_record?
36
+ association_instance_set(reflection.name, new_value.nil? ? nil : association)
37
+ else
38
+ self.send(reflection.name, new_value)
39
+ end
40
+ else
41
+ association.replace(new_value)
42
+ association_instance_set(reflection.name, new_value.nil? ? nil : association)
43
+ end
44
+ end
45
+
46
+ define_method("set_#{reflection.name}_target") do |target|
47
+ return if target.nil? and association_proxy_class == ActiveRecord::Associations::BelongsToAssociation
48
+ association = association_proxy_class.new(self, reflection)
49
+ association.target = target
50
+ association_instance_set(reflection.name, association)
51
+ end
52
+ end
53
+ end
54
+ end
55
+
56
+ ActiveRecord::Base.extend Kasket::FixForAssociationAccessorMethods
@@ -0,0 +1,13 @@
1
+ module Kasket
2
+ class RackMiddleware
3
+ def initialize(app)
4
+ @app = app
5
+ end
6
+
7
+ def call(env)
8
+ @app.call(env)
9
+ ensure
10
+ Kasket.cache.clear_local
11
+ end
12
+ end
13
+ end
@@ -3,12 +3,11 @@ module Kasket
3
3
  def reload_with_kasket_clearing(*args)
4
4
  if loaded?
5
5
  clear_local_kasket_indices if respond_to?(:clear_local_kasket_indices)
6
+ else
7
+ target_class = proxy_reflection.options[:polymorphic] ? association_class : proxy_reflection.klass
8
+ Kasket.cache.delete_matched_local(/^#{target_class.kasket_key_prefix}/) if target_class.respond_to?(:kasket_key_prefix)
6
9
  end
7
10
 
8
- # or maybe something like this?
9
- #target_class = proxy_reflection.options[:polymorphic] ? association_class : proxy_reflection.klass
10
- #Kasket.cache.delete_matched_local(/^#{target_class.kasket_key_prefix}/) if target_class.respond_to?(:kasket_key_prefix)
11
-
12
11
  reload_without_kasket_clearing(*args)
13
12
  end
14
13
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kasket
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mick Staugaard
@@ -49,10 +49,12 @@ files:
49
49
  - Rakefile
50
50
  - VERSION
51
51
  - lib/kasket.rb
52
+ - lib/kasket/active_record_patches.rb
52
53
  - lib/kasket/cache.rb
53
54
  - lib/kasket/conditions_parser.rb
54
55
  - lib/kasket/configuration_mixin.rb
55
56
  - lib/kasket/dirty_mixin.rb
57
+ - lib/kasket/rack_middleware.rb
56
58
  - lib/kasket/read_mixin.rb
57
59
  - lib/kasket/reload_association_mixin.rb
58
60
  - lib/kasket/write_mixin.rb