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 "      def \#{name}\n        @documents = action_documents\n\n        if request.post? && \#{callback}\n          if \#{validator}.blank? || send(\#{validator})\n\n            send(\#{callback}, @documents)\n\n            flash[:success] ||= I18n.t(\"blacklight.\#{name}.success\", default: nil)\n\n            respond_to do |format|\n              format.html do\n                return render \"\#{name}_success\", layout: false if request.xhr?\n                redirect_to action_success_redirect_path\n              end\n            end\n          else\n            # Not valid\n            respond_to do |format|\n              format.html do\n                return render layout: false, status: :unprocessable_entity if request.xhr?\n                # Otherwise draw the full page\n              end\n            end\n          end\n        else\n          respond_to do |format|\n            format.html do\n              return render layout: false if request.xhr?\n              # Otherwise draw the full page\n            end\n          end\n        end\n      end\n", __FILE__, __LINE__ + 1
end