Skip to content

Commit

Permalink
commentable projects and datasets
Browse files Browse the repository at this point in the history
  • Loading branch information
aivarsak committed Jun 26, 2012
1 parent a3380f5 commit 9a9a0d0
Show file tree
Hide file tree
Showing 22 changed files with 165 additions and 45 deletions.
3 changes: 1 addition & 2 deletions app/assets/javascripts/markdown.js.coffee
Expand Up @@ -3,5 +3,4 @@ jQuery ->
new Markdown.Editor(new Markdown.Converter(),postfix).run()

$(".wmd-panel").each ->
initializeWmd($(this).data("wmd"))

initializeWmd($(this).data("wmd"))
8 changes: 8 additions & 0 deletions app/assets/stylesheets/comments.css.scss
Expand Up @@ -54,4 +54,12 @@
height: 100px;
}
}
}
.tab-content {
.comments {
margin-left: 0px;
.markdown textarea {
width: 98%;
}
}
}
24 changes: 23 additions & 1 deletion app/helpers/application_helper.rb
@@ -1,6 +1,28 @@
module ApplicationHelper

def top_menu_class(instance)
'active' if controller.instance_of?(instance)
end

def comment_commentable_info(commentable)
info = ""
case commentable.class.to_s
when "Topic"
info = t("comments.commented_topic")
info << "&nbsp;"
info << content_tag(:strong, link_to(commentable.title, forum_topic_path(commentable.forum.slug, commentable.slug)))
when "Dataset"
info = t "datasets.commented_dataset"
info << "&nbsp;"
info << content_tag(:strong, link_to(commentable.name, dataset_path(commentable)))
when "Project"
info = t "projects.commented_project"
info << "&nbsp;"
info << content_tag(:strong, link_to(commentable.name, project_path(commentable)))
else
commentable.class
end
info.html_safe
end

end
9 changes: 9 additions & 0 deletions app/helpers/datasets_helper.rb
Expand Up @@ -9,4 +9,13 @@ def dataset_column_display_limit(column)
t "datasets.show.columns.display_limit.decimal", options.slice(:precision, :scale)
end
end

def dataset_info(dataset)
info = []
if (source_files_size = dataset.source_files.size) > 0
info << t("datasets.source_files_uploaded", :count => source_files_size)
end
info << t("datasets.comments", :count => dataset.comments.count) if dataset.any_comment?
info.join(', ')
end
end
18 changes: 15 additions & 3 deletions app/models/comment.rb
Expand Up @@ -28,8 +28,20 @@ def unread?(time)
time < created_at
end

def self.topic_recent(count = 5)
where(:commentable_type => 'Topic').order("created_at DESC").limit(count).includes(:commentable).includes(:user)
def self.recent(commentable = nil, count = 5)
if commentable.class == Fixnum
count = commentable
commentable = nil
end
if commentable
if commentable.respond_to?(:each)
conditions = where(:commentable_type => commentable.map{|c| c.class.to_s}.uniq, :commentable_id => commentable.map{|c| c.id}.uniq)
else
conditions = where(:commentable_type => commentable.class, :commentable_id => commentable.id)
end
else
conditions = order
end
conditions.order("created_at DESC").limit(count).includes(:commentable).includes(:user)
end

end
1 change: 1 addition & 0 deletions app/models/dataset.rb
Expand Up @@ -15,6 +15,7 @@
#

class Dataset < ActiveRecord::Base
is_commentable
belongs_to :project
has_many :source_files, :dependent => :destroy

Expand Down
2 changes: 2 additions & 0 deletions app/models/project.rb
Expand Up @@ -23,6 +23,8 @@ class Project < ActiveRecord::Base

attr_accessible :shortname, :name, :description, :homepage

is_commentable

