rooftop-rails 0.0.6.4 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9c763b6f784a114983b3477ba471e1ae4192c4d7
4
- data.tar.gz: 052fd96a5732b5af07af3f7d85c7a8a385f6c21e
3
+ metadata.gz: 999a8685afdfab12db176b020b83f959868fce71
4
+ data.tar.gz: 525946c0293c90ef21824651362949ed3747597b
5
5
  SHA512:
6
- metadata.gz: de633ef360fa5634eaf35a14b834074d76167cb8e34329e50fb4dfcb9b34bb742e51c5e6f734f5a6f9d78612e6e79b53fb73bb9e4966dcef3b7df50ff01e81ee
7
- data.tar.gz: 6822909f77f6c4973fd1838a79cab5cf8f353ef0f2a2152e021accfd3db7a31af8a9689f1eee22c2971f6c864eaad97e2800a9e4248345747e034d48357ed1bf
6
+ metadata.gz: 12d8a8a7b2485ba1e1adaa73c4d0a66a41f57b2035b0c779fc5ac0b8b446a99f17fa649c55b8a2ed13f082929bd3e332fc626e6510b1bfc17a05020fd1499cf1
7
+ data.tar.gz: 17369a9d432389976470c3c5ea77273bee5ea4624e96835b930c854480786655c074b69e6198f01316a91c6bb0e9e4fd0af4204a4d26aaffdbbc94b22098c867
@@ -0,0 +1,24 @@
1
+ module Rooftop
2
+ module Rails
3
+ # A controller mixin with utility methods for handling previews. This presupposes you're using the mounted Rooftop::Rails::PreviewController to receive POST requests for previews from Rooftop.
4
+ module Preview
5
+
6
+ def self.included(base)
7
+ base.send(:before_action, -> {
8
+ if preview?
9
+ Rooftop.include_drafts = true
10
+ else
11
+ Rooftop.include_drafts = false
12
+ end
13
+
14
+ })
15
+ end
16
+
17
+ def preview?
18
+ params[:preview].present? && session[:preview].present? && params[:preview] == session[:preview]["secret"]
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+
@@ -0,0 +1,38 @@
1
+ class Rooftop::Rails::PreviewController < ActionController::Base
2
+ skip_before_filter :verify_authenticity_token, :only => [:create]
3
+
4
+ #this is where we receive a message from Rooftop via a POST
5
+ def create
6
+ raise Rooftop::Content::PreviewKeyMissingError, "You need to POST a preview key" unless params[:preview_key].present?
7
+ session.delete(:preview)
8
+ # Rooftop will POST data to this endpoint
9
+ Rooftop.include_drafts = true
10
+
11
+ request.format = :json
12
+ session[:preview] = {
13
+ secret: Digest::SHA256.hexdigest(params[:preview_key]),
14
+ key: params[:preview_key]
15
+ }
16
+ item = Rooftop::Rails::PostTypeResolver.new(params[:post_type]).resolve.find(params[:id])
17
+ slug = item.respond_to?(:nested_path) ? item.nested_path : item.slug
18
+ if item.preview_key_matches?(params[:preview_key])
19
+ redirect_path = Rooftop::Rails::RouteResolver.new(params[:post_type].to_sym, slug).resolve(preview: session[:preview][:secret])
20
+ if redirect_path.nil?
21
+ raise ActionController::RoutingError, "Couldn't find a route to preview this post type"
22
+ else
23
+ redirect_to redirect_path
24
+ end
25
+ else
26
+ raise Rooftop::Content::PreviewKeyMismatchError, "The preview key received doesn't match the key in Rooftop"
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def preview_params
33
+ params.permit!
34
+ end
35
+
36
+
37
+
38
+ end
@@ -11,6 +11,12 @@ module Rooftop
11
11
  "#{self.class.cache_key_base}/#{self.id}"
12
12
  end
13
13
 
14
+ def expire_cache!
15
+ self.class.send(:expire_cache_for, self)
16
+ end
17
+
18
+ alias_method :expire!, :expire_cache!
19
+
14
20
  module ClassMethods
15
21
 
16
22
  # Base of the cache key for this class.
@@ -0,0 +1,16 @@
1
+ module Rooftop
2
+ module Rails
3
+ module PreviewModel
4
+ include ActiveSupport::Concern
5
+
6
+ # Call the preview() method defined in Rooftop, but expire the cache afterwards
7
+ def preview
8
+ # super here is defined in Rooftop::Preview#preview()
9
+ preview = super
10
+ # expire is defined in Rooftop::Rails::ObjectCache#expire!
11
+ preview.expire!
12
+ return preview
13
+ end
14
+ end
15
+ end
16
+ end
data/config/routes.rb CHANGED
@@ -6,4 +6,6 @@ Rooftop::Rails::Engine.routes.draw do
6
6
  end
7
7
  end
8
8
  end
9
+
10
+ resources :preview, only: [:create], defaults: { format: :json }
9
11
  end
data/lib/rooftop/rails.rb CHANGED
@@ -23,12 +23,7 @@ module Rooftop
23
23
  :authenticate_webhooks,
24
24
  :webhooks_username,
25
25
  :webhooks_password,
26
- :preview_username,
27
- :preview_password,
28
- :preview_domain,
29
- :enable_preview_domain,
30
26
  :perform_http_response_caching,
31
- :perform_object_caching,
32
27
  :cache_store,
33
28
  :cache_logger,
34
29
  :logger,
@@ -37,10 +32,22 @@ module Rooftop
37
32
  :resource_route_map,
38
33
  :post_type_mapping
39
34
 
