Sha256: 3afda71b9d25c446181b070030f461dd3acd6e321b6e1f2dab2893b4d758187f

Contents?: true

Size: 2 KB

Versions: 1

Compression:

Stored size: 2 KB

Contents

module Sections
  module Model
    ##
    # Model for storing section entry revisions and linking these to custom
    # field values.
    #
    # @since 2012-04-29
    #
    class Revision < Sequel::Model
      many_to_one :section_entry,
        :class => 'Sections::Model::SectionEntry'

      many_to_one :user,
        :class => 'Users::Model::User'

      one_to_many :custom_field_values,
        :class => 'CustomFields::Model::CustomFieldValue'

      plugin :timestamps, :create => :created_at

      plugin :association_dependencies,
        :custom_field_values => :delete

      ##
      # Hook executed before inserting a new row. This hook is used to purge
      # for a section entry if the amount of revisions for such an entry exceeds
      # the maximum amount.
      #
      # @since 2012-05-01
      #
      def before_create
        amount = Revision.filter(:section_entry_id => section_entry_id).count
        max    = get_setting(:maximum_revisions).value.to_i

        if amount >= max
          Revision.filter(:section_entry_id => section_entry_id) \
            .order(:id.asc) \
            .first \
            .delete
        end

        super
      end

      ##
      # Hook that is executed before deleting the revision. This hook is used to
      # prevent the last revision of an entry from being deleted.
      #
      # @since 2012-04-29
      #
      def before_destroy
        entry = section_entry
        _id   = id

        # Set the revision ID of the entry to the previous revision.
        if entry.revision_id == _id
          prev_revision = Revision \
            .filter(:section_entry_id => entry.id) { id < _id } \
            .limit(1) \
            .first

          if prev_revision
            entry.update(:revision_id => prev_revision.id)
          else
            raise(
              Sequel::Error::InvalidOperation,
              'You can not delete the last revision'
            )
          end
        end

        super
      end
    end # Revision
  end # Model
end # Sections

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
zen-0.4.3 lib/zen/package/sections/lib/sections/model/revision.rb