Sha256: 1ec65a6a5692b57822cd8c04d658f9eb4c95b9931b4d2c16e05aeaa126704493

Contents?: true

Size: 1.27 KB

Versions: 102

Compression:

Stored size: 1.27 KB

Contents

module Avo
  module Concerns
    module BorrowItemsHolder
      extend ActiveSupport::Concern

      included do
        attr_reader :items_holder
      end

      class_methods do
        def parse_block(parent:, **args, &block)
          # item = Avo::Resources::Items:: ...
          item = new(parent: parent, **args)

          # Borrow the current items holder to the parent (parent = Action || Resource, etc.)
          # Save parent's items holder to restore it after the block is parsed
          # This is useful when you execute parent's methods like `some_fields_method` inside some DSL block.
          # When you do that, Docile will not find the method in the current object (item), but in the parent.
          # So we need to temporarily replace the parent's items holder with the current one because the parent's methods
          # will be executed in the parent's context.
          # For more context: https://github.com/ms-ati/docile/issues/107
          parent_item_holder = parent.items_holder
          parent.items_holder = item.items_holder

          dsl_evaluation = Docile.dsl_eval(item, &block).build

          # Restore the parent's items holder
          parent.items_holder = parent_item_holder

          dsl_evaluation
        end
      end
    end
  end
end

Version data entries

102 entries across 102 versions & 1 rubygems

Version Path
avo-3.16.4 lib/avo/concerns/borrow_items_holder.rb
avo-3.16.3 lib/avo/concerns/borrow_items_holder.rb
avo-3.16.2 lib/avo/concerns/borrow_items_holder.rb
avo-3.16.1 lib/avo/concerns/borrow_items_holder.rb
avo-3.16.0 lib/avo/concerns/borrow_items_holder.rb
avo-3.15.7 lib/avo/concerns/borrow_items_holder.rb
avo-3.15.6 lib/avo/concerns/borrow_items_holder.rb
avo-3.15.5 lib/avo/concerns/borrow_items_holder.rb
avo-3.15.4 lib/avo/concerns/borrow_items_holder.rb
avo-3.15.3 lib/avo/concerns/borrow_items_holder.rb
avo-3.15.2 lib/avo/concerns/borrow_items_holder.rb
avo-3.15.1 lib/avo/concerns/borrow_items_holder.rb
avo-3.15.0 lib/avo/concerns/borrow_items_holder.rb
avo-3.14.5 lib/avo/concerns/borrow_items_holder.rb
avo-3.14.4 lib/avo/concerns/borrow_items_holder.rb
avo-3.14.3 lib/avo/concerns/borrow_items_holder.rb
avo-3.14.2 lib/avo/concerns/borrow_items_holder.rb
avo-3.14.1 lib/avo/concerns/borrow_items_holder.rb
avo-3.14.0 lib/avo/concerns/borrow_items_holder.rb
avo-3.13.7 lib/avo/concerns/borrow_items_holder.rb