Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

added .rvmrc to gitignore, fixed a few syntax typos #9

Merged
merged 6 commits into from

3 participants

@paxer

No description provided.

Pavel Kotlyar added some commits
@Draiken
Owner

Thank you very much! I18n was long overdue...

I'll check this as soon as possible and I'll merge and issue a new gem version.

Thanks again!

Pavel Kotlyar added some commits
Pavel Kotlyar 1) added new option - strip_html_tags_on_save 2)added simple_format o…
…n show comment view for allow nl to br conversion
84d41f6
Pavel Kotlyar more i18n 442f74a
@paxer

no probs at all, it's fun :)
One more thing I am going to add is - move comments "order_by" to the config (similar to what I've done with strip_html_tags_on_save).

What do you think ? How to do it better ?

@Draiken
Owner

The test is not gonna pass because the check for strip_html_tags_on_save is made when the opinio is loaded. so even tho you change the option on the setup, the before_save was already called.

@Draiken
Owner

I think it's good this way.

I thought about removing it from opinio, and letting the user set, for example, a default scope on the comment model
but I really don't like default scopes, so I think that's the best way to do it.

Since you're adding another configuration option, you can add it on the generator template for the opinio initializer so that people know they can change that too.

@paxer

yes, I've added "strip_html_tags_on_save" as well as "sort_order" to the generator template with default values.
Regarding the test - that's what I thought too. Any ideas how to make it pass ?

@Draiken
Owner

I really don't know how we can make that test pass...

The behaviour is correct, we don't need to check if the flag is on at runtime :/
I'll try to think of something.

@Draiken
Owner

I'm gonna fix that test as soon as I can. Gonna pull this in for now

@Draiken Draiken merged commit 4947fb0 into Draiken:master
@fred

Cool, thanks for this Pull.
I had done very similar customizations to the views, now it's official.
cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 27, 2012
  1. added .rvmrc to gitignore, fixed a few syntax typos

    Pavel Kotlyar authored
  2. i18n

    Pavel Kotlyar authored
  3. removed .rvmrc from repository

    Pavel Kotlyar authored
Commits on Mar 28, 2012
  1. 1) added new option - strip_html_tags_on_save 2)added simple_format o…

    Pavel Kotlyar authored
    …n show comment view for allow nl to br conversion
  2. more i18n

    Pavel Kotlyar authored
  3. moved comments sort order to the settings/options

    Pavel Kotlyar authored
