Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 6 commits into from Apr 10, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -7,3 +7,4 @@ spec/dummy/log/*.log
spec/dummy/tmp/ spec/dummy/tmp/
*.swp *.swp
*.gem *.gem
.rvmrc
1 change: 0 additions & 1 deletion .rvmrc

This file was deleted.

6 changes: 3 additions & 3 deletions app/controllers/opinio/comments_controller.rb
Expand Up @@ -10,9 +10,9 @@ def create
@comment = resource.comments.build(params[:comment]) @comment = resource.comments.build(params[:comment])
@comment.owner = send(Opinio.current_user_method) @comment.owner = send(Opinio.current_user_method)
if @comment.save if @comment.save
messages = {:notice => I18n.translate('opinio.comment.sent', :default => "Comment sent successfully.")} messages = { :notice => t('opinio.messages.comment_sent') }
else else
messages = {:error => I18n.translate('opinio.comment.error', :default => "Error sending the comment.")} messages = { :error => t('opinio.messages.comment_sending_error') }
end end


respond_to do |format| respond_to do |format|
Expand All @@ -26,7 +26,7 @@ def destroy


if can_destroy_opinio?(@comment) if can_destroy_opinio?(@comment)
@comment.destroy @comment.destroy
flash[:notice] = I18n.translate('opinio.comment.destroyed', :default => "Comment removed successfully") flash[:notice] = t('opinio.messages.comment_destroyed')
else else
#flash[:error] = I18n.translate('opinio.comment.not_permitted', :default => "Not permitted") #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}" logger.warn "user #{send(Opinio.current_user_method)} tried to remove a comment from another user #{@comment.owner.id}"
Expand Down
6 changes: 3 additions & 3 deletions app/views/opinio/comments/_comment.html.erb
Expand Up @@ -2,12 +2,12 @@
<li id="comment_<%= comment.id %>"> <li id="comment_<%= comment.id %>">
<span>User: <%= comment.owner.id %></span> <span>User: <%= comment.owner.id %></span>
<p> <p>
<%= comment.body %> <%= simple_format(comment.body) %>
</p> </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 %> <%# this enables only 1 level of replies %>
<% if Opinio.accept_replies && !reply %> <% 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"> <ul id="comment_<%= comment.id %>_replies" class="replies">
<%= render :partial => "opinio/comments/comment", :collection => comment.comments, :locals => {:reply => true} %> <%= render :partial => "opinio/comments/comment", :collection => comment.comments, :locals => {:reply => true} %>
</ul> </ul>
Expand Down
2 changes: 1 addition & 1 deletion app/views/opinio/comments/_comments.html.erb
Expand Up @@ -6,7 +6,7 @@
<% unless comments.empty? %> <% unless comments.empty? %>
<%= render :partial => 'opinio/comments/comment', :collection => comments %> <%= render :partial => 'opinio/comments/comment', :collection => comments %>
<% else %> <% 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 %> <% end %>
</ul> </ul>


Expand Down
11 changes: 5 additions & 6 deletions app/views/opinio/comments/_new.html.erb
@@ -1,18 +1,17 @@
<div id="new_comment"> <div id="new_comment">
<% if send(Opinio.current_user_method) %> <% if send(Opinio.current_user_method) %>
<h3>Send Comments</h3> <h3><%= t('opinio.messages.add_comment') %></h3>
<%= form_for Comment.new, :remote => true do |f| %> <%= form_for Comment.new, :remote => false do |f| %>
<p> <p>
<%= f.label :body, "Comment" %> <%= f.text_area :body %>
<%= f.text_area :body, :id => "Comment" %>
</p> </p>
<%= hidden_field_tag :commentable_id, commentable.id %> <%= hidden_field_tag :commentable_id, commentable.id %>
<%= hidden_field_tag :commentable_type, commentable.class.base_class.name.to_s %> <%= hidden_field_tag :commentable_type, commentable.class.base_class.name.to_s %>
<%= f.submit "Send Comment" %> <%= f.submit t('opinio.actions.add') %>
<% end %> <% end %>
<% else %> <% else %>
<p> <p>
Must be logged in to comment. <%= t('opinio.messages.must_be_logged_in_to_comment') %>
</p> </p>
<% end %> <% end %>
</div> </div>
13 changes: 7 additions & 6 deletions app/views/opinio/comments/create.js.erb
@@ -1,14 +1,15 @@
$('#no_comments').fadeOut() $('#no_comments').hide();
<% if @comment.valid? %> <% if @comment.valid? %>
<% if @reply %> <% if @reply %>
if($('#comment_<%= @comment.commentable_id %> ul').length == 0) 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 %>').append('<ul id="comment_<%= @comment.commentable_id %>_replies" class="replies"></ul>');
$('#comment_<%= @comment.commentable_id %>_replies').append("<%= escape_javascript( render @comment, :locals => {:reply => @reply} ) %>") $('#comment_<%= @comment.commentable_id %>_replies').append("<%= escape_javascript( render @comment, :locals => {:reply => @reply} ) %>");
$('#commentable_id').val('<%= @comment.commentable.commentable_id %>') $('#commentable_id').val('<%= @comment.commentable.commentable_id %>');
$('#commentable_type').val('<%= @comment.commentable.commentable_type %>') $('#commentable_type').val('<%= @comment.commentable.commentable_type %>');
<% else %> <% 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 %> <% end %>
$('textarea#comment_body').val('');
<% else %> <% else %>
$('#comments').prepend("<%= escape_javascript(flash[:notice]) %>"); $('#comments').prepend("<%= escape_javascript(flash[:notice]) %>");
$('#comments').prepend("<%= escape_javascript(flash[:error]) %>"); $('#comments').prepend("<%= escape_javascript(flash[:error]) %>");
Expand Down
2 changes: 1 addition & 1 deletion app/views/opinio/comments/index.html.erb
@@ -1,4 +1,4 @@
<h1>Comments</h1> <h1><%= t('opinio.messages.comments') %></h1>


<%= paginate @comments %> <%= paginate @comments %>


Expand Down
8 changes: 4 additions & 4 deletions app/views/opinio/comments/reply.js.erb
@@ -1,4 +1,4 @@
$("#commentable_id").val('<%= @commentable_id %>') $("#commentable_id").val('<%= @commentable_id %>');
$("#commentable_type").val('<%= @commentable_type %>') $("#commentable_type").val('<%= @commentable_type %>');
$("#new_comment textarea").val('') $("#new_comment textarea").val('');
$("#new_comment textarea").focus() $("#new_comment textarea").focus();
16 changes: 16 additions & 0 deletions 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"
Expand Up @@ -14,4 +14,10 @@ Opinio.setup do |config|
# Here you can change the method called to check who is the current user # Here you can change the method called to check who is the current user
# config.current_user_method = :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 end
6 changes: 6 additions & 0 deletions lib/opinio.rb
Expand Up @@ -31,6 +31,12 @@ module Controllers
mattr_accessor :current_user_method mattr_accessor :current_user_method
@@current_user_method = :current_user @@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 def self.setup
yield self yield self
end end
Expand Down
18 changes: 11 additions & 7 deletions lib/opinio/opinio_model.rb
Expand Up @@ -71,6 +71,11 @@ def opinio(*args)
send :include, RepliesSupport send :include, RepliesSupport
end end


if Opinio.strip_html_tags_on_save
send :include, ActionView::Helpers::SanitizeHelper
before_save :strip_html_tags
end

end end
end end


Expand All @@ -93,12 +98,7 @@ def last_comment_time
if (Time.now - last_comment.created_at).round >= self.comments_interval if (Time.now - last_comment.created_at).round >= self.comments_interval
true true
else else
errors.add( errors.add(:created_at, I18n.translate('opinio.messages.comment_interval', :time => self.comments_interval))
:created_at,
I18n.t('opinio.comment_interval',
:time => self.comments_interval,
:default => "You must wait %{time} seconds to comment again.")
)
false false
end end
else else
Expand All @@ -110,11 +110,15 @@ def last_comment_time
def cannot_be_comment_of_a_comments_comment def cannot_be_comment_of_a_comments_comment
if new_record? && self.commentable_type == Opinio.model_name if new_record? && self.commentable_type == Opinio.model_name
if commentable.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 end
end end


def strip_html_tags
self.body = strip_tags(self.body)
end

end end
end end
end end
2 changes: 1 addition & 1 deletion lib/opinio/opinio_subjectum.rb
Expand Up @@ -12,7 +12,7 @@ def opinio_subjectum(*args)
has_many :comments, has_many :comments,
:class_name => Opinio.model_name, :class_name => Opinio.model_name,
:as => :commentable, :as => :commentable,
:order => options.reverse_merge(:order => "created_at DESC")[:order], :order => options.reverse_merge(:order => "created_at #{Opinio.sort_order}")[:order],
:dependent => :destroy :dependent => :destroy




Expand Down
17 changes: 17 additions & 0 deletions spec/opinio_spec.rb
@@ -1,6 +1,7 @@
require 'spec_helper' require 'spec_helper'


describe Opinio do describe Opinio do

it "should be valid module" do it "should be valid module" do
Opinio.should be_a(Module) Opinio.should be_a(Module)
end end
Expand All @@ -12,6 +13,8 @@
Opinio.use_title.should == false Opinio.use_title.should == false
Opinio.use_title = true Opinio.use_title = true
Opinio.use_title.should == true Opinio.use_title.should == true
Opinio.strip_html_tags_on_save.should == true
Opinio.sort_order.should == "DESC"
end end


it "should accept identifiers" do it "should accept identifiers" do
Expand Down Expand Up @@ -62,4 +65,18 @@


end 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 end
4 changes: 2 additions & 2 deletions spec/support/helpers.rb
@@ -1,6 +1,6 @@


def create_valid_comment def create_valid_comment(body = 'A comment')
comment = Comment.new(:body => 'A comment') comment = Comment.new(:body => body)
comment.owner = User.first || User.create! comment.owner = User.first || User.create!
comment.commentable = create_valid_post comment.commentable = create_valid_post
comment.save comment.save
Expand Down