Module: Marfa::Helpers::Controller
- Defined in:
- lib/marfa/helpers/controller.rb
Instance Method Summary collapse
-
#csrf_tag ⇒ String
CSRF-tag.
-
#csrf_token ⇒ String
Generate CSRF token.
-
#get_cached_content(kind, path, tags = []) ⇒ String, Nil
Render page from cache, store to cache, return html.
-
#get_html(options) ⇒ String
Get HTML from cache or render new def get_html(path, tags, data, cache_time = Marfa.config.cache).
-
#render_block(options) ⇒ String
(also: #render_component)
Render block from cache, return html.
-
#render_cached_content(cache_key, path, data = {}, cache_time = Marfa.config.cache[:expiration_time]) ⇒ String
Rendering cached content.
- #render_content(path, data) ⇒ Object
-
#render_page(options) ⇒ String
Render page from cache, return html.
-
#render_static_block(path, data = {}) ⇒ String
(also: #render_static_component)
Render block from cache, return html without class eval.
Instance Method Details
#csrf_tag ⇒ String
CSRF-tag
111 112 113 |
# File 'lib/marfa/helpers/controller.rb', line 111 def csrf_tag Rack::Csrf.csrf_tag(env) end |
#csrf_token ⇒ String
Generate CSRF token
105 106 107 |
# File 'lib/marfa/helpers/controller.rb', line 105 def csrf_token Rack::Csrf.csrf_token(env) end |
#get_cached_content(kind, path, tags = []) ⇒ String, Nil
Render page from cache, store to cache, return html
48 49 50 51 52 |
# File 'lib/marfa/helpers/controller.rb', line 48 def get_cached_content(kind, path, = []) cache_key = Marfa.cache.create_key(kind, path, ) return Marfa.cache.get(cache_key) if Marfa.cache.exist?(cache_key) nil end |
#get_html(options) ⇒ String
Get HTML from cache or render new def get_html(path, tags, data, cache_time = Marfa.config.cache)
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/marfa/helpers/controller.rb', line 121 def get_html() cache_time = [:cache_time] || Marfa.config.cache[:expiration_time] if cache_time > 0 html = get_cached_content('page', [:path], [:tags]) html = render_page() if html.nil? else html = render_page() end html end |
#render_block(options) ⇒ String Also known as: render_component
Render block from cache, return html
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/marfa/helpers/controller.rb', line 59 def render_block() # TODO: Improve caching with parameters cache_time = [:cache_time] || Marfa.config.cache[:expiration_time] = [:tags] || [] if cache_time > 0 content = get_cached_content('block', [:path], ) return content unless content.nil? end classname = [:class_name] || ([:path].to_class_name + 'Block') return unless Object.const_defined?(classname) attrs = { user_data: @user_data || {}, query: [:query] || {} } block = Object.const_get(classname).new data = block.get_data(attrs) full_path = Marfa.config.block_templates_path + '/' + [:path] return render_content(full_path, data) if cache_time == 0 cache_key = Marfa.cache.create_key('block', [:path], ) render_cached_content(cache_key, full_path, data) end |
#render_cached_content(cache_key, path, data = {}, cache_time = Marfa.config.cache[:expiration_time]) ⇒ String
Rendering cached content
18 19 20 21 22 23 |
# File 'lib/marfa/helpers/controller.rb', line 18 def render_cached_content(cache_key, path, data = {}, cache_time = Marfa.config.cache[:expiration_time]) return Marfa.cache.get(cache_key) if Marfa.cache.exist?(cache_key) output = render_content(path, data) Marfa.cache.set(cache_key, output, cache_time) output end |
#render_content(path, data) ⇒ Object
7 8 9 |
# File 'lib/marfa/helpers/controller.rb', line 7 def render_content(path, data) haml :"#{path}", locals: data end |
#render_page(options) ⇒ String
Render page from cache, return html
30 31 32 33 34 35 36 37 38 |
# File 'lib/marfa/helpers/controller.rb', line 30 def render_page() cache_time = [:cache_time] || Marfa.config.cache[:expiration_time] full_path = 'pages/' + [:path] return render_content(full_path, [:data]) if cache_time == 0 cache_key = Marfa.cache.create_key('page', [:path], [:tags]) render_cached_content(cache_key, full_path, [:data]) end |
#render_static_block(path, data = {}) ⇒ String Also known as: render_static_component
Render block from cache, return html without class eval
93 94 95 96 97 98 99 100 101 |
# File 'lib/marfa/helpers/controller.rb', line 93 def render_static_block(path, data = {}) content = get_cached_content('block', path) return content unless content.nil? cache_key = Marfa.cache.create_key('block', path) full_path = Marfa.config.block_templates_path + '/' + path render_cached_content(cache_key, full_path, data) end |