Chore: Switch from Carrierwave to ActiveStorage (#393)

This commit is contained in:
Sojan Jose
2020-01-07 22:59:17 +05:30
committed by Pranav Raj S
parent f02d422b6a
commit f875a09fb7
29 changed files with 192 additions and 164 deletions

View File

@@ -8,7 +8,6 @@
# extension :string
# external_url :string
# fallback_title :string
# file :string
# file_type :integer default("image")
# created_at :datetime not null
# updated_at :datetime not null
@@ -19,12 +18,12 @@
require 'uri'
require 'open-uri'
class Attachment < ApplicationRecord
include Rails.application.routes.url_helpers
belongs_to :account
belongs_to :message
mount_uploader :file, AttachmentUploader # used for images
enum file_type: [:image, :audio, :video, :file, :location, :fallback]
has_one_attached :file
before_create :set_file_extension
enum file_type: [:image, :audio, :video, :file, :location, :fallback]
def push_event_data
return base_data.merge(location_metadata) if file_type.to_sym == :location
@@ -68,13 +67,7 @@ class Attachment < ApplicationRecord
}
end
def set_file_extension
if external_url && !fallback?
self.extension = begin
Pathname.new(URI(external_url).path).extname
rescue StandardError
nil
end
end
def file_url
file.attached? ? url_for(file) : ''
end
end

View File

@@ -3,7 +3,6 @@
# Table name: channel_facebook_pages
#
# id :integer not null, primary key
# avatar :string
# name :string not null
# page_access_token :string not null
# user_access_token :string not null
@@ -20,11 +19,13 @@
module Channel
class FacebookPage < ApplicationRecord
include Avatarable
self.table_name = 'channel_facebook_pages'
validates :account_id, presence: true
validates :page_id, uniqueness: { scope: :account_id }
mount_uploader :avatar, AvatarUploader
has_one_attached :avatar
belongs_to :account
has_one :inbox, as: :channel, dependent: :destroy

View File

@@ -0,0 +1,18 @@
# frozen_string_literal: true
module Avatarable
extend ActiveSupport::Concern
include Rails.application.routes.url_helpers
included do
has_one_attached :avatar
end
def avatar_url
if avatar.attached? && avatar.representable?
url_for(avatar.representation(resize: '250x250'))
else
''
end
end
end

View File

@@ -3,7 +3,6 @@
# Table name: contacts
#
# id :integer not null, primary key
# avatar :string
# email :string
# name :string
# phone_number :string
@@ -20,13 +19,13 @@
class Contact < ApplicationRecord
include Pubsubable
include Avatarable
validates :account_id, presence: true
belongs_to :account
has_many :conversations, dependent: :destroy
has_many :contact_inboxes, dependent: :destroy
has_many :inboxes, through: :contact_inboxes
mount_uploader :avatar, AvatarUploader
def get_source_id(inbox_id)
contact_inboxes.find_by!(inbox_id: inbox_id).source_id
@@ -36,7 +35,7 @@ class Contact < ApplicationRecord
{
id: id,
name: name,
thumbnail: avatar.thumb.url,
thumbnail: avatar_url,
pubsub_token: pubsub_token
}
end

View File

@@ -68,11 +68,10 @@ class Inbox < ApplicationRecord
Facebook::Messenger::Subscriptions.subscribe(
access_token: channel.page_access_token,
subscribed_fields: %w[
message_mention messages messaging_account_linking messaging_checkout_updates
message_echoes message_deliveries messaging_game_plays messaging_optins messaging_optouts
messaging_payments messaging_postbacks messaging_pre_checkouts message_reads messaging_referrals
messaging_handovers messaging_policy_enforcement messaging_page_feedback
messaging_appointments messaging_direct_sends
messages messaging_postbacks messaging_optins message_deliveries
message_reads messaging_payments messaging_pre_checkouts messaging_checkout_updates
messaging_account_linking messaging_referrals message_echoes messaging_game_plays
standby messaging_handovers messaging_policy_enforcement message_reactions
]
)
end

View File

@@ -47,6 +47,7 @@ class User < ApplicationRecord
include DeviseTokenAuth::Concerns::User
include Events::Types
include Pubsubable
include Avatarable
include Rails.application.routes.url_helpers
devise :database_authenticatable,
@@ -57,12 +58,6 @@ class User < ApplicationRecord
:validatable,
:confirmable
# Used by the actionCable/PubSub Service we use for real time communications
has_secure_token :pubsub_token
# Uses active storage for the avatar
has_one_attached :avatar
# The validation below has been commented out as it does not
# work because :validatable in devise overrides this.
# validates_uniqueness_of :email, scope: :account_id
@@ -108,14 +103,6 @@ class User < ApplicationRecord
Rails.configuration.dispatcher.dispatch(AGENT_REMOVED, Time.zone.now, account: account)
end
def avatar_url
if avatar.attached? && avatar.representable?
url_for(avatar.representation(resize: '250x250'))
else
''
end
end
def push_event_data
{
name: name,