Moderate has_many and has_one associations

master
Guillaume DOTT 2013-11-27 11:54:21 +01:00
parent 76ba4d22de
commit 539883ec67
1 changed files with 19 additions and 4 deletions

View File

@ -43,14 +43,29 @@ module TheModerator
def moderate_association(assoc, moderated_attributes)
assoc_fields = {}
send(assoc).each do |resource|
if respond_to?("#{assoc}_attributes=")
data = resource.moderation_data(*moderated_attributes)
assoc_fields[resource.id] = data.merge(id: resource.id) unless data.empty?
objects = send(assoc)
if respond_to?("#{assoc}_attributes=")
if objects.is_a?(Array)
assoc_fields = moderate_has_many_association(objects, moderated_attributes)
else
data = objects.moderation_data(*moderated_attributes)
assoc_fields = data.merge(id: objects.id) unless data.empty?
end
end
assoc_fields
end
def moderate_has_many_association(objects, moderated_attributes)
assoc_fields = {}
objects.each do |resource|
data = resource.moderation_data(*moderated_attributes)
assoc_fields[resource.id] = data.merge(id: resource.id) unless data.empty?
end
assoc_fields
end
end
end