This page is out of date. Refresh to see the latest.
View
1  .gitignore
@@ -7,3 +7,4 @@ spec/dummy/log/*.log
spec/dummy/tmp/
*.swp
*.gem
+.rvmrc
View
1  .rvmrc
@@ -1 +0,0 @@
-rvm use 1.9.2@opinio
View
6 app/controllers/opinio/comments_controller.rb
@@ -10,9 +10,9 @@ def create
@comment = resource.comments.build(params[:comment])
@comment.owner = send(Opinio.current_user_method)
if @comment.save
- messages = {:notice => I18n.translate('opinio.comment.sent', :default => "Comment sent successfully.")}
+ messages = { :notice => t('opinio.messages.comment_sent') }
else
- messages = {:error => I18n.translate('opinio.comment.error', :default => "Error sending the comment.")}
+ messages = { :error => t('opinio.messages.comment_sending_error') }
end
respond_to do |format|
@@ -26,7 +26,7 @@ def destroy
if can_destroy_opinio?(@comment)
@comment.destroy
- flash[:notice] = I18n.translate('opinio.comment.destroyed', :default => "Comment removed successfully")
+ flash[:notice] = t('opinio.messages.comment_destroyed')
else
#flash[:error] = I18n.translate('opinio.comment.not_permitted', :default => "Not permitted")
logger.warn "user #{send(Opinio.current_user_method)} tried to remove a comment from another user #{@comment.owner.id}"
View
6 app/views/opinio/comments/_comment.html.erb
@@ -2,12 +2,12 @@
<li id="comment_<%= comment.id %>">
<span>User: <%= comment.owner.id %></span>
<p>
- <%= comment.body %>
+ <%= simple_format(comment.body) %>
</p>
- <%= link_to 'Destroy', comment_path(comment), :method => :delete, :remote => true %>
+ <%= link_to t('opinio.actions.delete'), comment_path(comment), :method => :delete, :remote => true %>
<%# this enables only 1 level of replies %>
<% if Opinio.accept_replies && !reply %>
- <span><%= link_to t("opinio.reply", :default => "Reply"), reply_comment_path(comment), :remote => true %></span>
+ <span><%= link_to t('opinio.actions.reply'), reply_comment_path(comment), :remote => true %></span>
<ul id="comment_<%= comment.id %>_replies" class="replies">
<%= render :partial => "opinio/comments/comment", :collection => comment.comments, :locals => {:reply => true} %>
</ul>
View
2  app/views/opinio/comments/_comments.html.erb
@@ -6,7 +6,7 @@
<% unless comments.empty? %>
<%= render :partial => 'opinio/comments/comment', :collection => comments %>
<% else %>
- <li id="no_comments"><%= t 'opinio.no_comments_found', :default => "No comments found" %></li>
+ <li id="no_comments"><%= t('opinio.messages.no_comments_found') %></li>
<% end %>
</ul>
View
11 app/views/opinio/comments/_new.html.erb
@@ -1,18 +1,17 @@
<div id="new_comment">
<% if send(Opinio.current_user_method) %>
- <h3>Send Comments</h3>
- <%= form_for Comment.new, :remote => true do |f| %>
+ <h3><%= t('opinio.messages.add_comment') %></h3>
+ <%= form_for Comment.new, :remote => false do |f| %>
<p>
- <%= f.label :body, "Comment" %>
- <%= f.text_area :body, :id => "Comment" %>
+ <%= f.text_area :body %>
</p>
<%= hidden_field_tag :commentable_id, commentable.id %>
<%= hidden_field_tag :commentable_type, commentable.class.base_class.name.to_s %>
- <%= f.submit "Send Comment" %>
+ <%= f.submit t('opinio.actions.add') %>
<% end %>
<% else %>
<p>
- Must be logged in to comment.
+ <%= t('opinio.messages.must_be_logged_in_to_comment') %>
</p>
<% end %>
</div>
View
13 app/views/opinio/comments/create.js.erb
@@ -1,14 +1,15 @@
-$('#no_comments').fadeOut()
+$('#no_comments').hide();
<% if @comment.valid? %>
<% if @reply %>
if($('#comment_<%= @comment.commentable_id %> ul').length == 0)
- $('#comment_<%= @comment.commentable_id %>').append('<ul id="comment_<%= @comment.commentable_id %>_replies" class="replies"></ul>')
- $('#comment_<%= @comment.commentable_id %>_replies').append("<%= escape_javascript( render @comment, :locals => {:reply => @reply} ) %>")
- $('#commentable_id').val('<%= @comment.commentable.commentable_id %>')
- $('#commentable_type').val('<%= @comment.commentable.commentable_type %>')
+ $('#comment_<%= @comment.commentable_id %>').append('<ul id="comment_<%= @comment.commentable_id %>_replies" class="replies"></ul>');
+ $('#comment_<%= @comment.commentable_id %>_replies').append("<%= escape_javascript( render @comment, :locals => {:reply => @reply} ) %>");
+ $('#commentable_id').val('<%= @comment.commentable.commentable_id %>');
+ $('#commentable_type').val('<%= @comment.commentable.commentable_type %>');
<% else %>
- $('#comments').prepend("<%= escape_javascript( render @comment, :locals => {:reply => @reply} ) %>")
+ $('#comments').<%= Opinio.sort_order == 'ASC' ? 'append' : 'prepend'%>("<%= escape_javascript( render @comment, :locals => {:reply => @reply} ) %>");
<% end %>
+ $('textarea#comment_body').val('');
<% else %>
$('#comments').prepend("<%= escape_javascript(flash[:notice]) %>");
$('#comments').prepend("<%= escape_javascript(flash[:error]) %>");
View
2  app/views/opinio/comments/index.html.erb
@@ -1,4 +1,4 @@
-<h1>Comments</h1>
+<h1><%= t('opinio.messages.comments') %></h1>
<%= paginate @comments %>
View
8 app/views/opinio/comments/reply.js.erb
@@ -1,4 +1,4 @@
-$("#commentable_id").val('<%= @commentable_id %>')
-$("#commentable_type").val('<%= @commentable_type %>')
-$("#new_comment textarea").val('')
-$("#new_comment textarea").focus()
+$("#commentable_id").val('<%= @commentable_id %>');
+$("#commentable_type").val('<%= @commentable_type %>');
+$("#new_comment textarea").val('');
+$("#new_comment textarea").focus();
View
16 config/locales/opinio.en.yml
@@ -0,0 +1,16 @@
+en:
+ opinio:
+ actions:
+ delete: 'Delete'
+ reply: 'Reply'
+ add: 'Add comment'
+ messages:
+ no_comments_found: 'No comments found'
+ must_be_logged_in_to_comment: 'Must be logged in to comment.'
+ add_comment: 'Add comment'
+ comments: 'Comments'
+ comment_sent: 'Comment sent successfully.'
+ comment_sending_error: 'Error while sending the comment.'
+ comment_destroyed: 'Comment removed successfully.'
+ comment_interval: 'You must wait %{time} seconds to comment again.'
+ cannot_be_comment_of_comment: "Cannot reply another comment's reply"
View
6 lib/generators/opinio/install/templates/initializers/opinio.erb
@@ -14,4 +14,10 @@ Opinio.setup do |config|
# Here you can change the method called to check who is the current user
# config.current_user_method = :current_user
+ # Strip html tags on save comment
+ config.strip_html_tags_on_save = true
+
+ # Comments sort order by created_at (DESC or ASC)
+ config.sort_order = 'DESC'
+
end
View
6 lib/opinio.rb
@@ -31,6 +31,12 @@ module Controllers
mattr_accessor :current_user_method
@@current_user_method = :current_user
+ mattr_accessor :strip_html_tags_on_save
+ @@strip_html_tags_on_save = true
+
+ mattr_accessor :sort_order
+ @@sort_order = 'DESC'
+
def self.setup
yield self
end
View
18 lib/opinio/opinio_model.rb
@@ -71,6 +71,11 @@ def opinio(*args)
send :include, RepliesSupport
end
+ if Opinio.strip_html_tags_on_save
+ send :include, ActionView::Helpers::SanitizeHelper
+ before_save :strip_html_tags
+ end
+
end
end
@@ -93,12 +98,7 @@ def last_comment_time
if (Time.now - last_comment.created_at).round >= self.comments_interval
true
else
- errors.add(
- :created_at,
- I18n.t('opinio.comment_interval',
- :time => self.comments_interval,
- :default => "You must wait %{time} seconds to comment again.")
- )
+ errors.add(:created_at, I18n.translate('opinio.messages.comment_interval', :time => self.comments_interval))
false
end
else
@@ -110,11 +110,15 @@ def last_comment_time
def cannot_be_comment_of_a_comments_comment
if new_record? && self.commentable_type == Opinio.model_name
if commentable.commentable_type == Opinio.model_name
- errors.add :base, I18n.translate('opinio.cannot_be_comment_of_comment', :default => 'Cannot reply another comment\'s reply')
+ errors.add :base, I18n.t('opinio.messages,cannot_be_comment_of_comment')
end
end
end
+ def strip_html_tags
+ self.body = strip_tags(self.body)
+ end
+
end
end
end
View
2  lib/opinio/opinio_subjectum.rb
@@ -12,7 +12,7 @@ def opinio_subjectum(*args)
has_many :comments,
:class_name => Opinio.model_name,
:as => :commentable,
- :order => options.reverse_merge(:order => "created_at DESC")[:order],
+ :order => options.reverse_merge(:order => "created_at #{Opinio.sort_order}")[:order],
:dependent => :destroy
View
17 spec/opinio_spec.rb
@@ -1,6 +1,7 @@
require 'spec_helper'
describe Opinio do
+
it "should be valid module" do
Opinio.should be_a(Module)
end
@@ -12,6 +13,8 @@
Opinio.use_title.should == false
Opinio.use_title = true
Opinio.use_title.should == true
+ Opinio.strip_html_tags_on_save.should == true
+ Opinio.sort_order.should == "DESC"
end
it "should accept identifiers" do
@@ -62,4 +65,18 @@
end
+ it "should strip html tags on save if strip_html_tags_on_save is true" do
+ comment = create_valid_comment('<h1>Chuck will save us!</h1>')
+ comment.body.should == 'Chuck will save us!'
+ end
+
+ #TODO: Fixme, for some reasons test does not work
+ #it "should not strip html tags on save if strip_html_tags_on_save is false" do
+ # Opinio.setup do |c|
+ # c.strip_html_tags_on_save = false
+ # end
+ # comment = create_valid_comment('<h1>Chuck will save us!</h1>')
+ # comment.body.should == '<h1>Chuck will save us!</h1>'
+ #end
+
end
View
4 spec/support/helpers.rb
@@ -1,6 +1,6 @@
-def create_valid_comment
- comment = Comment.new(:body => 'A comment')
+def create_valid_comment(body = 'A comment')
+ comment = Comment.new(:body => body)
comment.owner = User.first || User.create!
comment.commentable = create_valid_post
comment.save
Something went wrong with that request. Please try again.