hello rubocop

This commit is contained in:
2020-04-28 01:40:06 +02:00
parent 2e5954124d
commit fa348cdeeb
69 changed files with 382 additions and 202 deletions

View File

@@ -1,3 +1,5 @@
# frozen_string_literal: true
module ApplicationCable
class Channel < ActionCable::Channel::Base
end

View File

@@ -1,3 +1,5 @@
# frozen_string_literal: true
module ApplicationCable
class Connection < ActionCable::Connection::Base
end

View File

@@ -1,8 +1,10 @@
# frozen_string_literal: true
class ApplicationController < ActionController::Base
helper_method :current_user, :logged_in?
def require_login
redirect_to login_url unless current_user.present?
redirect_to login_url if current_user.blank?
end
def current_user

View File

@@ -1,3 +1,5 @@
# frozen_string_literal: true
class FileUploadsController < ApplicationController
def show
@form = Form.find_by!(token: params[:form_id])

View File

@@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'google/apis/sheets_v4'
require 'google/api_client/client_secrets'
class FormsController < ApplicationController

View File

@@ -1,5 +1,6 @@
class HomeController < ApplicationController
# frozen_string_literal: true
class HomeController < ApplicationController
def index
redirect_to forms_url if logged_in?
end

View File

@@ -1,5 +1,6 @@
class OauthsController < ApplicationController
# frozen_string_literal: true
class OauthsController < ApplicationController
# Sends the user on a trip to the provider,
# and after authorizing there back to the callback url.
def oauth
@@ -9,26 +10,25 @@ class OauthsController < ApplicationController
def callback
provider = params[:provider]
if @user = login_from(provider)
redirect_to root_path, :notice => "Logged in from #{provider.titleize}!"
redirect_to root_path, notice: "Logged in from #{provider.titleize}!"
else
begin
@user = create_from(provider)
if authentication = @user.authentications.find_by(provider: provider)
authentication.update({
access_token: @access_token.token,
refresh_token: @access_token.refresh_token,
expires_at: Time.at(@access_token.expires_at)
})
access_token: @access_token.token,
refresh_token: @access_token.refresh_token,
expires_at: Time.zone.at(@access_token.expires_at)
})
end
reset_session
auto_login(@user)
redirect_to root_path, :notice => "Logged in from #{provider.titleize}!"
rescue
redirect_to root_path, notice: "Logged in from #{provider.titleize}!"
rescue StandardError
Rails.logger.error("Failed to login from #{provider}")
redirect_to root_path, :alert => "Failed to login from #{provider.titleize}!"
redirect_to root_path, alert: "Failed to login from #{provider.titleize}!"
end
end
end
end

View File

@@ -1,5 +1,6 @@
class SessionsController < ApplicationController
# frozen_string_literal: true
class SessionsController < ApplicationController
def new
reset_session
end
@@ -8,5 +9,4 @@ class SessionsController < ApplicationController
reset_session
redirect_to root_url
end
end

View File

@@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'google/apis/sheets_v4'
class SubmissionsController < ApplicationController
skip_before_action :verify_authenticity_token
@@ -14,10 +16,14 @@ class SubmissionsController < ApplicationController
respond_to do |format|
if @submission.save
format.html { redirect_to(@form.thank_you_url) if @form.thank_you_url.present? }
format.html do
redirect_to(@form.thank_you_url) if @form.thank_you_url.present?
end
format.json { render(json: { success: true, submission: @submission.data }) }
else
format.html { redirect_to(@form.thank_you_url) if @form.thank_you_url.present? }
format.html do
redirect_to(@form.thank_you_url) if @form.thank_you_url.present?
end
format.json { render(json: { error: @submission.errors }, status: 422) }
end
end

View File

@@ -1,2 +1,4 @@
# frozen_string_literal: true
module ApplicationHelper
end

View File

@@ -1,3 +1,5 @@
# frozen_string_literal: true
class ApplicationJob < ActiveJob::Base
# Automatically retry jobs that encountered a deadlock
# retry_on ActiveRecord::Deadlocked

View File

@@ -1,8 +1,10 @@
# frozen_string_literal: true
class SubmissionAppendJob < ApplicationJob
queue_as :default
rescue_from(Signet::AuthorizationError, Google::Apis::AuthorizationError) do |exception|
submission_id = self.arguments.first
rescue_from(Signet::AuthorizationError, Google::Apis::AuthorizationError) do |_exception|
submission_id = arguments.first
Rails.logger.error("AuthorizationError during SubmissionAppend: submission_id=#{submission_id}")
submission = Submission.find(submission_id)
submission.form.deactivate!('AuthorizationError')

