peanut_gallery 0.0.0 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a93345b0d1dad83b29b6d1dfa7d1f6cc93fcae24
4
- data.tar.gz: 34303919819f201c163e3518db77e4bc10c17749
3
+ metadata.gz: 949795aa2df5a636b51139bb19efb2152842c876
4
+ data.tar.gz: eb9d7a8b4dd9dbbd285b34b97c57f829dd0f558d
5
5
  SHA512:
6
- metadata.gz: af8024053600cb2ec09fb6ca1af5ed1018c242e0eb2f7d498c9d1278fed5ada374ef74e88beaa74a2c999c4730f41ac667b2e23699f1a0cfa0dd08f4e4c6009f
7
- data.tar.gz: e999e8c7cef9286df8276cdece4a0940edca23b44c6547048ff85e1c139b68009338304aab9ff2d5af348f10274171ae0d4c4da3f1aac73b334100b02f42823b
6
+ metadata.gz: 3e05af60a7d21977e460cb47334370a353422c42d532d22307cc3fc2c2234493e84bab88dc6bb17f9a575c26f5f53608ded505b44deefe306c052b7f28c6198d
7
+ data.tar.gz: 9ef2e06176cb5c5b177f395a37b26838c22f94fe32e71f9f95f43dcbac3c3f54aebdfee0ebb90930c314ce1f2ca0bd68f981a48b438933ce005ee8f9bb130ace
data/README.md CHANGED
@@ -1,8 +1,13 @@
1
- # PeanutGallery
1
+ PeanutGallery
2
+ ==========
2
3
 
3
- TODO: Write a gem description
4
+ PeanutGallery adds a simple commenting feature to your Ruby on Rails
5
+ application.
4
6
 
5
- ## Installation
7
+ Installation
8
+ ----------
9
+
10
+ ### Add Gem To Project
6
11
 
7
12
  Add this line to your application's Gemfile:
8
13
 
@@ -12,17 +17,128 @@ gem 'peanut_gallery'
12
17
 
13
18
  And then execute:
14
19
 
15
- $ bundle
20
+ ```text
21
+ $ bundle
22
+ ```
16
23
 
17
24
  Or install it yourself as:
18
25
 
19
- $ gem install peanut_gallery
26
+ ```text
27
+ $ gem install peanut_gallery
28
+ ```
29
+
30
+ ### Add the Comment Model
31
+
32
+ Next, you'll want to add a `Comment` model to your application. You can do this
33
+ by running the install generator:
34
+
35
+ ```text
36
+ $ bundle exec rails g peanut_gallery:install
37
+ ```
38
+
39
+ This will add a migration, so you'll want to migrate your database.
40
+
41
+ ```text
42
+ $ bundle exec rake db:migrate
43
+ ```
44
+
45
+ > The install generator adds a `Comment` model to your application. You'll
46
+ > notice the model file has no content, but still works. That's because it is
47
+ > inheriting from the `PeanutGallery::Comment` model. This is so we can hide
48
+ > the abstracted (repeated) logic. But, you're welcome to override any default
49
+ > logic in your project's `Comment` model.
50
+ >
51
+ > *Note: PeanutGallery never accesses the base model by default. It will always
52
+ > look for the `Comment` model in your project.*
53
+
54
+ Usage
55
+ ----------
56
+
57
+ There are three items to be concerned with to get PeanutGallery working
58
+ properly:
59
+
60
+ 1. Routes
61
+ 2. Associations
62
+ 3. Forms
63
+
64
+ ### Routes
65
+
66
+ Running the install generator should add the following to your
67
+ `config/routes.rb` file:
68
+
69
+ ```ruby
70
+ namespace :peanut_gallery do
71
+ resources :comments, :only => [:create, :update, :destroy]
72
+ end
73
+ ```
74
+
75
+ If something went wrong and you don't see this, go ahead and add it manually.
76
+
77
+ ### Associations
78
+
79
+ For any model that you want to have comments, all you have to do is all
80
+ `has_comments` to the model. For example, if a `Post` model has comments, your
81
+ model might look like this:
82
+
83
+ ```ruby
84
+ class Post < ActiveRecord::Base
85
+ has_comments
86
+ end
87
+ ```
88
+
89
+ This means you can call `comments` on any single object within this model.
90
+
91
+ ### Forms
92
+
93
+ There is a helper made available to you for rendering a form:
94
+
95
+ ```erb
96
+ <%= new_comment_form(object, author) %>
97
+ ```
98
+
99
+ Here the `object` is the item to which you want to attach the comment. The
100
+ `author` is the object of the user who is adding the comment.
101
+
102
+ > *Note: PeanutGallery assumes (but does not require) you're using a `User`
103
+ > model, and that the logged in user is `current_user`. Therefore, you can
104
+ > leave off the `author` parameter if you have your current user wrapped up in
105
+ > a `current_user` method.
106
+
107
+ Helpers
108
+ ----------
109
+
110
+ ### Deleting Comments
111
+
112
+ In addition to the forms helper, there is also a helper to delete a comment.
113
+
114
+ ```erb
115
+ <%= delete_comment_link(comment_object) %>
116
+ ```
117
+
118
+ Of course, you can write this manually (or add your own helper) if you'd like
119
+ to.
120
+
121
+ Customization
122
+ ----------
123
+
124
+ ### Overriding A User Model
125
+
126
+ PeanutGallery assumes the use of a user model. If you want to override that, you'll need to add the association into your `Comment` model:
127
+
128
+ ```ruby
129
+ class Comment < ActiveRecord::Base
130
+ belongs_to :author, :class_name => 'YourUserClassName'
131
+ end
132
+ ```
20
133
 
