Module: ContentfulRails::Preview

Extended by:
ActiveSupport::Concern
Defined in:
lib/contentful_rails/preview.rb

Instance Method Summary collapse

Instance Method Details

#check_preview_domainObject

Check whether the subdomain being presented is the preview domain. If so, set ContentfulModel to use the preview API, and request a username / password



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/contentful_rails/preview.rb', line 12

def check_preview_domain
  # If enable_preview_domain is not enabled, explicitly set use_preview_api false and return
  unless ContentfulRails.configuration.enable_preview_domain
    ContentfulModel.use_preview_api = false
    return
  end

  #check subdomain matches the configured one - we assume it's first sub.domain.in.the.array
  if request.subdomains.first == ContentfulRails.configuration.preview_domain
    authenticated = authenticate_with_http_basic  do |u,p|
      u == ContentfulRails.configuration.preview_username
      p == ContentfulRails.configuration.preview_password
    end
    # If user is authenticated, we're good to switch to the preview api
    if authenticated
      ContentfulModel.use_preview_api = true
    else
      #otherwise ask for user / pass
        request_http_basic_authentication
    end
  else
    #if the subdomain doesn't match the configured one, explicitly set to false
    ContentfulModel.use_preview_api = false
    return
  end

end

#preview?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/contentful_rails/preview.rb', line 49

def preview?
  ContentfulModel.use_preview_api == true
end

#remove_preview_cacheObject

If we’re in preview mode, we need to remove the preview view caches which were created. this is a bit of a hack but it’s probably not feasible to turn off caching in preview mode.



42
43
44
45
46
47
# File 'lib/contentful_rails/preview.rb', line 42

def remove_preview_cache
  # in preview mode, we alias_method_chain the cache_key method on ContentfulModel::Base to append 'preview/'
  # to the front of the key.
  return unless request.subdomain == ContentfulRails.configuration.preview_domain
  expire_fragment(%r{.*/preview/.*})
end