cached_belongs_to 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # CachedBelongsTo
1
+ # Cached belongs_to
2
2
 
3
3
  [![Build Status](https://secure.travis-ci.org/crowdint/cached_belongs_to.png)](http://travis-ci.org/crowdint/cached_belongs_to)
4
4
 
@@ -27,18 +27,25 @@ Or install it yourself as:
27
27
 
28
28
  ## Usage
29
29
 
30
- Just use it as you would use `belongs_to`. Add the `:caches` option to specify the
30
+ Currently, it works with a traditional `belongs_to` - `has_many` associations
31
+ between two models.
32
+
33
+ Just use it as you would normally use `belongs_to`. Add the `:caches` option to specify the
31
34
  fields in the association that you want to cache.
32
35
 
33
36
  #
34
37
  # table: authors
35
- #
36
- # name:string
38
+ # name:string
37
39
  #
38
40
  class Author
39
41
  has_many :books
40
42
  end
41
43
 
44
+ #
45
+ # table: books
46
+ # author_id:integer
47
+ # author_name:string
48
+ #
42
49
  class Book
43
50
  cached_belongs_to :author, :caches => :name
44
51
  end
@@ -1,3 +1,3 @@
1
1
  module CachedBelongsTo
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -3,35 +3,47 @@ require 'active_record'
3
3
 
4
4
  module CachedBelongsTo
5
5
  module ClassMethods
6
+ #
7
+ # Creates a many to one association between two models. Works
8
+ # exactly as ActiveRecord's belongs_to, except that it adds
9
+ # caching to it.
10
+ #
11
+ # Usage:
12
+ #
13
+ # class Book < ActiveRecord::Base
14
+ # cached_belongs_to :author, :caches => :name
15
+ # end
16
+ #
6
17
  def cached_belongs_to(*args)
7
18
  caches = Array(args[1].delete(:caches))
8
19
  klass = args[0]
9
20
 
10
21
  belongs_to(*args)
11
- children_hook_name = "cached_belongs_to_#{name.underscore}_after_save".to_sym
12
- create_cached_belongs_to_child_hooks(caches, klass, children_hook_name)
13
- create_cached_belongs_to_parent_hooks(caches, klass, children_hook_name)
22
+ children_callback_name = "cached_belongs_to_#{name.underscore}_callback".to_sym
23
+ create_cached_belongs_to_child_callbacks(caches, klass, children_callback_name)
24
+ create_cached_belongs_to_parent_callbacks(caches, klass, children_callback_name)
14
25
  end
15
26
 
16
- def create_cached_belongs_to_child_hooks(*args, caches, klass, children_hook_name)
17
- define_method children_hook_name do
27
+ private
28
+ def create_cached_belongs_to_child_callbacks(caches, klass, children_callback_name)
29
+ define_method children_callback_name do
18
30
  caches.each do |attr|
19
31
  send("#{klass}_#{attr}=", send(klass).send(attr)) if send(klass)
20
32
  end
21
33
  end
22
34
 
23
- before_save children_hook_name
35
+ before_save children_callback_name
24
36
  end
25
37
 
26
- def create_cached_belongs_to_parent_hooks(caches, parent_class_name, children_hook_name)
27
- method_name = "cached_belongs_to_#{parent_class_name}_after_save".to_sym
38
+ def create_cached_belongs_to_parent_callbacks(caches, parent_class_name, children_callback_name)
39
+ method_name = "cached_belongs_to_#{parent_class_name}_callback".to_sym
28
40
  has_many_association = self.name.underscore.pluralize.to_sym
29
41
  parent_class = parent_class_name.to_s.camelize.constantize
30
42
 
31
43
  parent_class.send(:define_method, method_name) do
32
44
  send(has_many_association).reload.each do |child|
33
45
  caches.each do |attr|
34
- child.send(children_hook_name)
46
+ child.send(children_callback_name)
35
47
  child.save
36
48
  end
37
49
  end
@@ -30,16 +30,16 @@ describe CachedBelongsTo do
30
30
  book_class.new.should respond_to(:author)
31
31
  end
32
32
 
33
- it "defines the cached_belongs_to_book_after_save" do
34
- book_class.new.should respond_to(:cached_belongs_to_book_after_save)
33
+ it "defines the cached_belongs_to_book_callback" do
34
+ book_class.new.should respond_to(:cached_belongs_to_book_callback)
35
35
  end
36
36
 
37
- it "defines the cached_belongs_to_author_after_save" do
38
- author_class.new.should respond_to(:cached_belongs_to_author_after_save)
37
+ it "defines the cached_belongs_to_author_callback" do
38
+ author_class.new.should respond_to(:cached_belongs_to_author_callback)
39
39
  end
40
40
  end
41
41
 
42
- context "cached_belongs_to_book_after_save" do
42
+ context "cached_belongs_to_book_callback" do
43
43
  before do
44
44
  book_class.send(:cached_belongs_to, :author, { :caches => :name })
45
45
  end
@@ -51,12 +51,12 @@ describe CachedBelongsTo do
51
51
 
52
52
  book.stub(:author).and_return author
53
53
 
54
- book.cached_belongs_to_book_after_save
54
+ book.cached_belongs_to_book_callback
55
55
  book.author_name.should eq author.name
56
56
  end
57
57
  end
58
58
 
59
- context "cached_belongs_to_author_after_save" do
59
+ context "cached_belongs_to_author_callback" do
60
60
  before do
61
61
  book_class.send(:cached_belongs_to, :author, { :caches => :name })
62
62
  author_class.send(:has_many, :books)
@@ -67,10 +67,10 @@ describe CachedBelongsTo do
67
67
  book = book_class.new
68
68
 
69
69
  author.stub_chain(:books, :reload).and_return([ book ])
70
- book.should_receive(:cached_belongs_to_book_after_save)
70
+ book.should_receive(:cached_belongs_to_book_callback)
71
71
  book.should_receive :save
72
72
 
73
- author.cached_belongs_to_author_after_save
73
+ author.cached_belongs_to_author_callback
74
74
  end
75
75
  end
76
76
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cached_belongs_to
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -165,7 +165,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
165
165
  version: '0'
166
166
  segments:
167
167
  - 0
168
- hash: -552693176683226932
168
+ hash: -1027759858126045779
169
169
  required_rubygems_version: !ruby/object:Gem::Requirement
170
170
  none: false
171
171
  requirements:
@@ -174,7 +174,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
174
174
  version: '0'
175
175
  segments:
176
176
  - 0
177
- hash: -552693176683226932
177
+ hash: -1027759858126045779
178
178
  requirements: []
179
179
  rubyforge_project:
180
180
  rubygems_version: 1.8.23