21
- ## Usage
134
+ ### Controller Actions
22
135
 
23
- TODO: Write usage instructions here
136
+ The controller doing all the work for PeanutGallery is the
137
+ `PeanutGallery::CommentsController`. If you want to override it, you can create
138
+ a controller at `app/controllers/peanut_gallery/comments_controller.rb`.
24
139
 
25
- ## Contributing
140
+ Contributing
141
+ ----------
26
142
 
27
143
  1. Fork it ( https://github.com/[my-github-username]/peanut_gallery/fork )
28
144
  2. Create your feature branch (`git checkout -b my-new-feature`)
@@ -0,0 +1,45 @@
1
+ class PeanutGallery::CommentsController < ApplicationController
2
+
3
+ def create
4
+ @comment = Comment.new(comment_params)
5
+ if @comment.save
6
+ redirect_to params[:peanut_gallery_comment][:redirect_route],
7
+ :notice => 'Comment created!'
8
+ else
9
+ redirect_to params[:peanut_gallery_comment][:redirect_route],
10
+ :notice => 'Comment could not be saved.'
11
+ end
12
+ end
13
+
14
+ def update
15
+ @comment = Comment.find_by_id(params[:id])
16
+ if @comment.nil?
17
+ raise "Could not find comment."
18
+ else
19
+ if @comment.save
20
+ redirect_to request.referrer, :notice => 'Comment saved!'
21
+ else
22
+ redirect_to request.referrer, :notice => 'Comment could not be saved.'
23
+ end
24
+ end
25
+ end
26
+
27
+ def destroy
28
+ @comment = Comment.find_by_id(params[:id])
29
+ if @comment.nil?
30
+ raise "Could not find comment."
31
+ else
32
+ @comment.destroy
33
+ redirect_to request.referrer, :notice => 'Comment deleted!'
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def comment_params
40
+ params
41
+ .require(:peanut_gallery_comment)
42
+ .permit(:markdown, :item_id, :item_type, :author_id)
43
+ end
44
+
45
+ end
@@ -0,0 +1,27 @@
1
+ module PeanutGallery
2
+ module CommentsHelper
3
+
4
+ def new_comment_form(obj, author = current_user)
5
+ @comment = Comment.new if @comment.nil?
6
+ simple_form_for @comment do |f|
7
+ o = f.input :markdown, :label => false,
8
+ :placeholder => 'Add a comment ...'
9
+ o += f.input :item_id, :as => :hidden,
10
+ :input_html => { :value => obj.id }
11
+ o += f.input :item_type, :as => :hidden,
12
+ :input_html => { :value => obj.class.to_s }
13
+ o += f.input :author_id, :as => :hidden,
14
+ :input_html => { :value => author.id }
15
+ o += f.input :redirect_route, :as => :hidden,
16
+ :input_html => { :value => request.path }
17
+ o += f.submit "Add Comment"
18
+ end
19
+ end
20
+
21
+ def delete_comment_link(comment)
22
+ link_to 'Delete Comment', peanut_gallery_comment_path(comment),
23
+ :method => :delete, :data => { :confirm => 'Are you sure?' }
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,15 @@
1
+ module PeanutGallery
2
+ class Comment < ActiveRecord::Base
3
+
4
+ converts_markdown :markdown, :html
5
+
6
+ belongs_to :item, :polymorphic => true
7
+ belongs_to :author, :class_name => 'User'
8
+
9
+ default_scope { includes(:item, :author) }
10
+
11
+ scope :newest_first, -> { order('created_at desc') }
12
+ scope :oldest_first, -> { order('created_at asc') }
13
+
14
+ end
15
+ end
@@ -0,0 +1,6 @@
1
+ <article class="comment">
2
+ <%= comment.html.html_safe %>
3
+ <%= content_tag :span, "by #{comment.author.name}", :class => 'author' %>
4
+ <%= content_tag :span, "#{time_ago_in_words(comment.created_at)} ago",
5
+ :class => 'date' %>
6
+ </article>
@@ -0,0 +1,29 @@
1
+ require 'rake'
2
+ require 'rails/generators'
3
+
4
+ module PeanutGallery
5
+ class InstallGenerator < Rails::Generators::Base
6
+ desc "Add comment model migration to your project"
7
+
8
+ source_root File.expand_path('../templates', __FILE__)
9
+
10
+ def add_comment_model
11
+ cols = "item_id:integer item_type:string author_id:integer "
12
+ cols += "markdown:text html:text"
13
+ generate "model comment #{cols}"
14
+ end
15
+
16
+ def change_model_inheritance
17
+ gsub_file 'app/models/comment.rb', /ActiveRecord\:\:Base/,
18
+ 'PeanutGallery::Comment'
19
+ end
20
+
21
+ def add_routes
22
+ routes = "\n namespace :peanut_gallery do\n"
23
+ routes += " resources :comments, :only => [:create, :update, :destroy]\n"
24
+ routes += " end\n"
25
+ inject_into_file 'config/routes.rb', routes,
26
+ :after => /Rails\.application\.routes\.draw\ do\n/
27
+ end
28
+ end
29
+ end
@@ -1,5 +1,9 @@
1
+ require 'peanut_gallery/engine'
2
+ require "peanut_gallery/has_comments"
1
3
  require "peanut_gallery/version"
2
4
 
5
+ require 'mark_it_zero'
6
+ require 'simple_form'
7
+
3
8
  module PeanutGallery
4
- # Your code goes here...
5
9
  end
@@ -0,0 +1,4 @@
1
+ module PeanutGallery
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,7 @@
1
+ class << ActiveRecord::Base
2
+
3
+ def has_comments
4
+ has_many :comments, :as => :item
5
+ end
6
+
7
+ end
@@ -1,3 +1,3 @@
1
1
  module PeanutGallery
2
- VERSION = "0.0.0"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -20,4 +20,7 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.7"
22
22
  spec.add_development_dependency "rake", "~> 10.0"
23
+
24
+ spec.add_dependency 'mark_it_zero', "~> 0.3.1"
25
+ spec.add_dependency 'simple_form'
23
26
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: peanut_gallery
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean C Davis
@@ -38,6 +38,34 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mark_it_zero
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.3.1
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.3.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: simple_form
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
41
69
  description: ''
42
70
  email:
43
71
  - scdavis41@gmail.com
@@ -50,7 +78,14 @@ files:
50
78
  - LICENSE.txt
51
79
  - README.md
52
80
  - Rakefile
81
+ - app/controllers/peanut_gallery/comments_controller.rb
82
+ - app/helpers/peanut_gallery/comments_helper.rb
83
+ - app/models/peanut_gallery/comment.rb
84
+ - app/views/comments/_comment.html.erb
85
+ - lib/generators/peanut_gallery/install_generator.rb
53
86
  - lib/peanut_gallery.rb
87
+ - lib/peanut_gallery/engine.rb
88
+ - lib/peanut_gallery/has_comments.rb
54
89
  - lib/peanut_gallery/version.rb
55
90
  - peanut_gallery.gemspec
56
91
  homepage: ''