Skip to content
This repository has been archived by the owner on Dec 26, 2019. It is now read-only.

Commit

Permalink
Added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
soulim committed Jan 13, 2011
1 parent 06af82e commit 454d544
Show file tree
Hide file tree
Showing 30 changed files with 375 additions and 49 deletions.
11 changes: 11 additions & 0 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CommentsController < ApplicationController
load_and_authorize_resource :item
load_and_authorize_resource :comment, :through => :item

respond_to :js

def create
@comment.user = current_user
respond_with(@item, @comment.save)
end
end
4 changes: 4 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ def avatar_url(user)
end
end
end

def icon_tag(icon)
content_tag :i, nil, :class => "icon #{icon}"
end
end
2 changes: 2 additions & 0 deletions app/helpers/comments_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module CommentsHelper
end
2 changes: 1 addition & 1 deletion app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def initialize(user)
can :read, Item

if !user.guest?
can :create, Item
can :create, [Item, Comment]
can(:update, Item) { |item| item.author?(user) }
can(:show, User) { |_user| _user.me?(user) }
end
Expand Down
14 changes: 14 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Comment < ActiveRecord::Base
belongs_to :item
belongs_to :user

validates_presence_of :item_id, :user_id, :markdown

before_save :assign_html, :if => :markdown?

private

def assign_html
self.html = RDiscount.new(self.markdown, :filter_styles, :filter_html, :autolink).to_html
end
end
1 change: 1 addition & 0 deletions app/models/item.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Item < ActiveRecord::Base
belongs_to :user
has_many :comments, :dependent => :destroy

validates_presence_of :title, :markdown
validates_presence_of :user_id, :on => :create
Expand Down
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class User < ActiveRecord::Base

has_many :authentications, :dependent => :destroy
has_many :items, :dependent => :nullify
has_many :comments, :dependent => :destroy

def self.create_with_omniauth!(auth)
self.create! do |user|
Expand Down
7 changes: 7 additions & 0 deletions app/views/comments/_comment.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<article class="comment">
<header>
<time datetime="<%= comment.created_at.to_s(:db) %>" pubdate="pubdate"><%= comment.created_at.strftime("%d.%m.%Y") %></time>
<%= comment.user.name %>
</header>
<%=raw comment.html %>
</article>
10 changes: 10 additions & 0 deletions app/views/comments/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<div id="wmd-button-bar"></div>
<div>
<%= f.text_area :markdown, :placeholder => t("activerecord.attributes.comment.markdown"), :rows => 10, :autofocus => true %>
</div>
<%= javascript_include_tag("wmd.combined.min") %>
<script type="text/javascript">
setup_wmd({
input: "comment_markdown"
});
</script>
11 changes: 11 additions & 0 deletions app/views/comments/_new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<section class="preview">
<%= render :partial => "comments/preview", :locals => { :comment => comment } %>
</section>
<section class="form">
<%= form_for [item, comment], :remote => true do |f| %>
<%= render :partial => "comments/form", :locals => { :f => f } %>
<div class="buttons">
<%= f.submit t(".create") %>
</div>
<% end %>
</section>
4 changes: 4 additions & 0 deletions app/views/comments/_preview.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div class="wmd-preview">
<header>Просмотр</header>
<div id="wmd-preview"><%=raw comment.html %></div>
</div>
6 changes: 6 additions & 0 deletions app/views/comments/create.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<% if @comment.errors.any? %>
$('#item-<%= @item.id %>-new-comment').html('<%= escape_javascript render(:partial => "comments/new", :locals => { :item => @item, :comment => @comment }) %>');
<% else %>
$('#item-<%= @item.id %>-new-comment').before('<%= escape_javascript render(@comment)%>');
$('#item-<%= @item.id %>-new-comment').html('<%= escape_javascript render(:partial => "comments/new", :locals => { :item => @item, :comment => @item.comments.new }) %>');
<% end %>
26 changes: 21 additions & 5 deletions app/views/items/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,30 @@
</header>
<aside>
<ul>
<li><a href="#" onclick="Evernote.doClip({styling:'none',suggestNotebook:'rubyflow.ru',providerName:'rubyflow.ru',contentId:'entry-1029-content',url:'http://rubyflow.ru/items/1029',title:'Трюки с Hash'}); return false;"><img src="http://static.evernote.com/site-mem-16.png" alt="Сохранить в Evernote" style="vertical-align:bottom"></a></li>
<li><a href="#" onclick="Evernote.doClip({styling:'none',suggestNotebook:'rubyflow.ru',providerName:'rubyflow.ru',contentId:'item-<%= item.id %>-html',url:'<%= item_url(item) %>',title:'<%= escape_javascript item.title %>'}); return false;"><img src="http://static.evernote.com/site-mem-16.png" alt="Сохранить в Evernote" style="vertical-align:bottom"></a></li>
</ul>
</aside>
<%= item.html.html_safe %>
<ul>
<li><%= link_to("edit", edit_item_url(item)) if can?(:edit, item) %></li>
<li><%= link_to("delete", item_url(item), :method => :delete, :confirm => t("layouts.commons.destroy_confirmation", :subject => item.title)) if can?(:destroy, item) %></li>
<%=raw item.html %>
<% if !item.comments.blank? %>
<section class="comments clearfix">
<header class="grid_2"><%= link_to t(".comments"), item_url(item, :anchor => "comments") %></header>
<section class="grid_6">
<article class="comment">
<header>
<time datetime="<%= item.comments.last.created_at.to_s(:db) %>" pubdate="pubdate"><%= item.comments.last.created_at.strftime("%d.%m.%Y") %></time>
<%= item.comments.last.user.name %>
</header>
<%=raw item.comments.last.html %>
</article>
</section>
</section>
<% end %>
<% if can? :manage, item %>
<ul class="admin-buttons">
<li><%= link_to(icon_tag("item-edit"), edit_item_url(item)) if can?(:edit, item) %></li>
<li><%= link_to(icon_tag("item-remove"), item_url(item), :method => :delete, :confirm => t("layouts.commons.destroy_confirmation", :subject => item.title)) if can?(:destroy, item) %></li>
</ul>
<% end %>
</article>
<% end %>
</section>
Expand Down
20 changes: 14 additions & 6 deletions app/views/items/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,25 @@
</header>
<aside>
<ul>
<li><a href="#" onclick="Evernote.doClip({styling:'none',suggestNotebook:'rubyflow.ru',providerName:'rubyflow.ru',contentId:'entry-1029-content',url:'http://rubyflow.ru/items/1029',title:'Трюки с Hash'}); return false;"><img src="http://static.evernote.com/site-mem-16.png" alt="Сохранить в Evernote" style="vertical-align:bottom"></a></li>
<li><a href="#" onclick="Evernote.doClip({styling:'none',suggestNotebook:'rubyflow.ru',providerName:'rubyflow.ru',contentId:'item-<%= @item.id %>-html',url:'<%= item_url(@item) %>',title:'<%= escape_javascript @item.title %>'}); return false;"><img src="http://static.evernote.com/site-mem-16.png" alt="Сохранить в Evernote" style="vertical-align:bottom"></a></li>
</ul>
</aside>
<%= @item.html.html_safe %>

