Class: LB::Project::Site

Inherits:
Object
  • Object
show all
Includes:
R18n::Helpers
Defined in:
lib/lb/project/site.rb

Overview

Site context for templates

Constant Summary collapse

DEFAULT_MESSAGE_TYPES =
{
  notice: { level: :success, timeout: 5 },
  alert:  { level: :error,   timeout: 0 }
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(context = {}) ⇒ Site

Returns a new instance of Site.



14
15
16
# File 'lib/lb/project/site.rb', line 14

def initialize(context = {})
  @context = context
end

Instance Method Details

#[](*args) ⇒ Object



22
23
24
# File 'lib/lb/project/site.rb', line 22

def [](*args)
  options.fetch(*args)
end

#csrf_metatagObject



94
95
96
# File 'lib/lb/project/site.rb', line 94

def csrf_metatag
  self[:csrf_metatag].call
end

#csrf_tagObject



98
99
100
# File 'lib/lb/project/site.rb', line 98

def csrf_tag
  self[:csrf_tag].call
end

#csrf_tokenObject



90
91
92
# File 'lib/lb/project/site.rb', line 90

def csrf_token
  self[:csrf_token].call
end

#css_for(bundle) ⇒ Object



43
44
45
# File 'lib/lb/project/site.rb', line 43

def css_for(bundle)
  path_for(bundle, 'css')
end

#escape_all(data) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/lb/project/site.rb', line 55

def escape_all(data)
  case data
  when Array
    escape_array(data)
  when Hash
    escape_hash(data)
  when String
    Rack::Utils.escape_html(data)
  else
    data
  end
end

#escape_array(data) ⇒ Object



68
69
70
# File 'lib/lb/project/site.rb', line 68

def escape_array(data)
  data.map(&method(:escape_all))
end

#escape_hash(data) ⇒ Object



72
73
74
75
76
# File 'lib/lb/project/site.rb', line 72

def escape_hash(data)
  data.map do |key, value|
    [key, escape_all(value)]
  end.to_h
end

#escaped_json(data) ⇒ Object



51
52
53
# File 'lib/lb/project/site.rb', line 51

def escaped_json(data)
  "JSON#{Rack::Utils.escape_html(JSON.generate(escape_all(data)))}"
end

#flashObject



86
87
88
# File 'lib/lb/project/site.rb', line 86

def flash
  self[:flash, {}]
end

#flash?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/lb/project/site.rb', line 78

def flash?
  i[notice alert].any? { |type| flash[type] }
end

#image_path(path) ⇒ Object



47
48
49
# File 'lib/lb/project/site.rb', line 47

def image_path(path)
  "#{LB::Project.config.image_base_path}/#{path}"
end

#js_for(bundle) ⇒ Object



39
40
41
# File 'lib/lb/project/site.rb', line 39

def js_for(bundle)
  path_for(bundle, 'js')
end

#logged_in?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/lb/project/site.rb', line 106

def logged_in?
  session.key?(:user_id)
end

#message_typesObject



110
111
112
# File 'lib/lb/project/site.rb', line 110

def message_types
  DEFAULT_MESSAGE_TYPES
end

#messagesObject



114
115
116
117
118
# File 'lib/lb/project/site.rb', line 114

def messages
  flash.map do |type, message|
    { message: message }.merge(message_types[type.to_sym])
  end
end

#optionsObject



18
19
20
# File 'lib/lb/project/site.rb', line 18

def options
  @options ||= {}
end

#path_for(bundle, type) ⇒ Object



32
33
34
35
36
37
# File 'lib/lb/project/site.rb', line 32

def path_for(bundle, type)
  path = 'webpack-assets.json'
  file = File.read(path)
  json = JSON.parse(file)
  LB::Project.config.base_path + json[bundle][type]
end

#sessionObject



102
103
104
# File 'lib/lb/project/site.rb', line 102

def session
  self[:session].call
end

#with(opts) ⇒ Object



26
27
28
29
30
# File 'lib/lb/project/site.rb', line 26

def with(opts)
  options.merge!(opts)

  self
end

#with_flash(flash) ⇒ Object



82
83
84
# File 'lib/lb/project/site.rb', line 82

def with_flash(flash)
  with(flash: flash)
end