Browse Source

Add approval code

* Add approval code generation
* Add a test
* Fix a bug
master
Peter J. Jones 11 years ago
parent
commit
2fc63f6588
5 changed files with 58 additions and 3 deletions
  1. 17
    3
      app/models/comment.rb
  2. 1
    0
      db/migrate/001_create_initial_schema.rb
  3. 1
    0
      init.rb
  4. 27
    0
      test/test_helper.rb
  5. 12
    0
      test/unit/comment_test.rb

+ 17
- 3
app/models/comment.rb View File

@@ -28,7 +28,7 @@ class Comment < ActiveRecord::Base
attr_protected(:user_id, :approved, :was_edited)
################################################################################
validates_presences_of(:content)
validates_presence_of(:content)
################################################################################
belongs_to(:banter, :polymorphic => true)
@@ -36,17 +36,31 @@ class Comment < ActiveRecord::Base
################################################################################
private

################################################################################
def create_approval_code
return unless approval_code.blank?
self.approval_code = Digest::MD5.hexdigest("#{object_id}#{id}#{Time.now.to_i}")
end
################################################################################
before_create do |comment|
# Automatically approve if a logged in user created the comment
comment.approved = true unless comment.user_id.nil?
end
################################################################################
after_create do |comment|
unless comment.approved?
comment.send(:create_approval_code)
Comment.update_all(["approval_code=?", comment.approval_code], ["id=?", comment.id])
end
end
################################################################################
before_save do |comment|
# Cache the contents as HTML
comment.content_cache = RedCloth.new(comment.content).to_html
end

end

+ 1
- 0
db/migrate/001_create_initial_schema.rb View File

@@ -14,6 +14,7 @@ class CreateInitialSchema < ActiveRecord::Migration
t.column(:wants_replies, :boolean, :default => false)
t.column(:was_edited, :boolean, :default => false)
t.column(:approved, :boolean, :default => false)
t.column(:approval_code, :string)
t.column(:content, :text)
t.column(:content_cache, :text)
t.column(:created_at, :datetime)

+ 1
- 0
init.rb View File

@@ -22,3 +22,4 @@
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
################################################################################
require 'digest/md5'

+ 27
- 0
test/test_helper.rb View File

@@ -0,0 +1,27 @@
################################################################################
#
# Copyright (C) 2006-2008 pmade inc. (Peter Jones pjones@pmade.com)
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
################################################################################
require File.join(File.dirname(__FILE__), '/../../../../test/test_helper')
require 'rubygems'
require 'mocha'

+ 12
- 0
test/unit/comment_test.rb View File

@@ -0,0 +1,12 @@
################################################################################
require File.dirname(__FILE__) + '/../test_helper'

################################################################################
class CommentTest < Test::Unit::TestCase
################################################################################
def test_syntax
assert(Comment.new)
end
end

Loading…
Cancel
Save