Sha256: 5e06ab26ddb032354c5ecd82c9f0c7f686b76204eaa9c963473d6c78b5c29e26

Contents?: true

Size: 1.25 KB

Versions: 14

Compression:

Stored size: 1.25 KB

Contents

# WIP
module Blocks
  module BuilderPermissions
    extend ActiveSupport::Concern

    METHODS_TO_PROTECT = [:render, :define, :deferred_render, :replace, :skip]

    included do
      attr_writer :permitted_blocks
      attr_accessor :allow_all_blocks
      attr_accessor :allow_anonymous_blocks

      METHODS_TO_PROTECT.each do |method_name|
        alias_method "original_#{method_name}", method_name
        define_method(method_name) do |*args, &block|
          if permitted?(args.first)
            self.send("original_#{method_name}", *args, &block)
          else
            InvalidPermissionsHandler.build(method_name, args.first)
          end
        end
      end
    end

    def restrict_blocks
      self.allow_all_blocks = false
    end

    def permitted_blocks
      @permitted_blocks ||= []
    end

    def permit(*names)
      self.permitted_blocks += names
    end

    def permit_all
      self.allow_all_blocks = true
    end

    def permit_anonymous_blocks
      self.allow_anonymous_blocks = true
    end

    def permitted?(name)
      allow_all_blocks ||
      (block_definitions[name].try(:anonymous) && allow_anonymous_blocks) ||
      !name.respond_to?(:to_sym) ||
      permitted_blocks.any? {|e| e.to_sym == name.to_sym }
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
blocks-3.0.4 lib/blocks/experimental/builder_permissions.rb
blocks-3.0.3 lib/blocks/experimental/builder_permissions.rb
blocks-3.0.2 lib/blocks/experimental/builder_permissions.rb
blocks-3.0.1 lib/blocks/experimental/builder_permissions.rb
blocks-3.0.0 lib/blocks/experimental/builder_permissions.rb
blocks-3.0.0.rc9 lib/blocks/experimental/builder_permissions.rb
blocks-3.0.0.rc8 lib/blocks/experimental/builder_permissions.rb
blocks-3.0.0.rc7 lib/blocks/experimental/builder_permissions.rb
blocks-3.0.0.rc6 lib/blocks/experimental/builder_permissions.rb
blocks-3.0.0.rc5 lib/blocks/experimental/builder_permissions.rb
blocks-3.0.0.rc4 lib/blocks/experimental/builder_permissions.rb
blocks-3.0.0.rc3 lib/blocks/experimental/builder_permissions.rb
blocks-3.0.0.rc2 lib/blocks/experimental/builder_permissions.rb
blocks-3.0.0.rc1 lib/blocks/experimental/builder_permissions.rb