Class: Mautic::Model

Inherits:
OpenStruct
  • Object
show all
Extended by:
ActiveModel::Callbacks
Defined in:
lib/mautic/model.rb

Overview

Virtual model for Mautic endpoint

@see https://developer.mautic.org/#endpoints

Direct Known Subclasses

Campaign, CompanyField, Contact, ContactField, Event, Form, Stage, Tag

Defined Under Namespace

Classes: Attribute, MauticHash

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, hash = nil) ⇒ Model

Returns a new instance of Model.

Parameters:



48
49
50
51
52
53
54
# File 'lib/mautic/model.rb', line 48

def initialize(connection, hash = nil)
  @connection = connection
  @table = MauticHash.new
  self.attributes = { id: hash['id'], created_at: hash['dateAdded']&.to_time, updated_at: hash['dateModified']&.to_time } if hash
  assign_attributes(hash)
  clear_changes
end

Instance Attribute Details

#changed=(value) ⇒ Object (writeonly)

Sets the attribute changed

Parameters:

  • value

    the value to set the attribute changed to.



45
46
47
# File 'lib/mautic/model.rb', line 45

def changed=(value)
  @changed = value
end

#connectionObject (readonly)

Returns the value of attribute connection.



43
44
45
# File 'lib/mautic/model.rb', line 43

def connection
  @connection
end

#errorsObject

Returns the value of attribute errors.



44
45
46
# File 'lib/mautic/model.rb', line 44

def errors
  @errors
end

Class Method Details

.endpointObject



31
32
33
# File 'lib/mautic/model.rb', line 31

def endpoint
  name.demodulize.underscore.pluralize
end

.in(connection) ⇒ Object



35
36
37
# File 'lib/mautic/model.rb', line 35

def in(connection)
  Proxy.new(connection, endpoint)
end

Instance Method Details

#attributesObject



124
125
126
# File 'lib/mautic/model.rb', line 124

def attributes
  @table.to_h
end

#attributes=(hash) ⇒ Object



128
129
130
131
132
133
# File 'lib/mautic/model.rb', line 128

def attributes=(hash)
  hash.each_pair do |k, v|
    k = k.to_sym
    @table[k] = v
  end
end

#changed?Boolean

Returns:

  • (Boolean)


118
119
120
121
122
# File 'lib/mautic/model.rb', line 118

def changed?
  return @changed unless @changed.nil?

  @changed = !changes.empty?
end

#changesObject



114
115
116
# File 'lib/mautic/model.rb', line 114

def changes
  @table.changes
end

#createObject



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/mautic/model.rb', line 90

def create
  begin
    run_callbacks :create do
      json = @connection.request(:post, "api/#{endpoint}/#{id && "#{id}/"}new", body: to_mautic)
      assign_attributes json[endpoint.singularize]
    end
    clear_changes
  rescue ValidationError => e
    self.errors = e.errors
  end

  errors.blank?
end

#destroyObject



104
105
106
107
108
109
110
111
112
# File 'lib/mautic/model.rb', line 104

def destroy
  begin
    @connection.request(:delete, "api/#{endpoint}/#{id}/delete")
    true
  rescue RequestError => e
    self.errors = e.errors
    false
  end
end

#mautic_idObject



56
57
58
# File 'lib/mautic/model.rb', line 56

def mautic_id
  "#{id}/#{@connection.id}"
end

#save(force = false) ⇒ Object



60
61
62
63
64
# File 'lib/mautic/model.rb', line 60

def save(force = false)
  run_callbacks :save do
    id.present? ? update(force) : create
  end
end

#to_mautic(data = @table) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/mautic/model.rb', line 135

def to_mautic(data = @table)
  data.transform_values do |val|
    if val.respond_to?(:to_mautic)
      val.to_mautic
    elsif val.is_a?(Array)
      val.join("|")
    else
      val
    end
  end
end

#update(force = false) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/mautic/model.rb', line 66

def update(force = false)
  return false unless changed?

  begin
    run_callbacks :create do
      json = @connection.request((force && :put || :patch), "api/#{endpoint}/#{id}/edit", body: to_mautic)
      assign_attributes json[endpoint.singularize]
    end
    clear_changes
  rescue ValidationError => e
    self.errors = e.errors
  end

  errors.blank?
end

#update_columns(attributes = {}) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/mautic/model.rb', line 82

def update_columns(attributes = {})
  json = @connection.request(:patch, "api/#{endpoint}/#{id}/edit", body: to_mautic(attributes))
  assign_attributes json[endpoint.singularize]
  clear_changes
rescue ValidationError => e
  self.errors = e.errors
end