Module: Locomotive::Steam::Middlewares::Helpers

Included in:
Auth, EntrySubmission, Favicon, Locale, LocaleRedirection, Logging, Page, PrivateAccess, Redirection, Renderer, Robots, Site, Sitemap, TemplatizedPage, Timezone, UrlRedirection
Defined in:
lib/locomotive/steam/middlewares/helpers.rb

Instance Method Summary collapse

Instance Method Details

#decorate_entry(entry) ⇒ Object



118
119
120
121
# File 'lib/locomotive/steam/middlewares/helpers.rb', line 118

def decorate_entry(entry)
  return nil if entry.nil?
  Locomotive::Steam::Decorators::I18nDecorator.new(entry, locale, default_locale)
end

#default_liquid_contextObject



123
124
125
126
127
128
129
130
131
# File 'lib/locomotive/steam/middlewares/helpers.rb', line 123

def default_liquid_context
  ::Liquid::Context.new({ 'site' => site.to_liquid }, {}, {
    request:        request,
    locale:         locale,
    site:           site,
    services:       services,
    repositories:   services.repositories
  }, true)
end

#default_localeObject



44
45
46
# File 'lib/locomotive/steam/middlewares/helpers.rb', line 44

def default_locale
  site.default_locale
end

#html?Boolean

Useful getters =

Returns:

  • (Boolean)


74
75
76
77
78
# File 'lib/locomotive/steam/middlewares/helpers.rb', line 74

def html?
  ['text/html', 'application/x-www-form-urlencoded', 'multipart/form-data'].include?(self.request.media_type) &&
  !self.request.xhr? &&
  !self.json?
end

#json?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/locomotive/steam/middlewares/helpers.rb', line 80

def json?
  self.request.content_type == 'application/json' || File.extname(self.request.path) == '.json'
end

#liquid_assignsObject



36
37
38
# File 'lib/locomotive/steam/middlewares/helpers.rb', line 36

def liquid_assigns
  @liquid_assigns ||= env.fetch('steam.liquid_assigns')
end

#live_editing?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/locomotive/steam/middlewares/helpers.rb', line 48

def live_editing?
  !!env['steam.live_editing']
end

#localeObject



32
33
34
# File 'lib/locomotive/steam/middlewares/helpers.rb', line 32

def locale
  @locale ||= env.fetch('steam.locale')
end

#localesObject



40
41
42
# File 'lib/locomotive/steam/middlewares/helpers.rb', line 40

def locales
  site.locales
end

#log(msg, offset = 2) ⇒ Object



133
134
135
# File 'lib/locomotive/steam/middlewares/helpers.rb', line 133

def log(msg, offset = 2)
  Locomotive::Common::Logger.info (' ' * offset) + msg
end

#make_local_path(location) ⇒ Object

make sure the location passed in parameter doesn’t include the “mounted_on” parameter. If so, returns the location without the “mounted_on” string.



113
114
115
116
# File 'lib/locomotive/steam/middlewares/helpers.rb', line 113

def make_local_path(location)
  return location if mounted_on.blank?
  location.gsub(Regexp.new('^' + mounted_on), '')
end

#modify_path(path = nil) {|segments| ... } ⇒ Object

Yields:

  • (segments)


98
99
100
101
102
103
104
105
106
107
108
# File 'lib/locomotive/steam/middlewares/helpers.rb', line 98

def modify_path(path = nil, &block)
  path ||= env['steam.path']

  segments = path.split('/')
  yield(segments)
  path = segments.join('/')

  path = '/' if path.blank?
  path += "?#{request.query_string}" unless request.query_string.empty?
  path
end

#mounted_onObject



52
53
54
# File 'lib/locomotive/steam/middlewares/helpers.rb', line 52

def mounted_on
  env['steam.mounted_on']
end

#pageObject



24
25
26
# File 'lib/locomotive/steam/middlewares/helpers.rb', line 24

def page
  @page ||= env.fetch('steam.page')
end

#paramsObject

if this is a JSON request with a JSON body, try to parse it if we are unable to parse it, fallback to the original params



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/locomotive/steam/middlewares/helpers.rb', line 58

def params
  @params = env['steam.params']

  return @params if @params.present?

  if json? && (request.post? || request.put?)
    @params = JSON.parse(request.body.read) rescue nil

    request.body.rewind # leave the body as it was before we touched it
  end

  @params = env['steam.params'] = (@params || request.params).with_indifferent_access
end

#pathObject



28
29
30
# File 'lib/locomotive/steam/middlewares/helpers.rb', line 28

def path
  @path ||= env.fetch('steam.path')
end

#redirect_to(location, type = 301) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/locomotive/steam/middlewares/helpers.rb', line 90

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

  @next_response = [type, { 'Content-Type' => 'text/html', 'Location' => _location }, []]
end

#render_response(content, code = 200, type = nil) ⇒ Object

Helper methods



86
87
88
# File 'lib/locomotive/steam/middlewares/helpers.rb', line 86

def render_response(content, code = 200, type = nil)
  @next_response = [code, { 'Content-Type' => type || 'text/html' }, [content]]
end

#repositoriesObject



12
13
14
# File 'lib/locomotive/steam/middlewares/helpers.rb', line 12

def repositories
  @repositories ||= services.repositories
end

#requestObject



16
17
18
# File 'lib/locomotive/steam/middlewares/helpers.rb', line 16

def request
  @request ||= env.fetch('steam.request')
end

#servicesObject

Shortcuts =



8
9
10
# File 'lib/locomotive/steam/middlewares/helpers.rb', line 8

def services
  @services ||= env.fetch('steam.services')
end

#siteObject



20
21
22
# File 'lib/locomotive/steam/middlewares/helpers.rb', line 20

def site
  @site ||= env.fetch('steam.site')
end