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).
-
#query_to_tags(query) ⇒ Array
convert query json to tags.
-
#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
129 130 131 |
# File 'lib/marfa/helpers/controller.rb', line 129 def csrf_tag Rack::Csrf.csrf_tag(env) end |
#csrf_token ⇒ String
Generate CSRF token
123 124 125 |
# File 'lib/marfa/helpers/controller.rb', line 123 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
51 52 53 54 55 |
# File 'lib/marfa/helpers/controller.rb', line 51 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)
139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/marfa/helpers/controller.rb', line 139 def get_html() cache_time = [:cache_time] || Marfa.config.cache[:expiration_time] if cache_time > 0 kind = 'page' kind += "-#{@device}" if Marfa.config.cache[:use_device] html = get_cached_content(kind, [:path], [:tags]) html = render_page() if html.nil? else html = render_page() end html end |
#query_to_tags(query) ⇒ Array
convert query json to tags
60 61 62 63 64 65 66 |
# File 'lib/marfa/helpers/controller.rb', line 60 def (query) result = [] if query.is_a? Hash query.each { |key, value| result << "#{key}-#{value}" } end result end |
#render_block(options) ⇒ String Also known as: render_component
Render block from cache, return html
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/marfa/helpers/controller.rb', line 73 def render_block() # TODO: Improve caching with parameters cache_time = [:cache_time] || Marfa.config.cache[:expiration_time] = [:tags] || [] kind = 'block' kind += "-#{@device}" if Marfa.config.cache[:use_device] += ([:query]) if cache_time > 0 content = get_cached_content(kind, [: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(kind, [: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 39 40 41 |
# File 'lib/marfa/helpers/controller.rb', line 30 def render_page() cache_time = [:cache_time] || Marfa.config.cache[:expiration_time] kind = 'page' kind += "-#{@device}" if Marfa.config.cache[:use_device] full_path = 'pages/' + [:path] return render_content(full_path, [:data]) if cache_time == 0 cache_key = Marfa.cache.create_key(kind, [: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
111 112 113 114 115 116 117 118 119 |
# File 'lib/marfa/helpers/controller.rb', line 111 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 |