Class: Gluttonberg::Middleware::Locales

Inherits:
Object
  • Object
show all
Defined in:
lib/gluttonberg/middleware/locales.rb

Overview

This middleware is used to extract locale info from current path. This helps gluttonberg pages and other controllers to access locale information easily.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Locales

Returns a new instance of Locales.



6
7
8
# File 'lib/gluttonberg/middleware/locales.rb', line 6

def initialize(app)
  @app = app
end

Class Method Details

.bypass_path?(path, env) ⇒ Boolean

Checks if we need to skip current path from analysing it for locales or gluttonberg pages

Parameters:

  • path (String)
  • env (Hash)

    Its not used anymore. CleaningRequired env param is not used anymore. Please remove it but make sure its also removed from the place where its called.

Returns:

  • (Boolean)

    fales if we cannot bypass current path otherwise true.



36
37
38
39
40
41
42
# File 'lib/gluttonberg/middleware/locales.rb', line 36

def self.bypass_path?(path, env)
  if path.blank?
    false
  else
    path =~ /^\/admin/ || path.start_with?("/stylesheets")  || path.start_with?("/javascripts")   || path.start_with?("/images") ||  path.start_with?("/gluttonberg")  || path.start_with?("/assets")  || path.start_with?("/user_asset")
  end
end

Instance Method Details

#call(env) ⇒ Object

Reads current path if it contains any valid Gluttonberg::Locale slug then it removes from path and add it to env in addition to that it addes locale object to env

Parameters:

  • env (Hash)

    looking for PATH_INFO



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/gluttonberg/middleware/locales.rb', line 15

def call(env)
  env['PATH_INFO'] = '' if env['PATH_INFO'].nil?
  path =  env['PATH_INFO']
  unless Gluttonberg::Middleware::Locales.bypass_path?(path, env)
    case Gluttonberg::Engine.config.identify_locale
      when :subdomain
        # return the sub-domain
      when :prefix
        handle_prefix(path, env)
      when :domain
        env['SERVER_NAME']
    end
  end
  @app.call(env)
end