Class: LB::Project::Site
- Inherits:
-
Object
- Object
- LB::Project::Site
- 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
- #[](*args) ⇒ Object
- #csrf_metatag ⇒ Object
- #csrf_tag ⇒ Object
- #csrf_token ⇒ Object
- #css_for(bundle) ⇒ Object
- #escape_all(data) ⇒ Object
- #escape_array(data) ⇒ Object
- #escape_hash(data) ⇒ Object
- #escaped_json(data) ⇒ Object
- #flash ⇒ Object
- #flash? ⇒ Boolean
- #image_path(path) ⇒ Object
-
#initialize(context = {}) ⇒ Site
constructor
A new instance of Site.
- #js_for(bundle) ⇒ Object
- #logged_in? ⇒ Boolean
- #message_types ⇒ Object
- #messages ⇒ Object
- #options ⇒ Object
- #path_for(bundle, type) ⇒ Object
- #session ⇒ Object
- #with(opts) ⇒ Object
- #with_flash(flash) ⇒ Object
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) .fetch(*args) end |
#csrf_metatag ⇒ Object
94 95 96 |
# File 'lib/lb/project/site.rb', line 94 def self[:csrf_metatag].call end |
#csrf_tag ⇒ Object
98 99 100 |
# File 'lib/lb/project/site.rb', line 98 def csrf_tag self[:csrf_tag].call end |
#csrf_token ⇒ Object
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 |
#flash ⇒ Object
86 87 88 |
# File 'lib/lb/project/site.rb', line 86 def flash self[:flash, {}] end |
#flash? ⇒ 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
106 107 108 |
# File 'lib/lb/project/site.rb', line 106 def logged_in? session.key?(:user_id) end |
#message_types ⇒ Object
110 111 112 |
# File 'lib/lb/project/site.rb', line 110 def DEFAULT_MESSAGE_TYPES end |
#messages ⇒ Object
114 115 116 117 118 |
# File 'lib/lb/project/site.rb', line 114 def flash.map do |type, | { message: }.merge([type.to_sym]) end end |
#options ⇒ Object
18 19 20 |
# File 'lib/lb/project/site.rb', line 18 def ||= {} 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 |
#session ⇒ Object
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) .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 |