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(path, tags, data) ⇒ String
Get HTML from cache or render new.
-
#render_block(path, tags) ⇒ String
Render block from cache, return html DEPRECATED.
-
#render_cached_content(cache_key, path, data = {}) ⇒ String
Rendering cached content.
-
#render_component(path, tags = [], query = {}) ⇒ String
Render block from cache, return html.
-
#render_page(path, tags, data) ⇒ String
Render page from cache, return html.
Instance Method Details
#csrf_tag ⇒ String
CSRF-tag
108 109 110 |
# File 'lib/marfa/helpers/controller.rb', line 108 def csrf_tag Rack::Csrf.csrf_tag(env) end |
#csrf_token ⇒ String
Generate CSRF token
102 103 104 |
# File 'lib/marfa/helpers/controller.rb', line 102 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
39 40 41 42 |
# File 'lib/marfa/helpers/controller.rb', line 39 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) end |
#get_html(path, tags, data) ⇒ String
Get HTML from cache or render new
119 120 121 122 123 |
# File 'lib/marfa/helpers/controller.rb', line 119 def get_html(path, , data) html = get_cached_content('page', path, ) html = render_page(path, , data) if html.nil? html end |
#render_block(path, tags) ⇒ String
Render block from cache, return html DEPRECATED
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/marfa/helpers/controller.rb', line 79 def render_block(path, ) # TODO: Improve caching with parameters content = get_cached_content('block', path, ) return content unless content.nil? classname = path.to_class_name + 'Block' return unless Object.const_defined?(classname) attrs = { user_data: @user_data || {}, query: params.to_h } block = Object.const_get(classname).new data = block.get_data(attrs) cache_key = Marfa.cache.create_key('block', path, ) full_path = 'blocks/' + path render_cached_content(cache_key, full_path, data) end |
#render_cached_content(cache_key, path, data = {}) ⇒ String
Rendering cached content
11 12 13 14 15 16 |
# File 'lib/marfa/helpers/controller.rb', line 11 def render_cached_content(cache_key, path, data = {}) return Marfa.cache.get(cache_key) if Marfa.cache.exist?(cache_key) output = haml :"#{path}", locals: data Marfa.cache.set(cache_key, output) output end |
#render_component(path, tags = [], query = {}) ⇒ String
Render block from cache, return html
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/marfa/helpers/controller.rb', line 50 def render_component(path, = [], query = {}) # TODO: Improve caching with parameters content = get_cached_content('block', path, ) return content unless content.nil? classname = 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) cache_key = Marfa.cache.create_key('block', path, ) full_path = 'components/' + path render_cached_content(cache_key, full_path, data) end |
#render_page(path, tags, data) ⇒ String
Render page from cache, return html
25 26 27 28 29 |
# File 'lib/marfa/helpers/controller.rb', line 25 def render_page(path, , data) cache_key = Marfa.cache.create_key('page', path, ) full_path = 'pages/' + path render_cached_content(cache_key, full_path, data) end |