Permalink
Browse files

Introducing Presenters for comment and attachment.

  • Loading branch information...
1 parent 8130f2e commit 42442cc25b4ab27b2a2f013d1430b209737fda78 @iboard committed Oct 5, 2011
View
16 app/assets/stylesheets/cba.css.scss
@@ -358,12 +358,6 @@ li.load_more {
font-size: 0.75em;
}
-.without_shadow {
- box-shadow: none;
- width: 64px;
- height: 64px;
-}
-
/* Load new definitions and overwrite previous ones */
@import "errors.css.scss";
@import "banner_and_session.css.scss";
@@ -380,3 +374,13 @@ li.load_more {
@import "will_paginate.css.scss";
@import "forms.css.scss";
@import "css3buttons.css.scss";
+
+.main-frame #container img.without-shadow,
+.main-frame #container img.without_shadow,
+.without-shadow,
+.without_shadow {
+ -webkit-box-shadow: 0 0 0 white;
+ box-shadow: 0 0 0 white;
+ width: 48px;
+ height: 48px;
+}
View
7 app/controllers/application_controller.rb
@@ -46,7 +46,7 @@ def is_current_user? usr
end
alias current_user? is_current_user?
- private
+private
# Top Pages are shown in the top-menu
def top_pages
@@ -134,5 +134,10 @@ def setup_asset_host
ActionController::Base.asset_host = DEFAULT_URL
end
end
+
@iboard
iboard Oct 5, 2011

A little helper to be able to initialize a presenter from within a controller.

+ def present(object, klass=nil)
+ klass ||= "#{object.class}Presenter".constantize
+ klass.new(view_context, object)
+ end
end
View
7 app/helpers/application_helper.rb
@@ -112,4 +112,11 @@ def with_format(view, format, &block)
view.formats = old_formats
end
+ def present(object, klass=nil)
@iboard
iboard Oct 5, 2011

Initialize a presenter for any model from within your views.

e.g.:

- present @my_model do |presenter|
  = presenter.display_something
+ klass ||= "#{object.class}Presenter".constantize
+ presenter = klass.new(object, self)
+ yield presenter if block_given?
+ presenter
+ end
+
end
View
1 app/helpers/layout_helper.rb
@@ -50,6 +50,7 @@ def set_browser_address(page,title)
end
# Replace blanks by %20 to satisfy w3c-validators
+ # Attantion: This is implemented in BasePresenter too!
@iboard
iboard Oct 5, 2011

Presenters will use this function. To make it visible to presenters I moved this to the BasePresenter.

def w3c_url(url)
url.gsub(' ', '%20')
end
View
11 app/presenters/attachment_presenter.rb
@@ -0,0 +1,11 @@
+class AttachmentPresenter < BasePresenter
+ presents :attachment
+
+ def image_or_link
+ if attachment.file.content_type =~ /image/
+ link_to_function( h.image_tag( w3c_url(attachment.file.url(:icon)) ), "image_popup('#{attachment.file.url(:popup)}')")
+ else
+ link_button attachment.file.original_filename, "button download small", attachment.file.url()
+ end
+ end
+end
View
32 app/presenters/base_presenter.rb
@@ -0,0 +1,32 @@
+class BasePresenter
+ attr_reader :object
+ def initialize(object, template)
+ @object = object
+ @template = template
+ end
+
+private
+ def self.presents(name)
+ define_method(name) do
+ @object
+ end
+ end
+
+ def h
+ @template
+ end
+
+ def markdown(text)
+ Redcarpet.new(text, :hard_wrap, :filter_html, :autolink).to_html.html_safe
+ end
+
+ # Replace blanks by %20 to satisfy w3c-validators
+ def w3c_url(url)
+ url.gsub(' ', '%20')
+ end
+
+ def method_missing(*args, &block)
+ @template.send(*args,&block)
+ end
+
+end
View
68 app/presenters/comment_presenter.rb
@@ -0,0 +1,68 @@
+class CommentPresenter < BasePresenter
+ presents :comment
+
+
+ def title_and_link(group=1)
+ if group == 1
+ link_button comment.commentable.title, "button link",
+ commentable_show_path(comment.commentable)
+ content_tag :small do
+ " (" + comment.commentable.class.to_s.humanize + ")"
+ end
+ else
+ I18n.translate(:orphanded_comment).html_safe
+ end
+ end
+
+ def avatar
+ if user=comment.user
+ image_tag( user.avatar_url(:thumb), :class => 'without-shadow', :style => 'float: right;')
+ else
+ image_tag( '/images/avatars/thumb/missing.png', :class => 'without-shadow', :style => 'float: right;')
+ end
+ end
+
+ def user_and_time
+ I18n.translate(:user_wrote_a_comment_at, :user => comment.name || 'Anonymous',
+ :at => distance_of_time_in_words_to_now(comment.created_at)).html_safe
+ end
+
+ def user_time_and_ip
+ user_and_time +
+ " (" + comment.created_at + ", " +
+ I18n.translate(:posted_from_ip, :ip => (comment.from_ip || "n/a") )
+ end
+
+ def render_comment
+ if comment.commentable
+ comment.commentable.render_for_html(comment.comment||'').html_safe
+ else
+ markdown(comment.comment||'')
+ end
+ end
+
+ def comment_class
+ comment.created_at||Time::now > current_user_field(:last_sign_in_at,Time::now()-1.hour) ? "new_comment" : "old_comment"
+ end
+
+ def link_to_edit
+ link_to( comment.time_left_to_edit > 0 ? \
+ I18n.translate(:edit_for_another_count_minutes,:count => comment.time_left_to_edit) :\
+ I18n.translate(:edit),
+ edit_comment_path(comment, (comment.commentable.class.to_s.underscore.downcase+"_id").to_sym => comment.commentable.id.to_s ),
+ :remote => true,
+ :class => "button edit tiny" ) if can?( :manage, comment, session[:comments] )
+ end
+
+ def link_to_destroy
+ if can?( :destroy, session[:comments] )
+ raw link_to( I18n.translate(:destroy),
+ comment_path(comment, (comment.commentable.class.to_s.underscore.downcase+"_id").to_sym => comment.commentable.id.to_s ),
+ :method => :delete,
+ :remote => true,
+ :confirm => t(:are_you_sure),
+ :class => "button delete tiny"
+ ).gsub("rel=\"nofollow\"","")
+ end
+ end
+end
View
2 app/views/attachments/_attachment.haml
@@ -0,0 +1,2 @@
+- present attachment do |p|
+ %li= p.image_or_link
View
8 app/views/attachments/_attachment.html.erb
@@ -1,8 +0,0 @@
-<li>
- <% if attachment.file.content_type =~ /image/ %>
- <%= link_to_function( image_tag( w3c_url(attachment.file.url(:icon)) ),
- "image_popup('#{attachment.file.url(:popup)}')") %>
- <% else %>
- <%= link_button attachment.file.original_filename, "button download small", attachment.file.url() -%>
- <% end %>
-</li>
View
13 app/views/comments/_comment.haml
@@ -0,0 +1,13 @@
+- unless comment.new_record?
+ - present comment do |presenter|
+ %div{:id => "comment_#{comment.id.to_s}", :class => 'comment'}
+ .comment_avatar= presenter.avatar
+ .comment_content
+ %address{ :class => presenter.comment_class }
+ = presenter.user_and_time
+ %br
+ = presenter.link_to_edit
+ = presenter.link_to_destroy
+ %div{ :id => "comment_comment_#{comment.id.to_s}"}
+ = presenter.render_comment
+ .clear
View
40 app/views/comments/_comment.html.erb
@@ -1,40 +0,0 @@
-<% unless comment.new_record? %>
- <%= content_tag :div, :id => "comment_#{comment.id.to_s}", :class => 'comment' do %>
- <div class='comment_avatar'>
- <% if user=comment.user %>
- <%= image_tag( user.avatar_url(:thumb), :class => 'without_shadow') %>
- <% else %>
- <%= image_tag( 'avatars/thumb/missing.png', :class => 'without_shadow') %>
- <% end %>
- </div>
- <div class='comment_content'>
- <address class='<%= comment.created_at||Time::now > current_user_field(:last_sign_in_at,Time::now()-1.hour) ? "new_comment" : "old_comment" -%>'>
- <%= t(:user_wrote_a_comment_at, :user => comment.name || 'Anonymous',
- :at => distance_of_time_in_words_to_now(comment.created_at)).html_safe %>
-
- <%= link_to( comment.time_left_to_edit > 0 ? \
- t(:edit_for_another_count_minutes,:count => comment.time_left_to_edit) :\
- t(:edit),
- edit_comment_path(comment, (comment.commentable.class.to_s.underscore.downcase+"_id").to_sym => comment.commentable.id.to_s ),
- :remote => true,
- :class => "button edit tiny" ) if can?( :manage, comment, session[:comments] )
- %>
-
- <%= raw link_to( t(:destroy),
- comment_path(comment,
- (comment.commentable.class.to_s.underscore.downcase+"_id").to_sym => comment.commentable.id.to_s ),
- :method => :delete,
- :remote => true,
- :confirm => t(:are_you_sure),
- :class => "button delete tiny"
- ).gsub("rel=\"nofollow\"","") if can?( :destroy, session[:comments] )
- %>
- </address>
-
- <div id='comment_comment_<%= comment.id.to_s -%>'>
- <%= sanitize(comment.commentable.render_for_html(comment.comment||""))%>
- </div>
- <div class='clear'></div>
- </div>
- <% end %>
-<% end %>
View
39 app/views/comments/index.haml
@@ -3,35 +3,10 @@
= comments[0].count.to_s + " / " + comments[1].count.to_s
- for group in [0,1] # 0 => orphaned, 1 => with commentable
-for comment in comments[group]
- %div{:class=>'comment', :id=>'comment_'+comment.id.to_s}
- - if group == 1
- %h2
- = link_button comment.commentable.title, "button link", commentable_show_path(comment.commentable)
- %small
- (
- = comment.commentable.class.to_s.humanize
- )
- - else
- = t(:orphanded_comment)
-
- %p
- - if user=comment.user
- = image_tag( user.avatar_url(:thumb), :class => 'without_shadow')
- - else
- = image_tag( 'avatars/thumb/missing.png', :class => 'without_shadow')
-
- %strong
- = t(:user_wrote_a_comment_at, :user => comment.name || 'Anonymous',:at => distance_of_time_in_words_to_now(comment.created_at) ).html_safe
- (
- = comment.created_at
- ,
- = t(:posted_from_ip, :ip => (comment.from_ip || "n/a") )
- )
-
- %p
- - if comment.commentable
- = comment.commentable.render_for_html(comment.comment||'').html_safe
- - else
- = ContentItem::markdown(comment.comment||'')
- %p
- = link_to( t(:destroy), comment, :method => :delete, :remote => true, :confirm => t(:are_you_sure), :class => 'button delete small' ) if can?( :destroy, comment )
+ - present comment do |presenter|
+ %div{:class=>'comment', :id=>'comment_'+comment.id.to_s}
+ %p= presenter.avatar
+ %h2= presenter.title_and_link(group)
+ %strong= presenter.user_time_and_ip
+ %p= presenter.render_comment
+ %p= link_to( t(:destroy), comment, :method => :delete, :remote => true, :confirm => t(:are_you_sure), :class => 'button delete small' ) if can?( :destroy, comment )
View
3 config/locales/de.yml
@@ -450,4 +450,5 @@ de:
invalid_search: 'Geben sie bitte mindestens 3 Zeichen ein'
update_page_component: 'Änderungen speichern'
page_component_successfully_updated: 'Änderungen wurden gespeichert'
- navigation: 'Navigation'
+ navigation: 'Navigation'
+ orphanded_comment: 'Verwaister Kommentar'
View
3 config/locales/en.yml
@@ -253,4 +253,5 @@ en:
invalid_search: 'Please provide 3 characters or more'
update_page_component: 'Update component'
page_component_successfully_updated: 'Page component successfully updated'
- navigation: 'Navigation'
+ navigation: 'Navigation'
+ orphanded_comment: 'Orphanded comment'

0 comments on commit 42442cc

Please sign in to comment.