Class: ViewModel::ActiveRecord::Visitor

Inherits:
Object
  • Object
show all
Defined in:
lib/view_model/active_record/visitor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(visit_referenced: true, visit_shared: true, for_edit: false) ⇒ Visitor

Returns a new instance of Visitor.



6
7
8
9
10
# File 'lib/view_model/active_record/visitor.rb', line 6

def initialize(visit_referenced: true, visit_shared: true, for_edit: false)
  @visit_referenced = visit_referenced
  @visit_shared     = visit_shared
  @for_edit         = for_edit
end

Instance Attribute Details

#for_editObject (readonly)

Returns the value of attribute for_edit.



4
5
6
# File 'lib/view_model/active_record/visitor.rb', line 4

def for_edit
  @for_edit
end

#visit_referencedObject (readonly)

Returns the value of attribute visit_referenced.



4
5
6
# File 'lib/view_model/active_record/visitor.rb', line 4

def visit_referenced
  @visit_referenced
end

#visit_sharedObject (readonly)

Returns the value of attribute visit_shared.



4
5
6
# File 'lib/view_model/active_record/visitor.rb', line 4

def visit_shared
  @visit_shared
end

Instance Method Details

#changes(_view) ⇒ Object

This method may be overridden by subclasses to specify the changes to be provided to callback hooks for each view. By default returns an empty Changes.



81
82
83
# File 'lib/view_model/active_record/visitor.rb', line 81

def changes(_view)
  ViewModel::Changes.new
end

#post_visit(_view, context: nil) ⇒ Object

Invoked for all view types after visit.



71
# File 'lib/view_model/active_record/visitor.rb', line 71

def post_visit(_view, context: nil); end

#pre_visit(_view, context: nil) ⇒ Object

Invoked for all view types before visit, may cancel visit by returning false.



66
67
68
# File 'lib/view_model/active_record/visitor.rb', line 66

def pre_visit(_view, context: nil)
  true
end

#run_callback(hook, view, context:, **args) ⇒ Object

If a context is provided, run the specified callback hook on it



74
75
76
# File 'lib/view_model/active_record/visitor.rb', line 74

def run_callback(hook, view, context:, **args)
  context.run_callback(hook, view, **args) if context
end

#visit(view, context: nil) ⇒ Object



12
13
14
15
16
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/view_model/active_record/visitor.rb', line 12

def visit(view, context: nil)
  reset_state!

  return unless pre_visit(view, context: context)

  run_callback(ViewModel::Callbacks::Hook::BeforeVisit, view, context: context)
  run_callback(ViewModel::Callbacks::Hook::BeforeDeserialize, view, context: context) if for_edit

  class_name = view.class.name.underscore.gsub('/', '__')
  visit      = :"visit_#{class_name}"
  end_visit  = :"end_visit_#{class_name}"

  visit_children =
    if respond_to?(visit, true)
      self.send(visit, view, context: context)
    else
      true
    end

  if visit_children
    # visit the underlying viewmodel for each association, ignoring any
    # customization
    ignored_associations = @ignored_associations
    view.class._members.each do |name, member_data|
      next unless member_data.association?
      next if ignored_associations.include?(name)
      next if member_data.referenced? && !visit_referenced
      next if !member_data.owned? && !visit_shared

      children = Array.wrap(view._read_association(name))
      children.each do |child|
        if context
          child_context = view.context_for_child(name, context: context)
        end
        self.visit(child, context: child_context)
      end
    end
  end

  self.send(end_visit, view, context: context) if respond_to?(end_visit, true)

  if for_edit
    view_changes = changes(view)
    run_callback(ViewModel::Callbacks::Hook::OnChange, view, context: context, changes: view_changes) if view_changes.changed?
    run_callback(ViewModel::Callbacks::Hook::AfterDeserialize, view, context: context, changes: view_changes)
  end

  run_callback(ViewModel::Callbacks::Hook::AfterVisit, view, context: context)

  post_visit(view, context: context)
end