<section id="item-<%= @item.id %>-html"><%= @item.html.html_safe %></section>
<section class="comments clearfix">
<header class="grid_2"><a href="">Комментарии</a></header>
<header class="grid_2"><a name="comments"></a>Комментарии</header>
<section class="grid_6">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent a est nisl. Proin molestie nisl diam, nec laoreet massa. Duis nisl est, malesuada et varius at, placerat et ante. Ut eu lorem in neque rhoncus accumsan. Curabitur varius nisl at tellus accumsan mattis. Cras est lorem, egestas id ornare iaculis, interdum commodo tortor. Vestibulum tempus tortor sit amet dui adipiscing sagittis. Vivamus semper tristique urna, vel posuere magna vehicula vel.
<% for comment in @item.comments %>
<%= render comment %>
<% end %>
<% if can?(:create, Comment) %>
<section id="item-<%= @item.id %>-new-comment" class="new-comment">
<%= render(:partial => "comments/new", :locals => { :item => @item, :comment => @item.comments.new }) %>
</section>
<p><%= link_to "#{icon_tag("comment-add")} #{t(".new_comment")}".html_safe, nil, :onclick => "$('#item-#{@item.id}-new-comment').slideDown('slow');$(this).hide('fast');return false;", :class => "link-with-icon" %></p>
<% else %>
<%= t(".please_sign_in_to_add_comment") %> <%= link_to t("layouts.application.sign_in"), sign_in_url %>
<% end %>
</section>
</section>
</article>

