Sha256: 9a400d99ec9b0e94eb42235211df98554d3f700428d7280f40be07eac8b77430

Contents?: true

Size: 1.78 KB

Versions: 11

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

module Blacklight
  # Dynamically creates methods on the given controller (typically CatalogController)
  # for handling configured show tools
  class ActionBuilder
    # @param [Object] klass
    # @param [String] name
    # @param [Hash] opts
    # @option opts [Symbol] callback
    # @option opts [Symbol] validator
    # @option opts [Boolean] define_method
    def initialize(klass, name, opts)
      @klass = klass
      @name = name
      @opts = opts
    end

    attr_reader :klass, :name, :opts

    # Define a simple action handler for the tool as long as the method
    # doesn't already exist or the `:define_method` option is not `false`
    def build
      return if skip?

      callback = opts.fetch(:callback, nil).inspect
      validator = opts.fetch(:validator, nil).inspect
      klass.class_eval <<EORUBY, __FILE__, __LINE__ + 1
          def #{name}
            @response, @documents = action_documents

            if request.post? && #{callback} &&
               (#{validator}.blank? || send(#{validator}))

              send(#{callback}, @documents)

              flash.now[:success] ||= I18n.t("blacklight.#{name}.success", default: nil)

              respond_to do |format|
                format.html do
                  return render "#{name}_success" if request.xhr?
                  redirect_to action_success_redirect_path
                end
              end
            else
              respond_to do |format|
                format.html do
                  return render layout: false if request.xhr?
                  # Otherwise draw the full page
                end
              end
            end
          end
EORUBY
    end

    private

    def skip?
      klass.method_defined?(name) || opts[:define_method] == false
    end
  end
end

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
blacklight-7.40.0 app/builders/blacklight/action_builder.rb
blacklight-7.39.0 app/builders/blacklight/action_builder.rb
blacklight-7.38.0 app/builders/blacklight/action_builder.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/blacklight-7.37.0/app/builders/blacklight/action_builder.rb
blacklight-7.37.0 app/builders/blacklight/action_builder.rb
blacklight-7.36.2 app/builders/blacklight/action_builder.rb
blacklight-7.36.1 app/builders/blacklight/action_builder.rb
blacklight-7.36.0 app/builders/blacklight/action_builder.rb
blacklight-7.35.0 app/builders/blacklight/action_builder.rb
blacklight-7.34.0 app/builders/blacklight/action_builder.rb
blacklight-7.33.1 app/builders/blacklight/action_builder.rb