Sha256: 9b43c7b996fe990ed8e1a134bdc8ffaafbb43c84a362cf41f70292f5d734fef2

Contents?: true

Size: 1.33 KB

Versions: 2

Compression:

Stored size: 1.33 KB

Contents

require 'active_support/concern'

module TheModerator
  module ModerationModel
    extend ActiveSupport::Concern

    included do
      belongs_to :moderatable, polymorphic: true
      serialize :data, coder: YAML
      serialize :data_display, coder: YAML
    end

    module ClassMethods
    end

    def accept
      self.class.transaction do
        destroy
        moderatable.update(data)
      end
    end

    def accept!
      accept || raise(TheModerator::ModerationNotAccepted)
    end

    def discard
      destroy
    end

    def preview
      preview = moderatable.clone
      preview.attributes = data
      preview.freeze
    end

    def parsed_data
      data
    end

    def parsed_data_display
      data_display
    end

    def include?(attribute)
      include_attribute?(attribute, data[:attributes])
    end

    def moderated_fields_for(assoc)
      (data[:attributes][assoc].try(:keys) || []) - [:id]
    end

    private

    def include_attribute?(attribute, attr_data)
      return false if attr_data.nil?
      if attribute.is_a?(Hash)
        include_assoc?(attribute, attr_data)
      else
        attr_data.keys.include?(attribute)
      end
    end

    def include_assoc?(attribute, assoc_data)
      include_attribute?(attribute.first.last,
                         assoc_data[attribute.first.first])
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
the_moderator-0.1.7 lib/the_moderator/moderation_model.rb
the_moderator-0.1.6 lib/the_moderator/moderation_model.rb