View File

@@ -1,3 +1,5 @@
# frozen_string_literal: true
class ApplicationMailer < ActionMailer::Base
default from: 'from@example.com'
layout 'mailer'

View File

@@ -1,3 +1,5 @@
# frozen_string_literal: true
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end

View File

@@ -1,7 +1,9 @@
# frozen_string_literal: true
class Authentication < ApplicationRecord
belongs_to :user
scope :for, -> (provider) { where(provider: provider) }
scope :for, ->(provider) { where(provider: provider) }
encrypts :access_token
encrypts :refresh_token
@@ -12,17 +14,17 @@ class Authentication < ApplicationRecord
def google_authorization
return nil unless provider == 'google'
@google_authorization ||= CLIENT_SECRETS.to_authorization.tap do |c|
c.access_token = self.access_token
c.refresh_token = self.refresh_token
c.expires_at = self.expires_at
c.access_token = access_token
c.refresh_token = refresh_token
c.expires_at = expires_at
if expires_at < 1.minute.from_now
c.refresh!
self.access_token = c.access_token if c.access_token.present?
self.refresh_token = c.refresh_token if c.refresh_token.present?
self.expires_at = Time.at(c.expires_at) if c.expires_at.present?
self.expires_at = Time.zone.at(c.expires_at) if c.expires_at.present?
end
end
end
end

View File

@@ -1,3 +1,5 @@
# frozen_string_literal: true
class Form < ApplicationRecord
belongs_to :user
has_many :submissions, dependent: :destroy
@@ -10,12 +12,12 @@ class Form < ApplicationRecord
encrypts :airtable_api_key
encrypts :airtable_app_key
validates_presence_of :title
validates_inclusion_of :backend_name, in: ['google_sheets', 'airtable']
validates :title, presence: true
validates :backend_name, inclusion: { in: %w[google_sheets airtable] }
# Airtable validations
validates_presence_of :airtable_api_key, if: :airtable?
validates_presence_of :airtable_app_key, if: :airtable?
validates_presence_of :airtable_table, if: :airtable?
validates :airtable_api_key, presence: { if: :airtable? }
validates :airtable_app_key, presence: { if: :airtable? }
validates :airtable_table, presence: { if: :airtable? }
# TODO: use counter_cache option on association
def submissions_count
@@ -27,12 +29,10 @@ class Form < ApplicationRecord
end
def deactivate!(reason = nil)
self.user.deactivate!(reason)
user.deactivate!(reason)
end
def active?
self.user.active?
end
delegate :active?, to: :user
def airtable?
backend_name == 'airtable'

View File

@@ -1,10 +1,12 @@
# frozen_string_literal: true
class Submission < ApplicationRecord
belongs_to :form
has_many_attached :files
acts_as_sequenced scope: :form_id
validates_presence_of :data, if: :appended_at?
validates :data, presence: { if: :appended_at? }
def process_data(submitted_data)
processed_data = {}
@@ -12,7 +14,7 @@ class Submission < ApplicationRecord
processed_data[key] = submission_value_for(value)
end
update_attribute(:data, processed_data)
SubmissionAppendJob.perform_later(self.id)
SubmissionAppendJob.perform_later(id)
end
def submission_value_for(value)
@@ -37,7 +39,8 @@ class Submission < ApplicationRecord
attachment = ActiveStorage::Attachment.new(record: self, name: 'files', blob: create_one.blob)
attachment.save
# return the URL that we use to show in the Spreadsheet
Rails.application.routes.url_helpers.file_upload_url(form_id: form, submission_id: self, id: attachment.token, host: DEFAULT_HOST, filename: attachment.blob.filename)
Rails.application.routes.url_helpers.file_upload_url(form_id: form, submission_id: self, id: attachment.token,
host: DEFAULT_HOST, filename: attachment.blob.filename)
else
value.to_s
end

View File

@@ -1,9 +1,11 @@
# frozen_string_literal: true
class User < ApplicationRecord
authenticates_with_sorcery!
has_many :authentications, dependent: :destroy
has_many :forms, dependent: :destroy
def deactivate!(reason = nil)
def deactivate!(_reason = nil)
# currently we only use deactivate if we get an authentication exception appending data to a spreadsheet
authentications.last&.update(expires_at: Time.current)
end