mautic 0.1.8 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA256:
3
- metadata.gz: 5fe0c00bf9adc2bb52aebcec2b270740492e49d6e8a3627635b1c5612e82b04a
4
- data.tar.gz: 40ada8b3e84aa4ee9bb3ced6392fe47c912cd3ccf32bd7c97ff0f08454f2d2ef
2
+ SHA1:
3
+ metadata.gz: 395ca0792f8a56986bc97cc6a267deb9c24cdb44
4
+ data.tar.gz: 15c8e1583f8ea06db7683400978c8bf064b54e77
5
5
  SHA512:
6
- metadata.gz: 46d1ac327fee620c46227d3b8e41e16d2b66edcd5d228bdf7a414caca606e107cae91cd2c503fead21e37028b497136f3df19135f9d588d5175a73900e560c8f
7
- data.tar.gz: a7eb6c40d61fa28df4907c022af430df7ec3d24c5ccbce5e284dda73f822991251737c3fbadf6ffc51e4505f2324defc2d726582b8f7c5bcc2c918fddf872bbd
6
+ metadata.gz: bfff68b112c2edb739a49d4d549d1165f91d728b412c4a4e24219387e23ba65b7ec4f3ddc6892ddb3206b494e85f99b1b31d3b3238bc262cdf12859d7ca337d5
7
+ data.tar.gz: 5ec16c2043356e0118c0b509a8358b8902b750645e30092b3fa50f92c21aa24de986b1ca17c91b6232a5a5ae1848ff57cb691712c7c1ab5a1d11f9c2e7181c42
data/README.md CHANGED
@@ -1,18 +1,10 @@
1
1
  # Mautic rails
2
2
  RoR helper / wrapper for Mautic API and forms
3
3
 
4
- *Rails 4 compatible*
5
4
  ## Usage
6
5
  ### Gem provides API connection to your Mautic(s)
7
6
  1. Create mautic connection
8
7
  2. Authorize it
9
-
10
- In mautic you need add API oauth2 login.
11
- For URI callback allow:
12
- ```
13
- http://localhost:3000/mautic/connections/:ID/oauth2
14
- ```
15
- ID = is your Mautic::Connection ID
16
8
 
17
9
  Find connection which you want to use:
18
10
  ```ruby
@@ -66,7 +58,7 @@ There are two options of usage:
66
58
  Add this line to your application's Gemfile:
67
59
 
68
60
  ```ruby
