From 1a6c4c19815f0cf026db1a7ab8405f2821610fe9 Mon Sep 17 00:00:00 2001 From: Rick Barrette Date: Thu, 7 Jan 2016 14:59:51 -0500 Subject: [PATCH] Moving Fat from the controller to the Customer Model --- app/controllers/qbo_controller.rb | 26 ++------------------------ app/models/qbo.rb | 19 +++++++++++++++++++ app/models/qbo_customers.rb | 12 ++++++++++++ init.rb | 1 + 4 files changed, 34 insertions(+), 24 deletions(-) diff --git a/app/controllers/qbo_controller.rb b/app/controllers/qbo_controller.rb index 2cf74bf..6500afb 100644 --- a/app/controllers/qbo_controller.rb +++ b/app/controllers/qbo_controller.rb @@ -11,17 +11,6 @@ class QboController < ApplicationController unloadable - # Load OAuth Token - QB_KEY = Setting.plugin_redmine_qbo['settingsOAuthConsumerKey'] - QB_SECRET = Setting.plugin_redmine_qbo['settingsOAuthConsumerSecret'] - - $qb_oauth_consumer = OAuth::Consumer.new(QB_KEY, QB_SECRET, { - :site => "https://oauth.intuit.com", - :request_token_path => "/oauth/v1/get_request_token", - :authorize_url => "https://appcenter.intuit.com/Connect/Begin", - :access_token_path => "/oauth/v1/get_access_token" - }) - # # Called when the QBO Top Menu us shown # @@ -34,7 +23,7 @@ class QboController < ApplicationController # def authenticate callback = request.base_url + qbo_oauth_callback_path - token = $qb_oauth_consumer.get_request_token(:oauth_callback => callback) + token = Qbo.get_oauth_consumer.get_request_token(:oauth_callback => callback) session[:qb_request_token] = token redirect_to("https://appcenter.intuit.com/Connect/Begin?oauth_token=#{token.token}") and return end @@ -71,18 +60,7 @@ class QboController < ApplicationController # def sync if Qbo.exists? then - qbo = Qbo.first - oauth_client = OAuth::AccessToken.new($qb_oauth_consumer, qbo.token, qbo.secret) - service = Quickbooks::Service::Customer.new(:company_id => qbo.realmId, :access_token => oauth_client) - - customers = service.all - - #update the customer table - customers.each { |customer| - qbo_customer = QboCustomers.find_or_create_by(id: customer.id) - qbo_customer.name = customer.display_name - qbo_customer.save! - } + QboCustomers.update_all end redirect_to qbo_path(:redmine_qbo), :flash => { :notice => "Successfully synced to Quickbooks" } diff --git a/app/models/qbo.rb b/app/models/qbo.rb index f6fe9bf..14827db 100644 --- a/app/models/qbo.rb +++ b/app/models/qbo.rb @@ -11,4 +11,23 @@ class Qbo < ActiveRecord::Base unloadable validates_presence_of :token, :secret, :realmId, :token_expires_at, :reconnect_token_at + + QB_KEY = Setting.plugin_redmine_qbo['settingsOAuthConsumerKey'] + QB_SECRET = Setting.plugin_redmine_qbo['settingsOAuthConsumerSecret'] + + $qb_oauth_consumer = OAuth::Consumer.new(QB_KEY, QB_SECRET, { + :site => "https://oauth.intuit.com", + :request_token_path => "/oauth/v1/get_request_token", + :authorize_url => "https://appcenter.intuit.com/Connect/Begin", + :access_token_path => "/oauth/v1/get_access_token" + }) + + def self.get_auth_token + qbo = first + return OAuth::AccessToken.new($qb_oauth_consumer, qbo.token, qbo.secret) + end + + def self.get_oauth_consumer + return $qb_oauth_consumer + end end diff --git a/app/models/qbo_customers.rb b/app/models/qbo_customers.rb index a829ff8..ff64af8 100644 --- a/app/models/qbo_customers.rb +++ b/app/models/qbo_customers.rb @@ -12,4 +12,16 @@ class QboCustomers < ActiveRecord::Base unloadable has_many :issues attr_accessible :name + + def self.update_all + qbo = Qbo.first + service = Quickbooks::Service::Customer.new(:company_id => qbo.realmId, :access_token => Qbo.get_auth_token) + + # Update the customer table + service.all.each { |customer| + qbo_customer = QboCustomers.find_or_create_by(id: customer.id) + qbo_customer.name = customer.display_name + qbo_customer.save! + } + end end diff --git a/init.rb b/init.rb index de756f8..9e75e6b 100644 --- a/init.rb +++ b/init.rb @@ -28,4 +28,5 @@ Redmine::Plugin.register :redmine_qbo do # Register QBO top menu item menu :top_menu, :qbo, { :controller => 'qbo', :action => 'index' }, :caption => 'Quickbooks' + end