From c5c66583468bb62081c3e1ccf5f34c7d2bf35525 Mon Sep 17 00:00:00 2001 From: Mike Campbell Date: Wed, 4 Mar 2015 12:23:39 +0000 Subject: [PATCH] Refactored Juixe::Acts::Commentable and got test running. - Juixe::Acts::Commentable was being included in AR::Base. It had a ClassMethods module which was being extended on AR::Base when the Commentable module was included. This is pointless indirection and we may aswell just extend the module instead of including it in the first place! :) - Added TestUnit to development gems and fixed Rails.version check --- Gemfile | 1 + Gemfile.lock | 4 +- lib/commentable_methods.rb | 82 ++++++++++++++++++-------------------- 3 files changed, 42 insertions(+), 45 deletions(-) diff --git a/Gemfile b/Gemfile index a06b7fb..b33d1ad 100644 --- a/Gemfile +++ b/Gemfile @@ -7,4 +7,5 @@ group :development do gem 'activerecord-deprecated_finders', git: 'git://github.com/rails/activerecord-deprecated_finders.git' gem 'sqlite3' gem 'pry' + gem 'test-unit' end diff --git a/Gemfile.lock b/Gemfile.lock index 27192e1..995e8a8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -49,7 +49,7 @@ GIT PATH remote: . specs: - acts_as_commentable (4.0.1) + acts_as_commentable (4.0.2) GEM remote: http://rubygems.org/ @@ -88,6 +88,7 @@ GEM activesupport (>= 3.0) sprockets (~> 2.8) sqlite3 (1.3.6) + test-unit (2.1.1.0) thor (0.18.1) thread_safe (0.1.3) atomic @@ -106,3 +107,4 @@ DEPENDENCIES pry rails! sqlite3 + test-unit diff --git a/lib/commentable_methods.rb b/lib/commentable_methods.rb index c1bdc7f..fb1ef6c 100644 --- a/lib/commentable_methods.rb +++ b/lib/commentable_methods.rb @@ -1,18 +1,14 @@ require 'active_record' +require 'rails/version' # ActsAsCommentable module Juixe 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) + send("define_role_based_inflection_#{Rails::VERSION::MAJOR}", role) end def define_role_based_inflection_3(role) @@ -35,56 +31,54 @@ def has_many_options(role) end end - module ClassMethods - include HelperMethods + include HelperMethods - def acts_as_commentable(*args) - options = args.to_a.flatten.compact.partition{ |opt| opt.kind_of? Hash } - comment_roles = options.last.blank? ? nil : options.last.flatten.compact.map(&:to_sym) + def acts_as_commentable(*args) + options = args.to_a.flatten.compact.partition{ |opt| opt.kind_of? Hash } + comment_roles = options.last.blank? ? nil : options.last.flatten.compact.map(&:to_sym) - join_options = options.first.blank? ? [{}] : options.first - throw 'Only one set of options can be supplied for the join' if join_options.length > 1 - join_options = join_options.first + join_options = options.first.blank? ? [{}] : options.first + throw 'Only one set of options can be supplied for the join' if join_options.length > 1 + join_options = join_options.first - class_attribute :comment_types - self.comment_types = (comment_roles.blank? ? [:comments] : comment_roles) + class_attribute :comment_types + self.comment_types = (comment_roles.blank? ? [:comments] : comment_roles) - if !comment_roles.blank? - comment_roles.each do |role| - define_role_based_inflection(role) - end - 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) + if !comment_roles.blank? + comment_roles.each do |role| + define_role_based_inflection(role) end + 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) + end - comment_types.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 + comment_types.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 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 #{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 + def add_#{method_name.singularize}(comment) + comment.role = "#{role.to_s}" + #{method_name} << comment + end + } end end end end end -ActiveRecord::Base.send(:include, Juixe::Acts::Commentable) +ActiveRecord::Base.send(:extend, Juixe::Acts::Commentable)