docbrown/app/models/concerns/purgeable.rb
Alex 7dfffac3db
[deploy] Copy ActiveRecord methods from fastly-rails (#7145)
* Add comment referencing Fastly gem

* Create purgable concern and include it

* Typo: deprecate --> deprecated

* Include Purgable on the Tag model

* Rename Purgable to Purgeable

* Reorganize fastly and service methods in Purgeable

* Add and fix specs

* Add development env guards
2020-04-13 10:46:15 -04:00

74 lines
1.3 KiB
Ruby

# Copied from the deprecated fastly-rails gem
# https://github.com/fastly/fastly-rails/blob/master/lib/fastly-rails/active_record/surrogate_key.rb
#
# This concern handles purge and purge_all calls to purge the edge cache (Fastly)
module Purgeable
extend ActiveSupport::Concern
module ClassMethods
def purge_all
return if Rails.env.development?
service.purge_by_key(table_key)
end
def soft_purge_all
return if Rails.env.development?
service.purge_by_key(table_key, true)
end
def table_key
table_name
end
def fastly
return if Rails.env.development?
Fastly.new(api_key: ApplicationConfig["FASTLY_API_KEY"])
end
def service
return if Rails.env.development?
Fastly::Service.new({ id: ApplicationConfig["FASTLY_SERVICE_ID"] }, fastly)
end
end
# Instance methods
def record_key
"#{table_key}/#{id}"
end
def table_key
self.class.table_key
end
def purge
return if Rails.env.development?
service.purge_by_key(record_key)
end
def soft_purge
return if Rails.env.development?
service.purge_by_key(record_key, true)
end
def purge_all
self.class.purge_all
end
def soft_purge_all
self.class.soft_purge_all
end
def fastly
self.class.fastly
end
def service
self.class.service
end
end