69
- gem 'mautic', '~>0.1'
61
+ gem 'mautic'
70
62
  ```
71
63
 
72
64
  And then execute:
@@ -86,7 +78,6 @@ add to `config/initializers/mautic.rb`:
86
78
  Mautic.configure do |config|
87
79
  # This is for oauth handshake token url. I need to know where your app listen
88
80
  config.base_url = "https://my-rails-app.com"
89
- # OR it can be Proc
90
81
  # *optional* This is your default mautic URL - used in form helper
91
82
  config.mautic_url = "https://mautic.my.app"
92
83
  end
@@ -10,7 +10,5 @@
10
10
  // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
11
  // about supported directives.
12
12
  //
13
- // require rails-ujs
14
- //= require jquery
15
- //= require jquery_ujs
13
+ //= require rails-ujs
16
14
  //= require_tree .
@@ -1,5 +1,109 @@
1
1
  module Mautic
2
2
  class ConnectionsController < ApplicationController
3
- include ::Mautic::ConnectionsControllerConcern
3
+ before_action :set_mautic_connection, only: [:show, :edit, :update, :destroy, :oauth2, :authorize]
4
+
5
+ # GET /mautic_connections
6
+ def index
7
+ @mautic_connections = Connection.order(:url)
8
+ respond_to do |format|
9
+ format.html { render layout: !request.xhr? }
10
+ format.json { render json: @mautic_connections }
11
+ end
12
+ end
13
+
14
+ # GET /mautic_connections/1
15
+ def show
16
+ respond_to do |format|
17
+ format.html { render layout: !request.xhr? }
18
+ end
19
+ end
20
+
21
+ # GET /mautic_connections/new
22
+ def new
23
+ @mautic_connection = Connection.new
24
+ respond_to do |format|
25
+ format.html { render layout: !request.xhr? }
26
+ end
27
+ end
28
+
29
+ # GET /mautic_connections/1/edit
30
+ def edit
31
+ respond_to do |format|
32
+ format.html { render layout: !request.xhr? }
33
+ end
34
+ end
35
+
36
+ # POST /mautic_connections
37
+ def create
38
+ @mautic_connection = Connection.new(mautic_connection_params)
39
+
40
+ respond_to do |format|
41
+ if @mautic_connection.save
42
+ format.html { redirect_to mautic.connection_path(@mautic_connection), notice: t('mautic.text_mautic_connection_created') }
43
+ format.js { head :no_content }
44
+ format.json { render json: @mautic_connection }
45
+ else
46
+ format.html { render :new }
47
+ format.js { head :unprocessable_entity }
48
+ format.json { render json: @mautic_connection.errors, status: :unprocessable_entity }
49
+ end
50
+ end
51
+ end
52
+
53
+ # PATCH/PUT /mautic_connections/1
54
+ def update
55
+ respond_to do |format|
56
+ if @mautic_connection.update(mautic_connection_params)
57
+ format.html { redirect_to mautic.connection_path(@mautic_connection), notice: t('mautic.text_mautic_connection_updated') }
58
+ format.js { head :no_content }
59
+ format.json { render json: @mautic_connection }
60
+ else
61
+ format.html { render :edit }
62
+ format.js { head :unprocessable_entity }
63
+ format.json { render json: @mautic_connection.errors, status: :unprocessable_entity }
64
+ end
65
+ end
66
+ end
67
+
68
+ # DELETE /mautic_connections/1
69
+ def destroy
70
+ @mautic_connection.destroy
71
+ respond_to do |format|
72
+ format.html { redirect_to :connections, notice: t('mautic.text_mautic_connection_destroyed') }
73
+ format.js { render js: "document.getElementById('#{view_context.dom_id(@mautic_connection)}').remove()" }
74
+ format.json { render json: @mautic_connection }
75
+ end
76
+ end
77
+
78
+ # ==--==--==--==--
79
+
80
+ def authorize
81
+ redirect_to @mautic_connection.authorize
82
+ end
83
+
84
+ def oauth2
85
+ begin
86
+ response = @mautic_connection.get_code(params.require(:code))
87
+ @mautic_connection.update(token: response.token, refresh_token: response.refresh_token)
88
+ return render plain: t('mautic.text_mautic_authorize_successfully')
89
+ rescue OAuth2::Error => e
90
+ flash[:error] = e.message
91
+ end
92
+
93
+ render :show
94
+ end
95
+
96
+ private
97
+ # Use callbacks to share common setup or constraints between actions.
98
+ def set_mautic_connection
99
+ @mautic_connection = Connection.find(params[:id])
100
+ rescue ActiveRecord::RecordNotFound => e
101
+ return render head: 404, plain: e.message
102
+ end
103
+
104
+ # Only allow a trusted parameter "white list" through.
105
+ def mautic_connection_params
106
+ params.require(:connection).permit(:url, :client_id, :secret, :type)
107
+ end
4
108
  end
5
109
  end
@@ -0,0 +1,3 @@
1
+ class ApplicationRecord < ActiveRecord::Base
2
+ self.abstract_class = true
3
+ end
@@ -3,8 +3,8 @@ module Mautic
3
3
 
4
4
  self.table_name = 'mautic_connections'
5
5
 
6
- validates :url, presence: true, format: URI::regexp(%w(http https))
7
- validates :client_id, :secret, presence: true, unless: :new_record?
6
+ validates :url, :client_id, :secret, presence: true
7
+ validates :url, format: URI::regexp(%w(http https))
8
8
 
9
9
  alias_attribute :access_token, :token
10
10
 
@@ -39,48 +39,9 @@ module Mautic
39
39
  end
40
40
 
41
41
  def request(type, path, params = {})
42
- @last_request = [type, path, params]
43
- response = raise NotImplementedError
44
- parse_response(response)
45
- end
46
-
47
- private
48
-
49
- def callback_url
50
- if (conf = Mautic.config.base_url).is_a?(Proc)
51
- conf = conf.call(self)
52
- end
53
-
54
- URI.parse(conf)
42
+ raise NotImplementedError
55
43
  end
56
44
 
57
- def parse_response(response)
58
- case response.status
59
- when 400
60
- raise Mautic::ValidationError.new(response)
61
- when 404
62
- raise Mautic::RecordNotFound.new(response)
63
- when 200, 201
64
- json = JSON.parse(response.body) rescue {}
65
- Array(json['errors']).each do |error|
66
- case error['code'].to_i
67
- when 401
68
- raise Mautic::TokenExpiredError.new(response) if @try_to_refresh
69
- @try_to_refresh = true
70
- refresh!
71
- json = request(*@last_request)
72
- when 404
73
- raise Mautic::RecordNotFound.new(response)
74
- else
75
- raise Mautic::RequestError.new(response)
76
- end
77
- end
78
- else
79
- raise Mautic::RequestError.new(response)
80
- end
81
-
82
- json
83
- end
84
45
 
85
46
  end
86
47
  end
@@ -30,15 +30,28 @@ module Mautic
30
30
  end
31
31
 
32
32
  def request(type, path, params = {})
33
- @last_request = [type, path, params]
34
- response = connection.request(type, path, params)
35
- parse_response(response)
33
+ json = JSON.parse connection.request(type, path, params).body
34
+ Array(json['errors']).each do |error|
35
+ case error['code']
36
+ when 400
37
+ # Validation error
38
+ when 401
39
+ raise Mautic::TokenExpiredError.new(error['message']) if @try_to_refresh
40
+ @try_to_refresh = true
41
+ refresh!
42
+ json = request(type, path, params)
43
+ else
44
+ raise Mautic::AuthorizeError.new("#{error['code']} - #{error['message']}")
45
+ end
46
+ end
47
+ json
36
48
  end
37
49
 
38
50
  private
39
51
 
40
52
  def callback_url
41
- uri = super
53
+ # return Mautic.config.oauth2_callback_url if Mautic.config.oauth2_callback_url
54
+ uri = URI.parse(Mautic.config.base_url)
42
55
  uri.path = Mautic::Engine.routes.url_helpers.oauth2_connection_path(self)
43
56
  uri.to_s
44
57
  end
@@ -1,8 +1,6 @@
1
1
  module Mautic
2
2
  class Contact < Model
3
3
 
4
- alias_attribute :first_name, :firstname
5
- alias_attribute :last_name, :lastname
6
4
  def self.in(connection)
7
5
  Proxy.new(connection, endpoint, default_params: { search: '!is:anonymous' })
8
6
  end
@@ -1,5 +1,4 @@
1
- <%#= form_with(model: mautic_connection.becomes(Mautic::Connection), local: true) do |form| %>
2
- <%= form_for(mautic_connection.becomes(Mautic::Connection)) do |form| %>
1
+ <%= form_with(model: mautic_connection.becomes(Mautic::Connection), local: true) do |form| %>
3
2
  <% if mautic_connection.errors.any? %>
4
3
  <div id="error_explanation">
5
4
  <h2><%= pluralize(mautic_connection.errors.count, "error") %> prohibited this mautic_connection from being saved:</h2>
@@ -1,4 +1,4 @@
1
- class CreateMauticMauticConnections < ActiveRecord::Migration
1
+ class CreateMauticMauticConnections < ActiveRecord::Migration[5.1]
2
2
  def change
3
3
  create_table :mautic_connections do |t|
4
4
  t.string :type
@@ -10,7 +10,7 @@ class CreateMauticMauticConnections < ActiveRecord::Migration
10
10
  t.string :token
11
11
  t.string :refresh_token
12
12
 
13
- t.timestamps null: false
13
+ t.timestamps
14
14
  end
15
15
  end
16
16
  end
@@ -1,5 +1,4 @@
1
1
  require "oauth2"
2
- require "jquery-rails"
3
2
  require "mautic/engine"
4
3
 
5
4
  module Mautic
@@ -9,46 +8,14 @@ module Mautic
9
8
  autoload :Proxy, 'mautic/proxy'
10
9
  autoload :Model, 'mautic/model'
11
10
 
12
- class RequestError < StandardError
13
-
14
- attr_reader :response, :errors
15
-
16
- def initialize(response, message = nil)
17
- @errors ||= []
18
- @response = response
19
- json_body = JSON.parse(response.body) rescue {}
20
- message ||= Array(json_body['errors']).collect do |error|
21
- msg = error['code'].to_s
22
- msg << " (#{error['type']}):" if error['type']
23
- msg << " #{error['message']}"
24
- @errors << error['message']
25
- msg
26
- end.join(', ')
27
-
28
- super(message)
29
- end
30
-
11
+ class TokenExpiredError < StandardError
31
12
  end
32
13
 
33
- class TokenExpiredError < RequestError
34
- end
35
-
36
- class ValidationError < RequestError
37
-
38
- def initialize(response, message = nil)
39
- @response = response
40
- json_body = JSON.parse(response.body) rescue {}
41
- @errors = Array(json_body['errors']).inject({}) { |mem, var| mem.merge!(var['details']); mem }
42
- message ||= @errors.collect { |field, msg| "#{field}: #{msg.join(', ')}" }.join('; ')
43
- super(response, message)
44
- end
45
-
46
- end
14
+ class ValidationError < StandardError
47
15
 
48
- class AuthorizeError < RequestError
49
16
  end
50
17
 
51
- class RecordNotFound < RequestError
18
+ class AuthorizeError < StandardError
52
19
  end
53
20
 
54
21
  configure do |config|
@@ -15,14 +15,6 @@ module Mautic
15
15
 
16
16
  end
17
17
 
18
- class Attribute < OpenStruct
19
-
20
- def name
21
- @alias
22
- end
23
-
24
- end
25
-
26
18
  class << self
27
19
 
28
20
  def endpoint
@@ -38,9 +30,8 @@ module Mautic
38
30
  def initialize(connection, hash=nil)
39
31
  @connection = connection
40
32
  @table = MauticHash.new
41
- self.attributes = { id: hash['id'], created_at: hash['dateAdded']&.to_time, updated_at: hash['dateModified']&.to_time } if hash
33
+ self.attributes = { created_at: hash['dateAdded']&.to_time, updated_at: hash['dateModified']&.to_time } if hash
42
34
  assign_attributes(hash)
43
- clear_changes
44
35
  end
45
36
 
46
37
  def save(force = false)
@@ -49,37 +40,29 @@ module Mautic
49
40
 
50
41
  def update(force = false)
51
42
  return false if changes.blank?
52
- begin
53
- json = @connection.request((force && :put || :patch), "api/#{endpoint}/#{id}/edit", { body: to_h })
43
+ json = @connection.request((force && :put || :patch), "api/#{endpoint}/#{id}/edit", { body: to_h })
44
+ if json['errors']
45
+ self.errors = json['errors']
46
+ else
54
47
  self.attributes = json[endpoint.singularize]
55
- clear_changes
56
- rescue ValidationError => e
57
- self.errors = e.errors
58
48
  end
59
-
60
- self.errors.blank?
49
+ json['errors'].blank?
61
50
  end
62
51
 
63
52
  def create
64
- begin
65
- json = @connection.request(:post, "api/#{endpoint}/#{id && "#{id}/"}new", { body: to_h })
53
+ json = @connection.request(:post, "api/#{endpoint}/#{id}/new", { body: to_h })
54
+ if json['errors']
55
+ self.errors = json['errors']
56
+ else
66
57
  self.attributes = json[endpoint.singularize]
67
- clear_changes
68
- rescue ValidationError => e
69
- self.errors = e.errors
70
58
  end
71
-
72
- self.errors.blank?
59
+ json['errors'].blank?
73
60
  end
74
61
 
75
62
  def destroy
76
- begin
77
- @connection.request(:delete, "api/#{endpoint}/#{id}/delete")
78
- true
79
- rescue RequestError => e
80
- self.errors = e.errors
81
- false
82
- end
63
+ json = @connection.request(:delete, "api/#{endpoint}/#{id}/delete")
64
+ self.errors = json['errors'] if json['errors']
65
+ json['errors'].blank?
83
66
  end
84
67
 
85
68
  def changes
@@ -90,42 +73,25 @@ module Mautic
90
73
  @table.to_h
91
74
  end
92
75
 
76
+ private
77
+
78
+ def endpoint
79
+ self.class.endpoint
80
+ end
81
+
93
82
  def attributes=(hash)
94
83
  hash.each_pair do |k, v|
95
84
  k = k.to_sym
96
85
  @table[k] = v
97
86
  end
98
- end
99
-
100
- private
101
-
102
- def clear_changes
103
87
  @table.instance_variable_set(:@changes, nil)
104
88
  end
105
89
 
106
- def endpoint
107
- self.class.endpoint
108
- end
109
-
110
90
  def assign_attributes(source = nil)
111
- @mautic_attributes = []
112
91
  source ||= {}
113
92
  data = {}
114
-
115
93
  if (fields = source['fields'])
116
- if fields['all']
117
- @mautic_attributes = fields['all'].collect do |key, value|
118
- data[key] = value
119
- Attribute.new(alias: key, value: value)
120
- end
121
- else
122
- fields.each do |_group, pairs|
123
- pairs.each do |key, attrs|
124
- @mautic_attributes << (a = Attribute.new(attrs))
125
- data[key] = a.value
126
- end
127
- end
128
- end
94
+ data.merge!(fields['all']) if fields['all']
129
95
  elsif source
130
96
  data = source
131
97
  end
@@ -133,4 +99,4 @@ module Mautic
133
99
  end
134
100
 
135
101
  end
136
- end
102
+ end
@@ -1,3 +1,3 @@
1
1
  module Mautic
2
- VERSION = '0.1.8'
2
+ VERSION = '1.0.1'
3
3
  end
@@ -1,7 +1,4 @@
1
1
  ENV['RAILS_ENV'] ||= 'test'
2
- require 'simplecov'
3
- SimpleCov.start
4
-
5
2
  require File.expand_path("../dummy/config/environment.rb", __FILE__)
6
3
  # Prevent database truncation if the environment is production
7
4
  abort('The Rails environment is running in production mode!') if Rails.env.production?
@@ -3,7 +3,6 @@ require 'factory_bot_rails'
3
3
  require 'database_cleaner'
4
4
  require 'faker'
5
5
  require 'webmock/rspec'
6
- require 'pry-rails'
7
6
  # require 'capybara/rspec'
8
7
 
9
8
  ActiveRecord::Migration.maintain_test_schema!
@@ -35,6 +34,10 @@ RSpec.configure do |config|
35
34
  end
36
35
 
37
36
  config.include FactoryBot::Syntax::Methods
37
+ # DatabaseCleaner.clean_with(
38
+ # :truncation,
39
+ # except: %w(ar_internal_metadata)
40
+ # )
38
41
 
39
42
  config.before(:suite) do
40
43
  DatabaseCleaner.clean_with(:truncation, except: %w(ar_internal_metadata))
@@ -44,6 +47,10 @@ RSpec.configure do |config|
44
47
  DatabaseCleaner.strategy = :transaction
45
48
  end
46
49
 
50
+ config.before(:each, js: true) do
51
+ DatabaseCleaner.strategy = :truncation
52
+ end
53
+
47
54
  config.before(:each) do
48
55
  DatabaseCleaner.start
49
56
  end
@@ -58,4 +65,21 @@ RSpec.configure do |config|
58
65
 
59
66
  config.filter_rails_from_backtrace!
60
67
 
68
+ # config.before(:each) do |ex|
69
+ # meta = ex.metadata
70
+ # unless meta[:null]
71
+ # case meta[:logged]
72
+ # when :admin
73
+ # logged(Symphonia::User, FactoryBot.create(:admin_user))
74
+ # when true
75
+ # logged(Symphonia::User, FactoryBot.create(:user))
76
+ # end
77
+ # end
78
+ # end
79
+
80
+ end
81
+
82
+
83
+ def logged(model, user)
84
+ allow(model).to receive(:current).and_return(user)
61
85
  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: 0.1.8
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lukáš Pokorný
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-28 00:00:00.000000000 Z
11
+ date: 2017-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -16,34 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '4.2'
20
- - - ">="
21
- - !ruby/object:Gem::Version
22
- version: 4.2.8
19
+ version: '5.1'
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
24
  - - "~>"
28
25
  - !ruby/object:Gem::Version
29
- version: '4.2'
30
- - - ">="
31
- - !ruby/object:Gem::Version
32
- version: 4.2.8
33
- - !ruby/object:Gem::Dependency
34
- name: jquery-rails
35
- requirement: !ruby/object:Gem::Requirement
36
- requirements:
37
- - - "~>"
38
- - !ruby/object:Gem::Version
39
- version: '3.1'
40
- type: :runtime
41
- prerelease: false
42
- version_requirements: !ruby/object:Gem::Requirement
43
- requirements:
44
- - - "~>"
45
- - !ruby/object:Gem::Version
46
- version: '3.1'
26
+ version: '5.1'
47
27
  - !ruby/object:Gem::Dependency
48
28
  name: oauth2
49
29
  requirement: !ruby/object:Gem::Requirement
@@ -186,12 +166,12 @@ files:
186
166
  - app/assets/stylesheets/mautic/application.css
187
167
  - app/assets/stylesheets/mautic/mautic_connections.css
188
168
  - app/assets/stylesheets/scaffold.css
189
- - app/controllers/concerns/mautic/connections_controller_concern.rb
190
169
  - app/controllers/mautic/application_controller.rb
191
170
  - app/controllers/mautic/connections_controller.rb
192
171
  - app/helpers/mautic/application_helper.rb
193
172
  - app/jobs/mautic/application_job.rb
194
173
  - app/mailers/mautic/application_mailer.rb
174
+ - app/models/application_record.rb
195
175
  - app/models/mautic/application_record.rb
196
176
  - app/models/mautic/connection.rb
197
177
  - app/models/mautic/connections/oauth2.rb
@@ -235,7 +215,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
235
215
  version: '0'
236
216
  requirements: []
237
217
  rubyforge_project:
238
- rubygems_version: 2.7.3
218
+ rubygems_version: 2.6.12
239
219
  signing_key:
240
220
  specification_version: 4
241
221
  summary: Ruby on Rails Mautic integration
@@ -1,118 +0,0 @@
1
- module Mautic
2
- module ConnectionsControllerConcern
3
- extend ActiveSupport::Concern
4
-
5
- included do
6
-
7
- before_action :set_mautic_connection, only: [:show, :edit, :update, :destroy, :oauth2, :authorize]
8
-
9
- end
10
-
11
-
12
- # GET /mautic_connections
13
- def index
14
- @mautic_connections = Mautic::Connection.order(:url)
15
- respond_to do |format|
16
- format.html { render layout: !request.xhr? }
17
- format.json { render json: @mautic_connections }
18
- end
19
- end
20
-
21
- # GET /mautic_connections/1
22
- def show
23
- respond_to do |format|
24
- format.html { render layout: !request.xhr? }
25
- end
26
- end
27
-
28
- # GET /mautic_connections/new
29
- def new
30
- @mautic_connection = Mautic::Connection.new
31
- respond_to do |format|
32
- format.html { render layout: !request.xhr? }
33
- end
34
- end
35
-
36
- # GET /mautic_connections/1/edit
37
- def edit
38
- respond_to do |format|
39
- format.html { render layout: !request.xhr? }
40
- end
41
- end
42
-
43
- # POST /mautic_connections
44
- def create
45
- @mautic_connection = Mautic::Connection.new(mautic_connection_params)
46
-
47
- respond_to do |format|
48
- if @mautic_connection.save
49
- format.html { render(:edit, notice: t('mautic.text_mautic_connection_created')) }
50
- format.js { head :no_content }
51
- format.json { render json: @mautic_connection }
52
- else
53
- format.html { render :new }
54
- format.js { head :unprocessable_entity }
55
- format.json { render json: @mautic_connection.errors, status: :unprocessable_entity }
56
- end
57
- end
58
- end
59
-
60
- # PATCH/PUT /mautic_connections/1
61
- def update
62
- respond_to do |format|
63
- if @mautic_connection.update(mautic_connection_params)
64
- format.html { redirect_to({ action: :index }, notice: t('mautic.text_mautic_connection_updated')) }
65
- format.js { head :no_content }
66
- format.json { render json: @mautic_connection }
67
- else
68
- format.html { render :edit }
69
- format.js { head :unprocessable_entity }
70
- format.json { render json: @mautic_connection.errors, status: :unprocessable_entity }
71
- end
72
- end
73
- end
74
-
75
- # DELETE /mautic_connections/1
76
- def destroy
77
- @mautic_connection.destroy
78
- respond_to do |format|
79
- format.html { redirect_to action: "index", notice: t('mautic.text_mautic_connection_destroyed') }
80
- format.js { render js: "document.getElementById('#{view_context.dom_id(@mautic_connection)}').remove()" }
81
- format.json { render json: @mautic_connection }
82
- end
83
- end
84
-
85
- # ==--==--==--==--
86
-
87
- def authorize
88
- redirect_to @mautic_connection.authorize
89
- end
90
-
91
- def oauth2
92
- begin
93
- response = @mautic_connection.get_code(params.require(:code))
94
- @mautic_connection.update(token: response.token, refresh_token: response.refresh_token)
95
- return render plain: t('mautic.text_mautic_authorize_successfully')
96
- rescue OAuth2::Error => e
97
- flash[:error] = e.message
98
- end
99
-
100
- render :show
101
- end
102
-
103
- private
104
-
105
- # Use callbacks to share common setup or constraints between actions.
106
- def set_mautic_connection
107
- @mautic_connection = Mautic::Connection.find(params[:id])
108
- rescue ActiveRecord::RecordNotFound => e
109
- return render head: 404, plain: e.message
110
- end
111
-
112
- # Only allow a trusted parameter "white list" through.
113
- def mautic_connection_params
114
- params.require(:connection).permit(:url, :client_id, :secret, :type)
115
- end
116
-
117
- end
118
- end