acts_as_commentable_with_replies 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ .idea
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in acts_as_commentable_with_replies.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Filvo Interactive
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # ActsAsCommentableWithReplies
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'acts_as_commentable_with_replies'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install acts_as_commentable_with_replies
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/acts_as_commentable_with_replies/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Filvo Interactive"]
6
+ gem.email = ["info@filvo.com"]
7
+ gem.description = %q{Acts as Commentable with Replies}
8
+ gem.summary = %q{A threaded comment system for Rails}
9
+ gem.homepage = "http://www.filvo.com"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "acts_as_commentable_with_replies"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = ActsAsCommentableWithReplies::VERSION
17
+
18
+ gem.add_development_dependency 'rails', '~> 3.0'
19
+
20
+ gem.add_dependency 'activerecord', '>= 3.0'
21
+ gem.add_dependency 'activesupport', '~> 3.0'
22
+ gem.add_dependency 'awesome_nested_set', '>= 2.0'
23
+ end
@@ -0,0 +1,17 @@
1
+ require 'active_record'
2
+ require 'active_support/inflector'
3
+ #require 'acts_as_commentable_with_replies/version'
4
+
5
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
6
+
7
+ module ActsAsCommentableWithReplies
8
+
9
+ if defined?(ActiveRecord::Base)
10
+ require 'acts_as_commentable_with_replies/extenders/commentable'
11
+ require 'acts_as_commentable_with_replies/extenders/commenter'
12
+ #require 'acts_as_commentable_with_replies/comment'
13
+ ActiveRecord::Base.extend ActsAsCommentableWithReplies::Extenders::Commentable
14
+ ActiveRecord::Base.extend ActsAsCommentableWithReplies::Extenders::Commenter
15
+ end
16
+
17
+ end
@@ -0,0 +1,81 @@
1
+ module ActsAsCommentableWithReplies
2
+ module Commentable
3
+
4
+ def self.included base
5
+ base.class_eval do
6
+ belongs_to :commentable, :polymorphic => true
7
+ has_many :comments, :as => :commentable do
8
+ def commenters
9
+ includes(:commenter).map(&:commenter)
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+
16
+ def default_conditions
17
+ {
18
+ :commentable_id => self.id,
19
+ :commentable_type => self.class.base_class.name.to_s
20
+ }
21
+ end
22
+
23
+ # voting
24
+ def comment(args = {})
25
+ return false if args[:commenter].nil? || args[:message].nil?
26
+
27
+ comment = Comment.new(
28
+ :commentable => self,
29
+ :commenter => args[:commenter],
30
+ :message => args[:message]
31
+ )
32
+
33
+ if comment.save
34
+ update_cached_comments
35
+ comment.move_to_child_of(args[:parent]) if !args[:parent].nil?
36
+ true
37
+ else
38
+ false
39
+ end
40
+ end
41
+
42
+
43
+ # caching
44
+ def update_cached_comments
45
+ updates = {}
46
+ if self.respond_to?(:cached_comments_total=)
47
+ updates[:cached_comments_total] = count_comments_total(true)
48
+ end
49
+ self.update_attributes(updates, :without_protection => true) if updates.size > 0
50
+ end
51
+
52
+
53
+ # counting
54
+ def count_comments_total(skip_cache = false)
55
+ if !skip_cache && self.respond_to?(:cached_comments_total)
56
+ return self.send(:cached_comments_total)
57
+ end
58
+ find_comments.count
59
+ end
60
+
61
+
62
+ # results
63
+ def find_comments(extra_conditions = {})
64
+ comments.where(extra_conditions)
65
+ end
66
+
67
+ def root_comments
68
+ find_comments :parent_id => nil
69
+ end
70
+
71
+
72
+ # commenters
73
+ def commented_by?(commenter)
74
+ comments = find_comments :commenter_id => commenter.id, :commenter_type => commenter.class.name
75
+ comments.count > 0
76
+ end
77
+
78
+
79
+
80
+ end
81
+ end
@@ -0,0 +1,36 @@
1
+ module ActsAsCommentableWithReplies
2
+ module Commenter
3
+
4
+ def self.included(base)
5
+ base.class_eval do
6
+ belongs_to :commenter, :polymorphic => true
7
+ has_many :comments, :as => :commenter do
8
+ def commentables
9
+ includes(:commentable).map(&:commentable)
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ # voting
16
+ def comment(args = {})
17
+ return false if args[:commentable].nil? || args[:message].nil?
18
+ args[:commentable].comment args.merge({:commenter => self})
19
+ end
20
+
21
+ # results
22
+ def commented_on?(commentable)
23
+ comments = find_comments(:commentable_id => commentable.id, :commentable_type => commentable.class.name)
24
+ comments.size > 0
25
+ end
26
+
27
+ def find_comments(extra_conditions = {})
28
+ comments.where(extra_conditions)
29
+ end
30
+
31
+ def find_comments_for_class(klass, extra_conditions = {})
32
+ find_comments extra_conditions.merge({:commentable_type => klass.name})
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,24 @@
1
+ module ActsAsCommentableWithReplies
2
+ module Extenders
3
+
4
+ module Commentable
5
+
6
+ def commentable?
7
+ false
8
+ end
9
+
10
+ def acts_as_commentable
11
+ require 'acts_as_commentable_with_replies/commentable'
12
+ include ActsAsCommentableWithReplies::Commentable
13
+
14
+ class_eval do
15
+ def self.commentable?
16
+ true
17
+ end
18
+ end
19
+ end
20
+
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,24 @@
1
+ module ActsAsCommentableWithReplies
2
+ module Extenders
3
+
4
+ module Commenter
5
+
6
+ def commenter?
7
+ false
8
+ end
9
+
10
+ def acts_as_commenter(*args)
11
+ require 'acts_as_commentable_with_replies/commenter'
12
+ include ActsAsCommentableWithReplies::Commenter
13
+
14
+ class_eval do
15
+ def self.commenter?
16
+ true
17
+ end
18
+ end
19
+
20
+ end
21
+
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module ActsAsCommentableWithReplies
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,32 @@
1
+ require 'rails/generators/migration'
2
+
3
+ module ActsAsCommentableWithReplies
4
+ class MigrationGenerator < Rails::Generators::Base
5
+ include Rails::Generators::Migration
6
+
7
+ desc "Generates migration for acts_as_commentable_with_replies (comments table)"
8
+
9
+ def self.orm
10
+ Rails::Generators.options[:rails][:orm]
11
+ end
12
+
13
+ def self.source_root
14
+ File.join(File.dirname(__FILE__), 'templates', (orm.to_s unless orm.class.eql?(String)) )
15
+ end
16
+
17
+ def self.orm_has_migration?
18
+ [:active_record].include? orm
19
+ end
20
+
21
+ def self.next_migration_number(path)
22
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
23
+ end
24
+
25
+ def create_migration_file
26
+ if self.class.orm_has_migration?
27
+ migration_template 'migration.rb', 'db/migrate/acts_as_commentable_with_replies_migration'
28
+ copy_file 'comment.rb', 'app/models/comment.rb'
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,15 @@
1
+ class Comment < ::ActiveRecord::Base
2
+ acts_as_nested_set
3
+
4
+ attr_accessible :commentable_id, :commentable_type,
5
+ :commenter_id, :commenter_type,
6
+ :commentable, :commenter,
7
+ :message
8
+
9
+ belongs_to :commentable, :polymorphic => true
10
+ belongs_to :commenter, :polymorphic => true
11
+
12
+ validates_presence_of :commentable_id
13
+ validates_presence_of :commenter_id
14
+ validates_presence_of :message
15
+ end
@@ -0,0 +1,18 @@
1
+ class ActsAsCommentableWithRepliesMigration < ActiveRecord::Migration
2
+
3
+ def self.up
4
+ create_table :comments do |t|
5
+ t.references :commentable, :polymorphic => true
6
+ t.references :commenter, :polymorphic => true
7
+
8
+ t.text :message
9
+ t.integer :parent_id, :lft, :rgt
10
+
11
+ t.timestamps
12
+ end
13
+
14
+ add_index :comments, [:commentable_id, :commentable_type]
15
+ add_index :comments, [:commenter_id, :commenter_type]
16
+ end
17
+
18
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_commentable_with_replies
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Filvo Interactive
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-07-28 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rails
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 7
29
+ segments:
30
+ - 3
31
+ - 0
32
+ version: "3.0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: activerecord
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 7
44
+ segments:
45
+ - 3
46
+ - 0
47
+ version: "3.0"
48
+ type: :runtime
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: activesupport
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ hash: 7
59
+ segments:
60
+ - 3
61
+ - 0
62
+ version: "3.0"
63
+ type: :runtime
64
+ version_requirements: *id003
65
+ - !ruby/object:Gem::Dependency
66
+ name: awesome_nested_set
67
+ prerelease: false
68
+ requirement: &id004 !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ hash: 3
74
+ segments:
75
+ - 2
76
+ - 0
77
+ version: "2.0"
78
+ type: :runtime
79
+ version_requirements: *id004
80
+ description: Acts as Commentable with Replies
81
+ email:
82
+ - info@filvo.com
83
+ executables: []
84
+
85
+ extensions: []
86
+
87
+ extra_rdoc_files: []
88
+
89
+ files:
90
+ - .gitignore
91
+ - Gemfile
92
+ - LICENSE
93
+ - README.md
94
+ - Rakefile
95
+ - acts_as_commentable_with_replies.gemspec
96
+ - lib/acts_as_commentable_with_replies.rb
97
+ - lib/acts_as_commentable_with_replies/commentable.rb
98
+ - lib/acts_as_commentable_with_replies/commenter.rb
99
+ - lib/acts_as_commentable_with_replies/extenders/commentable.rb
100
+ - lib/acts_as_commentable_with_replies/extenders/commenter.rb
101
+ - lib/acts_as_commentable_with_replies/version.rb
102
+ - lib/generators/acts_as_commentable_with_replies/migration/migration_generator.rb
103
+ - lib/generators/acts_as_commentable_with_replies/migration/templates/active_record/comment.rb
104
+ - lib/generators/acts_as_commentable_with_replies/migration/templates/active_record/migration.rb
105
+ homepage: http://www.filvo.com
106
+ licenses: []
107
+
108
+ post_install_message:
109
+ rdoc_options: []
110
+
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ hash: 3
119
+ segments:
120
+ - 0
121
+ version: "0"
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ none: false
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ hash: 3
128
+ segments:
129
+ - 0
130
+ version: "0"
131
+ requirements: []
132
+
133
+ rubyforge_project:
134
+ rubygems_version: 1.8.15
135
+ signing_key:
136
+ specification_version: 3
137
+ summary: A threaded comment system for Rails
138
+ test_files: []
139
+
140
+ has_rdoc: