Created
February 2, 2013 00:40
-
-
Save outworlder/4695257 to your computer and use it in GitHub Desktop.
Moderation Magic - Work in progress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Não suporta múltiplos campos, but who cares? 10 min pra fazer, mas eu não preciso disso...