Sha256: e4f4a98db810cf79f037b1599e4a602b7f2c08e8fd38048a6d8f8372eb79afa2

Contents?: true

Size: 1.15 KB

Versions: 118

Compression:

Stored size: 1.15 KB

Contents

module Scrivito
  class RestrictionSet
    Restriction = Struct.new(:attribute, :callable) do
      def call(obj)
        if skip?(obj)
          false
        else
          value = obj[attribute]

          callable.call(value)
        end
      end

      private

      def skip?(obj)
        !obj.has_attribute?(attribute) || obj.type_of_attribute(attribute) == 'widgetlist'
      end
    end

    def add(options, &block)
      options = options.with_indifferent_access
      attribute = options.fetch(:using) do
        raise ScrivitoError, 'No "using" option given when adding a publish restriction'
      end

      unless block_given?
        raise ScrivitoError, 'No block provided when adding a publish restriction'
      end

      restrictions << Restriction.new(attribute, block)
    end

    def restriction_messages_for(obj)
      restriction_messages = restrictions.inject([]) do |messages, restriction|
        message = restriction.call(obj)

        if message
          messages << message.to_s
        end

        messages
      end

      restriction_messages.uniq
    end

    private

    def restrictions
      @restrictions ||= []
    end
  end
end

Version data entries

118 entries across 118 versions & 1 rubygems

Version Path
scrivito_sdk-1.12.0.rc2 app/cms/scrivito/restriction_set.rb
scrivito_sdk-1.12.0.rc1 app/cms/scrivito/restriction_set.rb
scrivito_sdk-1.11.0 app/cms/scrivito/restriction_set.rb
scrivito_sdk-1.11.0.rc1 app/cms/scrivito/restriction_set.rb
scrivito_sdk-1.10.0 app/cms/scrivito/restriction_set.rb
scrivito_sdk-1.10.0.rc5 app/cms/scrivito/restriction_set.rb
scrivito_sdk-1.10.0.rc4 app/cms/scrivito/restriction_set.rb
scrivito_sdk-1.10.0.rc3 app/cms/scrivito/restriction_set.rb
scrivito_sdk-1.10.0.rc2 lib/scrivito/restriction_set.rb
scrivito_sdk-1.10.0.rc1 lib/scrivito/restriction_set.rb
scrivito_sdk-1.9.1 lib/scrivito/restriction_set.rb
scrivito_sdk-1.9.1.rc1 lib/scrivito/restriction_set.rb
scrivito_sdk-1.9.0 lib/scrivito/restriction_set.rb
scrivito_sdk-1.9.0.rc2 lib/scrivito/restriction_set.rb
scrivito_sdk-1.9.0.rc1 lib/scrivito/restriction_set.rb
scrivito_sdk-1.8.1 lib/scrivito/restriction_set.rb
scrivito_sdk-1.8.1.rc1 lib/scrivito/restriction_set.rb
scrivito_sdk-1.8.0 lib/scrivito/restriction_set.rb
scrivito_sdk-1.8.0.rc2 lib/scrivito/restriction_set.rb
scrivito_sdk-1.8.0.rc1 lib/scrivito/restriction_set.rb