Class: Blacklight::ActionBuilder
- Inherits:
-
Object
- Object
- Blacklight::ActionBuilder
- 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
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
Instance Method Summary collapse
-
#build ⇒ Object
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`.
-
#initialize(klass, name, opts) ⇒ ActionBuilder
constructor
A new instance of ActionBuilder.
Constructor Details
#initialize(klass, name, opts) ⇒ ActionBuilder
Returns a new instance of ActionBuilder.
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
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
19 20 21 |
# File 'app/builders/blacklight/action_builder.rb', line 19 def klass @klass end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
19 20 21 |
# File 'app/builders/blacklight/action_builder.rb', line 19 def name @name end |
#opts ⇒ Object (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
#build ⇒ Object
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 |