def self.recent(count = 5)
order("updated_at DESC").limit(count).includes(:account => :user).includes(:datasets)
end
Expand Down
4 changes: 4 additions & 0 deletions app/views/accounts/show.html.haml
Expand Up @@ -26,6 +26,8 @@
%p= simple_format project.description
- if project.homepage.present?
%p= link_to project.homepage, project.homepage, :target => "_blank"
- if project.any_comment?
%small=t("projects.comments", :count => project.comments.count)
- if project.datasets.present?
%h4
= t("projects.datasets") + ":"
Expand All @@ -35,3 +37,5 @@

.span6
%h2= t("nav.recent_activity")
%h2= t "nav.recent_comments"
= render :partial => 'comments/recent_comment', :collection => Comment.recent(@account.projects + @account.projects.map{|p|p.datasets}.flatten)
@@ -1,12 +1,10 @@
- comment = topic_comment
- topic = comment.commentable
- comment = recent_comment
.comment
.face
= gravatar_image_tag(comment.user.email, :gravatar => {:size => 30})
.created_at
= link_to comment.user.display_name, account_profile_path(comment.user.login)
= t "comments.commented_topic"
%strong= link_to topic.title, forum_topic_path(topic.forum.slug, topic.slug)
= comment_commentable_info(comment.commentable)
= t "datetime.ago", :distance => distance_of_time_in_words_to_now(comment.created_at)
.content.markdown
= markdown(truncate comment.content, :length => 200, :separator => ' ')
3 changes: 1 addition & 2 deletions app/views/datasets/_dataset.html.haml
Expand Up @@ -15,5 +15,4 @@
%i.icon-share
%strong= Dataset.human_attribute_name("source_url") + ':'
= link_to dataset.source_url, dataset.source_url, :target => "_blank"
- if (source_files_size = dataset.source_files.size) > 0
= t "datasets.source_files_uploaded", :count => source_files_size
=dataset_info(dataset)
12 changes: 9 additions & 3 deletions app/views/datasets/show.html.haml
Expand Up @@ -20,6 +20,8 @@
%i.icon-share
%strong= Dataset.human_attribute_name("source_url") + ':'
= link_to @dataset.source_url, @dataset.source_url, :target => "_blank"
- if @dataset.any_comment?
%small=t("datasets.comments", :count => @dataset.comments.count)

%ul.nav.nav-tabs
%li
Expand All @@ -28,6 +30,8 @@
%a{href: "#upload", data: {toggle: "tab"}}= t ".uploaded_files"
%li
%a{href: "#columns", data: {toggle: "tab"}}= t ".column_information"
%li
%a{href: "#comments", data: {toggle: "tab"}}= t "nav.comments"

.tab-content
#preview.tab-pane
Expand Down Expand Up @@ -108,16 +112,18 @@
:confirm => t("datasets.delete_all_columns_confirm")
- else
No dataset columns have been created. Upload CSV source file at first.


#comments.tab-pane
= comments_for(@dataset)

.span4
%h2= t "nav.recent_activity"
%h2= t "nav.recent_comments"
= render :partial => 'comments/recent_comment', :collection => Comment.recent(@dataset)

:javascript
$(function() {
new Datahub.DatasetShowView({
tab: "#{j flash[:tab]}",
previewTranslations: #{js_translations "datasets.datatable"}
});
});
});
6 changes: 3 additions & 3 deletions app/views/home/index.html.haml
Expand Up @@ -30,8 +30,8 @@
%h2= t ".about_us"
%p.lead= t ".about_us_lead.html"

%h2= t "nav.recent_comments"
= render :partial => 'comments/recent_comment', :collection => Comment.recent

%h2= t "forums.recent_topics"
= render :partial => 'topic', :collection => Topic.recent

%h2= t "forums.recent_comments"
= render :partial => 'topic_comment', :collection => Comment.topic_recent
3 changes: 3 additions & 0 deletions app/views/projects/_project.html.haml
Expand Up @@ -8,6 +8,9 @@
%p= simple_format project.description
- if project.homepage.present?
%p= link_to project.homepage, project.homepage, :target => "_blank"
- if project.any_comment?
%small=t("projects.comments", :count => project.comments.count)

