13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/commentable_methods.rb', line 13
def (*args)
= args.to_a.flatten.compact.map(&:to_sym)
class_attribute :comment_types
self. = (.blank? ? [:comments] : )
options = ((args.blank? or args[0].blank?) ? {} : args[0])
if !.blank?
.each do |role|
has_many "#{role.to_s}_comments".to_sym,
-> { where(role: role.to_s) },
{:class_name => "Comment",
:as => :commentable,
:dependent => :destroy,
:before_add => Proc.new { |x, c| c.role = role.to_s }}
end
has_many :all_comments, {:as => :commentable, :dependent => :destroy, class_name: "Comment"}
else
has_many :comments, {:as => :commentable, :dependent => :destroy}
end
.each do |role|
method_name = (role == :comments ? "comments" : "#{role.to_s}_comments").to_s
class_eval %{
def self.find_#{method_name}_for(obj)
commentable = self.base_class.name
Comment.find_comments_for_commentable(commentable, obj.id, "#{role.to_s}")
end
def self.find_#{method_name}_by_user(user)
commentable = self.base_class.name
Comment.where(["user_id = ? and commentable_type = ? and role = ?", user.id, commentable, "#{role.to_s}"]).order("created_at DESC")
end
def #{method_name}_ordered_by_submitted
Comment.find_comments_for_commentable(self.class.name, id, "#{role.to_s}").order("created_at")
end
def add_#{method_name.singularize}(comment)
comment.role = "#{role.to_s}"
#{method_name} << comment
end
}
end
end
|