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
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