Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #9 from paxer/master

Fixing JS typos. Adding i18n and other options
  • Loading branch information...
commit 4947fb09b5d23b5c44f6fd130b9b414060a911f3 2 parents f95c9b5 + 348ac99
@Draiken authored
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
Please sign in to comment.
Something went wrong with that request. Please try again.