Skip to content

Instantly share code, notes, and snippets.

@pnispel
Created October 4, 2017 18:38
Show Gist options
  • Save pnispel/41364ce2cc77cad05605a153b769beab to your computer and use it in GitHub Desktop.
Save pnispel/41364ce2cc77cad05605a153b769beab to your computer and use it in GitHub Desktop.
Class method include module
class ProstoreFile < ActiveRecord::Base
...
include ProstoreFile::UrlHelpers
...
end
class ProstoreFile
module UrlHelpers
def self.included(base)
base.extend ClassMethods
base.class_eval do
scope :disabled, -> { where(disabled: true) }
end
end
module ClassMethods
# https://gist.github.com/Manfred/be551053857be12763a58afbb30c0d49
def url(profile: 'default', purpose: 'general', key:, uuid:)
return procore_url("utilities", "prostore_local", key) if profile == "local" # NOTE: this is to support future work
bucket = S3Store.new(profile)[purpose].bucket
if binder_enabled?
binder_client.signed_url(
uuid: uuid,
key: key,
bucket: bucket.name,
)
else
bucket.objects[key].url_for(:read, force_path_style: true, expires: 20.years.from_now).to_s
end
end
def binder_enabled?
ENV.fetch("BINDER_ENABLED", "true") == "true"
end
private
def binder_client
Binder::Client.new(
secret: Rails.application.secrets.binder_secret,
base_url: Rails.application.config.binder_host,
)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment