Skip to content

Commit

Permalink
Merge pull request #83 from it3s/talk
Browse files Browse the repository at this point in the history
Talk
  • Loading branch information
LuizArmesto committed Oct 18, 2014
2 parents 60ce41c + 32e8b8a commit dcaa8a9
Show file tree
Hide file tree
Showing 29 changed files with 301 additions and 18 deletions.
20 changes: 20 additions & 0 deletions app/assets/javascripts/components/comments.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

App.components.comments = ->
attributes: ->
list: @container.find('#comments-list')
identifier: 'remoteForm:comment-form'

initialize: ->
App.mediator.subscribe 'remoteForm:success', @onSuccess.bind(this)

onSuccess: (evt, data) ->
if data.identifier is @attr.identifier
@cleanEditor()
@addToList data.response.comment_html
App.utils.flashMessage data.response.flash

addToList: (comment_html) ->
@attr.list.prepend $(comment_html)

cleanEditor: ->
App.mediator.publish 'editor:clean', {identifier: 'editor:comment_comment'}
5 changes: 5 additions & 0 deletions app/assets/javascripts/components/editor.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ App.components.editor = ->
setup: (ed) =>
ed.on 'change', _.debounce(@onEditorChange).bind(this)
})
App.mediator.subscribe 'editor:clean', @clean.bind(this)

onEditorChange: (ed)->
App.mediator.publish 'tinymce:changed', {id: @container.attr('id'), content: ed.target.getContent()}

clean: (evt, data) ->
if data.identifier is @identifier
tinyMCE.get(@container.attr('id')).setContent ''

2 changes: 1 addition & 1 deletion app/assets/javascripts/components/remoteForm.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ App.components.remoteForm = ->
App.mediator.publish 'remoteForm:beforeSubmit', @container

onAjaxComplete: (el, response) ->
@cleanErrors()
callback = if parseInt(response.status, 10) is 200 then @onSuccess else @onError
callback.bind(this)(el, JSON.parse(response.responseText))

Expand All @@ -18,7 +19,6 @@ App.components.remoteForm = ->

onError: (el, response) ->
err = response.errors || 'Error'
@cleanErrors()
if _.isObject err
_.each err, (value, key) =>
field = @container.find("[name*='[#{key}]']").closest('.field')
Expand Down
1 change: 1 addition & 0 deletions app/assets/stylesheets/application.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,4 @@
@import "notifications";
@import "pictures";
@import "import";
@import "comments";
46 changes: 46 additions & 0 deletions app/assets/stylesheets/comments.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
.comments {
#comment-form {
position: relative;

input[type=submit] {
margin-top: 10px;
position: absolute;
right: 0;
}
}

.comments-list {
margin-top: 60px;

.comment {
border-top: solid 1px $soft-grey;
padding: 10px;
color: $dark-grey;

.comment-headline {
a {
color: $link-color;
text-decoration: none;
}
.avatar {
width: 30px;
height: 30px;
border-radius: 15px;
top: 10px;
position: relative;
margin-right: 5px;
}
.comment-time {
color: $grey;
font-size: 0.9em;
margin-left: 10px;
}
}
.comment-text {
margin: 20px 15px;
border-left: solid 3px $ultra-soft-grey;
padding-left: 15px;
}
}
}
}
36 changes: 36 additions & 0 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class CommentsController < ApplicationController

before_action :require_login
before_action :find_commentable
after_action :publish_commented_event

def create
@comment = Comment.new comment_params
if @comment.save
render json: {flash: flash_xhr(t 'comments.saved'), comment_html: comment_html}
else
render json: {errors: @comment.errors.messages}, status: :unprocessable_entity
end
end

private

def find_commentable
@commentable ||= find_polymorphic_object
end

def comment_params
{user: current_user, content: @commentable, comment: params[:comment][:comment]}
end

def comment_html
render_to_string(partial: 'comments/comment', locals: {comment: @comment})
end

def publish_commented_event
if response.ok?
EventBus.publish "commented", object: @commentable, current_user: current_user, changes: {'comment' => @comment.comment}
end
end

end
4 changes: 4 additions & 0 deletions app/listeners/activity_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ def followed(payload)
save_activity payload, :object, :follow
end

def commented(payload)
save_activity payload, :object, :comment
end

private

def save_activity(params, key, action)
Expand Down
8 changes: 8 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :content, polymorphic: true

validates :user, :content, :comment, presence: true

default_scope { includes(:user).order('created_at desc') }
end
7 changes: 7 additions & 0 deletions app/models/concerns/commentable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Commentable
extend ActiveSupport::Concern

included do
has_many :comments, as: :content, dependent: :destroy
end
end
1 change: 1 addition & 0 deletions app/models/geo_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class GeoData < ActiveRecord::Base
include Relationships
include Exportable
include PublicActivity::Common
include Commentable

geojson_field :location
searchable_tags :tags
Expand Down
2 changes: 2 additions & 0 deletions app/models/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Map < ActiveRecord::Base
include Searchable
include Exportable
include PublicActivity::Common
include Commentable

belongs_to :administrator, class_name: 'User'

Expand Down Expand Up @@ -45,4 +46,5 @@ def geo_data_features
def geo_data_ids
geo_data.all.map { |data| data.id }
end

end
14 changes: 14 additions & 0 deletions app/views/comments/_comment.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<div class="comment">
<div class="comment-headline">
<%= image_tag comment.user.avatar.thumb , class: "avatar", title: comment.user.name %>
<a href="<%= user_path(comment.user) %>">
<span><%= comment.user.name %></span>
</a>
<time class="comment-time" datetime="<%= comment.created_at.to_datetime.rfc3339 %>">
<%= t 'time_ago', time: time_ago_in_words(comment.created_at) %>
</time>
</div>
<div class="comment-text">
<%= raw comment.comment %>
</div>
</div>
30 changes: 30 additions & 0 deletions app/views/comments/_list.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<div id="comments" class="comments" data-components='comments'>

<% if logged_in? && !edit_mode? %>
<%= form_tag url_for([object, :comments]), method: :post, id: "comment-form",
remote: true, "data-components" => "remoteForm" do %>
<div class="field">
<textarea id="comment_comment" class="comment_input tinymce" name="comment[comment]"
data-components="editor" data-editor-options='{"height":70}' >
</textarea>
</div>
<%= submit_tag 'Add comment' %>
<% end %>

<ul id="comments-list" class="comments-list">
<%= render comments %>
</ul>

<% else %>
<% if comments.empty? %>
<p class="empty"><%= t 'comments.empty', {name: object.name} %></p>
<% else %>
<ul id="comments-list" class="comments-list">
<%= render comments %>
</ul>
<% end %>
<% end %>
</div>
2 changes: 1 addition & 1 deletion app/views/geo_data/_content.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<%= render 'pictures/gallery', object: geo_data %>
</div>
<div class="tab-content" id="talk">
Talk Placeholder
<%= render 'comments/list', comments: geo_data.comments, object: geo_data %>
</div>
</div>

Expand Down
2 changes: 1 addition & 1 deletion app/views/geo_data/_content_edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<%= render 'pictures/uploader', object: geo_data %>
</div>
<div class="tab-content" id="talk">
Talk Placeholder
<%= render 'comments/list', comments: geo_data.comments, object: geo_data %>
</div>
</div>

Expand Down
2 changes: 1 addition & 1 deletion app/views/maps/_content.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
</div>

<div class="tab-content" id="talk">
Talk Placeholder
<%= render 'comments/list', comments: map.comments, object: map %>
</div>
</div>

Expand Down
2 changes: 1 addition & 1 deletion app/views/maps/_content_edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
Data Placeholder
</div>
<div class="tab-content" id="talk">
Talk Placeholder
<%= render 'comments/list', comments: map.comments, object: map %>
</div>
</div>

Expand Down
9 changes: 5 additions & 4 deletions config/locales/en/activities.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ en:
empty: "There are no activity recorded for this user yet"

event:
update: 'updated'
create: 'created'
delete: 'deleted'
follow: 'followed'
update: 'updated'
create: 'created'
delete: 'deleted'
follow: 'followed'
comment: 'commented'
5 changes: 5 additions & 0 deletions config/locales/en/comments.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
en:
comments:
empty: '%{name} has no comments yet.'
submit: 'Add comment'
saved: 'Comment added'
9 changes: 5 additions & 4 deletions config/locales/pt-BR/activities.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ pt-BR:
empty: "Não há registro de atividade para este usuário ainda"

event:
update: 'atualizou'
create: 'criou'
delete: 'deletou'
follow: 'seguiu'
update: 'atualizou'
create: 'criou'
delete: 'deletou'
follow: 'seguiu'
comment: 'commentou'
6 changes: 6 additions & 0 deletions config/locales/pt-BR/comments.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pt-BR:
comments:
empty: '%{name} não possui comentários ainda.'
submit: 'Adicionar comentário'
saved: 'Comentário adicionado'

9 changes: 7 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
resources :pictures, only: [:show, :update, :destroy]
end

concern :commentable do
resource :comments, on: :member, only: [:create]
end

resources :users, except: [:destroy, :index],
concerns: [:contributor, :followable, :follower] do
member do
Expand All @@ -74,7 +78,7 @@

resources :geo_data, except: [:destroy],
concerns: [:contributable, :followable, :versionable,
:downloadable, :has_media] do
:downloadable, :commentable, :has_media] do
member do
get :maps
post :add_map
Expand All @@ -87,7 +91,8 @@
end

resources :maps, except: [:destroy],
concerns: [:contributable, :followable, :versionable, :downloadable] do
concerns: [:contributable, :followable, :versionable,
:downloadable, :commentable] do
member do
get :geo_data
post :add_geo_data
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20141013145901_create_comments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.references :user, null: false, index: true
t.references :content, null: false, index: true, polymorphic: true
t.text :comment, null: false

t.timestamps
end
end
end
17 changes: 14 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20140930182821) do
ActiveRecord::Schema.define(version: 20141013145901) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
enable_extension "hstore"
enable_extension "uuid-ossp"
enable_extension "postgis"
enable_extension "hstore"
enable_extension "pg_trgm"
enable_extension "fuzzystrmatch"

Expand Down Expand Up @@ -46,6 +45,18 @@
t.datetime "updated_at"
end

create_table "comments", force: true do |t|
t.integer "user_id", null: false
t.integer "content_id", null: false
t.string "content_type", null: false
t.text "comment", null: false
t.datetime "created_at"
t.datetime "updated_at"
end

add_index "comments", ["content_id", "content_type"], :name => "index_comments_on_content_id_and_content_type"
add_index "comments", ["user_id"], :name => "index_comments_on_user_id"

create_table "contributings", force: true do |t|
t.integer "contributor_id"
t.integer "contributable_id"
Expand Down
Loading

0 comments on commit dcaa8a9

Please sign in to comment.