35
+ def perform_object_caching=(perform_caching)
36
+ if perform_caching.is_a?(Proc)
37
+ @perform_object_caching = perform_caching
38
+ else
39
+ @perform_object_caching = ->{perform_caching}
40
+ end
41
+ end
42
+
43
+ def perform_object_caching
44
+ @perform_object_caching.call
45
+ end
46
+
40
47
  def initialize
41
48
  @authenticate_webhooks = true
42
49
  @perform_http_response_caching = ::Rails.configuration.action_controller.perform_caching
43
- @perform_object_caching = ::Rails.configuration.action_controller.perform_caching
50
+ @perform_object_caching = ->{::Rails.configuration.action_controller.perform_caching}
44
51
  @cache_store = ::Rails.cache
45
52
  @cache_logger = ::Rails.logger
46
53
  @ssl_options = {}
@@ -50,6 +50,13 @@ module Rooftop
50
50
  end
51
51
  end
52
52
 
53
+ initializer "add_preview_to_rooftop_models" do
54
+ ::Rails.application.eager_load!
55
+ Rooftop::Base.included_classes.each do |klass|
56
+ klass.send(:include, Rooftop::Rails::PreviewModel)
57
+ end
58
+ end
59
+
53
60
  initializer "clear_caches_on_webhook_notification" do
54
61
  ActiveSupport::Notifications.subscribe(/rooftop.*/) do |name, start, finish, id, payload|
55
62
  Rooftop::Rails::CacheExpirer.expire(payload)
@@ -1,5 +1,5 @@
1
1
  module Rooftop
2
2
  module Rails
3
- VERSION = "0.0.6.4"
3
+ VERSION = "0.1.3"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rooftop-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6.4
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Error Studio
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-26 00:00:00.000000000 Z
11
+ date: 2016-09-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: require_all
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - ~>
60
60
  - !ruby/object:Gem::Version
61
- version: 0.0.7
61
+ version: 0.1.0
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - ~>
67
67
  - !ruby/object:Gem::Version
68
- version: 0.0.7
68
+ version: 0.1.0
69
69
  description: This gem provides utility methods and a mountable engine for Rails applications
70
70
  using the Rooftop gem
71
71
  email:
@@ -76,11 +76,14 @@ extra_rdoc_files: []
76
76
  files:
77
77
  - Rakefile
78
78
  - app/controllers/concerns/rooftop/rails/nested_resource.rb
79
+ - app/controllers/concerns/rooftop/rails/preview.rb
80
+ - app/controllers/rooftop/rails/preview_controller.rb
79
81
  - app/controllers/rooftop/rails/webhooks_controller.rb
80
82
  - app/helpers/rooftop/rails/content_helper.rb
81
83
  - app/helpers/rooftop/rails/menu_helper.rb
82
84
  - app/models/concerns/rooftop/rails/nested_model.rb
83
85
  - app/models/concerns/rooftop/rails/object_cache.rb
86
+ - app/models/concerns/rooftop/rails/preview_model.rb
84
87
  - config/routes.rb
85
88
  - lib/generators/rooftop/rooftop_generator.rb
86
89
  - lib/generators/rooftop/templates/initializer.rb.erb
@@ -90,7 +93,6 @@ files:
90
93
  - lib/rooftop/rails/engine.rb
91
94
  - lib/rooftop/rails/errors.rb
92
95
  - lib/rooftop/rails/post_type_resolver.rb
93
- - lib/rooftop/rails/preview.rb
94
96
  - lib/rooftop/rails/route_resolver.rb
95
97
  - lib/rooftop/rails/version.rb
96
98
  - test/controllers/rooftop_rails/webhooks_controller_test.rb
@@ -1,53 +0,0 @@
1
- module Rooftop::Rails
2
- module Preview
3
- extend ActiveSupport::Concern
4
-
5
- included do
6
- before_action :check_preview_domain
7
- after_action :remove_preview_cache
8
- helper_method :preview?
9
- end
10
- # Check whether the subdomain being presented is the preview domain.
11
- # If so, set Rooftop to add the preview header
12
- def check_preview_domain
13
- # If enable_preview_domain is not enabled, explicitly set use_preview_api false and return
14
- unless Rooftop::Rails.configuration.enable_preview_domain
15
- Rooftop.preview = false
16
- return
17
- end
18
-
19
- #check subdomain matches the configured one - we assume it's first sub.domain.in.the.array
20
- if request.subdomains.first == Rooftop::Rails.configuration.preview_domain
21
- authenticated = authenticate_with_http_basic do |u,p|
22
- u == Rooftop::Rails.configuration.preview_username
23
- p == Rooftop::Rails.configuration.preview_password
24
- end
25
- # If user is authenticated, we're good to switch to the preview api
26
- if authenticated
27
- Rooftop.preview = true
28
- else
29
- #otherwise ask for user / pass
30
- request_http_basic_authentication
31
- end
32
- else
33
- #if the subdomain doesn't match the configured one, explicitly set to false
34
- Rooftop.preview = false
35
- return
36
- end
37
-
38
- end
39
-
40
- # If we're in preview mode, we need to remove the preview view caches which were created.
41
- # this is a bit of a hack but it's probably not feasible to turn off caching in preview mode.
42
- def remove_preview_cache
43
- # in preview mode, we alias_method_chain the cache_key method on Rooftop::Base to append 'preview/'
44
- # to the front of the key.
45
- return unless request.subdomain == Rooftop::Rails.configuration.preview_domain
46
- expire_fragment(%r{.*/preview/.*})
47
- end
48
-
49
- def preview?
50
- Rooftop.preview == true
51
- end
52
- end
53
- end