class Mautic::Model

Virtual model for Mautic endpoint

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

Attributes

changed[W]
connection[R]
errors[RW]

Public Class Methods

endpoint() click to toggle source
# File lib/mautic/model.rb, line 31
def endpoint
  name.demodulize.underscore.pluralize
end
in(connection) click to toggle source
# File lib/mautic/model.rb, line 35
def in(connection)
  Proxy.new(connection, endpoint)
end
new(connection, hash = nil) click to toggle source

@param [Mautic::Connection] connection

# 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

Public Instance Methods

attributes() click to toggle source
# File lib/mautic/model.rb, line 124
def attributes
  @table.to_h
end
attributes=(hash) click to toggle source
# 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?() click to toggle source
# File lib/mautic/model.rb, line 118
def changed?
  return @changed unless @changed.nil?

  @changed = !changes.empty?
end
changes() click to toggle source
# File lib/mautic/model.rb, line 114
def changes
  @table.changes
end
create() click to toggle source
# 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
destroy() click to toggle source
# 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_id() click to toggle source
# File lib/mautic/model.rb, line 56
def mautic_id
  "#{id}/#{@connection.id}"
end
save(force = false) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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 = {}) click to toggle source
# 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

Private Instance Methods

assign_attributes(source = nil) click to toggle source
# File lib/mautic/model.rb, line 158
def assign_attributes(source = nil)
  @mautic_attributes = []
  source ||= {}

  data = if (fields = source['fields'])
           attributes_from_fields(fields)
         elsif source
           source
         end
  self.attributes = data
end
attributes_from_fields(fields) click to toggle source
# File lib/mautic/model.rb, line 170
def attributes_from_fields(fields)
  data = {}
  if fields['all']
    @mautic_attributes = fields['all'].collect do |key, value|
      data[key] = value
      Attribute.new(alias: key, value: value)
    end
  else
    fields.each do |_group, pairs|
      next unless pairs.is_a?(Hash)

      pairs.each do |key, attrs|
        @mautic_attributes << (a = Attribute.new(attrs))
        data[key] = a.value
      end
    end
  end

  data
end
clear_changes() click to toggle source
# File lib/mautic/model.rb, line 149
def clear_changes
  @changed = nil
  @table.instance_variable_set(:@changes, nil)
end
endpoint() click to toggle source
# File lib/mautic/model.rb, line 154
def endpoint
  self.class.endpoint
end