Sha256: 4b8981dced2918b1a7c970fbdc7e53648b652d8fe992e050905835987eabf018

Contents?: true

Size: 1.66 KB

Versions: 4

Compression:

Stored size: 1.66 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:, user: nil, **options)
          new(user).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.merge! object: wrap_object(object || scope.resolve.find(id))
      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_show(**options)
        options = setup_show_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
        options = setup(**options)
        setup_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
snfoil-0.4.3 lib/sn_foil/contexts/show_context.rb
snfoil-0.4.2 lib/sn_foil/contexts/show_context.rb
snfoil-0.4.1 lib/sn_foil/contexts/show_context.rb
snfoil-0.4.0 lib/sn_foil/contexts/show_context.rb