acts_as_king 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,6 +13,11 @@ module ActsAsKing
13
13
  # association (parent) and has_many association (children). It also defines
14
14
  # a <tt>kings</tt> scope for your model to return all top level records.
15
15
  #
16
+ # === Options
17
+ #
18
+ # * <tt>:foreign_key</tt> - The foreign key used for the association. Normally this will be 'parent_id'. Default is 'parent_id'.
19
+ # * <tt>:counter_cache</tt> - Defines a counter_cache for the belongs_to association that is created. Set true for the default column name (ex: Comment model would be 'comments_count'). Otherwise specify the column name you wish to use. Default is false (no counter cache).
20
+ #
16
21
  # === Example
17
22
  #
18
23
  # class Comment < ActiveRecord::Base
@@ -33,16 +38,13 @@ module ActsAsKing
33
38
  #
34
39
  #
35
40
  def acts_as_king(options = {})
36
- options = { :foreign_key => 'parent_id' }.merge(options)
41
+ options = { :foreign_key => 'parent_id', :counter_cache => false }.merge(options)
37
42
 
38
- belongs_to :parent, :class_name => name, :foreign_key => options[:foreign_key]
43
+ belongs_to :parent, :class_name => name, :foreign_key => options[:foreign_key], :counter_cache => options[:counter_cache]
39
44
  has_many :children, :class_name => name, :foreign_key => options[:foreign_key], :dependent => :destroy
40
45
 
41
- class_eval <<-EOL
42
- include ActsAsKing::InstanceMethods
43
-
44
- scope :kings, where("#{options[:foreign_key]} IS NULL")
45
- EOL
46
+ include ActsAsKing::InstanceMethods
47
+ scope :kings, where("#{options[:foreign_key]} IS NULL")
46
48
 
47
49
  end
48
50
 
@@ -1,3 +1,3 @@
1
1
  module ActsAsKing
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -39,6 +39,11 @@ class ActsAsKingTest < Test::Unit::TestCase
39
39
  should "return multiple ancestors" do
40
40
  assert_equal [@sub_comment, @comment], @level_3.ancestors
41
41
  end
42
+
43
+ should "should use counter caching" do
44
+ assert_equal 1, @comment.reload.comments_count
45
+ assert_equal 1, @sub_comment.reload.comments_count
46
+ end
42
47
 
43
48
  end
44
49
 
@@ -1,5 +1,6 @@
1
1
  $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
2
2
 
3
+ require 'logger'
3
4
  require 'active_record'
4
5
  require 'acts_as_king'
5
6
  require 'test/unit'
@@ -9,6 +10,8 @@ ActiveRecord::Base.send(:include, ActsAsKing)
9
10
  ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
10
11
  ActiveRecord::Migration.verbose = false
11
12
 
13
+ ActiveRecord::Base.logger = Logger.new(STDOUT) if ENV['SHOW_SQL'] && ENV['SHOW_SQL'] == 'true'
14
+
12
15
  def setup_db
13
16
  ActiveRecord::Schema.define(:version => 1) do
14
17
  create_table :posts, :force => true do |t|
@@ -17,6 +20,7 @@ def setup_db
17
20
  create_table :comments, :force => true do |t|
18
21
  t.integer :post_id, :null => false
19
22
  t.integer :parent_id
23
+ t.integer :comments_count
20
24
  end
21
25
  end
22
26
  end
@@ -33,6 +37,6 @@ end
33
37
 
34
38
  class Comment < ActiveRecord::Base
35
39
  belongs_to :post
36
- acts_as_king
40
+ acts_as_king :counter_cache => true
37
41
  end
38
42
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: acts_as_king
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Drew Tempelmeyer
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-14 00:00:00 Z
13
+ date: 2011-05-16 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -99,7 +99,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
99
99
  requirements: []
100
100
 
101
101
  rubyforge_project:
102
- rubygems_version: 1.8.2
102
+ rubygems_version: 1.7.2
103
103
  signing_key:
104
104
  specification_version: 3
105
105
  summary: Hierarchical ActiveRecord models. Inspired by acts_as_tree. Trees are shady.