Class: Dionysus::Producer::Outbox::Publishable

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/dionysus/producer/outbox/publishable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, config: Dionysus::Producer.configuration) ⇒ Publishable

Returns a new instance of Publishable.



7
8
9
10
11
12
# File 'lib/dionysus/producer/outbox/publishable.rb', line 7

def initialize(model, config: Dionysus::Producer.configuration)
  @model = model
  @config = config
  @soft_delete_column = config.soft_delete_column.to_s
  super(model)
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



4
5
6
# File 'lib/dionysus/producer/outbox/publishable.rb', line 4

def model
  @model
end

Instance Method Details

#changesetObject



86
87
88
89
90
91
92
93
94
95
# File 'lib/dionysus/producer/outbox/publishable.rb', line 86

def changeset
  if model.destroyed?
    {
      model.class.primary_key => [model.public_send(model.class.primary_key), nil],
      "created_at" => [model.created_at, nil]
    }
  else
    model.previous_changes
  end
end

#model_classObject



14
15
16
# File 'lib/dionysus/producer/outbox/publishable.rb', line 14

def model_class
  model.class
end

#model_nameObject



26
27
28
# File 'lib/dionysus/producer/outbox/publishable.rb', line 26

def model_name
  model.class.model_name
end

#previous_changed_still_visible?Boolean

TODO: Check if this is needed

Returns:

  • (Boolean)


55
56
57
# File 'lib/dionysus/producer/outbox/publishable.rb', line 55

def previous_changed_still_visible?
  previous_changes_include_canceled? && previous_changes[soft_delete_column][0].blank? && visible?
end

#previous_changes_canceled?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/dionysus/producer/outbox/publishable.rb', line 46

def previous_changes_canceled?
  previous_changes_include_canceled? && previous_changes[soft_delete_column][0].blank? && soft_deleted?
end

#previous_changes_include_canceled?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/dionysus/producer/outbox/publishable.rb', line 38

def previous_changes_include_canceled?
  !!previous_changes[soft_delete_column]
end

#previous_changes_still_canceled?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/dionysus/producer/outbox/publishable.rb', line 50

def previous_changes_still_canceled?
  previous_changes_include_canceled? && previous_changes[soft_delete_column][0].present? && soft_deleted?
end

#previous_changes_uncanceled?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/dionysus/producer/outbox/publishable.rb', line 42

def previous_changes_uncanceled?
  previous_changes_include_canceled? && previous_changes[soft_delete_column][0].present? && visible?
end

#previously_changed?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/dionysus/producer/outbox/publishable.rb', line 34

def previously_changed?
  previous_changes.present?
end

#primary_key_attributeObject



18
19
20
# File 'lib/dionysus/producer/outbox/publishable.rb', line 18

def primary_key_attribute
  model.class.primary_key
end

#publishable_idObject



22
23
24
# File 'lib/dionysus/producer/outbox/publishable.rb', line 22

def publishable_id
  model.public_send(primary_key_attribute)
end

#resource_nameObject



30
31
32
# File 'lib/dionysus/producer/outbox/publishable.rb', line 30

def resource_name
  model_name.singular
end

#soft_deletable?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/dionysus/producer/outbox/publishable.rb', line 63

def soft_deletable?
  model.respond_to?(soft_delete_column)
end

#soft_deleted?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/dionysus/producer/outbox/publishable.rb', line 59

def soft_deleted?
  public_send(soft_delete_column).present?
end

#topicsObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/dionysus/producer/outbox/publishable.rb', line 71

def topics
  top_level_topics = Dionysus::Producer
    .responders_for(model.class)
    .map(&:primary_topic)
  topics_from_dependencies = Dionysus::Producer
    .responders_for_dependency_parent(model.class)
    .map(&:last)
    .map(&:primary_topic)

  [top_level_topics, topics_from_dependencies]
    .flatten
    .uniq
    .tap { |standard_topics| standard_topics << observer_topic if add_observer_topic? }
end

#visible?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/dionysus/producer/outbox/publishable.rb', line 67

def visible?
  !soft_deleted?
end