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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 82fb9899d8883d3beafdbf2f8ca5c56d9432a888
4
- data.tar.gz: 370116730009818699aa006177784dfb05d8903b
3
+ metadata.gz: 672806220d9adb336f161186d27b0efa7712f0c9
4
+ data.tar.gz: 10235ad9b411cfd878c26c7c75bfaabc47efc911
5
5
  SHA512:
6
- metadata.gz: db7145766eda6390af0469b2f7c8a21c07e744c0a7adf13955546f1a7ea5d0581f3af25ce62fc459b642f1e5e270860a661bef5a119b960d5e7d448a8d1b3e4b
7
- data.tar.gz: 177574f64e96bb3c54ee28b9333cea523cc02ff6a6133a400977814c826b34d9293f4fe64b40d4958c203187017661800df2142d993266935b5578293344543e
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 :comment_model
15
- mattr_accessor :comment_roles
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
- default_options = {as: :commentable, dependent: :destroy, class_name: 'Comment'}
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
- types = types.flatten.compact.map(&:to_sym)
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
- association_options = default_options.merge(options.compact)
22
- association_comment_name = (as || association_options[:class_name].demodulize.underscore.to_sym).to_s.pluralize
23
- self.comment_roles = types.present? ? types : [association_comment_name.singularize.to_sym]
24
- self.comment_model = association_options[:class_name].classify.constantize
25
- enable_counter_cache = counter_cache
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, association_options)
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(association_comment_name, association_options[:as])
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.to_s}_#{association_comment_name.to_s}"
43
- post_define_role_based_inflection(role, association_name, association_options)
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(association_options[:as])
56
+ comment_define_can_change_role_of(aacm_commentable_options[:comment_model], aacm_association_options[:as])
52
57
  end
53
58
 
54
- # counter cache for comment model
55
- comment_define_counter_cache_role(association_comment_name, association_options[:as]) if enable_counter_cache
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.to_s}_#{all_counter_field}"
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.to_s}_#{all_counter_field}"
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.to_s}_count"]
68
- if post_model.comment_roles.size > 1
69
- post_model.comment_roles.each do |role|
70
- counter_fields << "#{role.to_s}_#{association_comment_name.to_s}_count"
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.to_s) if role.present?
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.to_sym)
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.to_s + "?'") unless can_change_role?(role.to_s)
55
- self.role == role.to_s
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.to_s + "?'") unless can_change_role?(role.to_s)
62
- self.role = role.to_s
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.to_s + "?'") unless can_change_role?(role.to_s)
70
- self.update(role: role.to_s)
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, join_options)
9
- send("define_role_based_inflection_#{Rails.version.first}", role, association_comment_name,join_options)
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, association_comment_name, join_options)
13
- has_many "#{association_comment_name.to_s}".to_sym,
14
- has_many_options(role, join_options).merge(:conditions => { role: role.to_s })
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, association_comment_name, join_options)
18
- has_many "#{association_comment_name.to_s}".to_sym,
19
- -> { includes(join_options[:as].to_sym, :user).where(role: role.to_s) },
20
- has_many_options(role, join_options)
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, join_options)
24
- { before_add: Proc.new { |post, comment| comment.role = role.to_s } }.merge(join_options)
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.to_s.pluralize}") do |attributes = nil|
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 comment_name, commentable
7
- redefine_method("all_#{comment_name.to_s}") do
8
- comment_model
9
- .includes(commentable.to_sym, :user)
10
- .where(commentable.to_sym => self)
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
@@ -1,3 +1,3 @@
1
1
  module ActsAsCommentableMore
2
- VERSION = "1.2.8"
2
+ VERSION = "1.2.9"
3
3
  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 + "able" %> }, as: :<%= class_name.demodulize.downcase.pluralize %> , counter_cache: true
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 + "able" %>, :polymorphic => true
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.8
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-02 00:00:00.000000000 Z
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.4.5
123
+ rubygems_version: 2.2.2
124
124
  signing_key:
125
125
  specification_version: 4
126
126
  summary: gem that provides comment functionality.