Created
October 4, 2017 18:38
-
-
Save pnispel/41364ce2cc77cad05605a153b769beab to your computer and use it in GitHub Desktop.
Class method include module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ProstoreFile < ActiveRecord::Base | |
... | |
include ProstoreFile::UrlHelpers | |
... | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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