</section>
49 changes: 33 additions & 16 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
<meta name="keywords" content="<%= yield(:meta_keywords).blank? ? t(".meta_keywords") : yield(:meta_keywords) %>" />
<meta name="author" content="<%= yield(:meta_author).blank? ? t(".meta_author") : yield(:meta_author) %>" />
<link rel="shortcut icon" href="/favicon.ico" />
<%= stylesheet_link_tag "reset", "application", "960gs", "form", "wmd", "code", :cache => true %>
<link rel="alternate" type="application/rss+xml" title="RubyFlow.ru" href="http://feedproxy.google.com/rubyflowru" />
<%= stylesheet_link_tag "reset", "application", "960gs", "form", "icon", "wmd", "code", :cache => true %>
<%= csrf_meta_tag %>
<title><%= "#{yield(:title)} &mdash; ".html_safe unless yield(:title).blank? -%><%= t('.title') -%></title>
</head>
Expand All @@ -30,25 +31,41 @@
<section id="content" class="content container_12 clearfix">
<%= yield %>
</section>
<footer id="footer">
<section class="container_12 clearfix">
<section class="grid_2">
<p>&copy;&nbsp;2008—2010, <a href="http://soulim.com">Alex Soulim</a></p>
<p>При поддержке хостинг-провайдера <a href="http://locum.ru"><span>locum.ru</span></a></p>
</section>
<section class="grid_8">
&nbsp;
</section>
<section class="grid_2">
&nbsp;
</section>
<footer id="footer" class="container_12 clearfix">
<section class="grid_2">
<p>&copy;&nbsp;2008—2010, <a href="http://soulim.com">Alex Soulim</a></p>
<p>При поддержке хостинг-провайдера <a href="http://locum.ru"><span>locum.ru</span></a></p>
</section>
<section class="grid_8">
<nav>
<ul>
<li><%= link_to t(".about"), nil %></li>
<li><%= link_to t(".terms"), nil %></li>
<li><%= link_to t(".contacts"), nil %></li>
</ul>
</nav>
</section>
<section class="grid_2">
<a href="http://feeds.feedburner.com/rubyflowru"><img src="http://feeds.feedburner.com/~fc/rubyflowru?bg=eeeeee&amp;fg=444444&amp;anim=0" height="26" width="88" style="border:0" alt="" /></a>
</section>
</footer>
<%= javascript_include_tag :defaults, "jquery.flash", "highlight.pack", :cache => true %>
<%= javascript_include_tag "http://static.evernote.com/noteit.js", :cache => false %>
<%= javascript_include_tag :defaults, "jquery.flash", "highlight.pack", :cache => true %>
<script type="text/javascript">
$('pre code').each(function(i, e) { hljs.highlightBlock(e, ' ')});
$('pre').each(function(i, e) { hljs.highlightBlock(e, ' ')});
</script>
<%= yield(:footer_js) %>
<% if Rails.env.production? %>
<%= javascript_include_tag "http://static.evernote.com/noteit.js", :cache => false %>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-391915-11");
pageTracker._initData();
pageTracker._trackPageview();
</script>
<% end %>
</body>
</html>
4 changes: 3 additions & 1 deletion config/locales/activerecord.ru.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,6 @@ ru:
email: "Электронная почта"
item:
title: "Заголовок"
markdown: "Текст статьи"
markdown: "Текст статьи"
comment:
markdown: "Текст комментария"
4 changes: 4 additions & 0 deletions config/locales/comments.ru.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ru:
comments:
new:
create: "Добавить"
6 changes: 5 additions & 1 deletion config/locales/items.ru.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ ru:
items:
index:
title: "Коллективный блог сообщества разработчиков, использующих Ruby"
comments: "Комментарии"
new:
title: "Новая статья"
create: "Опубликовать"
cancel: "Отмена"
edit:
title: "Редактирование статьи"
update: "Обновить"
cancel: "Отмена"
cancel: "Отмена"
show:
new_comment: "Добавить свое мнение"
please_sign_in_to_add_comment: "Пожалуйста авторизуйтесь, чтобы добавить комментарий."
3 changes: 3 additions & 0 deletions config/locales/layouts.ru.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ ru:
account: "Аккаунт"
sign_in: "Вход"
sign_out: "Выход"
about: "О проекте"
terms: "Условия"
contacts: "Контактная информация"
commons:
destroy_confirmation: "Действительно удалить '%{subject}'?"
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
put 'unapprove'
end
end
resources :items
resources :items do
resources :comments
end

root :to => "items#index"
end
18 changes: 18 additions & 0 deletions db/migrate/20110107071143_create_comments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class CreateComments < ActiveRecord::Migration
def self.up
create_table :comments do |t|
t.belongs_to :item
t.belongs_to :user
t.text :markdown
t.text :html
t.timestamps
end

add_index :comments, :item_id
add_index :comments, :user_id
end

def self.down
drop_table :comments
end
end
Binary file added public/images/icon-sprite.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 454d544

Please sign in to comment.