2005-04-03 06:52:05 -04:00
|
|
|
class Author < ActiveRecord::Base
|
2005-04-03 13:50:11 -04:00
|
|
|
has_many :posts
|
2005-11-06 15:39:34 -05:00
|
|
|
has_many :posts_with_comments, :include => :comments, :class_name => "Post"
|
|
|
|
has_many :posts_with_categories, :include => :categories, :class_name => "Post"
|
2005-11-07 23:36:37 -05:00
|
|
|
has_many :posts_with_comments_and_categories, :include => [ :comments, :categories ], :order => "posts.id", :class_name => "Post"
|
2005-11-06 15:39:34 -05:00
|
|
|
|
2005-07-04 04:43:57 -04:00
|
|
|
has_many :posts_with_callbacks, :class_name => "Post", :before_add => :log_before_adding,
|
2005-11-07 23:36:37 -05:00
|
|
|
:after_add => :log_after_adding, :before_remove => :log_before_removing,
|
|
|
|
:after_remove => :log_after_removing
|
|
|
|
has_many :posts_with_proc_callbacks, :class_name => "Post",
|
2005-07-04 04:43:57 -04:00
|
|
|
:before_add => Proc.new {|o, r| o.post_log << "before_adding#{r.id}"},
|
2005-11-07 23:36:37 -05:00
|
|
|
:after_add => Proc.new {|o, r| o.post_log << "after_adding#{r.id}"},
|
|
|
|
:before_remove => Proc.new {|o, r| o.post_log << "before_removing#{r.id}"},
|
|
|
|
:after_remove => Proc.new {|o, r| o.post_log << "after_removing#{r.id}"}
|
|
|
|
has_many :posts_with_multiple_callbacks, :class_name => "Post",
|
2005-07-04 04:43:57 -04:00
|
|
|
:before_add => [:log_before_adding, Proc.new {|o, r| o.post_log << "before_adding_proc#{r.id}"}],
|
2005-11-07 23:36:37 -05:00
|
|
|
:after_add => [:log_after_adding, Proc.new {|o, r| o.post_log << "after_adding_proc#{r.id}"}]
|
2005-07-04 04:43:57 -04:00
|
|
|
has_many :unchangable_posts, :class_name => "Post", :before_add => :raise_exception, :after_add => :log_after_adding
|
|
|
|
|
2005-12-11 13:06:51 -05:00
|
|
|
has_many :categorizations
|
|
|
|
has_many :categories, :through => :categorizations
|
|
|
|
|
2005-07-04 04:43:57 -04:00
|
|
|
attr_accessor :post_log
|
|
|
|
|
|
|
|
def after_initialize
|
2005-11-07 23:36:37 -05:00
|
|
|
@post_log = []
|
2005-07-04 04:43:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2005-11-07 23:36:37 -05:00
|
|
|
def log_before_adding(object)
|
|
|
|
@post_log << "before_adding#{object.id}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_after_adding(object)
|
|
|
|
@post_log << "after_adding#{object.id}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_before_removing(object)
|
|
|
|
@post_log << "before_removing#{object.id}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_after_removing(object)
|
|
|
|
@post_log << "after_removing#{object.id}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def raise_exception(object)
|
|
|
|
raise Exception.new("You can't add a post")
|
|
|
|
end
|
|
|
|
end
|