commenter_mongoid 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'rails', '3.1.0.rc1'
4
+ gem 'rake', '0.8.7'
5
+
6
+ gem 'bson_ext'
7
+ gem 'mongoid', '2.0.2'
8
+ gem 'devise'
9
+ gem 'inherited_resources'
10
+
11
+ group :development, :test do
12
+ gem 'jeweler'
13
+ end
14
+
@@ -0,0 +1,116 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ actionmailer (3.1.0.rc1)
5
+ actionpack (= 3.1.0.rc1)
6
+ mail (~> 2.3.0)
7
+ actionpack (3.1.0.rc1)
8
+ activemodel (= 3.1.0.rc1)
9
+ activesupport (= 3.1.0.rc1)
10
+ builder (~> 3.0.0)
11
+ erubis (~> 2.7.0)
12
+ i18n (~> 0.6.0beta1)
13
+ rack (~> 1.3.0.beta2)
14
+ rack-cache (~> 1.0.1)
15
+ rack-mount (~> 0.8.1)
16
+ rack-test (~> 0.6.0)
17
+ sprockets (~> 2.0.0.beta.5)
18
+ tzinfo (~> 0.3.27)
19
+ activemodel (3.1.0.rc1)
20
+ activesupport (= 3.1.0.rc1)
21
+ bcrypt-ruby (~> 2.1.4)
22
+ builder (~> 3.0.0)
23
+ i18n (~> 0.6.0beta1)
24
+ activerecord (3.1.0.rc1)
25
+ activemodel (= 3.1.0.rc1)
26
+ activesupport (= 3.1.0.rc1)
27
+ arel (~> 2.1.1)
28
+ tzinfo (~> 0.3.27)
29
+ activeresource (3.1.0.rc1)
30
+ activemodel (= 3.1.0.rc1)
31
+ activesupport (= 3.1.0.rc1)
32
+ activesupport (3.1.0.rc1)
33
+ multi_json (~> 1.0)
34
+ arel (2.1.1)
35
+ bcrypt-ruby (2.1.4)
36
+ bson (1.3.1)
37
+ bson_ext (1.3.1)
38
+ builder (3.0.0)
39
+ devise (1.3.4)
40
+ bcrypt-ruby (~> 2.1.2)
41
+ orm_adapter (~> 0.0.3)
42
+ warden (~> 1.0.3)
43
+ erubis (2.7.0)
44
+ git (1.2.5)
45
+ has_scope (0.5.0)
46
+ hike (1.0.0)
47
+ i18n (0.6.0)
48
+ inherited_resources (1.2.2)
49
+ has_scope (~> 0.5.0)
50
+ responders (~> 0.6.0)
51
+ jeweler (1.6.2)
52
+ bundler (~> 1.0)
53
+ git (>= 1.2.5)
54
+ rake
55
+ mail (2.3.0)
56
+ i18n (>= 0.4.0)
57
+ mime-types (~> 1.16)
58
+ treetop (~> 1.4.8)
59
+ mime-types (1.16)
60
+ mongo (1.3.1)
61
+ bson (>= 1.3.1)
62
+ mongoid (2.0.2)
63
+ activemodel (~> 3.0)
64
+ mongo (~> 1.3)
65
+ tzinfo (~> 0.3.22)
66
+ multi_json (1.0.3)
67
+ orm_adapter (0.0.5)
68
+ polyglot (0.3.1)
69
+ rack (1.3.0)
70
+ rack-cache (1.0.2)
71
+ rack (>= 0.4)
72
+ rack-mount (0.8.1)
73
+ rack (>= 1.0.0)
74
+ rack-ssl (1.3.2)
75
+ rack
76
+ rack-test (0.6.0)
77
+ rack (>= 1.0)
78
+ rails (3.1.0.rc1)
79
+ actionmailer (= 3.1.0.rc1)
80
+ actionpack (= 3.1.0.rc1)
81
+ activerecord (= 3.1.0.rc1)
82
+ activeresource (= 3.1.0.rc1)
83
+ activesupport (= 3.1.0.rc1)
84
+ bundler (~> 1.0)
85
+ railties (= 3.1.0.rc1)
86
+ railties (3.1.0.rc1)
87
+ actionpack (= 3.1.0.rc1)
88
+ activesupport (= 3.1.0.rc1)
89
+ rack-ssl (~> 1.3.2)
90
+ rake (>= 0.8.7)
91
+ thor (~> 0.14.6)
92
+ rake (0.8.7)
93
+ responders (0.6.4)
94
+ sprockets (2.0.0.beta.8)
95
+ hike (~> 1.0)
96
+ rack (~> 1.0)
97
+ tilt (!= 1.3.0, ~> 1.1)
98
+ thor (0.14.6)
99
+ tilt (1.3.1)
100
+ treetop (1.4.9)
101
+ polyglot (>= 0.3.1)
102
+ tzinfo (0.3.27)
103
+ warden (1.0.4)
104
+ rack (>= 1.0)
105
+
106
+ PLATFORMS
107
+ ruby
108
+
109
+ DEPENDENCIES
110
+ bson_ext
111
+ devise
112
+ inherited_resources
113
+ jeweler
114
+ mongoid (= 2.0.2)
115
+ rails (= 3.1.0.rc1)
116
+ rake (= 0.8.7)
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Tyler Gannon
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.
@@ -0,0 +1,27 @@
1
+ = commenter
2
+
3
+ Adds basic commentability to any model.
4
+
5
+ == Installation
6
+
7
+ * Add to the Gemfile:
8
+ gem 'commenter'
9
+
10
+ * Add a call to the helper into the view for your model:
11
+ = commentable :resource => @my_resource
12
+
13
+ == Contributing to commenter
14
+
15
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
16
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
17
+ * Fork the project
18
+ * Start a feature/bugfix branch
19
+ * Commit and push until you are happy with your contribution
20
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
21
+ * 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.
22
+
23
+ == Copyright
24
+
25
+ Copyright (c) 2011 Tyler Gannon. See LICENSE.txt for
26
+ further details.
27
+
@@ -0,0 +1,41 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "commenter_mongoid"
18
+ gem.homepage = "http://github.com/tylergannon/commenter_mongoid"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{A very simple rails comment engine designed for mongoid.}
21
+ gem.description = %Q{
22
+ Handles adding comments to models in a very simple way.
23
+ }
24
+ gem.email = "tgannon@gmail.com"
25
+ gem.authors = ["Tyler Gannon"]
26
+ gem.files = Dir["{app}/**/*", "{lib}/**/*", "{vendor}/**/*", "Gemfile*",
27
+ "config.ru", "Rakefile", "README.rdoc", "commenter_mongoid.gemspec"]
28
+ end
29
+ Jeweler::RubygemsDotOrgTasks.new
30
+
31
+ require 'rake/rdoctask'
32
+ Rake::RDocTask.new do |rdoc|
33
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
34
+
35
+ rdoc.rdoc_dir = 'rdoc'
36
+ rdoc.title = "commenter_mongoid #{version}"
37
+ rdoc.rdoc_files.include('README*', 'LICENSE.txt', 'Gemfile*', 'Rakefile')
38
+ rdoc.rdoc_files.include('lib/**/*.rb')
39
+ rdoc.rdoc_files.include('app/**/*.rb')
40
+ end
41
+
@@ -0,0 +1,59 @@
1
+ class CommentsController < ApplicationController
2
+ respond_to :html, :js, :json, :xml, :except => [:index, :show, :edit]
3
+ before_filter :new_comment, :only => [:create]
4
+ before_filter :load_comment, :only => [:update, :destroy]
5
+ authorize_resource :comment, :only => [:create, :update, :destroy]
6
+
7
+ helper_method :container_id
8
+
9
+ def create
10
+ @comment.user_id = current_user.id
11
+ @comment.date_created = DateTime.now
12
+ if @comment.save
13
+ respond_with @comment do |format|
14
+ format.html { redirect_to resource }
15
+ end
16
+ end
17
+ end
18
+
19
+ def destroy
20
+ @comment.destroy
21
+ respond_with @comment do |format|
22
+ format.html { redirect_to resource }
23
+ end
24
+ end
25
+
26
+ def resource
27
+ @resource ||= @comment.resource
28
+ end
29
+
30
+ protected
31
+ def new_comment
32
+ @comment = Comment.new params[:comment]
33
+ end
34
+ def load_comment
35
+ @comment = Comment.find params[:id]
36
+ end
37
+
38
+ def resource_class
39
+ @resource_class ||= params[:resource_class].camelize.constantize unless params[:resource_class].blank?
40
+ end
41
+
42
+ def resource_id
43
+ @resource_id ||= params[:resource_id] unless params[:resource_id].blank?
44
+ end
45
+ def container_id
46
+ @container_id ||= lambda{
47
+ resource_type = self.class.to_s.underscore.split('_')[0].singularize
48
+ if params[:container_id]
49
+ params[:container_id]
50
+ elsif params[:id]
51
+ "#{resource_type}_#{params[:id]}"
52
+ else
53
+ raise "I don't know"
54
+ end
55
+ }.call
56
+ end
57
+
58
+
59
+ end
@@ -0,0 +1,24 @@
1
+ class Comment
2
+ include Mongoid::Document
3
+ belongs_to :user
4
+ field :text, :type => String
5
+ field :resource_class, :type => String
6
+ field :resource_id, :type => String
7
+ field :date_created, :type => DateTime
8
+
9
+ validates :text, :presence => true
10
+ validates :resource_class, :presence => true
11
+ validates :resource_id, :presence => true
12
+ validates :date_created, :presence => true
13
+
14
+ default_scope order_by([:date_created, :asc])
15
+
16
+ scope :find_by_resource_class_and_id, lambda{|resource_class, resource_id|
17
+ where(:resource_id => resource_id,
18
+ :resource_class => resource_class.to_s)
19
+ }
20
+
21
+ def resource
22
+ @resource ||= self.resource_class.camelize.constantize.find(self.resource_id) unless resource_class.blank? or resource_id.blank?
23
+ end
24
+ end
@@ -0,0 +1,6 @@
1
+ %div[resource, :comments]
2
+ - for comment in comments
3
+ = render :partial => 'comments/show', :locals => {:comment => comment}
4
+ - if can? :create, new_comment
5
+ = render :partial => 'comments/new', :locals => {:new_comment => new_comment}
6
+
@@ -0,0 +1,8 @@
1
+ %div[new_comment]
2
+ = form_for new_comment, :remote => true do |f|
3
+ = hidden_field_tag :container_id, "comment_#{new_comment.id}"
4
+ = f.text_area :text
5
+ = f.hidden_field :resource_class
6
+ = f.hidden_field :resource_id
7
+ = f.submit 'Add comment'
8
+
@@ -0,0 +1,6 @@
1
+ %div[comment]
2
+ .comment_header By #{comment.user.email} on #{comment.date_created}
3
+ .comment_text= comment.text.html_safe
4
+ - if can? :destroy, comment
5
+ = link_to 'Delete', comment, :confirm => 'Are you sure?', :method => :delete, :remote => true
6
+
@@ -0,0 +1,2 @@
1
+ $('#<%= container_id %>').before('<%= raw escape_javascript(render(:partial=>'show', :locals => {:comment=>@comment})) %>');
2
+
@@ -0,0 +1,2 @@
1
+ $('#<%= container_id %>').remove();
2
+
@@ -0,0 +1,17 @@
1
+ require 'commenter/commentable'
2
+ require 'commenter/controller_extensions'
3
+ require 'commenter/helpers'
4
+
5
+ module Commenter
6
+ class Engine < ::Rails::Engine
7
+ initializer 'commenter' do |app|
8
+ unless defined? ::Mongoid
9
+ raise "I only work with Mongoid."
10
+ end
11
+
12
+ ApplicationController.send :helper, Commenter::Helpers
13
+ ApplicationController.send :include, Commenter::ControllerExtensions
14
+ CommentsController.send :include, Commenter::ControllerExtensions
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,12 @@
1
+ module Commenter
2
+ module Commentable
3
+ extend ActiveSupport::Concern
4
+
5
+ module InstanceMethods
6
+ def commentable(params={})
7
+ resource = params[:resource]
8
+
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,40 @@
1
+ module Commenter
2
+ module ControllerExtensions
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ helper_method :container_id
7
+ alias_method_chain :render, :responses
8
+ end
9
+
10
+ module ClassMethods
11
+ def js_responses params={}
12
+ @js_responses = params
13
+ puts "Laggerbagger \n|\n|\n|\n|\n|\n|\n|\n|\n|\n|\n|\n|"
14
+ end
15
+ end
16
+
17
+ module InstanceMethods
18
+ def container_id
19
+ @container_id ||= lambda{
20
+ resource_type = self.class.to_s.underscore.split('_')[0].singularize
21
+ if params[:container_id]
22
+ params[:container_id]
23
+ elsif params[:id]
24
+ "#{resource_type}_#{params[:id]}"
25
+ else
26
+ raise "I don't know"
27
+ end
28
+ }.call
29
+ end
30
+
31
+ def render_with_responses params={}
32
+ if params.blank? and params[:format] == :js and @js_responses
33
+ params[:layout] ||= "responses/#{params[:action]}"
34
+ puts "Juggerbugger \n$\n$\n$\n$\n$\n$\n$\n$\n$\n$\n$"
35
+ end
36
+ render_without_responses params
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,21 @@
1
+ module Commenter
2
+ module Helpers
3
+ def commentable(params={})
4
+ if res=params[:resource]
5
+ id = res.id
6
+ klass = res.class.to_s
7
+ else
8
+ raise "invalid parameters"
9
+ end
10
+ comments = Comment.where(:resource_class => klass, :resource_id => id)
11
+ new_comment = Comment.new(:resource_class => klass, :resource_id => id)
12
+ locals = {:comments => comments,
13
+ :new_comment => new_comment,
14
+ :resource => res}
15
+ html = render(:partial => 'comments/index', :locals => locals)
16
+ html.html_safe
17
+ end
18
+ end
19
+ end
20
+
21
+
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: commenter_mongoid
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Tyler Gannon
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-07 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rails
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - "="
23
+ - !ruby/object:Gem::Version
24
+ version: 3.1.0.rc1
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - "="
34
+ - !ruby/object:Gem::Version
35
+ version: 0.8.7
36
+ type: :runtime
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: bson_ext
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ type: :runtime
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: mongoid
51
+ prerelease: false
52
+ requirement: &id004 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - "="
56
+ - !ruby/object:Gem::Version
57
+ version: 2.0.2
58
+ type: :runtime
59
+ version_requirements: *id004
60
+ - !ruby/object:Gem::Dependency
61
+ name: devise
62
+ prerelease: false
63
+ requirement: &id005 !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ type: :runtime
70
+ version_requirements: *id005
71
+ - !ruby/object:Gem::Dependency
72
+ name: inherited_resources
73
+ prerelease: false
74
+ requirement: &id006 !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: "0"
80
+ type: :runtime
81
+ version_requirements: *id006
82
+ - !ruby/object:Gem::Dependency
83
+ name: jeweler
84
+ prerelease: false
85
+ requirement: &id007 !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: "0"
91
+ type: :development
92
+ version_requirements: *id007
93
+ description: "\n Handles adding comments to models in a very simple way.\n "
94
+ email: tgannon@gmail.com
95
+ executables: []
96
+
97
+ extensions: []
98
+
99
+ extra_rdoc_files:
100
+ - LICENSE.txt
101
+ - README.rdoc
102
+ files:
103
+ - Gemfile
104
+ - Gemfile.lock
105
+ - README.rdoc
106
+ - Rakefile
107
+ - app/controllers/comments_controller.rb
108
+ - app/models/comment.rb
109
+ - app/views/comments/_index.html.haml
110
+ - app/views/comments/_new.html.haml
111
+ - app/views/comments/_show.html.haml
112
+ - app/views/comments/create.js.erb
113
+ - app/views/comments/destroy.js.erb
114
+ - lib/commenter.rb
115
+ - lib/commenter/commentable.rb
116
+ - lib/commenter/controller_extensions.rb
117
+ - lib/commenter/helpers.rb
118
+ - LICENSE.txt
119
+ has_rdoc: true
120
+ homepage: http://github.com/tylergannon/commenter_mongoid
121
+ licenses:
122
+ - MIT
123
+ post_install_message:
124
+ rdoc_options: []
125
+
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: "0"
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ none: false
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: "0"
140
+ requirements: []
141
+
142
+ rubyforge_project:
143
+ rubygems_version: 1.6.2
144
+ signing_key:
145
+ specification_version: 3
146
+ summary: A very simple rails comment engine designed for mongoid.
147
+ test_files: []
148
+