Class: Fabulator::Exhibit::Actions::Item

Inherits:
Action
  • Object
show all
Defined in:
lib/fabulator/exhibit/actions/item.rb

Instance Method Summary collapse

Instance Method Details

#run(context, autovivify = false) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fabulator/exhibit/actions/item.rb', line 17

def run(context, autovivify = false)
  @context.with(context) do |ctx|
    items = self.select(ctx)

    db = @database.run(ctx).first.to_s

    items.each do |item|
      ctx.set_scoped_info('exhibit/item/info', { })
      self.run_actions(ctx.with_root(item))
      info = ctx.get_scoped_info('exhibit/item/info')

      info['id'] = (@id.run(ctx.with_root(item)).first.to_s rescue nil)
      if self.mode == 'add'
        if info['id'].nil?
          @@uuid ||= UUID.new
          info['id'] = @@uuid.generate(:compact)
        end
        info['type'] = self.type(ctx.with_root(item)).first.to_s
        info['label'] = self.label(ctx.with_root(item)).first.to_s
        Fabulator::Exhibit::Lib.add_info(db, :items, info)
      elsif self.mode == 'remove' && !info['id'].nil?
        Fabulator::Exhibit::Lib.remove_info(db, :items, info['id'])
      end
    end
  end
end