acts_as_commentable_more 1.2.8 → 1.2.9
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.
- checksums.yaml +4 -4
- data/lib/acts_as_commentable_more/commentable_methods.rb +32 -26
- data/lib/acts_as_commentable_more/helpers/comment/cache_counter_helper.rb +7 -7
- data/lib/acts_as_commentable_more/helpers/comment/instance_methods_helper.rb +15 -15
- data/lib/acts_as_commentable_more/helpers/post/associations_helper.rb +12 -11
- data/lib/acts_as_commentable_more/helpers/post/methods_helper.rb +1 -1
- data/lib/acts_as_commentable_more/helpers/post/scopes_helper.rb +5 -5
- data/lib/acts_as_commentable_more/version.rb +1 -1
- data/lib/generators/commentable/templates/comment.rb +2 -2
- data/lib/generators/commentable/templates/create_comments.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 672806220d9adb336f161186d27b0efa7712f0c9
|
|
4
|
+
data.tar.gz: 10235ad9b411cfd878c26c7c75bfaabc47efc911
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b38325194551c42e0b1f47e38fe4c64f76294dd9b16852fa6e02d847a83fde4de6757b0462e9a82c934674062fea509c935ad7dc8e3fed7714eb49b2d6e9c5a3
|
|
7
|
+
data.tar.gz: 4ebb78f27f14789e26d5fcec562c15651dad5281895efa4968ad9504d8405c75fadad88d644f94d298e9276daef54ec8112c89a1f1657d54dad9506c883296b9
|
|
@@ -10,53 +10,59 @@ module ActsAsCommentableMore
|
|
|
10
10
|
include Helpers::Comment::CacheCounterHelper
|
|
11
11
|
include Helpers::Comment::InstanceMethodsHelper
|
|
12
12
|
|
|
13
|
-
def acts_as_commentable(types: [], options: {}, as: nil, counter_cache: true)
|
|
14
|
-
mattr_accessor :
|
|
15
|
-
mattr_accessor :
|
|
13
|
+
def acts_as_commentable(association_comment_name, *args)#types: [], options: {}, as: nil, counter_cache: true)
|
|
14
|
+
mattr_accessor :aacm_commentable_options
|
|
15
|
+
mattr_accessor :aacm_association_options
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
default_commentable_options = { types: [], options: {}, as: nil, counter_cache: true }
|
|
18
|
+
default_association_options = { as: :commentable, dependent: :destroy, class_name: 'Comment' }
|
|
18
19
|
|
|
19
|
-
|
|
20
|
+
self.aacm_commentable_options = default_commentable_options.merge(args.extract_options!)
|
|
21
|
+
self.aacm_association_options = default_association_options.merge(aacm_commentable_options[:options].compact)
|
|
20
22
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
self.
|
|
24
|
-
self.
|
|
25
|
-
|
|
23
|
+
self.aacm_commentable_options[:association_comment_name] = association_comment_name.to_s
|
|
24
|
+
self.aacm_commentable_options[:types] = aacm_commentable_options[:types].flatten.compact
|
|
25
|
+
self.aacm_commentable_options[:comment_model] = aacm_association_options[:class_name].classify.constantize
|
|
26
|
+
self.aacm_commentable_options[:comment_roles] = if aacm_commentable_options[:types].present?
|
|
27
|
+
aacm_commentable_options[:types]
|
|
28
|
+
else
|
|
29
|
+
[aacm_commentable_options[:association_comment_name].singularize]
|
|
30
|
+
end.map(&:to_s)
|
|
26
31
|
|
|
27
|
-
if comment_roles.size == 1
|
|
32
|
+
if aacm_commentable_options[:comment_roles].size == 1
|
|
28
33
|
###########################
|
|
29
34
|
### basic comment ###
|
|
30
35
|
###########################
|
|
31
|
-
post_define_role_based_inflection(comment_roles.first, association_comment_name
|
|
32
|
-
post_define_create_role(association_comment_name)
|
|
36
|
+
post_define_role_based_inflection(aacm_commentable_options[:comment_roles].first, aacm_commentable_options[:association_comment_name])
|
|
37
|
+
post_define_create_role(aacm_commentable_options[:association_comment_name])
|
|
33
38
|
else
|
|
34
39
|
###########################
|
|
35
40
|
### many roles comment ###
|
|
36
41
|
###########################
|
|
37
42
|
# scope method for post model
|
|
38
|
-
post_define_all_scope
|
|
43
|
+
post_define_all_scope
|
|
39
44
|
|
|
40
|
-
comment_roles.each do |role|
|
|
45
|
+
aacm_commentable_options[:comment_roles].each do |role|
|
|
41
46
|
# association for post model
|
|
42
|
-
association_name = "#{role
|
|
43
|
-
post_define_role_based_inflection(role, association_name
|
|
47
|
+
association_name = "#{role}_#{aacm_commentable_options[:association_comment_name]}"
|
|
48
|
+
post_define_role_based_inflection(role, association_name)
|
|
44
49
|
# support method for comment model
|
|
45
50
|
post_define_create_role(association_name)
|
|
46
|
-
comment_define_is_role?(role)
|
|
47
|
-
comment_define_to_role(role)
|
|
48
|
-
comment_define_to_role!(role)
|
|
51
|
+
comment_define_is_role?(aacm_commentable_options[:comment_model], role)
|
|
52
|
+
comment_define_to_role(aacm_commentable_options[:comment_model], role)
|
|
53
|
+
comment_define_to_role!(aacm_commentable_options[:comment_model], role)
|
|
49
54
|
end
|
|
50
55
|
# helpper method for comment model
|
|
51
|
-
comment_define_can_change_role_of(
|
|
56
|
+
comment_define_can_change_role_of(aacm_commentable_options[:comment_model], aacm_association_options[:as])
|
|
52
57
|
end
|
|
53
58
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
59
|
+
# counter cache for comment model
|
|
60
|
+
if aacm_commentable_options[:counter_cache]
|
|
61
|
+
comment_define_counter_cache_role(aacm_commentable_options[:comment_model], aacm_commentable_options[:association_comment_name], aacm_association_options[:as])
|
|
62
|
+
end
|
|
57
63
|
# instance method for comment
|
|
58
|
-
comment_define_instance_method
|
|
59
|
-
comment_define_class_method
|
|
64
|
+
comment_define_instance_method(aacm_commentable_options[:comment_model])
|
|
65
|
+
comment_define_class_method(aacm_commentable_options[:comment_model])
|
|
60
66
|
end
|
|
61
67
|
|
|
62
68
|
end
|
|
@@ -5,7 +5,7 @@ module ActsAsCommentableMore
|
|
|
5
5
|
|
|
6
6
|
private
|
|
7
7
|
|
|
8
|
-
def comment_define_counter_cache_role association_comment_name, commentable_name
|
|
8
|
+
def comment_define_counter_cache_role(comment_model, association_comment_name, commentable_name)
|
|
9
9
|
never_has_counter_cache = !comment_model._create_callbacks.select {|cb| cb.kind == :after }.collect(&:filter).include?(:acts_as_commentable_more_increment!)
|
|
10
10
|
|
|
11
11
|
if never_has_counter_cache
|
|
@@ -17,7 +17,7 @@ module ActsAsCommentableMore
|
|
|
17
17
|
|
|
18
18
|
comment_model.redefine_method("acts_as_commentable_more_increment!") do
|
|
19
19
|
all_counter_field = "#{association_comment_name}_count"
|
|
20
|
-
role_counter_field = "#{self.role
|
|
20
|
+
role_counter_field = "#{self.role}_#{all_counter_field}"
|
|
21
21
|
post_model = self.send("#{commentable_name}_type").classify.constantize
|
|
22
22
|
attributes_post = post_model.column_names
|
|
23
23
|
|
|
@@ -32,7 +32,7 @@ module ActsAsCommentableMore
|
|
|
32
32
|
|
|
33
33
|
comment_model.redefine_method("acts_as_commentable_more_decrement!") do
|
|
34
34
|
all_counter_field = "#{association_comment_name}_count"
|
|
35
|
-
role_counter_field = "#{self.role
|
|
35
|
+
role_counter_field = "#{self.role}_#{all_counter_field}"
|
|
36
36
|
post_model = self.send("#{commentable_name}_type").classify.constantize
|
|
37
37
|
attributes_post = post_model.column_names
|
|
38
38
|
|
|
@@ -64,10 +64,10 @@ module ActsAsCommentableMore
|
|
|
64
64
|
|
|
65
65
|
# setting attributes for read only
|
|
66
66
|
post_model = self
|
|
67
|
-
counter_fields = ["#{association_comment_name
|
|
68
|
-
if post_model.comment_roles.size > 1
|
|
69
|
-
post_model.comment_roles.each do |role|
|
|
70
|
-
counter_fields << "#{role
|
|
67
|
+
counter_fields = ["#{association_comment_name}_count"]
|
|
68
|
+
if post_model.aacm_commentable_options[:comment_roles].size > 1
|
|
69
|
+
post_model.aacm_commentable_options[:comment_roles].each do |role|
|
|
70
|
+
counter_fields << "#{role}_#{association_comment_name}_count"
|
|
71
71
|
end
|
|
72
72
|
end
|
|
73
73
|
post_model.attr_readonly(*counter_fields)
|
|
@@ -5,7 +5,7 @@ module ActsAsCommentableMore
|
|
|
5
5
|
|
|
6
6
|
private
|
|
7
7
|
|
|
8
|
-
def comment_define_instance_method
|
|
8
|
+
def comment_define_instance_method(comment_model)
|
|
9
9
|
|
|
10
10
|
comment_model.class_eval do
|
|
11
11
|
|
|
@@ -23,7 +23,7 @@ module ActsAsCommentableMore
|
|
|
23
23
|
|
|
24
24
|
end
|
|
25
25
|
|
|
26
|
-
def comment_define_class_method
|
|
26
|
+
def comment_define_class_method(comment_model)
|
|
27
27
|
|
|
28
28
|
comment_model.class_eval do
|
|
29
29
|
|
|
@@ -31,7 +31,7 @@ module ActsAsCommentableMore
|
|
|
31
31
|
|
|
32
32
|
def find_comments_by_user(user, role = nil)
|
|
33
33
|
attr_finder = { user: user }
|
|
34
|
-
attr_finder.merge!(role: role
|
|
34
|
+
attr_finder.merge!(role: role) if role.present?
|
|
35
35
|
where(attr_finder).order(created_at: :desc)
|
|
36
36
|
end
|
|
37
37
|
|
|
@@ -40,34 +40,34 @@ module ActsAsCommentableMore
|
|
|
40
40
|
|
|
41
41
|
end
|
|
42
42
|
|
|
43
|
-
def comment_define_can_change_role_of(commentable_name)
|
|
43
|
+
def comment_define_can_change_role_of(comment_model, commentable_name)
|
|
44
44
|
comment_model.redefine_method("can_change_role?") do |role|
|
|
45
45
|
commentable_class = send(commentable_name).class
|
|
46
|
-
limit_role = commentable_class.comment_roles
|
|
47
|
-
limit_role.include?(role
|
|
46
|
+
limit_role = commentable_class.aacm_commentable_options[:comment_roles]
|
|
47
|
+
limit_role.include?(role)
|
|
48
48
|
end
|
|
49
49
|
comment_model.send(:private, "can_change_role?".to_sym)
|
|
50
50
|
end
|
|
51
51
|
|
|
52
|
-
def comment_define_is_role?(role)
|
|
52
|
+
def comment_define_is_role?(comment_model, role)
|
|
53
53
|
comment_model.redefine_method("is_#{role}?") do
|
|
54
|
-
raise(NoMethodError, "undefined method 'is_" + role
|
|
55
|
-
self.role == role
|
|
54
|
+
raise(NoMethodError, "undefined method 'is_" + role + "?'") unless can_change_role?(role)
|
|
55
|
+
self.role == role
|
|
56
56
|
end
|
|
57
57
|
end
|
|
58
58
|
|
|
59
|
-
def comment_define_to_role(role)
|
|
59
|
+
def comment_define_to_role(comment_model, role)
|
|
60
60
|
comment_model.redefine_method("to_#{role}") do
|
|
61
|
-
raise(NoMethodError, "undefined method 'is_" + role
|
|
62
|
-
self.role = role
|
|
61
|
+
raise(NoMethodError, "undefined method 'is_" + role + "?'") unless can_change_role?(role)
|
|
62
|
+
self.role = role
|
|
63
63
|
self
|
|
64
64
|
end
|
|
65
65
|
end
|
|
66
66
|
|
|
67
|
-
def comment_define_to_role!(role)
|
|
67
|
+
def comment_define_to_role!(comment_model, role)
|
|
68
68
|
comment_model.redefine_method("to_#{role}!") do
|
|
69
|
-
raise(NoMethodError, "undefined method 'is_" + role
|
|
70
|
-
self.update(role: role
|
|
69
|
+
raise(NoMethodError, "undefined method 'is_" + role + "?'") unless can_change_role?(role)
|
|
70
|
+
self.update(role: role)
|
|
71
71
|
self
|
|
72
72
|
end
|
|
73
73
|
end
|
|
@@ -5,23 +5,24 @@ module ActsAsCommentableMore
|
|
|
5
5
|
|
|
6
6
|
private
|
|
7
7
|
|
|
8
|
-
def post_define_role_based_inflection(role, association_comment_name
|
|
9
|
-
|
|
8
|
+
def post_define_role_based_inflection(role, association_comment_name)
|
|
9
|
+
commantable_name = aacm_association_options[:as].to_sym
|
|
10
|
+
send("define_role_based_inflection_#{Rails.version.first}", role, commantable_name, association_comment_name)
|
|
10
11
|
end
|
|
11
12
|
|
|
12
|
-
def define_role_based_inflection_3(role,
|
|
13
|
-
has_many "#{association_comment_name
|
|
14
|
-
has_many_options(role
|
|
13
|
+
def define_role_based_inflection_3(role, commantable_name, association_comment_name)
|
|
14
|
+
has_many "#{association_comment_name}".to_sym,
|
|
15
|
+
has_many_options(role).merge(:conditions => { role: role })
|
|
15
16
|
end
|
|
16
17
|
|
|
17
|
-
def define_role_based_inflection_4(role,
|
|
18
|
-
has_many "#{association_comment_name
|
|
19
|
-
-> { includes(
|
|
20
|
-
has_many_options(role
|
|
18
|
+
def define_role_based_inflection_4(role, commantable_name, association_comment_name)
|
|
19
|
+
has_many "#{association_comment_name}".to_sym,
|
|
20
|
+
-> { includes(commantable_name, :user).where(role: role) },
|
|
21
|
+
has_many_options(role)
|
|
21
22
|
end
|
|
22
23
|
|
|
23
|
-
def has_many_options(role
|
|
24
|
-
{ before_add: Proc.new { |post, comment| comment.role = role
|
|
24
|
+
def has_many_options(role)
|
|
25
|
+
{ before_add: Proc.new { |post, comment| comment.role = role } }.merge(aacm_association_options)
|
|
25
26
|
end
|
|
26
27
|
|
|
27
28
|
end
|
|
@@ -6,7 +6,7 @@ module ActsAsCommentableMore
|
|
|
6
6
|
private
|
|
7
7
|
|
|
8
8
|
def post_define_create_role(association_comment_name)
|
|
9
|
-
redefine_method("creates_#{association_comment_name.
|
|
9
|
+
redefine_method("creates_#{association_comment_name.pluralize}") do |attributes = nil|
|
|
10
10
|
send(association_comment_name).create(attributes)
|
|
11
11
|
end
|
|
12
12
|
end
|
|
@@ -3,11 +3,11 @@ module ActsAsCommentableMore
|
|
|
3
3
|
module Post
|
|
4
4
|
module ScopesHelper
|
|
5
5
|
|
|
6
|
-
def post_define_all_scope
|
|
7
|
-
redefine_method("all_#{
|
|
8
|
-
comment_model
|
|
9
|
-
.includes(
|
|
10
|
-
.where(
|
|
6
|
+
def post_define_all_scope
|
|
7
|
+
redefine_method("all_#{aacm_commentable_options[:association_comment_name]}") do
|
|
8
|
+
aacm_commentable_options[:comment_model]
|
|
9
|
+
.includes(aacm_association_options[:as].to_sym, :user)
|
|
10
|
+
.where(aacm_association_options[:as].to_sym => self)
|
|
11
11
|
.order(created_at: :desc)
|
|
12
12
|
end
|
|
13
13
|
end
|
|
@@ -2,13 +2,13 @@
|
|
|
2
2
|
class <%= class_name %> < ActiveRecord::Base
|
|
3
3
|
###################################################################
|
|
4
4
|
### To implement commentable add the following line to your model
|
|
5
|
-
### acts_as_commentable types: [:hide, :show], options: { class_name: '<%= class_name %>', as: :<%= class_name.demodulize.underscore
|
|
5
|
+
### acts_as_commentable :<%= class_name.demodulize.downcase.pluralize %>, types: [:hide, :show], options: { class_name: '<%= class_name %>', as: :<%= class_name.demodulize.underscore %>able }, counter_cache: true
|
|
6
6
|
|
|
7
7
|
### types is an array of possible comment type
|
|
8
8
|
### for example if you have public and private comment
|
|
9
9
|
### your types would be [:public, :private]
|
|
10
10
|
|
|
11
|
-
belongs_to :<%= class_name.demodulize.underscore
|
|
11
|
+
belongs_to :<%= class_name.demodulize.underscore %>able, polymorphic: true
|
|
12
12
|
belongs_to :user, polymorphic: true
|
|
13
13
|
|
|
14
14
|
###################################################################
|
|
@@ -8,7 +8,7 @@ class <%= migration_class_name %> < ActiveRecord::Migration
|
|
|
8
8
|
t.references :<%= class_name.demodulize.underscore + "able" %>, polymorphic: true
|
|
9
9
|
t.references :user, polymorphic: true, index: true
|
|
10
10
|
t.string :role, default: nil
|
|
11
|
-
t.hstore :related_attributes
|
|
11
|
+
t.hstore :related_attributes, default: ''
|
|
12
12
|
t.timestamps
|
|
13
13
|
end
|
|
14
14
|
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: acts_as_commentable_more
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.2.
|
|
4
|
+
version: 1.2.9
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- piya23300
|
|
8
8
|
autorequire: acts_as_commentable_more
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2015-07-
|
|
11
|
+
date: 2015-07-03 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rake
|
|
@@ -120,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
120
120
|
version: '0'
|
|
121
121
|
requirements: []
|
|
122
122
|
rubyforge_project:
|
|
123
|
-
rubygems_version: 2.
|
|
123
|
+
rubygems_version: 2.2.2
|
|
124
124
|
signing_key:
|
|
125
125
|
specification_version: 4
|
|
126
126
|
summary: gem that provides comment functionality.
|