GithubHelp home page GithubHelp logo

lml / commontator Goto Github PK

View Code? Open in Web Editor NEW
352.0 13.0 100.0 958 KB

A Rails engine for comments

License: MIT License

CSS 0.32% Ruby 83.41% HTML 10.32% JavaScript 4.41% SCSS 1.53%
comment-threads rails-engine jquery-ujs

commontator's Introduction

Commontator

Gem Version Tests Code Climate Code Coverage

Commontator is a Rails engine for comments. It is compatible with Rails 5.2+. Being an engine means it is fully functional as soon as you install and configure the gem, providing models, views and controllers of its own. At the same time, almost anything about it can be configured or customized to suit your needs.

Installation

Follow the steps below to install Commontator:

  1. Gem

Add this line to your application's Gemfile:

gem 'commontator'

You will also need jquery and a sass compiler, which can be either be installed through the webpacker gem and yarn/npm/bower or through the jquery-rails and sass[c]-rails gems:

gem 'jquery-rails'
gem 'sassc-rails'

Then execute:

$ bundle install
  1. Initializer and Migrations

Run the following command to copy Commontator's initializer and migrations to your app:

$ rake commontator:install

Or alternatively:

$ rake commontator:install:initializers

$ rake commontator:install:migrations

And then execute:

$ rails db:migrate
  1. Configuration

Change Commontator's configurations to suit your needs by editing config/initializers/commontator.rb. Make sure to check that your configuration file is up to date every time you update the gem, as available options can change with each minor version. If you have deprecated options in your initializer, Commontator will issue warnings (usually printed to your console).

Commontator relies on Rails's sanitize helper method to sanitize user input before display. The default allowed tags and attributes are very permissive, basically only blocking tags, attributes and attribute values that could be used for XSS. Read more about configuring the Rails sanitize helper..

  1. Routes

Add this line to your Rails application's routes.rb file:

mount Commontator::Engine => '/commontator'

You can change the mount path if you would like a different one.

Assets

  1. Javascripts

Make sure your application.js requires jquery and rails-ujs or jquery-ujs:

Rails 5.1+:

//= require jquery
//= require rails-ujs

Rails 5.0:

//= require jquery
// If jquery-ujs was installed through jquery-rails
//= require jquery_ujs
// If jquery-ujs was installed through webpacker and yarn/npm/bower
//= require jquery-ujs

If using Commontator's mentions functionality, also require Commontator's application.js:

//= require commontator/application
  1. Stylesheets

In order to display comment threads properly, you must require Commontator's application.scss in your application.[s]css:

*= require commontator/application

Sprockets 4+

You must require Commontator's manifest.js in your app's manifest.js for images to work properly:

//= link commontator/manifest.js

You also need to either add the necessary link tag commands to your layout to load commontator/application.js and commontator/application.css or require them in your app's application.js and application.css like in Sprockets 3.

Usage

Follow the steps below to add Commontator to your models and views:

  1. Models

Add this line to your user model(s) (or any models that should be able to post comments):

acts_as_commontator

Add this line to any models you want to be able to comment on (i.e. models that have comment threads):

acts_as_commontable

if you want the thread and all its comments removed when your commontable model is destroyed pass :destroy as the :dependent option toacts_as_commontable:

acts_as_commontable dependent: :destroy

instead of :destroy you may use any other supported :dependent option from rails has_one association.

  1. Views

In the following instructions, @commontable is an instance of a model that acts_as_commontable. You must supply this variable to the views that will use Commontator.

Wherever you would like to display comments, call commontator_thread(@commontable):

<%= commontator_thread(@commontable) %>

This will create a link that can be clicked to display the comment thread.

Note that model's record must already exist in the database, so do not use this in new.html.erb, _form.html.erb or similar views. We recommend you use this in the model's show.html.erb view or the equivalent for your app.

  1. Controllers

By default, the commontator_thread method only provides a link to the desired comment thread. Sometimes it may be desirable to have the thread display right away when the corresponding page is loaded. In that case, just add the following method call to the controller action that displays the page in question:

commontator_thread_show(@commontable)

Note that the call to commontator_thread in the view is still necessary in either case.

The commontator_thread_show method checks the current user's read permission on the thread and will display the thread if the user is allowed to read it, according to the options in the initializer.

That's it! Commontator is now ready for use.

Emails

When you enable subscriptions, emails are sent automatically by Commontator. If sending emails, remember to add your host URL's to your environment files (test.rb, development.rb and production.rb):

config.action_mailer.default_url_options = { host: "https://www.example.com" }

Sometimes you may need to subscribe (commontator) users automatically when some event happens. You can call object.commontator_thread.subscribe(user) to subscribe users programmatically. Batch sending through Mailgun is also supported and automatically detected. Read the Customization section to see how to customize subscription emails.

Voting

You can allow users to vote on each others' comments by adding the acts_as_votable gem to your Gemfile:

gem 'acts_as_votable'

And enabling the relevant option in Commontator's initializer:

config.comment_voting = :ld # See the initializer for available options

Mentions

You can allow users to mention other users in the comments. Mentioned users are automatically subscribed to the thread and receive email notifications.

First make sure you required Commontator's application.js in your application.js as explained in the Javascripts section. Then enable mentions in Commontator's initializer:

config.mentions_enabled = true

Finally configure the user_mentions_proc, which receives the current user, the current thread, and the search query inputted by that user and should return a relation containing the users that can be mentioned and match the query string:

config.user_mentions_proc = ->(current_user, thread, query) { ... }

Please be aware that with mentions enabled, any registered user can use the user_mentions_proc to search for other users. Make sure to properly escape SQL in this proc and do not allow searches on sensitive fields.

Use '@' with at least three other characters to mention someone in a new/edited comment.

The mentions script assumes that Commontator is mounted at /commontator, so make sure that is indeed the case if you plan to use mentions.

Browser Support

Commontator should work properly on any of the major browsers. The mentions functionality won't work with IE before version 8. To function properly, this gem requires that visitors to the site have javascript enabled.

Customization

Copy Commontator's files to your app using any of the following commands:

$ rake commontator:copy:locales

$ rake commontator:copy:images
$ rake commontator:copy:javascripts
$ rake commontator:copy:stylesheets

$ rake commontator:copy:views
$ rake commontator:copy:helpers

$ rake commontator:copy:controllers
$ rake commontator:copy:mailers

$ rake commontator:copy:models

You are now free to modify them and have any changes made manifest in your application. You can safely remove files you do not want to customize.

You can customize subscription emails (mailer views) with rake commontator:copy:views.

If copying Commontator's locales, please note that by default Rails will not autoload locales in subfolders of config/locales (like ours) unless you add the following to your application.rb:

config.i18n.load_path += Dir[root.join('config', 'locales', '**', '*.yml')]

Contributing

  1. Fork the lml/commontator repo on Github
  2. Create a feature or bugfix branch (git checkout -b my-new-feature)
  3. Write tests for the feature/bugfix
  4. Implement the new feature/bugfix
  5. Make sure both new and old tests pass (rake)
  6. Commit your changes (git commit -am 'Added some feature')
  7. Push the branch (git push origin my-new-feature)
  8. Create a new Pull Request to lml/commontator on Github

Development Environment Setup

  1. Use bundler to install all dependencies:
$ bundle install
  1. Setup the database:
$ rails db:setup

Testing

To run all existing tests for Commontator, simply execute the following from the main folder:

$ rake

License

This gem is distributed under the terms of the MIT license. See the MIT-LICENSE file for details.

commontator's People

Contributors

akz92 avatar alaarab avatar andreibondarev avatar atrox avatar bolandrm avatar cpg avatar cyclingzealot avatar d4rky-pl avatar danmaz74 avatar dantemss avatar felipekk avatar fosterfarrell9 avatar fourcolors avatar g8d3 avatar gambala avatar imgarylai avatar jdugarte avatar jpslav avatar ledzep443 avatar mpakus avatar mulander avatar nimir avatar pustomytnyk avatar stormmaster42 avatar tbprojects avatar triptate avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

commontator's Issues

2 Questions: pagination and date format

Hi:

I have 2 questions: when I don't use commontator_thread_show at the controller the pagination works fine, but when I use commontator_thread_show I can't change the pages, don't know why,

The second: how can I change the format of the date? (posted at abr 17 2014 at 05:32PM UTC)

Thanks a lot

undefined method `thread' for nil:NilClass

I just installed and I followed all of the instructions, but I'm getting this error. Here is the trace: app/controllers/posts_controller.rb:19:in `index'

here is my code that I added manually:

class PostsController < ApplicationController

before_action :authenticate_user!

def index
@posts = Post.includes(:user).all.sort_by &:created_at
@posts.reverse!
commontator_thread_show(@post)

end

def show
@post = Post.find(params[:id])
commontator_thread_show(@post)
end

Post and user models have acts_as_commontable and acts_as_commontator respectively.

my view (index.html.erb):

<%= render "global/new_post_form" %>

<% @posts.each do |post| %>

<%= post.user.name %>

<%= post.post_content %>

<%= commontator_thread(@post) %>

<% end %>

Thanks for any help!

undefined method `current_user'

undefined method `current_user' for #ProductsController:0xc73cce0
using the method:
def show
commontator_thread_show(@Product)
end

stops in
config/initializers/commontator.rb:15:in `block (2 levels) in <top (required)>'

the line
config.current_user_proc = lambda { |controller| controller.current_user }

New Comment

Hello, when I click on make a new comment, a form pops as usual. However, whenever I go to submit a new comment it says "comment could not be posted because the comment is a duplicate of another comment." Then when I go back to look the comment saves for some reason, but when i click on show comment it doesn't load.

Ordering depends on database implementation?

Hi,
I'm having a problem ordering the comments "earliest first". The gem does respects ordering by highest voted first in my app. Whenever I use config.comment_order = :ve (or :e), the comments with the same quantity of votes are still ordered with the most recently updated comment first. After going through the code, checking the method ordered_comments in https://github.com/lml/commontator/blob/master/app/models/commontator/thread.rb, I don't see:
a) code for handling :e within the case block;
b) a default ordering scope.

This means that when comment_order is either :e or :ve, the comments (with the same amount of votes for :ve) are going to presented in whatever order the database fetches them.

This leads me to believe that the gem, especially when config.comment_order = :e, is relying on the default ordering of the database, which I believe is a bad practice.

I'm using PostgreSQL on a Mac btw.

pagination

Any plans to add a pagination feature? If not I'm going to take a look at doing this myself in a fork.

undefined local variable or method `commontator'

Hi,
I've installed the gem according to your instructions, including the acts_as_votable (+migrations) on my Rails 4.1.4 app. I have a model User that has acts_as_commontator. I have a model Recipe that has acts_as_commontable. On my view for RecipesController#show (that doesn't require a signed in user, but I've tested both ways anyway) I have <%= commontator_thread(@recipe) %>. After restarting the webserver, when trying to show a recipe I get the abovementioned error, linking to commontator (4.7.2) app/views/commontator/shared/_thread.html.erb.

On my server I see:

Rendered recipes/_step.html.erb (9.3ms)
  Commontator::Thread Load (1.3ms)  SELECT  "commontator_threads".* FROM "commontator_threads"  WHERE "commontator_threads"."commontable_id" = $1 AND "commontator_threads"."commontable_type" = $2 LIMIT 1  [["commontable_id", 7], ["commontable_type", "Recipe"]]
Recipe Load (0.5ms)  SELECT  "recipes".* FROM "recipes"  WHERE "recipes"."id" = $1 LIMIT 1  [["id", 7]]
  Commontator::Subscription Load (0.4ms)  SELECT  "commontator_subscriptions".* FROM "commontator_subscriptions"  WHERE "commontator_subscriptions"."subscriber_id" = $1 AND "commontator_subscriptions"."subscriber_type" = $2 AND "commontator_subscriptions"."thread_id" = 1  ORDER BY "commontator_subscriptions"."id" ASC LIMIT 1  [["subscriber_id", 2], ["subscriber_type", "User"]]
   (0.3ms)  SELECT COUNT(*) FROM "commontator_comments"  WHERE "commontator_comments"."thread_id" = $1  [["thread_id", 1]]
  Rendered /Users/anon/.rvm/gems/ruby-2.1.2@app/gems/commontator-4.7.2/app/views/commontator/shared/_thread.html.erb (44.5ms)
  Rendered recipes/show.html.erb within layouts/application (76.9ms)
Completed 500 Internal Server Error in 88ms

Any ideas? What other information can I provide you with to help find what could be the problem?

How do you customize the username?

I have a current_user with a username however its always showing up as Anonymous. I checked the commontator.rb file and noticed it has a default set to Anonymous however there isn't a way to change what field its look for on my current_user. Is there an easy way to do this?

Editing a comment

I can only edit the last comment that was posted. Is there any way for all comments to be edited (assuming that the current user is the one that created the comment)?

Delete comment from database and view

Hello gem creator,

I am having some issue here figuring out how to delete the comment completely after selecting the "delete" link. As for now, when I delete the comment it is still shown in my view as "comment deleted by ..." and I would like it to be completely deleted so that it doesn't show as a deleted comment in my view. I couldn't find anything in the documentation that deals with that. Any help?
thanks!

acts_as_votable broken methods names causes exceptions

After adding acts_as_votable to my Gemfile, creating votes migration, restarting the server, following exceptions got fired from _votes.html.erb:

  1. comment.rb L41 votes.where throws
    undefined local variable or method `votes' for #Commontator::Comment:0x007ffb653c6910
  2. _votes.html.erb L41 upvotes throws
    undefined method `upvotes' for #Commontator::Comment:0x007ffb652c8d38
  3. _votes.html.erb L41 downvotes throws
    undefined method `downvotes' for #Commontator::Comment:0x0000000624a530

Comment nesting

Any thoughts on implementing ancestry for nested comments? That would tip the scales and make this even more powerful out of the box than it already is.

Question: Missing partial when using "commontator_thread_show"

I have the gem running fine, but when I try to use: commontator_thread_show at the controller, I get the error:

Missing partial players/reply, application/reply with {:locale=>[:es], :formats=>[:html], :handlers=>[:erb, :builder, :coffee, :jbuilder, :axlsx, :wxlsx]}. Searched in:

  • "/home/ubuntu/web-app/app/views"
  • "/home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/commontator-4.5.0/app/views"
  • "/home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/kaminari-0.15.1/app/views"
  • "/home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/devise-3.1.1/app/views"

Extracted source (around line #59):

56: <% end %>
57:
58: <% if thread.config.comment_order != :l %>
59: <%= render :partial => 'reply', :locals => {:thread => thread, :user => user} %>
60: <% end %>

Can you help me?

Commontator Interaction with auto_html gem

Another great gem, auto_html parses text into links. Generally, its stored in the model of the text you wish to parse.

auto_html comes with a helper method that can be called from a view: <%= auto_html('url') {conditions} %>

Installing commontator alongside auto_html causes rails to throw a no-method error for auto_html. Removing commontator instantly fixes the error.

Is commontator using auto_html or any similar keywords that could be shared in scope?

How to track activity on the commontator

Hi,
I am wondering how to use Public activity to track the activity in commontator? I need to place the track command to the model of this commontator but I cannot find the commentator model anywhere.

Thank you very much

[resolved] undefined local variable or method `commontable'

