From afbdf6942edb167c8e19ac69d468ecdcb4b9027f Mon Sep 17 00:00:00 2001 From: Claudio Fiorini Date: Sat, 29 Mar 2014 19:35:04 +0100 Subject: [PATCH 1/3] added wiselinks_canonical method --- lib/assets/javascripts/_request_manager.js.coffee | 6 ++++++ lib/assets/javascripts/_response.js.coffee | 9 +++++++++ lib/wiselinks/controller_methods.rb | 8 ++++++++ 3 files changed, 23 insertions(+) diff --git a/lib/assets/javascripts/_request_manager.js.coffee b/lib/assets/javascripts/_request_manager.js.coffee index 396eea6..04e3f1d 100644 --- a/lib/assets/javascripts/_request_manager.js.coffee +++ b/lib/assets/javascripts/_request_manager.js.coffee @@ -81,6 +81,7 @@ class RequestManager => @_title(response.title()) @_description(response.description()) + @_canonical(response.canonical()) @_done($target, status, state, response.content()) ) @@ -102,6 +103,11 @@ class RequestManager $(document).trigger('page:description', decodeURI(value)) $('meta[name="description"]').attr('content', decodeURI(value)) + _canonical: (value) -> + if value? + $(document).trigger('page:canonical', decodeURI(value)) + $('link[rel="canonical"]').attr('href', decodeURI(value)) + window._Wiselinks = {} if window._Wiselinks == undefined window._Wiselinks.RequestManager = RequestManager diff --git a/lib/assets/javascripts/_response.js.coffee b/lib/assets/javascripts/_response.js.coffee index 2540024..9a2b3dd 100644 --- a/lib/assets/javascripts/_response.js.coffee +++ b/lib/assets/javascripts/_response.js.coffee @@ -42,6 +42,15 @@ class Response else @xhr.getResponseHeader('X-Wiselinks-Description') + canonical: -> + @_canonical ?= @_extract_canonical() + + _extract_canonical: -> + if @_is_full_document_response() + $('link[rel="canonical"]', @_get_doc()).text() + else + @xhr.getResponseHeader('X-Wiselinks-Canonical') + _extract_content: -> if @_is_full_document_response() @_get_doc_target_node().html() diff --git a/lib/wiselinks/controller_methods.rb b/lib/wiselinks/controller_methods.rb index d102456..f5e84c4 100644 --- a/lib/wiselinks/controller_methods.rb +++ b/lib/wiselinks/controller_methods.rb @@ -4,6 +4,7 @@ module ControllerMethods def self.included(base) base.helper_method :wiselinks_title base.helper_method :wiselinks_description + base.helper_method :wiselinks_canonical base.before_filter :set_wiselinks_url end @@ -27,6 +28,13 @@ def wiselinks_description(value) end end + def wiselinks_canonical(value) + if self.request.wiselinks? && value.present? + Wiselinks.log("canonical: #{value}") + self.response.headers['X-Wiselinks-Canonical'] = URI.encode(value) + end + end + def set_wiselinks_url # self.response.headers['X-Wiselinks-Url'] = request.env['REQUEST_URI'] if self.request.wiselinks? self.response.headers['X-Wiselinks-Url'] = request.url if self.request.wiselinks? From 83fa5698d7095ee6adb6433d1945b898cf9b9842 Mon Sep 17 00:00:00 2001 From: Claudio Fiorini Date: Sat, 29 Mar 2014 20:57:01 +0100 Subject: [PATCH 2/3] address wiselinks_robots wiselinks_link_rel_prev wiselinks_link_rel_next --- .../javascripts/_request_manager.js.coffee | 18 ++++++++++++ lib/assets/javascripts/_response.js.coffee | 29 +++++++++++++++++++ lib/wiselinks/controller_methods.rb | 24 +++++++++++++++ 3 files changed, 71 insertions(+) diff --git a/lib/assets/javascripts/_request_manager.js.coffee b/lib/assets/javascripts/_request_manager.js.coffee index 04e3f1d..14ba64d 100644 --- a/lib/assets/javascripts/_request_manager.js.coffee +++ b/lib/assets/javascripts/_request_manager.js.coffee @@ -82,6 +82,9 @@ class RequestManager @_title(response.title()) @_description(response.description()) @_canonical(response.canonical()) + @_robots(response.robots()) + @_link_rel_prev(response.link_rel_prev()) + @_link_rel_next(response.link_rel_next()) @_done($target, status, state, response.content()) ) @@ -108,6 +111,21 @@ class RequestManager $(document).trigger('page:canonical', decodeURI(value)) $('link[rel="canonical"]').attr('href', decodeURI(value)) + _robots: (value) -> + if value? + $(document).trigger('page:robots', decodeURI(value)) + $('meta[name="robots"]').attr('content', decodeURI(value)) + + _link_rel_prev: (value) -> + if value? + $(document).trigger('page:link_rel_prev', decodeURI(value)) + $('link[rel="prev"]').attr('href', decodeURI(value)) + + _link_rel_next: (value) -> + if value? + $(document).trigger('page:link_rel_next', decodeURI(value)) + $('link[rel="next"]').attr('href', decodeURI(value)) + window._Wiselinks = {} if window._Wiselinks == undefined window._Wiselinks.RequestManager = RequestManager diff --git a/lib/assets/javascripts/_response.js.coffee b/lib/assets/javascripts/_response.js.coffee index 9a2b3dd..548354d 100644 --- a/lib/assets/javascripts/_response.js.coffee +++ b/lib/assets/javascripts/_response.js.coffee @@ -51,6 +51,35 @@ class Response else @xhr.getResponseHeader('X-Wiselinks-Canonical') + + robots: -> + @_robots ?= @_extract_robots() + + _extract_robots: -> + if @_is_full_document_response() + $('meta[name="robots"]', @_get_doc()).text() + else + @xhr.getResponseHeader('X-Wiselinks-Robots') + + + link_rel_prev: -> + @_link_rel_prev ?= @_extract_link_rel_prev() + + _extract_link_rel_prev: -> + if @_is_full_document_response() + $('link[rel="prev"]', @_get_doc()).text() + else + @xhr.getResponseHeader('X-Wiselinks-LinkRelPrev') + + link_rel_next: -> + @_link_rel_next ?= @_extract_link_rel_next() + + _extract_link_rel_next: -> + if @_is_full_document_response() + $('link[rel="next"]', @_get_doc()).text() + else + @xhr.getResponseHeader('X-Wiselinks-LinkRelNext') + _extract_content: -> if @_is_full_document_response() @_get_doc_target_node().html() diff --git a/lib/wiselinks/controller_methods.rb b/lib/wiselinks/controller_methods.rb index f5e84c4..20375b8 100644 --- a/lib/wiselinks/controller_methods.rb +++ b/lib/wiselinks/controller_methods.rb @@ -5,6 +5,9 @@ def self.included(base) base.helper_method :wiselinks_title base.helper_method :wiselinks_description base.helper_method :wiselinks_canonical + base.helper_method :wiselinks_robots + base.helper_method :wiselinks_link_rel_prev + base.helper_method :wiselinks_link_rel_next base.before_filter :set_wiselinks_url end @@ -35,6 +38,27 @@ def wiselinks_canonical(value) end end + def wiselinks_robots(value) + if self.request.wiselinks? && value.present? + Wiselinks.log("robots: #{value}") + self.response.headers['X-Wiselinks-Robots'] = URI.encode(value) + end + end + + def wiselinks_link_rel_prev(value) + if self.request.wiselinks? && value.present? + Wiselinks.log("link_rel_prev: #{value}") + self.response.headers['X-Wiselinks-LinkRelPrev'] = URI.encode(value) + end + end + + def wiselinks_link_rel_next(value) + if self.request.wiselinks? && value.present? + Wiselinks.log("link_rel_next: #{value}") + self.response.headers['X-Wiselinks-LinkRelNext'] = URI.encode(value) + end + end + def set_wiselinks_url # self.response.headers['X-Wiselinks-Url'] = request.env['REQUEST_URI'] if self.request.wiselinks? self.response.headers['X-Wiselinks-Url'] = request.url if self.request.wiselinks? From 9ebb4178cd1f16dbb495a8fc24651899ec3d6cff Mon Sep 17 00:00:00 2001 From: Claudio Fiorini Date: Sun, 30 Mar 2014 20:37:06 +0200 Subject: [PATCH 3/3] if I have custom pages for 404 or 500 as well as giving http status code I still want to show the result of my custom page that can be handle page:fail with one more parameter --- lib/assets/javascripts/_request_manager.js.coffee | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/assets/javascripts/_request_manager.js.coffee b/lib/assets/javascripts/_request_manager.js.coffee index 14ba64d..f735c0c 100644 --- a/lib/assets/javascripts/_request_manager.js.coffee +++ b/lib/assets/javascripts/_request_manager.js.coffee @@ -30,7 +30,7 @@ class RequestManager self._html_loaded($target, data, status, xhr) ).fail( (xhr, status, error) -> - self._fail($target, status, state, error, xhr.status) + self._fail($target, status, state, error, xhr.status, xhr.responseText) ).always( (data_or_xhr, status, xhr_or_error)-> self._always($target, status, state) @@ -88,9 +88,9 @@ class RequestManager @_done($target, status, state, response.content()) ) - _fail: ($target, status, state, error, code) -> + _fail: ($target, status, state, error, code, data) -> $(document).trigger('page:fail' - [$target, status, decodeURI(state.url), error, code] + [$target, status, decodeURI(state.url), error, code, data] ) _always: ($target, status, state) ->