Sha256: 9e9c5e87000179c1c542666b133c08e0aa7d6d41dd38ee11f240e2fccd9f3803

Contents?: true

Size: 1.68 KB

Versions: 2

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

require 'active_support/concern'
require_relative './setup_context'

module SnFoil
  module Contexts
    module ShowContext
      extend ActiveSupport::Concern

      included do
        include SetupContext
      end

      class_methods do
        attr_reader :i_setup_show_hooks

        def show(id:, entity: nil, **options)
          new(entity).show(**options, id: id)
        end

        def setup_show(method = nil, **options, &block)
          raise ArgumentError, '#setup_show requires either a method name or a block' if method.nil? && block.nil?

          (@i_setup_show_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
        end
      end

      def setup_show_object(id: nil, object: nil, **options)
        raise ArgumentError, 'one of the following keywords is required: id, object' unless id || object

        options[:object] = wrap_object(object || scope.resolve.find(id))
        options
      end

      def setup_show_hooks
        self.class.i_setup_show_hooks || []
      end

      def show(**options)
        options[:action] = :show
        options = before_setup_show(**options)
        options = setup_show_object(**options)
        authorize(options[:object], :show?, **options)
        unwrap_object options[:object]
      end

      def setup_show(**options)
        options
      end

      private

      def before_setup_show(**options)
        options = setup(**options)
        options = setup_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
        options = setup_show(**options)
        setup_show_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
snfoil-0.8.5 lib/sn_foil/contexts/show_context.rb
snfoil-0.8.4 lib/sn_foil/contexts/show_context.rb