Hello,
i followed the "readme.md" on github gem page of commontator, but i think i can not understand this error (i'm a newbie on rails).
So... I'm on rails-4.0.1 with the commontator-4.2.
Maybe this can be an error from Devise or Cancan gem in association with commontator ? But i can not understand.

So... first, after install commontator, when try to go again on rails server (webrick), i had some error from ./config/initializer/commontator.rb on lines for config declaration (lignes: 8, 23, 32, 36, 77, 122, 135). For pass through, i had to make # for comment them out.

maybe it is not a bug, but i see nothing about this on google or stackoverflow. Could you provide some help or information about that, or can it be a bug from commontator ?

thanks.

(PS: when doing the code indicate after "development" indication... this erase all inside tables created before. Because... no update happen but create goes onside the other existed and erase all content tables. So... maybe it can be important for newbies to indicate this happen)

Sorry... i not understand the readme.rd file. When read #12 bug report, i understand.
The problemn was due tu the (commentable) who is in fact: the name of the variable define for the class controller of the designed with comments inside...
Again... i'm sorry for not been able to understand this.
Thanks for your great work.

Always Show Create Comment Form

Love the gem.

But currently there is no option to show the 'add comment form' automatically when the page loads. Always have to click 'New Comment' to fetch the form, would love it if there was a config option to do this.

At the moment after digging around in the files I've had to do a botch job of copying the HTML there and displaying it on the page without commontator running it.

Question: adding filter

I added a "label" field to the commontator comments. Now I want to filter the comments displayed by using a form to select the label. Could you please give me some directions on the best way to do that?

Thanks a lot

Can't delete an entry due to association

Hello, the error is:

Cannot modify association 'Movie#subscriptions' because the source reflection class 'Subscription' is associated to 'Commontator::Thread' via :has_many.

I think there is some model rails parameters (like has_many or belongs_to) to write on model(s) that are "commentable", but i'm not sure which can be.

undefined method `can_be_read_by?' for nil:NilClass

Hi,

I have just installed your gem on an existing Rails app with a post model and a user.
The database has already some posts. And I guess no thread is created for existing post.
Is there a migration to create threads for existing models ?

Thanks

Issue while creating new post

I have used this gem for my appliaction and its configured fine.
I have posts and on that each post comments will be given by users.These posts will be created by admin[rails_admin gem].
While creating new post by admin it is giving me following error:
ActionController::UrlGenerationError at /post/new
No route matches {:action=>"show", :controller=>"rails_admin/main", :id=>nil, :model_name=>"commontator~thread"}

Please help me on this?

Getting error when user not logged in.

Basically it works perfectly fine when a user is logged in. However when I log out and go to a page with the commentator_thread(@pin) then it breaks with this message.

NoMethodError in PinsController#show
undefined method `is_commontator' for nil:NilClass

The controller currently is as follows

  def show
    respond_with(@pin)
    commontator_thread_show(@pin)
  end

And the view is as follows:

    <div class="panel-body">
    <%= commontator_thread(@pin) %>
    </div>

It's pretty basic. My guess was that it requires a current_user to not be nil so my alternative to fixing it was to to just put an if statement to only show the comments if they are logged in but what if I want it to always be visible?

"body has already been posted" error

Sometimes when I try to post a comment I get the following message:

"This comment could not be posted because of the following error:
Body has already been posted"

However, my comment still ends up being posted. Any ideas what is causing the error? Thanks!

commontator_thread_show breaks when current_user returns false

There are many many cases where current_user could return false instead of nil (Devise, Sorcery etc) do this. In the commontator_thread_show(commontable) method this line

raise SecurityTransgression unless (user.nil? || user.is_commontator)

breaks. Mainly because false.is_commontator breaks.

Error on show.

Hi, I got this error. Im using rails 4.

Showing /Users/administrador/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/commontator-2.0.1/app/views/commontator/threads/_show.html.erb where line #39 raised:

uninitialized constant Commontator::Thread::Comment

Extracted source (around line #39):

<% thread.ordered_comments.each do |comment| %> <% next unless comment.can_be_read_by?(user) %> <%= render :partial => 'commontator/comments/show', :locals => {:comment => comment,

"undefined method `thread' for true:TrueClass"

I'm not sure I understand the installation instructions. I want the comments to appear on my commentable's show page. In my controller I have:

@note = Note.publishable.find(params[:id])
@comments = commontator_thread_show(@note)

Then in my view I have:

= commontator_thread(@comments)

But I get an error: "undefined method `thread' for true:TrueClass". (When I just put a link to the comments, it works fine.)

Users can only Upvote/Downvote once same comment

When a user up vote a comment from 0 to +1 and then tries to up vote the same comment again, the view changes from +1 to 0. Same for down vote. Down vote the comment once (0 to -1), down vote the same comment again (-1 to 0).

I would like to allow users to only vote on a comment once whether up voted or down voted.

New comment form not showing when config.new_comment_style = :l

I had an issue that sounded similar to #47, but without the javascript error. If I changed config.new_comment_style to :t, it worked as expected.

I found out that the form was rendered, but hidden. It seems to me that this is because in app/views/commontator/threads/_reply.html.erb, @new_comment has not been set, even though commontator_set_new_comment has been called via set_thread due to the before_filter in CommentsController. I'm not sure whether @new_comment is in a scope that should be visible to the form but I confirmed that it is nil in the form, hence the form being hidden.

It is also unclear to me what happens to the @comment created in the new action of the CommentsController, so there seems to be some disconnect there.

Unfortunately I can't check this in a clean clone of your repo due to:

Bundler::GemspecError: Could not read gem at /Users/my_user/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/cache/actionview-4.1.8.gem. It may be corrupted.
An error occurred while installing actionview (4.1.8), and Bundler cannot continue.
Make sure that `gem install actionview -v '4.1.8'` succeeds before bundling.

Not sure what to do about that.

Hope this is helpful.

Undefined local variable or method `votes'

I added the acts_as_votable gem and changed the initializer configuration to config.comment_voting = :ld
Now when I want to show comments I get an Error undefined local variable or method``votes'

The problem seems to appear on here:
<% vote = comment.get_vote_by(user) %>

Do you have any idea why I get this error message?

Thanks!

Add optional avatar location

Currently there is no options for an Avatar. It's defaulted to use gravitar. I think there should be an option for a normal image that falls back to gravitar.

need some require_dependency's?

Running commontator and it looks like the commontator controllers are finding my app's application controller instead of the commontator one.

maybe need to add require_dependency 'commontator/application_controller' at the top of the controller files?

specifically my app was complaining that it couldn't find the get_thread method called in a before_filter before the CommentsController's new action.

Replying

Is it allowable to make user reply on comments ?
and how ?

Migration Error

Mysql2::Error: BLOB/TEXT column 'body' can't have a default value.

:D

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.