Skip to content

Instantly share code, notes, and snippets.

@outworlder
Created February 2, 2013 00:40
Show Gist options
  • Save outworlder/4695257 to your computer and use it in GitHub Desktop.
Save outworlder/4695257 to your computer and use it in GitHub Desktop.
Moderation Magic - Work in progress
module ModeratedModel
extend ActiveSupport::Concern
included do
belongs_to :admin_approval
scope :moderated, where(moderated: true)
scope :not_moderated, where(moderated: false)
end
module ClassMethods
def moderates(field)
self.instance_eval do
before_save do
# The object can start its life moderated already. This is mostly used for testing or inside RailsAdmin.
# In this case, we ignore it.
return true if self.new_record? && self.moderated
# These 'to_s' are there to guard against a field being changed from nil to ''
# RailsAdmin does that sometimes
if self.send("#{field}_changed?") && (self.send("#{field}_was").to_s != self.send("#{field}").to_s)
self.moderated = false
AdminApproval.create!(entity_type: self.class.to_s, content: field, moderated: false)
end
true
end
end
end
end
end
# usage (for example, user.rb)
class User < ActiveRecord::Base
#blahblah
include ModeratedModel
moderates :bio
end
@outworlder
Copy link
Author

Não suporta múltiplos campos, but who cares? 10 min pra fazer, mas eu não preciso disso...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment