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.
-
#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) ⇒ String
Render content.
-
#render_page(options) ⇒ String
Render page from cache, return html.
-
#render_pagination(data, _template = nil) ⇒ String
Render pagination panel.
-
#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
136 137 138 |
# File 'lib/marfa/helpers/controller.rb', line 136 def csrf_tag Rack::Csrf.csrf_tag(env) end |
#csrf_token ⇒ String
Generate CSRF token
130 131 132 |
# File 'lib/marfa/helpers/controller.rb', line 130 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
56 57 58 59 60 |
# File 'lib/marfa/helpers/controller.rb', line 56 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
145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/marfa/helpers/controller.rb', line 145 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
65 66 67 68 69 70 71 |
# File 'lib/marfa/helpers/controller.rb', line 65 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
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 104 105 106 107 108 109 110 |
# File 'lib/marfa/helpers/controller.rb', line 78 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) data = data.merge([:locals]) unless [:locals].nil? 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
23 24 25 26 27 28 |
# File 'lib/marfa/helpers/controller.rb', line 23 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) ⇒ String
Render content
12 13 14 |
# File 'lib/marfa/helpers/controller.rb', line 12 def render_content(path, data) haml :"#{path}", locals: data end |
#render_page(options) ⇒ String
Render page from cache, return html
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/marfa/helpers/controller.rb', line 35 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_pagination(data, _template = nil) ⇒ String
Render pagination panel
164 165 166 167 |
# File 'lib/marfa/helpers/controller.rb', line 164 def render_pagination(data, _template=nil) template = _template || Marfa.config.pagination_template haml :"#{template}", locals: data end |
#render_static_block(path, data = {}) ⇒ String Also known as: render_static_component
Render block from cache, return html without class eval
118 119 120 121 122 123 124 125 126 |
# File 'lib/marfa/helpers/controller.rb', line 118 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 |