activerecord_sqlserver_crm 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2a8250ddd778516f78d9384c50b8a518d57c675e
4
+ data.tar.gz: 8ead3ca50d31355f9ebcda6b57583ef562d1c087
5
+ SHA512:
6
+ metadata.gz: 464560a8ee3b02f0c7d5b4907bc7e0054f6406e61c28bdde85fc8a7a2195642dd1e9be6470a1f39174e448307b03eb99056d169d285ea372cd630854bac57503
7
+ data.tar.gz: 68fd565277a9f977b78886e80b31aa5807c18de801c673e215c1d9a8fdd5dd24e71ee603844067ee9f0bea7e1b5e81da5ce3ca3aab237ebaeece670102655ded
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2016 Rolf Lawrenz
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = ActiverecordSqlserverCrm
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,37 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'ActiverecordSqlserverCrm'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+ load 'rails/tasks/statistics.rake'
22
+
23
+
24
+
25
+ Bundler::GemHelper.install_tasks
26
+
27
+ # require 'rake/testtask'
28
+ #
29
+ # Rake::TestTask.new(:test) do |t|
30
+ # t.libs << 'lib'
31
+ # t.libs << 'test'
32
+ # t.pattern = 'test/**/*_test.rb'
33
+ # t.verbose = false
34
+ # end
35
+ #
36
+ #
37
+ # task default: :test
@@ -0,0 +1,10 @@
1
+ module Crm
2
+ class Account < ActiveRecord::Base
3
+ self.table_name = "Account"
4
+ self.primary_key = "AccountId"
5
+
6
+ has_many :notes, foreign_key: 'ObjectId'
7
+ has_many :cases, foreign_key: 'AccountId'
8
+
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ module Crm
2
+ class Campaign < ActiveRecord::Base
3
+ self.table_name = "Campaign"
4
+ self.primary_key = "CampaignId"
5
+
6
+ has_many :notes, foreign_key: 'ObjectId'
7
+
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Crm
2
+ class CampaignResponse < ActiveRecord::Base
3
+ self.table_name = "CampaignResponse"
4
+ self.primary_key = "ActivityId"
5
+
6
+ has_many :notes, foreign_key: 'ObjectId'
7
+
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ module Crm
2
+ class Case < ActiveRecord::Base
3
+ self.table_name = "Incident"
4
+ self.primary_key = "IncidentId"
5
+
6
+ belongs_to :contact, foreign_key: 'IncidentId', crm_key: 'customerid_contact'
7
+ belongs_to :account, foreign_key: 'IncidentId', crm_key: 'customerid_account'
8
+
9
+ has_many :notes, foreign_key: 'ObjectId'
10
+
11
+ end
12
+ end
@@ -0,0 +1,14 @@
1
+ module Crm
2
+ class Contact < ActiveRecord::Base
3
+ self.table_name = "Contact"
4
+ self.primary_key = "ContactId"
5
+
6
+ has_many :cases, foreign_key: 'ContactId'
7
+ has_many :invoices, foreign_key: 'ContactId'
8
+ has_many :notes, foreign_key: 'ObjectId'
9
+
10
+ validates :FirstName, presence: true
11
+ validates :LastName, presence: true
12
+
13
+ end
14
+ end
@@ -0,0 +1,7 @@
1
+ module Crm
2
+ class Currency < ActiveRecord::Base
3
+ self.table_name = "TransactionCurrency"
4
+ self.primary_key = "TransactionCurrencyId"
5
+
6
+ end
7
+ end
@@ -0,0 +1,13 @@
1
+ module Crm
2
+ class Invoice < ActiveRecord::Base
3
+ self.table_name = "Invoice"
4
+ self.primary_key = "InvoiceId"
5
+
6
+ belongs_to :contact, foreign_key: 'InvoiceId', crm_key: 'customerid_contact'
7
+ belongs_to :price_list, foreign_key: 'InvoiceId', crm_key: 'pricelevelid'
8
+
9
+ has_many :invoice_products, foreign_key: 'InvoiceId'
10
+ has_many :notes, foreign_key: 'ObjectId'
11
+
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ module Crm
2
+ class InvoiceProduct < ActiveRecord::Base
3
+ self.table_name = "InvoiceDetail"
4
+ self.primary_key = "InvoiceDetailId"
5
+
6
+ belongs_to :invoice, foreign_key: 'InvoiceDetailId', crm_key: 'invoiceid'
7
+
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ module Crm
2
+ class Note < ActiveRecord::Base
3
+ self.table_name = "Annotation"
4
+ self.primary_key = "AnnotationId"
5
+
6
+ belongs_to :account, foreign_key: 'objectid_account', crm_key: 'Account_Annotation'
7
+ belongs_to :campaign, foreign_key: 'objectid_account', crm_key: 'Campaign_Annotation'
8
+ belongs_to :campaign_response, foreign_key: 'objectid_account', crm_key: 'CampaignResponse_Annotation'
9
+ belongs_to :case, foreign_key: 'objectid_account', crm_key: 'Case_Annotation'
10
+ belongs_to :contact, foreign_key: 'objectid_account', crm_key: 'Contact_Annotation'
11
+ belongs_to :invoice, foreign_key: 'objectid_account', crm_key: 'Invoice_Annotation'
12
+
13
+ end
14
+ end
@@ -0,0 +1,11 @@
1
+ module Crm
2
+ class PriceList < ActiveRecord::Base
3
+ self.table_name = "PriceLevel"
4
+ self.primary_key = "PriceLevelId"
5
+
6
+ has_many :invoices, foreign_key: 'PriceLevelId'
7
+ has_many :price_list_items, foreign_key: 'PriceLevelId'
8
+ has_many :products, foreign_key: 'PriceLevelId'
9
+
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ module Crm
2
+ class PriceListItem < ActiveRecord::Base
3
+ self.table_name = "ProductPriceLevel"
4
+ self.primary_key = "ProductPriceLevelId"
5
+
6
+ belongs_to :price_list, foreign_key: 'ProductPriceLevelId', crm_key: 'pricelevelid'
7
+
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ module Crm
2
+ class Product < ActiveRecord::Base
3
+ self.table_name = "Product"
4
+ self.primary_key = "ProductId"
5
+
6
+ belongs_to :price_list, foreign_key: 'InvoiceId', crm_key: 'pricelevelid'
7
+
8
+ has_many :notes, foreign_key: 'ObjectId'
9
+ has_many :cases, foreign_key: 'ProductId'
10
+
11
+ end
12
+ end
@@ -0,0 +1,9 @@
1
+ module Crm
2
+ class User < ActiveRecord::Base
3
+ self.table_name = "SystemUser"
4
+ self.primary_key = "SystemUserId"
5
+
6
+ has_many :notes, foreign_key: 'ObjectId'
7
+
8
+ end
9
+ end
@@ -0,0 +1 @@
1
+ require "active_record_extension"
@@ -0,0 +1 @@
1
+ ODATA_CONFIG = YAML.load_file(Rails.root.join('config', 'odata.yml'))
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ Rails.application.routes.draw do
2
+ end
@@ -0,0 +1,91 @@
1
+ require 'odata/model'
2
+
3
+ module ActiveRecordExtension
4
+
5
+ extend ActiveSupport::Concern
6
+
7
+ def delete
8
+ ::OData::Model.destroy(self)
9
+ end
10
+
11
+ def destroy
12
+ destroy!
13
+ rescue
14
+ false
15
+ end
16
+
17
+ def destroy!
18
+ run_callbacks :destroy do
19
+ ::OData::Model.destroy(self)
20
+ end
21
+ has_errors = errors.present?
22
+ if has_errors
23
+ raise_record_not_destroyed
24
+ end
25
+ !has_errors
26
+ end
27
+
28
+ def save(*)
29
+ save!
30
+ rescue
31
+ false
32
+ end
33
+
34
+ def save!(*)
35
+ validate!
36
+ run_callbacks :save do
37
+ ::OData::Model.save(self)
38
+ end
39
+ has_errors = errors.present?
40
+ if has_errors
41
+ raise_record_invalid
42
+ else
43
+ reload
44
+ end
45
+ !has_errors
46
+ end
47
+
48
+ def update(attributes)
49
+ attributes.each do |k,v|
50
+ write_attribute(k,v)
51
+ end
52
+ save
53
+ end
54
+
55
+ def update_attribute(name, value)
56
+ write_attribute(name,value)
57
+ save
58
+ end
59
+
60
+ # add your static(class) methods here
61
+ module ClassMethods
62
+ def belongs_to_field?(field)
63
+ @belongs_to_fields ||= belongs_to_fields
64
+ @belongs_to_fields.map(&:foreign_key).include?(field)
65
+ end
66
+
67
+ def belongs_to_field(field)
68
+ @belongs_to_fields ||= belongs_to_fields
69
+ @belongs_to_fields.select{|f| f.foreign_key == field}.first
70
+ end
71
+
72
+ def belongs_to_fields
73
+ associations = reflect_on_all_associations
74
+ associations.select { |a| a.macro == :belongs_to }
75
+ end
76
+ end
77
+ end
78
+
79
+ # include the extension
80
+ ActiveRecord::Base.send(:include, ActiveRecordExtension)
81
+
82
+ # Extend belongs_to for crm_key field
83
+ module BelongsToActiveRecordExtension
84
+ def valid_options
85
+ super + [:crm_key]
86
+ end
87
+ end
88
+
89
+ class ActiveRecord::Associations::Builder::BelongsTo
90
+ include ::BelongsToActiveRecordExtension
91
+ end
@@ -0,0 +1,4 @@
1
+ require "activerecord_sqlserver_crm/engine"
2
+
3
+ module ActiverecordSqlserverCrm
4
+ end
@@ -0,0 +1,4 @@
1
+ module ActiverecordSqlserverCrm
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module ActiverecordSqlserverCrm
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,43 @@
1
+ module OData
2
+ class CreateOperation < Operation
3
+
4
+ def handle_operation_response(response)
5
+ # Grab the id, and put back into the active record instance
6
+ if response.headers['OData-EntityId']
7
+ id = response.headers['OData-EntityId'].scan(/\(([\w-]*)\)/)
8
+ @ar.id = id[0][0] unless id.nil? || id[0].nil?
9
+ @ar.errors[:base] << "Failed to #{operation_callback_name} entity. [http code #{response.code}]" if @ar.id.nil?
10
+ else
11
+ @ar.errors[:base] << "Could not #{operation_callback_name} entity. [http code #{response.code}]" if @ar.id.nil?
12
+ end
13
+ check_response_errors(response)
14
+ end
15
+
16
+ def operation_body
17
+ body = {}
18
+ # Add changed fields and values
19
+ @ar.changes.each do |field, values|
20
+ # If a belongs to field, add association the way OData wants it
21
+ if @ar.class.belongs_to_field?(field)
22
+ belongs_to_field = @ar.class.belongs_to_field(field)
23
+ body["#{belongs_to_field.options[:crm_key]}@odata.bind"] = "/#{belongs_to_field.class_name.downcase}s(#{values[1]})"
24
+ else
25
+ body[field.downcase] = values[1]
26
+ end
27
+ end
28
+ body.to_json
29
+ end
30
+
31
+ def operation_method
32
+ :post
33
+ end
34
+
35
+ def operation_url
36
+ "#{base_url}#{entity_name}"
37
+ end
38
+
39
+ def operation_callback_name
40
+ :create
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,25 @@
1
+ module OData
2
+ class DeleteOperation < Operation
3
+
4
+ def handle_operation_response(response)
5
+ check_response_errors(response)
6
+ end
7
+
8
+ def operation_body
9
+ body = {}
10
+ body.to_json
11
+ end
12
+
13
+ def operation_method
14
+ :delete
15
+ end
16
+
17
+ def operation_callback_name
18
+ :destroy
19
+ end
20
+
21
+ def operation_url
22
+ "#{base_url}#{entity_name}(#{@ar.id})"
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,21 @@
1
+ require 'odata/operation'
2
+ require 'odata/create_operation'
3
+ require 'odata/update_operation'
4
+ require 'odata/delete_operation'
5
+
6
+ module OData
7
+ class Model
8
+ def self.save(ar)
9
+ return unless ar.changed?
10
+ operation = ar.new_record? ? OData::CreateOperation.new(ar) : OData::UpdateOperation.new(ar)
11
+ operation.run
12
+ ar
13
+ end
14
+
15
+ def self.destroy(ar)
16
+ operation = OData::DeleteOperation.new(ar)
17
+ operation.run
18
+ ar
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,88 @@
1
+ require 'typhoeus'
2
+
3
+ module OData
4
+ class Operation
5
+
6
+ attr_accessor :ar
7
+
8
+ def initialize(ar)
9
+ @ar = ar
10
+ end
11
+
12
+ def base_url
13
+ ODATA_CONFIG[Rails.env]['data_url']
14
+ end
15
+
16
+ def check_response_errors(response)
17
+ # Check for Http error
18
+ if response.code.to_i >= 400
19
+ error_message = begin
20
+ JSON.parse(response.body)['error']['message']
21
+ rescue
22
+ "An error occurred"
23
+ end
24
+ @ar.errors[:base] << "#{error_message} [http code #{response.code}]"
25
+ end
26
+ end
27
+
28
+ def entity_name
29
+ "#{@ar.class.table_name.downcase}s"
30
+ end
31
+
32
+ def handle_operation_response(response)
33
+ raise NotImplementedError
34
+ end
35
+
36
+ def operation_body
37
+ nil
38
+ end
39
+
40
+ def operation_callback_name
41
+ raise NotImplementedError
42
+ end
43
+
44
+ def operation_headers
45
+ {
46
+ 'Accept' => 'application/json',
47
+ 'Content-Type' => 'application/json; charset=utf-8'
48
+ }
49
+ end
50
+
51
+ def operation_method
52
+ raise NotImplementedError
53
+ end
54
+
55
+ def operation_password
56
+ ODATA_CONFIG[Rails.env]['password']
57
+ end
58
+
59
+ def operation_url
60
+ raise NotImplementedError
61
+ end
62
+
63
+ def operation_username
64
+ ODATA_CONFIG[Rails.env]['username']
65
+ end
66
+
67
+ def run
68
+ response = send_odata
69
+ handle_operation_response(response)
70
+ end
71
+
72
+ def send_odata
73
+ @ar.run_callbacks operation_callback_name do
74
+ request = ::Typhoeus::Request.new(
75
+ operation_url,
76
+ method: operation_method,
77
+ body: operation_body,
78
+ headers: operation_headers,
79
+ username: operation_username,
80
+ password: operation_password,
81
+ httpauth: :ntlm
82
+ )
83
+ request.run
84
+ end
85
+ end
86
+
87
+ end
88
+ end
@@ -0,0 +1,17 @@
1
+ module OData
2
+ class UpdateOperation < CreateOperation
3
+
4
+ def operation_method
5
+ :patch
6
+ end
7
+
8
+ def operation_callback_name
9
+ :update
10
+ end
11
+
12
+ def operation_url
13
+ "#{base_url}#{entity_name}(#{@ar.id})"
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :activerecord_sqlserver_crm do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord_sqlserver_crm
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Rolf Lawrenz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 4.2.6
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 4.2.6
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord-sqlserver-adapter
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 4.2.13
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 4.2.13
41
+ - !ruby/object:Gem::Dependency
42
+ name: tiny_tds
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.7.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.7.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: typhoeus
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.8.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.8.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-rails
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.4'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.4'
83
+ - !ruby/object:Gem::Dependency
84
+ name: byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: A rails engine that uses ActiveRecord SQL Server to read from CRM SQL
98
+ Server, and OData to write to CRM.
99
+ email:
100
+ - rolf.lawrenz@gmail.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - MIT-LICENSE
106
+ - README.rdoc
107
+ - Rakefile
108
+ - app/models/crm/account.rb
109
+ - app/models/crm/campaign.rb
110
+ - app/models/crm/campaign_response.rb
111
+ - app/models/crm/case.rb
112
+ - app/models/crm/contact.rb
113
+ - app/models/crm/currency.rb
114
+ - app/models/crm/invoice.rb
115
+ - app/models/crm/invoice_product.rb
116
+ - app/models/crm/note.rb
117
+ - app/models/crm/price_list.rb
118
+ - app/models/crm/price_list_item.rb
119
+ - app/models/crm/product.rb
120
+ - app/models/crm/user.rb
121
+ - config/initializers/extensions.rb
122
+ - config/initializers/odata.rb
123
+ - config/routes.rb
124
+ - lib/active_record_extension.rb
125
+ - lib/activerecord_sqlserver_crm.rb
126
+ - lib/activerecord_sqlserver_crm/engine.rb
127
+ - lib/activerecord_sqlserver_crm/version.rb
128
+ - lib/odata/create_operation.rb
129
+ - lib/odata/delete_operation.rb
130
+ - lib/odata/model.rb
131
+ - lib/odata/operation.rb
132
+ - lib/odata/update_operation.rb
133
+ - lib/tasks/activerecord_sqlserver_crm_tasks.rake
134
+ homepage: https://github.com/RolfLawrenz/activerecord_sqlserver_crm
135
+ licenses:
136
+ - MIT
137
+ metadata: {}
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubyforge_project:
154
+ rubygems_version: 2.5.1
155
+ signing_key:
156
+ specification_version: 4
157
+ summary: A rails engine that uses ActiveRecord SQL Server to read from CRM SQL Server,
158
+ and OData to write to CRM.
159
+ test_files: []