From c49fb7ca22af0b4387887ceb02aa2d2119a781f3 Mon Sep 17 00:00:00 2001 From: danaustin Date: Thu, 29 May 2014 14:35:14 +0100 Subject: [PATCH 1/4] Add accepts_nested_attributes_for to commentable models --- lib/commentable_methods.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/commentable_methods.rb b/lib/commentable_methods.rb index c1bdc7f..51a3960 100644 --- a/lib/commentable_methods.rb +++ b/lib/commentable_methods.rb @@ -54,8 +54,10 @@ def acts_as_commentable(*args) define_role_based_inflection(role) end has_many :all_comments, { :as => :commentable, :dependent => :destroy, class_name: 'Comment' }.merge(join_options) + accepts_nested_attributes_for "#{role.to_s}_comments".to_sym, allow_destroy: true, reject_if: proc { |attributes| attributes['title'].blank? && attributes['comment'].blank? } else has_many :comments, {:as => :commentable, :dependent => :destroy}.merge(join_options) + accepts_nested_attributes_for :comments, allow_destroy: true, reject_if: proc { |attributes| attributes['title'].blank? && attributes['comment'].blank? } end comment_types.each do |role| From dffc644c43b795e44129c69e1f01f1a81044ac11 Mon Sep 17 00:00:00 2001 From: danaustin Date: Thu, 29 May 2014 14:44:57 +0100 Subject: [PATCH 2/4] Add accepts_nested_attributes_for to commentable models --- lib/commentable_methods.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/commentable_methods.rb b/lib/commentable_methods.rb index 51a3960..802f59b 100644 --- a/lib/commentable_methods.rb +++ b/lib/commentable_methods.rb @@ -52,9 +52,9 @@ def acts_as_commentable(*args) if !comment_roles.blank? comment_roles.each do |role| define_role_based_inflection(role) + accepts_nested_attributes_for "#{role.to_s}_comments".to_sym, allow_destroy: true, reject_if: proc { |attributes| attributes['title'].blank? && attributes['comment'].blank? } end has_many :all_comments, { :as => :commentable, :dependent => :destroy, class_name: 'Comment' }.merge(join_options) - accepts_nested_attributes_for "#{role.to_s}_comments".to_sym, allow_destroy: true, reject_if: proc { |attributes| attributes['title'].blank? && attributes['comment'].blank? } else has_many :comments, {:as => :commentable, :dependent => :destroy}.merge(join_options) accepts_nested_attributes_for :comments, allow_destroy: true, reject_if: proc { |attributes| attributes['title'].blank? && attributes['comment'].blank? } From 27fbb81858affa28560e19cb6a3572cd2e0630c3 Mon Sep 17 00:00:00 2001 From: Jon Gilbraith Date: Thu, 3 Sep 2020 10:42:28 +0100 Subject: [PATCH 3/4] Remove deprecated entry in gemspec --- acts_as_commentable.gemspec | 1 - 1 file changed, 1 deletion(-) diff --git a/acts_as_commentable.gemspec b/acts_as_commentable.gemspec index 790165e..bb665bd 100644 --- a/acts_as_commentable.gemspec +++ b/acts_as_commentable.gemspec @@ -11,7 +11,6 @@ Gem::Specification.new do |s| s.email = %q{unknown@juixe.com} s.extra_rdoc_files = ["README.rdoc", "MIT-LICENSE"] s.files = ["MIT-LICENSE", "README.rdoc", "lib/acts_as_commentable.rb", "lib/comment_methods.rb", "lib/commentable_methods.rb", "lib/generators", "lib/generators/comment", "lib/generators/comment/comment_generator.rb", "lib/generators/comment/templates", "lib/generators/comment/templates/comment.rb", "lib/generators/comment/templates/create_comments.rb", "lib/generators/comment/USAGE", "init.rb", "install.rb"] - s.has_rdoc = false s.homepage = %q{http://www.juixe.com/techknow/index.php/2006/06/18/acts-as-commentable-plugin/} s.require_paths = ["lib"] s.rubygems_version = %q{1.3.6} From d0eb068bf51e8be0b6e8aad9f50b29393b181a4d Mon Sep 17 00:00:00 2001 From: Stephen Butt Date: Wed, 11 Jan 2023 12:36:21 +0000 Subject: [PATCH 4/4] Upgrades for Ruby 3.0.5. --- acts_as_commentable.gemspec | 35 ++++++++++++------------- lib/commentable_methods.rb | 52 ++++++++++++++++++++----------------- 2 files changed, 44 insertions(+), 43 deletions(-) diff --git a/acts_as_commentable.gemspec b/acts_as_commentable.gemspec index bb665bd..853e914 100644 --- a/acts_as_commentable.gemspec +++ b/acts_as_commentable.gemspec @@ -1,29 +1,26 @@ -# -*- encoding: utf-8 -*- - Gem::Specification.new do |s| - s.name = %q{acts_as_commentable} - s.version = "4.0.2" + s.name = 'acts_as_commentable' + s.version = '4.0.3' - s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= - s.authors = ["Cosmin Radoi, Jack Dempsey, Xelipe, Chris Eppstein"] - s.autorequire = %q{acts_as_commentable} - s.description = %q{Plugin/gem that provides comment functionality} - s.email = %q{unknown@juixe.com} - s.extra_rdoc_files = ["README.rdoc", "MIT-LICENSE"] - s.files = ["MIT-LICENSE", "README.rdoc", "lib/acts_as_commentable.rb", "lib/comment_methods.rb", "lib/commentable_methods.rb", "lib/generators", "lib/generators/comment", "lib/generators/comment/comment_generator.rb", "lib/generators/comment/templates", "lib/generators/comment/templates/comment.rb", "lib/generators/comment/templates/create_comments.rb", "lib/generators/comment/USAGE", "init.rb", "install.rb"] - s.homepage = %q{http://www.juixe.com/techknow/index.php/2006/06/18/acts-as-commentable-plugin/} - s.require_paths = ["lib"] - s.rubygems_version = %q{1.3.6} - s.summary = %q{Plugin/gem that provides comment functionality} + s.required_rubygems_version = Gem::Requirement.new('>= 0') if s.respond_to? :required_rubygems_version= + s.authors = ['Cosmin Radoi, Jack Dempsey, Xelipe, Chris Eppstein'] + s.autorequire = 'acts_as_commentable' + s.description = 'Plugin/gem that provides comment functionality' + s.email = 'unknown@juixe.com' + s.extra_rdoc_files = ['README.rdoc', 'MIT-LICENSE'] + s.files = ['MIT-LICENSE', 'README.rdoc', 'lib/acts_as_commentable.rb', 'lib/comment_methods.rb', + 'lib/commentable_methods.rb', 'lib/generators', 'lib/generators/comment', 'lib/generators/comment/comment_generator.rb', 'lib/generators/comment/templates', 'lib/generators/comment/templates/comment.rb', 'lib/generators/comment/templates/create_comments.rb', 'lib/generators/comment/USAGE', 'init.rb', 'install.rb'] + s.homepage = 'http://www.juixe.com/techknow/index.php/2006/06/18/acts-as-commentable-plugin/' + s.require_paths = ['lib'] + s.rubygems_version = '1.3.6' + s.summary = 'Plugin/gem that provides comment functionality' s.license = 'MIT' - if s.respond_to? :specification_version then + if s.respond_to? :specification_version current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION s.specification_version = 3 - if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then - else + if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') end - else end end diff --git a/lib/commentable_methods.rb b/lib/commentable_methods.rb index 802f59b..a2493b4 100644 --- a/lib/commentable_methods.rb +++ b/lib/commentable_methods.rb @@ -2,36 +2,35 @@ # ActsAsCommentable module Juixe - module Acts #:nodoc: - module Commentable #:nodoc: - + module Acts # :nodoc: + module Commentable # :nodoc: def self.included(base) base.extend ClassMethods end module HelperMethods private + def define_role_based_inflection(role) send("define_role_based_inflection_#{Rails.version.first}", role) end def define_role_based_inflection_3(role) - has_many "#{role.to_s}_comments".to_sym, - has_many_options(role).merge(:conditions => { role: role.to_s }) + has_many "#{role}_comments".to_sym, + **has_many_options(role).merge(conditions: { role: role.to_s }) end def define_role_based_inflection_4(role) - has_many "#{role.to_s}_comments".to_sym, + has_many "#{role}_comments".to_sym, -> { where(role: role.to_s) }, - has_many_options(role) + **has_many_options(role) end def has_many_options(role) - {:class_name => "Comment", - :as => :commentable, - :dependent => :destroy, - :before_add => Proc.new { |x, c| c.role = role.to_s } - } + { class_name: 'Comment', + as: :commentable, + dependent: :destroy, + before_add: proc { |_x, c| c.role = role.to_s } } end end @@ -39,7 +38,7 @@ module ClassMethods include HelperMethods def acts_as_commentable(*args) - options = args.to_a.flatten.compact.partition{ |opt| opt.kind_of? Hash } + options = args.to_a.flatten.compact.partition { |opt| opt.is_a? Hash } comment_roles = options.last.blank? ? nil : options.last.flatten.compact.map(&:to_sym) join_options = options.first.blank? ? [{}] : options.first @@ -52,36 +51,41 @@ def acts_as_commentable(*args) if !comment_roles.blank? comment_roles.each do |role| define_role_based_inflection(role) - accepts_nested_attributes_for "#{role.to_s}_comments".to_sym, allow_destroy: true, reject_if: proc { |attributes| attributes['title'].blank? && attributes['comment'].blank? } + accepts_nested_attributes_for "#{role}_comments".to_sym, allow_destroy: true, reject_if: proc { |attributes| + attributes['title'].blank? && attributes['comment'].blank? + } end - has_many :all_comments, { :as => :commentable, :dependent => :destroy, class_name: 'Comment' }.merge(join_options) + has_many :all_comments, + **{ as: :commentable, dependent: :destroy, class_name: 'Comment' }.merge(join_options) else - has_many :comments, {:as => :commentable, :dependent => :destroy}.merge(join_options) - accepts_nested_attributes_for :comments, allow_destroy: true, reject_if: proc { |attributes| attributes['title'].blank? && attributes['comment'].blank? } + has_many :comments, **{ as: :commentable, dependent: :destroy }.merge(join_options) + accepts_nested_attributes_for :comments, allow_destroy: true, reject_if: proc { |attributes| + attributes['title'].blank? && attributes['comment'].blank? + } end comment_types.each do |role| - method_name = (role == :comments ? "comments" : "#{role.to_s}_comments").to_s + method_name = (role == :comments ? 'comments' : "#{role}_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}") + Comment.find_comments_for_commentable(commentable, obj.id, "#{role}") 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") + Comment.where(["user_id = ? and commentable_type = ? and role = ?", user.id, commentable, "#{role}"]).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") + Comment.find_comments_for_commentable(self.class.name, id, "#{role}").order("created_at") end def add_#{method_name.singularize}(comment) - comment.role = "#{role.to_s}" + comment.role = "#{role}" #{method_name} << comment end - } + }, __FILE__, __LINE__ - 19 end end end @@ -89,4 +93,4 @@ def add_#{method_name.singularize}(comment) end end -ActiveRecord::Base.send(:include, Juixe::Acts::Commentable) +ActiveRecord::Base.include Juixe::Acts::Commentable