Class: ActiveRecord::Journal::Journable::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/journal/journable/context.rb

Defined Under Namespace

Classes: Storage

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**tags_args) ⇒ Context

Returns a new instance of Context.



35
36
37
# File 'lib/active_record/journal/journable/context.rb', line 35

def initialize(**tags_args)
  @tags_args = tags_args
end

Instance Attribute Details

#generate_tagObject



61
62
63
# File 'lib/active_record/journal/journable/context.rb', line 61

def generate_tag
  @generate_tag || false
end

#ignore_actionsObject



57
58
59
# File 'lib/active_record/journal/journable/context.rb', line 57

def ignore_actions
  @ignore_actions || false
end

Instance Method Details

#actionsObject



49
50
51
52
53
54
55
# File 'lib/active_record/journal/journable/context.rb', line 49

def actions
  ActiveRecord::Journal.context_override = self
  yield
  ActiveRecord::Journal.context_override = nil
rescue StandardError
  ActiveRecord::Journal.context_override = nil
end

#configured_for?(action) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/active_record/journal/journable/context.rb', line 39

def configured_for?(action)
  rules.search_by(action: action.to_s)&.any? || false
end

#record(journable, type = nil, **with) ⇒ Object



43
44
45
46
47
# File 'lib/active_record/journal/journable/context.rb', line 43

def record(journable, type = nil, **with)
  options = ActiveRecord::Journal::Journable::Options.parse(with, type)
  rule = ActiveRecord::Journal::Journable::Rule.new(journable, options)
  options.on.each { |action| rules.add(action: action, journable: journable, rule: rule) }
end

#rulesObject



65
66
67
# File 'lib/active_record/journal/journable/context.rb', line 65

def rules
  @rules ||= Storage.new
end

#tag(tags_class) ⇒ Object



69
70
71
72
73
# File 'lib/active_record/journal/journable/context.rb', line 69

def tag(tags_class)
  return @tag if defined?(@tag)

  @tag = tags_class.create!(**@tags_args) if generate_tag
end