mautic 2.5.0 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eb4e903dff064ccd272c769b31a9692b0479cc93f86d6caa7586f73adb0190d6
4
- data.tar.gz: a96abb5a2f455fb0026bfe56be46640f49d6fdcf89673f27fb59ebae8f66aa42
3
+ metadata.gz: 8369fa0948409afca63ba594d3c630729dd160bc27fd9808dbaa941fab2e6a6e
4
+ data.tar.gz: 0cbc398f5515825575367d69866f176659713af2835df403901ee7321bdd0d85
5
5
  SHA512:
6
- metadata.gz: 49997f77ce24fa44c9a3ec96a785b31d0ae382ccd754e9c3c89b1adabf33869b31dc3fbc0aaf25f38b3d988192cafa6ccc7d617b7c90a53ae516a6f218b8fa38
7
- data.tar.gz: ab3974fd38ba6ce83c8b489634bce1a637b18effdddbcb779b98e5531d865cf4bd6363c899b57927a9374a409b35cc0f27865cffca7086963954bbfad9aa9828
6
+ metadata.gz: 623bedbe8dce28df4d92147ba646ff88506e1e2ddb883903ac687cfba6927164579279011307fbe53587d6940cd94a71feb55a31ef0a4642a7140a1e86f80253
7
+ data.tar.gz: e33d151ab4d0b44b4c978c347f6b56b1b4407520d814d9c62a2f0db1b363dab893cea944727158eab115ef17d9c9a0dec754c80ef25aaaf85bcfa3be5a556320
@@ -1,6 +1,9 @@
1
1
  module Mautic
2
2
  class Contact < Model
3
3
 
4
+ around_create :ensure_stage, if: ->(contact) { contact.changes.has_key?(:stage_id) }
5
+ around_update :ensure_stage, if: ->(contact) { contact.changes.has_key?(:stage_id) }
6
+
4
7
  alias_attribute :first_name, :firstname
5
8
  alias_attribute :last_name, :lastname
6
9
 
@@ -41,11 +44,13 @@ module Mautic
41
44
 
42
45
  if source
43
46
  self.owner = source['owner'] || {}
47
+ self.stage = source['stage'] || {}
44
48
  tags = (source['tags'] || []).map { |t| Mautic::Tag.new(self, t) }.sort_by(&:name)
45
49
  self.attributes = {
46
50
  tags: Tag::Collection.new(self, *tags),
47
51
  doNotContact: source['doNotContact'] || [],
48
52
  owner: owner['id'],
53
+ stage_id: stage&.id,
49
54
  }
50
55
  end
51
56
  end
@@ -134,11 +139,47 @@ module Mautic
134
139
 
135
140
  # !endgroup
136
141
 
142
+ # @!group Stage
143
+
144
+ # @return [Mautic::Stage, nil]
145
+ def stage
146
+ @stage
147
+ end
148
+
149
+ # @param [Mautic:::Stage,Hash, nil] hash
150
+ def stage=(object_or_hash)
151
+ @stage = case object_or_hash
152
+ when Mautic::Stage
153
+ object_or_hash
154
+ when Hash
155
+ Mautic::Stage.new(connection, object_or_hash)
156
+ end
157
+ @table[:stage_id] = @stage&.id
158
+ @stage
159
+ end
160
+
161
+ # !endgroup
162
+
137
163
  private
138
164
 
139
165
  def clear_change
140
166
  super
141
167
  remove_instance_variable :@do_not_contact
142
168
  end
169
+
170
+ # Add or Remove contact in stage after contact save
171
+ def ensure_stage
172
+ stage_id_change = changes[:stage_id]
173
+ yield
174
+ if stage_id_change
175
+ self.stage = Mautic::Stage.in(connection).find(stage_id_change)
176
+ stage.add_contact!(id)
177
+ @table[:stage_id] = stage.id
178
+ else
179
+ stage.remove_contact!(id)
180
+ @table.delete(:stage_id)
181
+ end
182
+ clear_changes
183
+ end
143
184
  end
144
- end
185
+ end
@@ -0,0 +1,22 @@
1
+ module Mautic
2
+ class Stage < Model
3
+
4
+ # @see https://developer.mautic.org/#add-contact-to-a-stage
5
+ # @param [Integer] id of Mautic::Contact
6
+ def add_contact!(id)
7
+ json = @connection.request(:post, "api/stages/#{self.id}/contact/#{id}/add")
8
+ json["success"]
9
+ rescue RequestError => _e
10
+ false
11
+ end
12
+
13
+ # @see https://developer.mautic.org/#remove-contact-from-a-stage
14
+ # @param [Integer] id of Mautic::Contact
15
+ def remove_contact!(id)
16
+ json = @connection.request(:post, "api/stages/#{self.id}/contact/#{id}/remove")
17
+ json["success"]
18
+ rescue RequestError => _e
19
+ false
20
+ end
21
+ end
22
+ end
data/lib/mautic/model.rb CHANGED
@@ -2,6 +2,7 @@ module Mautic
2
2
  # Virtual model for Mautic endpoint