- if project.datasets.present?
%h4
= t("projects.datasets") + ":"
Expand Down
65 changes: 41 additions & 24 deletions app/views/projects/show.html.haml
Expand Up @@ -14,32 +14,49 @@
%p= simple_format @project.description
- if @project.homepage.present?
%p= link_to @project.homepage, @project.homepage, :target => "_blank"
- if @project.any_comment?
%small=t("projects.comments", :count => @project.comments.count)

%h3
= t "projects.datasets"
%small= "(#{@project.datasets.size})"
- if can? :create_dataset, @project
= link_to t("datasets.new_dataset"), new_account_project_dataset_path(@account, @project), :class => "btn btn-success btn-mini pull-right"
%ul.nav.nav-tabs
%li
%a{href: "#datasets", data: {toggle: "tab"}}
= "#{t("projects.datasets")} (#{@project.datasets.size})"
%li
%a{href: "#comments", data: {toggle: "tab"}}= t "nav.comments"

- @project.datasets.each do |dataset|
.well
%h3
%i.icon-list-alt
= link_to dataset.name, dataset_path(dataset)
- if can? :manage, dataset
.pull-right
= link_to t("actions.edit"), edit_account_project_dataset_path(@account, @project, dataset), :class => "btn btn-mini"
= link_to t("actions.delete"), account_project_dataset_path(@account, @project, dataset), :method => :delete, :class => "btn btn-mini btn-danger",
:confirm => t("datasets.delete_confirm")
- if dataset.description.present?
%p= simple_format dataset.description
- if dataset.source_url.present?
%p
%i.icon-share
%strong= Dataset.human_attribute_name(:source_url) + ':'
= link_to dataset.source_url, dataset.source_url, :target => "_blank"
- if (source_files_size = dataset.source_files.size) > 0
= t "datasets.source_files_uploaded", :count => source_files_size
- if can? :create_dataset, @project
= link_to t("datasets.new_dataset"), new_account_project_dataset_path(@account, @project), :class => "btn btn-success btn-mini pull-right", :style => "margin-top: -50px;"

.tab-content
#datasets.tab-pane
- @project.datasets.each do |dataset|
.well
%h3
%i.icon-list-alt
= link_to dataset.name, dataset_path(dataset)
- if can? :manage, dataset
%span.pull-right
= link_to t("actions.edit"), edit_account_project_dataset_path(@account, @project, dataset), :class => "btn btn-mini"
= link_to t("actions.delete"), account_project_dataset_path(@account, @project, dataset), :method => :delete, :class => "btn btn-mini btn-danger",
:confirm => t("datasets.delete_confirm")
- if dataset.description.present?
%p= simple_format dataset.description
- if dataset.source_url.present?
%p
%i.icon-share
%strong= Dataset.human_attribute_name(:source_url) + ':'
= link_to dataset.source_url, dataset.source_url, :target => "_blank"
=dataset_info(dataset)

#comments.tab-pane
= comments_for(@project)

.span4
%h2= t "nav.recent_activity"
%h2= t "nav.recent_comments"
= render :partial => 'comments/recent_comment', :collection => Comment.recent([@project] + @project.datasets)

:javascript
$(function() {
$("ul.nav-tabs a[href=#datasets]").tab("show")
});
2 changes: 2 additions & 0 deletions config/locales/application.en.yml
Expand Up @@ -14,6 +14,8 @@ en:
more_disable_with: "Loading..."

recent_activity: "Recent activity"
recent_comments: Recent comments
comments: "Comments"

actions:
update: "Save"
Expand Down
2 changes: 2 additions & 0 deletions config/locales/application.lv.yml
Expand Up @@ -14,6 +14,8 @@ lv:
more_disable_with: "Ielādē..."

recent_activity: "Pēdējās aktivitātes"
recent_comments: Jaunākie komentāri
comments: "Komentāri"

