Sha256: 73a5c243cf1f3fd5046cf626d0eb60cd06e43b5b8e58c7000e9389823e421f9c

Contents?: true

Size: 1.58 KB

Versions: 19

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

module Blacklight
  # Dynamically creates methods on the given controller (typically CatalogController)
  # for handling configured show tools
  class ActionBuilder
    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[: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

19 entries across 19 versions & 1 rubygems

Version Path
blacklight-7.11.1 app/builders/blacklight/action_builder.rb
blacklight-7.10.0 app/builders/blacklight/action_builder.rb
blacklight-7.9.0 app/builders/blacklight/action_builder.rb
blacklight-7.8.1 app/builders/blacklight/action_builder.rb
blacklight-7.8.0 app/builders/blacklight/action_builder.rb
blacklight-7.7.0 app/builders/blacklight/action_builder.rb
blacklight-7.6.0 app/builders/blacklight/action_builder.rb
blacklight-7.5.1 app/builders/blacklight/action_builder.rb
blacklight-7.5.0 app/builders/blacklight/action_builder.rb
blacklight-7.4.2 app/builders/blacklight/action_builder.rb
blacklight-7.4.1 app/builders/blacklight/action_builder.rb
blacklight-7.4.0 app/builders/blacklight/action_builder.rb
blacklight-7.3.0 app/builders/blacklight/action_builder.rb
blacklight-7.2.0 app/builders/blacklight/action_builder.rb
blacklight-7.1.0 app/builders/blacklight/action_builder.rb
blacklight-7.1.0.alpha app/builders/blacklight/action_builder.rb
blacklight-7.0.1 app/builders/blacklight/action_builder.rb
blacklight-7.0.0 app/builders/blacklight/action_builder.rb
blacklight-7.0.0.rc2 app/builders/blacklight/action_builder.rb