3
3
  # @see https://developer.mautic.org/#endpoints
4
4
  class Model < OpenStruct
5
+ extend ActiveModel::Callbacks
5
6
 
6
7
  class MauticHash < Hash
7
8
 
@@ -37,6 +38,8 @@ module Mautic
37
38
 
38
39
  end
39
40
 
41
+ define_model_callbacks :create, :update, :save
42
+
40
43
  attr_reader :connection
41
44
  attr_accessor :errors
42
45
  attr_writer :changed
@@ -55,15 +58,19 @@ module Mautic
55
58
  end
56
59
 
57
60
  def save(force = false)
58
- id.present? ? update(force) : create
61
+ run_callbacks :save do
62
+ id.present? ? update(force) : create
63
+ end
59
64
  end
60
65
 
61
66
  def update(force = false)
62
67
  return false unless changed?
63
68
 
64
69
  begin
65
- json = @connection.request((force && :put || :patch), "api/#{endpoint}/#{id}/edit", body: to_mautic)
66
- assign_attributes json[endpoint.singularize]
70
+ run_callbacks :create do
71
+ json = @connection.request((force && :put || :patch), "api/#{endpoint}/#{id}/edit", body: to_mautic)
72
+ assign_attributes json[endpoint.singularize]
73
+ end
67
74
  clear_changes
68
75
  rescue ValidationError => e
69
76
  self.errors = e.errors
@@ -82,8 +89,10 @@ module Mautic
82
89
 
83
90
  def create
84
91
  begin
85
- json = @connection.request(:post, "api/#{endpoint}/#{id && "#{id}/"}new", body: to_mautic)
86
- assign_attributes json[endpoint.singularize]
92
+ run_callbacks :create do
93
+ json = @connection.request(:post, "api/#{endpoint}/#{id && "#{id}/"}new", body: to_mautic)
94
+ assign_attributes json[endpoint.singularize]
95
+ end
87
96
  clear_changes
88
97
  rescue ValidationError => e
89
98
  self.errors = e.errors
@@ -124,14 +133,14 @@ module Mautic
124
133
  end
125
134
 
126
135
  def to_mautic(data = @table)
127
- data.each_with_object({}) do |(key, val), mem|
128
- mem[key] = if val.respond_to?(:to_mautic)
129
- val.to_mautic
130
- elsif val.is_a?(Array)
131
- val.join("|")
132
- else
133
- val
134
- end
136
+ data.transform_values do |val|
137
+ if val.respond_to?(:to_mautic)
138
+ val.to_mautic
139
+ elsif val.is_a?(Array)
140
+ val.join("|")
141
+ else
142
+ val
143
+ end
135
144
  end
136
145
  end
137
146
 
@@ -1,3 +1,3 @@
1
1
  module Mautic
2
- VERSION = '2.5.0'
2
+ VERSION = '2.6.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mautic
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.5.0
4
+ version: 2.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lukáš Pokorný
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-12-26 00:00:00.000000000 Z
11
+ date: 2021-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -176,6 +176,7 @@ files:
176
176
  - app/models/mautic/contact_field.rb
177
177
  - app/models/mautic/event.rb
178
178
  - app/models/mautic/form.rb
179
+ - app/models/mautic/stage.rb
179
180
  - app/models/mautic/tag.rb
180
181
  - app/models/mautic/web_hook.rb
181
182
  - app/views/layouts/mautic/application.html.erb
@@ -204,7 +205,7 @@ licenses:
204
205
  - MIT
205
206
  metadata:
206
207
  allowed_push_host: https://rubygems.org
207
- post_install_message:
208
+ post_install_message:
208
209
  rdoc_options: []
209
210
  require_paths:
210
211
  - lib
@@ -219,8 +220,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
219
220
  - !ruby/object:Gem::Version
220
221
  version: '0'
221
222
  requirements: []
222
- rubygems_version: 3.1.4
223
- signing_key:
223
+ rubygems_version: 3.0.8
224
+ signing_key:
224
225
  specification_version: 4
225
226
  summary: Ruby on Rails Mautic integration
226
227
  test_files: