Add field for displayed data

master
Michèle Marais 2014-04-24 14:21:43 +02:00
parent d47bc11145
commit 6f09cf80a7
3 changed files with 33 additions and 11 deletions

View File

@ -3,6 +3,7 @@ class CreateModerations < ActiveRecord::Migration
create_table "moderations" do |t|
t.references :moderatable, polymorphic: true
t.text :data, :null => false
t.text :data_display, :null => false
t.timestamps
end

View File

@ -13,7 +13,8 @@ module TheModerator
def moderate(*moderated_attributes)
data = moderation_data(*moderated_attributes)
moderations.build(data: {attributes: data}) unless data.empty?
moderations.build(data: {attributes: data[:data]},
data_display: data[:data_display]) unless data.empty?
end
def moderated?(attr_name)
@ -37,47 +38,62 @@ module TheModerator
private
def moderate_object(moderated_attributes)
object_fields = {}
object_fields, object_fields_display = {}, {}
moderated_attributes.each do |attribute|
if attribute.is_a?(Hash)
attribute.each do |key, value|
data = moderate_association(key, value)
object_fields[:"#{key}_attributes"] = data unless data.empty?
object_fields[:"#{key}_attributes"] = data[:data] unless data[:data].empty?
object_fields_display.merge!(data[:data_display]) unless data[:data_display].empty?
end
elsif changed.include?(attribute.to_s)
object_fields[attribute.to_sym] = send(attribute)
class_name ||= self.class.name.to_sym
object_fields_display[class_name] ||= {}
object_fields_display[class_name].merge!(attribute.to_sym => send(attribute))
send("#{attribute}=", changed_attributes[attribute.to_s])
end
end
object_fields
{ data: object_fields, data_display: object_fields_display }
end
def moderate_association(assoc, moderated_attributes)
assoc_fields = {}
assoc_fields, assoc_fields_display = {}, {}
objects = send(assoc)
if respond_to?("#{assoc}_attributes=")
if objects.is_a?(Array)
assoc_fields = moderate_has_many_association(objects, moderated_attributes)
data = moderate_has_many_association(objects, moderated_attributes)
assoc_fields = data[:data]
assoc_fields_display = data[:data_display]
else
data = objects.moderation_data(*moderated_attributes)
assoc_fields = data.merge(id: objects.id) unless data.empty?
assoc_fields = data[:data].merge(id: objects.id) unless data[:data].empty?
assoc_fields_display = data[:data_display] unless data[:data_display].empty?
end
end
assoc_fields
{ data: assoc_fields, data_display: assoc_fields_display }
end
def moderate_has_many_association(objects, moderated_attributes)
assoc_fields = {}
assoc_fields, assoc_fields_display = {}, {}
tab = []
objects.each do |resource|
data = resource.moderation_data(*moderated_attributes)
assoc_fields[resource.id] = data.merge(id: resource.id) unless data.empty?
assoc_fields[resource.id] = data[:data].merge(id: resource.id) unless data[:data].empty?
unless data[:data_display].empty?
key = data[:data_display].keys.first
assoc_fields_display[key] ||= {}
assoc_fields_display[key] = tab.push(data[:data])
end
end
assoc_fields
{ data: assoc_fields, data_display: assoc_fields_display }
end
end
end

View File

@ -7,6 +7,7 @@ module TheModerator
included do
belongs_to :moderatable, polymorphic: true
serialize :data
serialize :data_display
end
module ClassMethods
@ -37,6 +38,10 @@ module TheModerator
data
end
def parsed_data_display
data_display
end
def include?(attribute)
include_attribute?(attribute, data[:attributes])
end