use Context module syntaxe for middlewares
Julien Girard
committed May 23, 2018
commit 14a4dfc68ab88fab19cbb2a70912c57d394c5f48
Showing 20
changed files with
61 additions
and 62 deletions
locomotive/steam/middlewares/auth.rb b/lib/locomotive/steam/middlewares/auth.rb
+1
-1
| @@ | @@ -13,7 +13,7 @@ module Locomotive::Steam |
| # | |
| class Auth < ThreadSafe | |
| - | include Helpers |
| + | include Concerns::Helpers |
| def _call | |
| load_authenticated_entry | |
locomotive/steam/middlewares/concerns/helpers.rb b/lib/locomotive/steam/middlewares/concerns/helpers.rb
+40
-40
| @@ | @@ -1,59 +1,59 @@ |
| module Locomotive::Steam | |
| module Middlewares | |
| + | module Concerns |
| + | module Helpers |
| - | module Helpers |
| + | def html? |
| + | ['text/html', 'application/x-www-form-urlencoded', 'multipart/form-data'].include?(self.request.media_type) && |
| + | !self.request.xhr? && |
| + | !self.json? |
| + | end |
| - | def html? |
| - | ['text/html', 'application/x-www-form-urlencoded', 'multipart/form-data'].include?(self.request.media_type) && |
| - | !self.request.xhr? && |
| - | !self.json? |
| - | end |
| + | def json? |
| + | self.request.content_type == 'application/json' || File.extname(self.request.path) == '.json' |
| + | end |
| - | def json? |
| - | self.request.content_type == 'application/json' || File.extname(self.request.path) == '.json' |
| - | end |
| + | def render_response(content, code = 200, type = nil) |
| + | @next_response = [code, { 'Content-Type' => type || 'text/html' }, [content]] |
| + | end |
| - | def render_response(content, code = 200, type = nil) |
| - | @next_response = [code, { 'Content-Type' => type || 'text/html' }, [content]] |
| - | end |
| + | def redirect_to(location, type = 301) |
| + | _location = mounted_on && !location.starts_with?(mounted_on) && (location =~ Locomotive::Steam::IsHTTP).nil? ? "#{mounted_on}#{location}" : location |
| - | def redirect_to(location, type = 301) |
| - | _location = mounted_on && !location.starts_with?(mounted_on) && (location =~ Locomotive::Steam::IsHTTP).nil? ? "#{mounted_on}#{location}" : location |
| + | self.log "Redirected to #{_location}".blue |
| - | self.log "Redirected to #{_location}".blue |
| + | @next_response = [type, { 'Content-Type' => 'text/html', 'Location' => _location }, []] |
| + | end |
| - | @next_response = [type, { 'Content-Type' => 'text/html', 'Location' => _location }, []] |
| - | end |
| + | def modify_path(path = nil, &block) |
| + | path ||= env['steam.path'] |
| - | def modify_path(path = nil, &block) |
| - | path ||= env['steam.path'] |
| + | segments = path.split('/') |
| + | yield(segments) |
| + | path = segments.join('/') |
| - | segments = path.split('/') |
| - | yield(segments) |
| - | path = segments.join('/') |
| + | path = '/' if path.blank? |
| + | path += "?#{request.query_string}" unless request.query_string.empty? |
| + | path |
| + | end |
| - | path = '/' if path.blank? |
| - | path += "?#{request.query_string}" unless request.query_string.empty? |
| - | path |
| - | end |
| + | # make sure the location passed in parameter doesn't |
| + | # include the "mounted_on" parameter. |
| + | # If so, returns the location without the "mounted_on" string. |
| + | def make_local_path(location) |
| + | return location if mounted_on.blank? |
| + | location.gsub(Regexp.new('^' + mounted_on), '') |
| + | end |
| - | # make sure the location passed in parameter doesn't |
| - | # include the "mounted_on" parameter. |
| - | # If so, returns the location without the "mounted_on" string. |
| - | def make_local_path(location) |
| - | return location if mounted_on.blank? |
| - | location.gsub(Regexp.new('^' + mounted_on), '') |
| - | end |
| + | def mounted_on |
| + | request.env['steam.mounted_on'] |
| + | end |
| - | def mounted_on |
| - | request.env['steam.mounted_on'] |
| - | end |
| + | def log(msg, offset = 2) |
| + | Locomotive::Common::Logger.info (' ' * offset) + msg |
| + | end |
| - | def log(msg, offset = 2) |
| - | Locomotive::Common::Logger.info (' ' * offset) + msg |
| end | |
| - | |
| end | |
| - | |
| end | |
| end | |
locomotive/steam/middlewares/concerns/liquid_context.rb b/lib/locomotive/steam/middlewares/concerns/liquid_context.rb
+1
-1
| @@ | @@ -1,6 +1,6 @@ |
| module Locomotive::Steam | |
| module Middlewares | |
| - | module LiquidContext |
| + | module Concerns::LiquidContext |
| def liquid_context | |
| ::Liquid::Context.new(liquid_assigns, {}, liquid_registers, true) | |
locomotive/steam/middlewares/entry_submission.rb b/lib/locomotive/steam/middlewares/entry_submission.rb
+1
-1
| @@ | @@ -5,7 +5,7 @@ module Locomotive::Steam |
| # | |
| class EntrySubmission < ThreadSafe | |
| - | include Helpers |
| + | include Concerns::Helpers |
| HTTP_REGEXP = /^https?:\/\//o | |
| ENTRY_SUBMISSION_REGEXP = /^\/entry_submissions\/(\w+)/o | |
locomotive/steam/middlewares/favicon.rb b/lib/locomotive/steam/middlewares/favicon.rb
+1
-1
| @@ | @@ -5,7 +5,7 @@ module Locomotive::Steam |
| attr_accessor_initialize :app | |
| - | include Helpers |
| + | include Concerns::Helpers |
| def call(env) | |
| if env['PATH_INFO'] == '/favicon.ico' | |
locomotive/steam/middlewares/locale.rb b/lib/locomotive/steam/middlewares/locale.rb
+1
-1
| @@ | @@ -13,7 +13,7 @@ module Locomotive::Steam |
| # | |
| class Locale < ThreadSafe | |
| - | include Helpers |
| + | include Concerns::Helpers |
| def _call | |
| locale = extract_locale | |
locomotive/steam/middlewares/locale_redirection.rb b/lib/locomotive/steam/middlewares/locale_redirection.rb
+1
-1
| @@ | @@ -8,7 +8,7 @@ module Locomotive::Steam |
| # | |
| class LocaleRedirection < ThreadSafe | |
| - | include Helpers |
| + | include Concerns::Helpers |
| def _call | |
| if url = redirect_url | |
locomotive/steam/middlewares/logging.rb b/lib/locomotive/steam/middlewares/logging.rb
+1
-1
| @@ | @@ -5,7 +5,7 @@ module Locomotive::Steam |
| # | |
| class Logging | |
| - | include Helpers |
| + | include Concerns::Helpers |
| attr_accessor_initialize :app | |
locomotive/steam/middlewares/page.rb b/lib/locomotive/steam/middlewares/page.rb
+1
-1
| @@ | @@ -6,7 +6,7 @@ module Locomotive::Steam |
| # | |
| class Page < ThreadSafe | |
| - | include Helpers |
| + | include Concerns::Helpers |
| def _call | |
| return env['steam.page'] if env['steam.page'] | |
locomotive/steam/middlewares/private_access.rb b/lib/locomotive/steam/middlewares/private_access.rb
+1
-1
| @@ | @@ -8,7 +8,7 @@ module Locomotive::Steam |
| # | |
| class PrivateAccess < ThreadSafe | |
| - | include Helpers |
| + | include Concerns::Helpers |
| def _call | |
| return if env['steam.private_access_disabled'] | |
locomotive/steam/middlewares/redirection.rb b/lib/locomotive/steam/middlewares/redirection.rb
+1
-1
| @@ | @@ -8,7 +8,7 @@ module Locomotive::Steam |
| # | |
| class Redirection < ThreadSafe | |
| - | include Helpers |
| + | include Concerns::Helpers |
| def _call | |
| begin | |
locomotive/steam/middlewares/renderer.rb b/lib/locomotive/steam/middlewares/renderer.rb
+2
-2
| @@ | @@ -3,8 +3,8 @@ module Locomotive::Steam |
| class Renderer < ThreadSafe | |
| - | include Helpers |
| - | include LiquidContext |
| + | include Concerns::Helpers |
| + | include Concerns::LiquidContext |
| def _call | |
| if page | |
locomotive/steam/middlewares/robots.rb b/lib/locomotive/steam/middlewares/robots.rb
+1
-1
| @@ | @@ -3,7 +3,7 @@ module Locomotive::Steam |
| class Robots | |
| - | include Helpers |
| + | include Concerns::Helpers |
| attr_accessor_initialize :app | |
locomotive/steam/middlewares/section.rb b/lib/locomotive/steam/middlewares/section.rb
+2
-2
| @@ | @@ -2,8 +2,8 @@ module Locomotive::Steam |
| module Middlewares | |
| class Section < ThreadSafe | |
| - | include Helpers |
| - | include LiquidContext |
| + | include Concerns::Helpers |
| + | include Concerns::LiquidContext |
| def _call | |
| if section_id = get_section_id(env['PATH_INFO']) | |
locomotive/steam/middlewares/site.rb b/lib/locomotive/steam/middlewares/site.rb
+1
-1
| @@ | @@ -6,7 +6,7 @@ module Locomotive::Steam |
| # | |
| class Site < ThreadSafe | |
| - | include Helpers |
| + | include Concerns::Helpers |
| def _call | |
| site = find_site | |
locomotive/steam/middlewares/sitemap.rb b/lib/locomotive/steam/middlewares/sitemap.rb
+1
-1
| @@ | @@ -3,7 +3,7 @@ module Locomotive::Steam |
| class Sitemap < ThreadSafe | |
| - | include Helpers |
| + | include Concerns::Helpers |
| def _call | |
| if env['PATH_INFO'] == '/sitemap.xml' && (page.nil? || page.not_found?) | |
locomotive/steam/middlewares/templatized_page.rb b/lib/locomotive/steam/middlewares/templatized_page.rb
+1
-1
| @@ | @@ -3,7 +3,7 @@ module Locomotive::Steam |
| class TemplatizedPage < ThreadSafe | |
| - | include Helpers |
| + | include Concerns::Helpers |
| def _call | |
| if page && page.templatized? | |
locomotive/steam/middlewares/timezone.rb b/lib/locomotive/steam/middlewares/timezone.rb
+1
-1
| @@ | @@ -5,7 +5,7 @@ module Locomotive::Steam |
| # | |
| class Timezone < ThreadSafe | |
| - | include Helpers |
| + | include Concerns::Helpers |
| def _call | |
| timezone = site.try(:timezone) | |
locomotive/steam/middlewares/url_redirection.rb b/lib/locomotive/steam/middlewares/url_redirection.rb
+1
-1
| @@ | @@ -9,7 +9,7 @@ module Locomotive::Steam |
| # | |
| class UrlRedirection < ThreadSafe | |
| - | include Helpers |
| + | include Concerns::Helpers |
| def _call | |
| if url = redirect_url | |
locomotive/steam/server.rb b/lib/locomotive/steam/server.rb
+1
-2
| @@ | @@ -67,8 +67,7 @@ module Locomotive::Steam |
| Middlewares::Path, | |
| Middlewares::Page, | |
| Middlewares::Sitemap, | |
| - | Middlewares::TemplatizedPage, |
| - | Middlewares::Section |
| + | Middlewares::TemplatizedPage |
| ] | |
| end | |