Permalink
Browse files

Removed unnecessary options and more refactoring on opinio_model

Separating grouped functionality into modules and removing unnecessary
options from the opinio method. Also added the test for strip_html_on_save
when the option is false.
  • Loading branch information...
1 parent 6a71958 commit 20a72d78da31924fcae7f69c85dc1189ea0c7a46 @Draiken committed Jul 24, 2012
Showing with 95 additions and 76 deletions.
  1. +1 −0 .gitignore
  2. +14 −61 lib/opinio/opinio_model.rb
  3. +50 −0 lib/opinio/opinio_model/validations.rb
  4. +28 −0 lib/opinio/shared_examples.rb
  5. +0 −13 spec/opinio_spec.rb
  6. +2 −2 spec/support/helpers.rb
View
@@ -11,3 +11,4 @@ spec/dummy/tmp/
.DS_Store
Gemfile.lock
+tags
View
@@ -1,3 +1,5 @@
+require 'opinio/opinio_model/validations'
+
module Opinio
module OpinioModel
@@ -9,73 +11,40 @@ module ClassMethods
# Adds the Opinio functionallity to the model
# You can pass a hash of options to customize the Opinio model
- #
- # === Options
- #
- # [:belongs_to]
- # You can specify the class that owns the comments on
- # +config/initializers/opinio.rb+
- # but you can also pass it explicitly here:
- # eg. <tt>:belongs_to => "Admin"</tt>
- # [:counter_cache]
- # Customize the counter cache here, defaults to false
- # [:body_length]
- # You can pass a <tt>Range</tt> to determine the size of the body that will be
- # validated
- # [:title_length]
- # If you are using titles in your opinio model (set on the
- # initializer) you can pass a <tt>Range</tt> so it is validated.
def opinio(*args)
return if self.included_modules.include?(Opinio::OpinioModel::InstanceMethods)
options = args.extract_options!
if Opinio.use_title
attr_accessible :title
- validates :title,
- {:presence => true}.merge( :length => options[:title_length] )
end
attr_accessible :body
- commentable_options = { :polymorphic => true }
- if options[:counter_cache]
- commentable_options.merge!(:counter_cache => options[:counter_cache])
- end
-
- belongs_to :commentable, commentable_options
- belongs_to :owner, :class_name => options.reverse_merge(:belongs_to => Opinio.owner_class_name)[:belongs_to]
-
- #TODO: refactor this
- if Opinio.interval_between_comments
- validate :last_comment_time, :if => :new_record?
- cattr_accessor :comments_interval
- self.comments_interval = options.reverse_merge(:time => Opinio.interval_between_comments)[:time]
- end
-
- extra_options = {}
- if options[:body_length]
- extra_options = { :length => options[:body_length] }
- end
-
- validates :body,
- { :presence => true }.reverse_merge(extra_options)
-
- validates_presence_of :commentable
-
- validates :owner, :presence => true, :associated => true
+ belongs_to :commentable, :polymorphic => true, :counter_cache => options.fetch(:counter_cache, false)
+ belongs_to :owner, :class_name => Opinio.owner_class_name
scope :owned_by, lambda {|owner| where('owner_id = ?', owner.id) }
+ send :include, Opinio::OpinioModel::Validations
send :include, Opinio::OpinioModel::InstanceMethods
if Opinio.accept_replies
send :include, RepliesSupport
end
if Opinio.strip_html_tags_on_save
+ send :include, Sanitizing
+ end
+
+ end
+ end
+
+ module Sanitizing
+ def self.included(base)
+ base.class_eval do
send :include, ActionView::Helpers::SanitizeHelper
before_save :strip_html_tags
end
-
end
end
@@ -90,22 +59,6 @@ module InstanceMethods
private
- # Checks the time of the last comment
- # made by the same owner
- def last_comment_time
- last_comment = Comment.owned_by(self.owner).order('created_at DESC').last
- if last_comment
- if (Time.now - last_comment.created_at).round >= self.comments_interval
- true
- else
- errors.add(:created_at, I18n.translate('opinio.messages.comment_interval', :time => self.comments_interval))
- false
- end
- else
- true
- end
- end
-
# Validates that you cannot comment on a comment's comment
def cannot_be_comment_of_a_comments_comment
if new_record? && self.commentable_type == Opinio.model_name
@@ -0,0 +1,50 @@
+module Opinio
+ module OpinioModel
+ module Validations
+
+ extend ActiveSupport::Concern
+
+ included do
+
+ #TODO: refactor this
+ if Opinio.interval_between_comments
+ include Opinio::OpinioModel::Validations::IntervalMethods
+ validate :validate_last_comment_time, :if => :new_record?
+ cattr_accessor :comments_interval
+ self.comments_interval = options.reverse_merge(:time => Opinio.interval_between_comments)[:time]
+ end
+
+ validates :body, :presence => true
+ validates :commentable, :presence => true
+ validates :owner, :presence => true, :associated => true
+
+ end
+
+
+ module IntervalMethods
+
+ private
+
+ # Checks the time of the last comment
+ # made by the same owner
+ def validate_last_comment_time
+ last_comment = Comment.owned_by(self.owner).order('created_at DESC').last
+ if last_comment
+ if (Time.now - last_comment.created_at).round >= self.comments_interval
+ true
+ else
+ errors.add(:created_at,
+ I18n.translate('opinio.messages.comment_interval',
+ :time => self.comments_interval))
+ false
+ end
+ else
+ true
+ end
+ end
+
+ end # IntervalMethods
+
+ end # Validations
+ end # OpinioModel
+end # Opinio
@@ -45,6 +45,34 @@
c.save.should == false
end
+ context "when strip_html_tags_on_save is true" do
+ it "should strip html tags" do
+ comment = create_valid_comment('<h1>Chuck will save us!</h1>')
+ comment.body.should == 'Chuck will save us!'
+ end
+ end
+
+ context "when strip_html_tags_on_save is false" do
+
+ # we have to create a different class
+ # because opinio adds the strip tags callbacks
+ # only once when opinio is called on the class
+ class NoSanitizerComment < ActiveRecord::Base
+ self.table_name = :comments
+ end
+
+ before do
+ Opinio.stub(:strip_html_tags_on_save).and_return(false)
+ NoSanitizerComment.class_eval do
+ opinio
+ end
+ end
+ it "should not strip html tags" do
+ comment = create_valid_comment('<h1>Chuck will save us!</h1>', NoSanitizerComment)
+ comment.body.should == '<h1>Chuck will save us!</h1>'
+ end
+ end
+
end
View
@@ -66,18 +66,5 @@
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
@@ -1,6 +1,6 @@
-def create_valid_comment(body = 'A comment')
- comment = Comment.new(:body => body)
+def create_valid_comment(body = 'A comment', klass = Comment)
+ comment = klass.new(:body => body)
comment.owner = User.first || User.create!
comment.commentable = create_valid_post
comment.save

0 comments on commit 20a72d7

Please sign in to comment.