From 539883ec67ea227d98afea8bab0efb8f80022d1c Mon Sep 17 00:00:00 2001 From: Guillaume DOTT Date: Wed, 27 Nov 2013 11:54:21 +0100 Subject: [PATCH] Moderate has_many and has_one associations --- lib/the_moderator/model.rb | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/the_moderator/model.rb b/lib/the_moderator/model.rb index 4a3393f..a310574 100644 --- a/lib/the_moderator/model.rb +++ b/lib/the_moderator/model.rb @@ -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