acts_as_commentable_with_reply 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5a32bb048282677d32467d9497594ab8460c8932
4
+ data.tar.gz: ba0b1f24d4c6038938dc0537bb5b2c39aa148a86
5
+ SHA512:
6
+ metadata.gz: cf67de519d7768066a44b0ee81caaeb12991762e8b67b6ba848249547019a0f165a4624b8eb1cf1df49bdc2e3c120463898d05346521619a401c6be3d6164669
7
+ data.tar.gz: eb19cbb00e6d4ae6c86c884b9ccaa8975626a5aef4664e661925247d156818a04826996df5b24e6989f311aebbd469d2199846a5af78ff39c6d5edc7d1b90426
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in acts_as_commentable_with_reply.gemspec
4
+ gemspec
5
+
6
+ group :development do
7
+ gem 'rails' #, :git => 'git://github.com/rails/rails.git', :branch => '4-0-stable'
8
+ # https://github.com/rails/rails/issues/6039
9
+ gem 'activerecord-deprecated_finders', git: 'git://github.com/rails/activerecord-deprecated_finders.git'
10
+ gem 'sqlite3'
11
+ gem 'pry'
12
+ end
13
+
14
+ gem 'test-unit'
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2015 Lungelo Ximba
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,95 @@
1
+ = Acts As Commentable with Reply
2
+
3
+ Provides a single Comment model that can be attached to any model(s) within your app. It creates
4
+ Comment and CommentReply models and handles the plumbing between those models and any models that you declare to be
5
+ commentable models. The replies are limited to one-level nesting. If you need no nesting then check out
6
+ [acts_as_commentable](https://github.com/jackdempsey/acts_as_commentable). If you need deeper nesting then checkout
7
+ [acts_as_commentable_with_threading](https://github.com/elight/acts_as_commentable_with_threading).
8
+
9
+ The advantage of this gem over acts_as_commentable_with_threading is that it does not use the nested set model and
10
+ this leads to faster writes. Check [this Wikipedia](http://en.wikipedia.org/wiki/Nested_set_model) article for
11
+ further reading on the nested set model.
12
+
13
+
14
+ == Installation :
15
+
16
+ Add the following line to your Gemfile
17
+
18
+ === Rails 4
19
+
20
+ gem 'acts_as_commentable'
21
+
22
+ == Generator
23
+
24
+
25
+ === Rails 4
26
+
27
+ rails g comment
28
+
29
+ Then migrate your database to add the comments and comment_replies models:
30
+
31
+ rake db:migrate
32
+
33
+ == Usage
34
+
35
+ Make the desired ActiveRecord model act as commentable:
36
+
37
+ class Post < ActiveRecord::Base
38
+ acts_as_commentable
39
+ end
40
+
41
+ Add a comment to a model instance by adding the following to the controller:
42
+
43
+ commentable = Post.create
44
+ comment = commentable.comments.create
45
+ comment.title = "First comment."
46
+ comment.comment = "This is the first comment."
47
+ comment.save
48
+
49
+ Add a reply to a comment instance by adding the following to the controller:
50
+
51
+ comment = Comment.find x # Any other way of obtaining a comment instance
52
+ reply = comment.comment_replies.create :reply => "This is the first reply."
53
+
54
+ Fetch comments for the commentable model by adding the following to the view:
55
+
56
+ commentable = Post.find(1)
57
+ comments = commentable.comments.recent.limit(10).all
58
+
59
+ Fetch replies for a comment instance by adding the following to the controller/view:
60
+
61
+ comment = Comment.find x # Any other way of obtaining a comment instance
62
+ replies = CommentReply.find_comment_replies comment
63
+
64
+ You can also add different types of comments to a model:
65
+
66
+ class Todo < ActiveRecord::Base
67
+ acts_as_commentable :public, :private
68
+ end
69
+
70
+ To fetch the different types of comments for this model:
71
+
72
+ public_comments = Todo.find(1).public_comments
73
+ private_comments = Todo.find(1).private_comments
74
+
75
+
76
+ By default, `acts_as_commentable` will assume you are using a `Comment` model.
77
+ To change this, or change any other join options, pass in an options hash:
78
+
79
+ class Todo < ActiveRecord::Base
80
+ acts_as_commentable class_name: 'MyComment'
81
+ end
82
+
83
+ This also works in conjunction with comment types:
84
+
85
+ class Todo < ActiveRecord::Base
86
+ acts_as_commentable :public, :private, { class_name: 'MyComment' }
87
+ end
88
+
89
+ == Credits
90
+
91
+ Jack Dempsey - This plugin is heavily influenced by Acts As Commentable.
92
+
93
+ == Contributors
94
+
95
+ Lungelo Ximba
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'rake/testtask'
2
+
3
+ desc 'Test the acts_as_commentable_with_reply plugin.'
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'lib'
6
+ t.pattern = 'test/**/*_test.rb'
7
+ t.verbose = true
8
+ end
9
+
10
+ task default: :test
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "acts_as_commentable_with_reply"
7
+ spec.version = "1.0.0"
8
+ spec.authors = ["Lungelo Ximba"]
9
+ spec.email = ["lungeloximba@gmail.com"]
10
+
11
+ spec.summary = %q{Rails gem that provides comment functionality with one-level nesting of comment replies}
12
+ spec.description = %q{Rails gem that provides comment functionality with one-level nesting of comment replies (based on acts_as_commentable)}
13
+ spec.homepage = %q{https://github.com/sximba/acts_as_commentable_with_reply}
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.has_rdoc = false
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+ spec.license = 'MIT'
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.9"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "acts_as_commentable_with_reply"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), 'rails', 'init')
data/install.rb ADDED
@@ -0,0 +1,2 @@
1
+ puts "To create the comment model please run:"
2
+ puts "script/generate comment"
@@ -0,0 +1,2 @@
1
+ require 'commentable_methods'
2
+ require 'comment_methods'
@@ -0,0 +1,73 @@
1
+ module ActsAsCommentableWithReply
2
+ # including this module into your CommentReply model will give you finders and named
3
+ # scopes useful for working with CommentReplies.
4
+ # The named scopes are:
5
+ # in_order: Returns replies in the order they were created (created_at ASC).
6
+ # recent: Returns replies by how recently they were created (created_at DESC).
7
+ # limit(N): Return no more than N comments.
8
+ module CommentReply
9
+
10
+ def self.included(comment_reply_model)
11
+ comment_reply_model.extend Finders
12
+ comment_reply_model.scope :in_order, -> { comment_reply_model.order('created_at ASC') }
13
+ comment_reply_model.scope :recent, -> { comment_reply_model.reorder('created_at DESC') }
14
+ end
15
+
16
+ def is_comment_reply_type?(type)
17
+ type.to_s == role.singularize.to_s
18
+ end
19
+
20
+ module Finders
21
+ # Helper class method to lookup all comment replies assigned
22
+ # to a specific comment for a given user.
23
+ def find_comment_replies_by_user(comment, user)
24
+ where(["comment_id = ? and user_id = ?", comment.id, user.id]).order("created_at DESC")
25
+ end
26
+
27
+ # Helper class method to look up all replies for a comment
28
+ def find_comment_replies(comment)
29
+ where(["comment_id = ?", comment.id]).order("created_at DESC")
30
+ end
31
+ end
32
+ end
33
+
34
+ # including this module into your Comment model will give you finders and named scopes
35
+ # useful for working with Comments.
36
+ # The named scopes are:
37
+ # in_order: Returns comments in the order they were created (created_at ASC).
38
+ # recent: Returns comments by how recently they were created (created_at DESC).
39
+ # limit(N): Return no more than N comments.
40
+ module Comment
41
+
42
+ def self.included(comment_model)
43
+ comment_model.extend Finders
44
+ comment_model.scope :in_order, -> { comment_model.order('created_at ASC') }
45
+ comment_model.scope :recent, -> { comment_model.reorder('created_at DESC') }
46
+ end
47
+
48
+ def is_comment_type?(type)
49
+ type.to_s == role.singularize.to_s
50
+ end
51
+
52
+ module Finders
53
+ # Helper class method to lookup all comments assigned
54
+ # to all commentable types for a given user.
55
+ def find_comments_by_user(user, role = "comments")
56
+ where(["user_id = ? and role = ?", user.id, role]).order("created_at DESC")
57
+ end
58
+
59
+ # Helper class method to look up all comments for
60
+ # commentable class name and commentable id.
61
+ def find_comments_for_commentable(commentable_str, commentable_id, role = "comments")
62
+ where(["commentable_type = ? and commentable_id = ? and role = ?", commentable_str, commentable_id, role]).order("created_at DESC")
63
+ end
64
+
65
+ # Helper class method to look up a commentable object
66
+ # given the commentable class name and id
67
+ def find_commentable(commentable_str, commentable_id)
68
+ model = commentable_str.constantize
69
+ model.respond_to?(:find_comments_for) ? model.find(commentable_id) : nil
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,90 @@
1
+ require 'active_record'
2
+
3
+ # ActsAsCommentable
4
+ module Juixe
5
+ module Acts #:nodoc:
6
+ module Commentable #:nodoc:
7
+
8
+ def self.included(base)
9
+ base.extend ClassMethods
10
+ end
11
+
12
+ module HelperMethods
13
+ private
14
+ def define_role_based_inflection(role)
15
+ send("define_role_based_inflection_4", role)
16
+ end
17
+
18
+ def define_role_based_inflection_3(role)
19
+ has_many "#{role.to_s}_comments".to_sym,
20
+ has_many_options(role).merge(:conditions => { role: role.to_s })
21
+ end
22
+
23
+ def define_role_based_inflection_4(role)
24
+ has_many "#{role.to_s}_comments".to_sym,
25
+ -> { where(role: role.to_s) },
26
+ has_many_options(role)
27
+ end
28
+
29
+ def has_many_options(role)
30
+ {:class_name => "Comment",
31
+ :as => :commentable,
32
+ :dependent => :destroy,
33
+ :before_add => Proc.new { |x, c| c.role = role.to_s }
34
+ }
35
+ end
36
+ end
37
+
38
+ module ClassMethods
39
+ include HelperMethods
40
+
41
+ def acts_as_commentable(*args)
42
+ options = args.to_a.flatten.compact.partition{ |opt| opt.kind_of? Hash }
43
+ comment_roles = options.last.blank? ? nil : options.last.flatten.compact.map(&:to_sym)
44
+
45
+ join_options = options.first.blank? ? [{}] : options.first
46
+ throw 'Only one set of options can be supplied for the join' if join_options.length > 1
47
+ join_options = join_options.first
48
+
49
+ class_attribute :comment_types
50
+ self.comment_types = (comment_roles.blank? ? [:comments] : comment_roles)
51
+
52
+ if !comment_roles.blank?
53
+ comment_roles.each do |role|
54
+ define_role_based_inflection(role)
55
+ end
56
+ has_many :all_comments, { :as => :commentable, :dependent => :destroy, class_name: 'Comment' }.merge(join_options)
57
+ else
58
+ has_many :comments, {:as => :commentable, :dependent => :destroy}.merge(join_options)
59
+ end
60
+
61
+ comment_types.each do |role|
62
+ method_name = (role == :comments ? "comments" : "#{role.to_s}_comments").to_s
63
+ class_eval %{
64
+ def self.find_#{method_name}_for(obj)
65
+ commentable = self.base_class.name
66
+ Comment.find_comments_for_commentable(commentable, obj.id, "#{role.to_s}")
67
+ end
68
+
69
+ def self.find_#{method_name}_by_user(user)
70
+ commentable = self.base_class.name
71
+ Comment.where(["user_id = ? and commentable_type = ? and role = ?", user.id, commentable, "#{role.to_s}"]).order("created_at DESC")
72
+ end
73
+
74
+ def #{method_name}_ordered_by_submitted
75
+ Comment.find_comments_for_commentable(self.class.name, id, "#{role.to_s}").order("created_at")
76
+ end
77
+
78
+ def add_#{method_name.singularize}(comment)
79
+ comment.role = "#{role.to_s}"
80
+ #{method_name} << comment
81
+ end
82
+ }
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
89
+
90
+ ActiveRecord::Base.send(:include, Juixe::Acts::Commentable)
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Copies comment.rb to app/models/.
3
+ Copies comment_reply.rb to app/models/.
4
+ Copies create_comment.rb to db/migrate
5
+ Copies create_comment_replies.rb to db/migrate
6
+
7
+ Examples:
8
+ `rails generate comment`
@@ -0,0 +1,19 @@
1
+ require 'rails/generators/migration'
2
+
3
+ class CommentGenerator < Rails::Generators::Base
4
+ include Rails::Generators::Migration
5
+
6
+ def self.source_root
7
+ @_acts_as_commentable_with_reply_source_root ||= File.expand_path("../templates", __FILE__)
8
+ end
9
+
10
+ def self.next_migration_number(path)
11
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
12
+ end
13
+
14
+ def create_model_file
15
+ template "comment.rb", "app/models/comment.rb"
16
+ template "comment_reply.rb", "app/models/comment_reply.rb"
17
+ migration_template "create_comments.rb", "db/migrate/create_comments.rb"
18
+ end
19
+ end
@@ -0,0 +1,17 @@
1
+ class Comment < ActiveRecord::Base
2
+
3
+ include ActsAsCommentableWithReply::Comment
4
+
5
+ belongs_to :commentable, :polymorphic => true
6
+
7
+ default_scope -> { order('created_at ASC') }
8
+
9
+ # NOTE: install the acts_as_votable plugin if you
10
+ # want user to vote on the quality of comments.
11
+ #acts_as_votable
12
+
13
+ # NOTE: Comments belong to a user
14
+ belongs_to :user
15
+
16
+ has_many :comment_replies
17
+ end
@@ -0,0 +1,15 @@
1
+ class CommentReply < ActiveRecord::Base
2
+
3
+ include ActsAsCommentableWithReply::CommentReply
4
+
5
+ belongs_to :comment
6
+
7
+ default_scope -> { order('created_at ASC')}
8
+
9
+ # NOTE: install the acts_as_votable plugin if you
10
+ # want user to vote on the quality of comments.
11
+ #acts_as_votable
12
+
13
+ # NOTE: Comments belong to a user
14
+ belongs_to :user
15
+ end
@@ -0,0 +1,31 @@
1
+ class CreateComments < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :comments do |t|
4
+ t.string :title, :limit => 50, :default => ""
5
+ t.text :comment
6
+ t.references :commentable, :polymorphic => true
7
+ t.references :user
8
+ t.string :role, :default => "comments"
9
+ t.timestamps null: true
10
+ end
11
+
12
+ create_table :comment_replies do |t|
13
+ t.string :reply
14
+ t.references :comment
15
+ t.references :user
16
+ t.timestamps null: true
17
+ end
18
+
19
+ add_index :comments, :commentable_type
20
+ add_index :comments, :commentable_id
21
+ add_index :comments, :user_id
22
+
23
+ add_index :comment_replies, :comment_id
24
+ add_index :comment_replies, :user_id
25
+ end
26
+
27
+ def self.down
28
+ drop_table :comment_replies
29
+ drop_table :comments
30
+ end
31
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'acts_as_commentable_with_reply')
2
+
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_commentable_with_reply
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Lungelo Ximba
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-05-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Rails gem that provides comment functionality with one-level nesting
42
+ of comment replies (based on acts_as_commentable)
43
+ email:
44
+ - lungeloximba@gmail.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - ".rspec"
51
+ - ".travis.yml"
52
+ - Gemfile
53
+ - MIT-LICENSE
54
+ - README.rdoc
55
+ - Rakefile
56
+ - acts_as_commentable_with_reply.gemspec
57
+ - bin/console
58
+ - bin/setup
59
+ - init.rb
60
+ - install.rb
61
+ - lib/acts_as_commentable_with_reply.rb
62
+ - lib/comment_methods.rb
63
+ - lib/commentable_methods.rb
64
+ - lib/generators/comment/USAGE
65
+ - lib/generators/comment/comment_generator.rb
66
+ - lib/generators/comment/templates/comment.rb
67
+ - lib/generators/comment/templates/comment_reply.rb
68
+ - lib/generators/comment/templates/create_comments.rb
69
+ - rails/init.rb
70
+ homepage: https://github.com/sximba/acts_as_commentable_with_reply
71
+ licenses:
72
+ - MIT
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.4.5
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Rails gem that provides comment functionality with one-level nesting of comment
94
+ replies
95
+ test_files: []