actions:
update: "Saglabāt"
Expand Down
4 changes: 4 additions & 0 deletions config/locales/datasets.en.yml
Expand Up @@ -4,6 +4,10 @@ en:
source_files_uploaded:
one: "1 source file uploaded"
other: "%{count} source files uploaded"
comments:
one: "added %{count} comment"
other: "added %{count} comments"
commented_dataset: "commented dataset"
search_more: "Search more datasets"
new_dataset: "New dataset"
edit_dataset: "Edit dataset"
Expand Down
4 changes: 4 additions & 0 deletions config/locales/datasets.lv.yml
Expand Up @@ -4,6 +4,10 @@ lv:
source_files_uploaded:
one: "Augšupielādēts 1 fails"
other: "Augšupielādēti %{count} faili"
comments:
one: "pievienots %{count} komentārs"
other: "pievienoti %{count} komentāri"
commented_dataset: "komentēja datu kopu"
search_more: "Meklēt citas datu kopas"
new_dataset: "Jauna datu kopa"
edit_dataset: "Mainīt datu kopu"
Expand Down
4 changes: 4 additions & 0 deletions config/locales/projects.en.yml
Expand Up @@ -11,6 +11,10 @@ en:
delete_confirm: "Are you sure to delete this project with all project datasets?"
search_placeholder: "Search by name and description"
search_results: "Project search results"
commented_project: "commented project"
comments:
one: "added %{count} comment"
other: "added %{count} comments"

activerecord:
models:
Expand Down
4 changes: 4 additions & 0 deletions config/locales/projects.lv.yml
Expand Up @@ -11,6 +11,10 @@ lv:
delete_confirm: "Vai vēlaties izdzēst šo projektu un visas projekta datu kopas?"
search_placeholder: "Meklēt pēc nosaukuma un apraksta"
search_results: "Projektu meklēšanas rezultāti"
commented_project: "komentēja projektu"
comments:
one: "pievienots %{count} komentārs"
other: "pievienoti %{count} komentāri"

activerecord:
models:
Expand Down
4 changes: 4 additions & 0 deletions lib/addons/commentable/commentable.rb
Expand Up @@ -9,6 +9,10 @@ module InstanceMethods
def commentable?
true
end

def any_comment?
comments.count > 0
end
end
end
end
Expand Down
22 changes: 19 additions & 3 deletions spec/models/comment_spec.rb
Expand Up @@ -60,7 +60,7 @@
end
end

describe ".topic_recent" do
describe ".recent" do
before do
(1..5).each do |i|
instance_variable_set("@comment_#{i}", create(:comment, :commentable_type => "Topic",
Expand All @@ -69,11 +69,27 @@
end

it "returns 5 comments ordered descendingly if no count given" do
Comment.topic_recent.should eq([@comment_5, @comment_4, @comment_3, @comment_2, @comment_1])
Comment.recent.should eq([@comment_5, @comment_4, @comment_3, @comment_2, @comment_1])
end

it "returns n comments ordered descendingly given count" do
Comment.topic_recent(3).should eq([@comment_5, @comment_4, @comment_3])
Comment.recent(3).should eq([@comment_5, @comment_4, @comment_3])
end

it "returns comments for given commentable" do
project = create(:project)
comment = create(:comment, :commentable_type => "Project", :commentable_id => project.id, :created_at => Time.zone.now)
Comment.recent(project).should include(comment)
end

it "returns comments for given commentables" do
project = create(:project)
dataset = create(:dataset, :project => project)
comment_p = create(:comment, :commentable_type => "Project", :commentable_id => project.id, :created_at => Time.zone.now)
comment_d = create(:comment, :commentable_type => "Dataset", :commentable_id => dataset.id, :created_at => Time.zone.now)
recent = Comment.recent([project, dataset])
recent.should include(comment_p)
recent.should include(comment_d)
end
end

Expand Down

0 comments on commit 9a9a0d0

Please sign in to comment.