Class: Blacklight::ActionBuilder

Inherits:
Object
  • Object
show all
Defined in:
app/builders/blacklight/action_builder.rb

Overview

Dynamically creates methods on the given controller (typically CatalogController) for handling configured show tools

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, name, opts) ⇒ ActionBuilder

Returns a new instance of ActionBuilder.

Parameters:

  • klass (Object)
  • name (String)
  • opts (Hash)

Options Hash (opts):

  • callback (Symbol)
  • validator (Symbol)
  • define_method (Boolean)


13
14
15
16
17
# File 'app/builders/blacklight/action_builder.rb', line 13

def initialize(klass, name, opts)
  @klass = klass
  @name = name
  @opts = opts
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



19
20
21
# File 'app/builders/blacklight/action_builder.rb', line 19

def klass
  @klass
end

#nameObject (readonly)

Returns the value of attribute name.



19
20
21
# File 'app/builders/blacklight/action_builder.rb', line 19

def name
  @name
end

#optsObject (readonly)

Returns the value of attribute opts.



19
20
21
# File 'app/builders/blacklight/action_builder.rb', line 19

def opts
  @opts
end

Instance Method Details

#buildObject

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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/builders/blacklight/action_builder.rb', line 23

def build # rubocop:disable Metrics/MethodLength
  return if skip?

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

        if request.post? && #{callback}
          if #{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", layout: false if request.xhr?
                redirect_to action_success_redirect_path
              end
            end
          else
            # Not valid
            respond_to do |format|
              format.html do
                return render layout: false, status: :unprocessable_entity if request.xhr?
                # Otherwise draw the full page
              end
            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