mongoid_commentable 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "mongoid", "~> 2.0"
4
+ gem "bson_ext", "~> 1.3"
5
+
6
+ group :development do
7
+ gem "rspec", "~> 2.3.0"
8
+ gem "database_cleaner"
9
+ gem "bundler", "~> 1.0.0"
10
+ gem "jeweler", "~> 1.6.0"
11
+ gem "rcov", ">= 0"
12
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,50 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activemodel (3.0.7)
5
+ activesupport (= 3.0.7)
6
+ builder (~> 2.1.2)
7
+ i18n (~> 0.5.0)
8
+ activesupport (3.0.7)
9
+ bson (1.3.0)
10
+ bson_ext (1.3.0)
11
+ builder (2.1.2)
12
+ database_cleaner (0.6.6)
13
+ diff-lcs (1.1.2)
14
+ git (1.2.5)
15
+ i18n (0.5.0)
16
+ jeweler (1.6.0)
17
+ bundler (~> 1.0.0)
18
+ git (>= 1.2.5)
19
+ rake
20
+ mongo (1.3.0)
21
+ bson (>= 1.3.0)
22
+ mongoid (2.0.1)
23
+ activemodel (~> 3.0)
24
+ mongo (~> 1.3)
25
+ tzinfo (~> 0.3.22)
26
+ will_paginate (~> 3.0.pre)
27
+ rake (0.8.7)
28
+ rcov (0.9.9)
29
+ rspec (2.3.0)
30
+ rspec-core (~> 2.3.0)
31
+ rspec-expectations (~> 2.3.0)
32
+ rspec-mocks (~> 2.3.0)
33
+ rspec-core (2.3.1)
34
+ rspec-expectations (2.3.0)
35
+ diff-lcs (~> 1.1.2)
36
+ rspec-mocks (2.3.0)
37
+ tzinfo (0.3.27)
38
+ will_paginate (3.0.pre2)
39
+
40
+ PLATFORMS
41
+ ruby
42
+
43
+ DEPENDENCIES
44
+ bson_ext (~> 1.3)
45
+ bundler (~> 1.0.0)
46
+ database_cleaner
47
+ jeweler (~> 1.6.0)
48
+ mongoid (~> 2.0)
49
+ rcov
50
+ rspec (~> 2.3.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Max Golovnia
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,70 @@
1
+ = mongoid_commentable
2
+
3
+ Mongoid_commentable provides methods to create commentable documents
4
+
5
+ == Installation
6
+ You can use latest gem:
7
+ gem install mongoid_commentable
8
+
9
+ or in Gemfile:
10
+ gem 'mongoid_commentable'
11
+
12
+ == Usage
13
+ To make model commentable you need to include Mongoid::Commentable into your document:
14
+ class Article
15
+ include Mongoid::Document
16
+ include Mongoid::Commentable
17
+
18
+ field author
19
+ field content
20
+ end
21
+
22
+ Then you need to set up a nested resource in your routes.rb:
23
+ resources :articles do
24
+ resources :comments
25
+ end
26
+
27
+ And in your view file:
28
+ <%= link_to "Leave a comment", new_article_comment_path(:article_id => @article.id.to_s) %>
29
+
30
+ For a reply link add parent comment id:
31
+ <%= link_to "Reply", new_article_comment_path(:article_id => @article.id.to_s, :parent => comment.id.to_s) %>
32
+
33
+ If you want to make a custom views for a comments, run:
34
+ rails g mongoid_commentable:views
35
+
36
+ To get a list of all comments, use following method:
37
+ @article.comments_list
38
+ it will return comments in ascending order. To get comments in descending order use:
39
+ @article.comments_list(:sort => :desc)
40
+ NOTE: For descending ordered comments you need to add index to your model manually:
41
+ index [['comments', MONGO::DESCENDING]]
42
+
43
+ To get branch for a comment, use following method:
44
+ @article.branch_for(parent_comment_id)
45
+ it will return whole branch with all childrens for a certain comment
46
+
47
+ To get comment level, use following method:
48
+ comment.level
49
+
50
+ To mark comment as deleted, use following method:
51
+ @article.mark_comment_deleted(comment_id)
52
+
53
+ To get comments count, use:
54
+ @article.comments_count
55
+
56
+ == Contributing to mongoid_commentable
57
+
58
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
59
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
60
+ * Fork the project
61
+ * Start a feature/bugfix branch
62
+ * Commit and push until you are happy with your contribution
63
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
64
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
65
+
66
+ == Copyright
67
+
68
+ Copyright (c) 2011 Max Golovnia. See LICENSE.txt for
69
+ further details.
70
+
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ gem.name = "mongoid_commentable"
15
+ gem.homepage = "http://github.com/mgolovnia/mongoid_commentable"
16
+ gem.license = "MIT"
17
+ gem.summary = %Q{Comments for Mongoid documents}
18
+ gem.description = %Q{Mongoid_commentable provides methods to create commentable documents}
19
+ gem.email = "mgolovnia@gmail.com"
20
+ gem.authors = ["Max Golovnia"]
21
+ end
22
+ Jeweler::RubygemsDotOrgTasks.new
23
+
24
+ require 'rspec/core'
25
+ require 'rspec/core/rake_task'
26
+ RSpec::Core::RakeTask.new(:spec) do |spec|
27
+ spec.pattern = FileList['spec/**/*_spec.rb']
28
+ end
29
+
30
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
31
+ spec.pattern = 'spec/**/*_spec.rb'
32
+ spec.rcov = true
33
+ end
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "mongoid_commentable #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,76 @@
1
+ class CommentsController < ActionController::Base
2
+
3
+ prepend_before_filter :get_model
4
+ before_filter :get_comment, :only => [:show, :edit, :update]
5
+
6
+ respond_to :html
7
+
8
+ def index
9
+ @comments = @model.comments
10
+ respond_with([@model,@comments])
11
+ end
12
+
13
+ def show
14
+ respond_with([@model,@comment])
15
+ end
16
+
17
+ def new
18
+ respond_with([@model,@comment = Comment.new(:parent => params[:parent])])
19
+ end
20
+
21
+ def edit
22
+ respond_with([@model,@comment])
23
+ end
24
+
25
+ def create
26
+ @comment = @model.create_comment!(params[:comment])
27
+ if @comment.save
28
+ flash[:notice] = 'Comment was successfully created.'
29
+ else
30
+ flash[:error] = 'Comment wasn\'t created.'
31
+ end
32
+ respond_with(@model)
33
+ end
34
+
35
+ def update
36
+ if @comment.update_attributes(params[:comment])
37
+ flash[:notice] = 'Comment was successfully updated.'
38
+ else
39
+ flash[:error] = 'Comment wasn\'t deleted.'
40
+ end
41
+ respond_with([@model,@comment], :location => @model)
42
+ end
43
+
44
+ def destroy
45
+ @model.mark_deleted(params[:comment])
46
+ respond_with(@model)
47
+ end
48
+
49
+ private
50
+
51
+ def classname
52
+ params.each do |name, value|
53
+ if name =~ /(.+)_id$/
54
+ return $1.classify.constantize
55
+ end
56
+ end
57
+ end
58
+
59
+ def model_id
60
+ params.each do |name, value|
61
+ if name =~ /.+_id$/
62
+ return name
63
+ end
64
+ end
65
+ nil
66
+ end
67
+
68
+ def get_model
69
+ @model = classname.find(params[model_id.to_sym])
70
+ end
71
+
72
+ def get_comment
73
+ @comment = @model.comments.find(params[:id])
74
+ end
75
+
76
+ end
@@ -0,0 +1,17 @@
1
+ class Comment
2
+ include Mongoid::Document
3
+ attr_accessible :text, :author, :path, :parent
4
+ field :text, :type => String
5
+ field :author, :type => String
6
+ field :path, :type => String, :default => ""
7
+ field :deleted, :type => Boolean, :default => false
8
+ field :parent, :type => String
9
+ embedded_in :commentable, :polymorphic => true
10
+
11
+ def level
12
+ path.count('.')
13
+ end
14
+
15
+
16
+
17
+ end
@@ -0,0 +1,27 @@
1
+ <p>Leave a comment</p>
2
+ <%= form_for [model,comment] do |f| %>
3
+ <% if model.errors.any? %>
4
+ <div id="error_explanation">
5
+ <h2><%= pluralize(model.errors.count, "error") %> prohibited this comment from being saved:</h2>
6
+ <ul>
7
+ <% model.errors.full_messages.each do |msg| %>
8
+ <li><%= msg %></li>
9
+ <% end %>
10
+ </ul>
11
+ </div>
12
+ <% end %>
13
+ <% if comment.parent %>
14
+ <%= f.hidden_field :parent, :value => comment.parent %>
15
+ <% end %>
16
+ <div class="field">
17
+ <%= f.label :author %><br />
18
+ <%= f.text_field :author %>
19
+ </div>
20
+ <div class="field">
21
+ <%= f.label :text %><br />
22
+ <%= f.text_area :text %>
23
+ </div>
24
+ <div class="actions">
25
+ <%= f.submit %>
26
+ </div>
27
+ <% end %>
@@ -0,0 +1,3 @@
1
+ <h1>Update Comment</h1>
2
+
3
+ <%= render :partial => 'form', :locals => {:model => @model,:comment => @comment} %>
@@ -0,0 +1,9 @@
1
+ <% @model.comments_list.each do |comment| %>
2
+ <p>
3
+ <% if comment.deleted %>
4
+ <strong>DELETED</strong>
5
+ <% end %>
6
+ <%= comment.text%>
7
+ </p>
8
+ <% end %>
9
+
@@ -0,0 +1 @@
1
+ <%= render :partial => 'form', :locals => {:model => @model, :comment => @comment}%>
@@ -0,0 +1,6 @@
1
+ <p>
2
+ <%=@comment.author%>
3
+ </p>
4
+ <p>
5
+ <%=@comment.text%>
6
+ </p>
@@ -0,0 +1,15 @@
1
+ require 'tmpdir'
2
+ module Mongoid_Commentable
3
+ module Generators
4
+ class ViewsGenerator < Rails::Generators::Base
5
+ source_root File.expand_path("../../../../app/views", __FILE__)
6
+ desc "Copies all mongoid_commentable views to your application."
7
+ argument :scope, :required => false, :default => nil,
8
+ :desc => "The scope to copy views to"
9
+ def copy_views
10
+ directory "comments", "app/views/#{scope || :comments}"
11
+ end
12
+ end
13
+ end
14
+ end
15
+
@@ -0,0 +1,43 @@
1
+ module Mongoid::Commentable
2
+ extend ActiveSupport::Concern
3
+
4
+ included do |base|
5
+ base.embeds_many :comments, :as => :commentable
6
+ base.field :comments_count, :type => Integer, :default => 0
7
+ base.index [['comments', Mongo::ASCENDING]]
8
+ end
9
+
10
+ module ClassMethods
11
+
12
+ def commentable?
13
+ true
14
+ end
15
+
16
+ end
17
+
18
+
19
+ def create_comment!(params)
20
+ comment = comments.create!(params)
21
+ comment.path = comment.parent ? comments.find(comment.parent).path + '.' + comment.id.to_s : "root."+comment.id.to_s
22
+ self.inc(:comments_count,1)
23
+ comment
24
+ end
25
+
26
+ def comments_list(sort=:asc)
27
+ if Comment.respond_to?(sort)
28
+ comments.send(sort,:path)
29
+ else
30
+ raise ArgumentError, "Wrong argument!"
31
+ end
32
+ end
33
+
34
+ def branch_for(comment_id)
35
+ comments.select{|i| i.path =~ Regexp.new('^' + comments.find(comment_id).path)}
36
+ end
37
+
38
+ def mark_comment_deleted(comment_id)
39
+ comments.find(comment_id).deleted = true
40
+ self.inc(:comments_count,-1)
41
+ end
42
+
43
+ end
@@ -0,0 +1,9 @@
1
+ require File.join(File.dirname(__FILE__), 'mongoid/commentable')
2
+
3
+ %w{ models controllers }.each do |dir|
4
+ path = File.join(File.dirname(__FILE__), '../app', dir)
5
+ $LOAD_PATH << path
6
+ ActiveSupport::Dependencies.autoload_paths << path
7
+ ActiveSupport::Dependencies.autoload_once_paths.delete(path)
8
+ end
9
+
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+ class TestModel
3
+ include Mongoid::Document
4
+ include Mongoid::Commentable
5
+ end
6
+ describe 'Make commentable' do
7
+
8
+ let(:test_model){TestModel.create!}
9
+
10
+
11
+ it 'should be commentable' do
12
+ test_model.class.commentable?.should == true
13
+ end
14
+
15
+ it 'should allow to add a comment' do
16
+ comment = test_model.create_comment!(:author => 'Test author', :text => 'Hello, World!')
17
+ comment.author.should == 'Test author'
18
+ comment.text.should == 'Hello, World!'
19
+ comment.path.should == "root.#{comment.id.to_s}"
20
+ comment.deleted.should == false
21
+ test_model.comments_count == 1
22
+ end
23
+
24
+ it 'should allow to add a reply to a comment' do
25
+ comment = test_model.create_comment!(:author => 'Author', :text => 'Parent')
26
+ reply = test_model.create_comment!(:author => 'Author2', :text => 'Reply', :parent => comment.id.to_s)
27
+ test_model.comments.find(reply.id).path.should == "#{comment.path}.#{reply.id.to_s}"
28
+ end
29
+
30
+ it 'should mark comment as deleted' do
31
+ comment = test_model.create_comment!(:author => 'Author', :text => 'Parent')
32
+ test_model.comments_count == 1
33
+ test_model.mark_comment_deleted(comment.id)
34
+ test_model.comments.find(comment.id).deleted.should == true
35
+ test_model.comments_count == 0
36
+ end
37
+
38
+ it 'should return a branch of comments' do
39
+ parent1 = test_model.create_comment!(:author => 'Test author', :text => 'Parent1')
40
+ child1 = test_model.create_comment!(:author => 'Test author', :text => 'Child1', :parent => parent1.id.to_s)
41
+ child2 = test_model.create_comment!(:author => 'Test author', :text => 'Parent1', :parent => child1.id.to_s)
42
+ parent2 = test_model.create_comment!(:author => 'Test author', :text => 'Parent2')
43
+ branch = test_model.branch_for(parent1.id)
44
+ branch.should == [parent1,child1,child2]
45
+ end
46
+
47
+ it 'should return wrong argument error' do
48
+ expect{test_model.comments_list(:wrong_argument)}.to raise_error ArgumentError
49
+ end
50
+
51
+
52
+
53
+ end
@@ -0,0 +1,27 @@
1
+
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
3
+ require 'mongoid'
4
+ require 'rubygems'
5
+ require 'bundler'
6
+ require 'logger'
7
+ require 'rspec'
8
+ require 'database_cleaner'
9
+ require 'mongoid_commentable'
10
+
11
+
12
+ RSpec.configure do |config|
13
+ config.mock_with :rspec
14
+ config.before(:suite) do
15
+ DatabaseCleaner.strategy = :truncation
16
+ DatabaseCleaner.orm = "mongoid"
17
+ end
18
+
19
+ config.after(:each) do
20
+ DatabaseCleaner.clean
21
+ end
22
+ end
23
+
24
+ Mongoid.configure do |config|
25
+ config.master = Mongo::Connection.new.db("mongoid_commentable_test")
26
+ end
27
+
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid_commentable
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Max Golovnia
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-02 00:00:00 +03:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: mongoid
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: "2.0"
24
+ type: :runtime
25
+ prerelease: false
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: bson_ext
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: "1.3"
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: rspec
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 2.3.0
46
+ type: :development
47
+ prerelease: false
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: database_cleaner
51
+ requirement: &id004 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: *id004
60
+ - !ruby/object:Gem::Dependency
61
+ name: bundler
62
+ requirement: &id005 !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ~>
66
+ - !ruby/object:Gem::Version
67
+ version: 1.0.0
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: *id005
71
+ - !ruby/object:Gem::Dependency
72
+ name: jeweler
73
+ requirement: &id006 !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ~>
77
+ - !ruby/object:Gem::Version
78
+ version: 1.6.0
79
+ type: :development
80
+ prerelease: false
81
+ version_requirements: *id006
82
+ - !ruby/object:Gem::Dependency
83
+ name: rcov
84
+ requirement: &id007 !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: "0"
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: *id007
93
+ description: Mongoid_commentable provides methods to create commentable documents
94
+ email: mgolovnia@gmail.com
95
+ executables: []
96
+
97
+ extensions: []
98
+
99
+ extra_rdoc_files:
100
+ - LICENSE.txt
101
+ - README.rdoc
102
+ files:
103
+ - .document
104
+ - .rspec
105
+ - Gemfile
106
+ - Gemfile.lock
107
+ - LICENSE.txt
108
+ - README.rdoc
109
+ - Rakefile
110
+ - VERSION
111
+ - app/controllers/comments_controller.rb
112
+ - app/models/comment.rb
113
+ - app/views/comments/_form.html.erb
114
+ - app/views/comments/edit.html.erb
115
+ - app/views/comments/index.html.erb
116
+ - app/views/comments/new.html.erb
117
+ - app/views/comments/show.html.erb
118
+ - lib/generators/mongoid_commentable/views_generator.rb
119
+ - lib/mongoid/commentable.rb
120
+ - lib/mongoid_commentable.rb
121
+ - spec/mongoid/commentable_spec.rb
122
+ - spec/spec_helper.rb
123
+ has_rdoc: true
124
+ homepage: http://github.com/mgolovnia/mongoid_commentable
125
+ licenses:
126
+ - MIT
127
+ post_install_message:
128
+ rdoc_options: []
129
+
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ none: false
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ hash: 3250783174328786593
138
+ segments:
139
+ - 0
140
+ version: "0"
141
+ required_rubygems_version: !ruby/object:Gem::Requirement
142
+ none: false
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: "0"
147
+ requirements: []
148
+
149
+ rubyforge_project:
150
+ rubygems_version: 1.6.2
151
+ signing_key:
152
+ specification_version: 3
153
+ summary: Comments for Mongoid documents
154
+ test_files: []
155
+