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(cache_key) ⇒ String, Nil
Render page from cache, store to cache, return html.
-
#query_to_tags(query) ⇒ String
convert query json to tags.
-
#render_block(options) ⇒ String
(also: #render_component)
Render block from cache, return html.
-
#render_block_with_data(options) ⇒ String
Render block with data 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(options) ⇒ String
(also: #render_static_component)
Render block from cache, return html without class eval.
Instance Method Details
#csrf_tag ⇒ String
CSRF-tag
110 111 112 |
# File 'lib/marfa/helpers/controller.rb', line 110 def csrf_tag Rack::Csrf.csrf_tag(env) end |
#csrf_token ⇒ String
Generate CSRF token
104 105 106 |
# File 'lib/marfa/helpers/controller.rb', line 104 def csrf_token Rack::Csrf.csrf_token(env) end |
#get_cached_content(cache_key) ⇒ String, Nil
Render page from cache, store to cache, return html
48 49 50 51 |
# File 'lib/marfa/helpers/controller.rb', line 48 def get_cached_content(cache_key) return Marfa.cache.get(cache_key) if Marfa.cache.exist?(cache_key) nil end |
#query_to_tags(query) ⇒ String
convert query json to tags
56 57 58 59 60 61 62 |
# File 'lib/marfa/helpers/controller.rb', line 56 def (query) result = [] if query.is_a? Hash query.each { |key, value| result << "#{key}-#{value}" } end result.join('_') end |
#render_block(options) ⇒ String Also known as: render_component
Render block from cache, return html
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/marfa/helpers/controller.rb', line 69 def render_block() cache_block = [:cache_block] if cache_block content = get_cached_content([:cache_key]) return content unless content.nil? end model_name = [:model] return unless Object.const_defined?(model_name) model = Object.const_get(model_name) return unless model.respond_to? [:method].to_sym data = model.send([:method].to_sym, [:params]) data = data.merge([:locals]) unless [:locals].nil? full_path = Marfa.config.block_templates_path + '/' + [:path] return render_content(full_path, data) unless cache_block render_cached_content([:cache_key], full_path, data) end |
#render_block_with_data(options) ⇒ String
Render block with data from cache, return html
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/marfa/helpers/controller.rb', line 128 def render_block_with_data() cache_block = [:cache_block] # tags += query_to_tags(options[:query]) if cache_block content = get_cached_content([:cache_key]) return content unless content.nil? end data = [:data] data = data.merge([:locals]) unless [:locals].nil? full_path = Marfa.config.block_templates_path + '/' + [:path] return render_content(full_path, data) unless cache_block 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 |
# File 'lib/marfa/helpers/controller.rb', line 35 def render_page() full_path = 'pages/' + [:path] return render_content(full_path, [:data]) if [:cache_page].blank? || [:cache_key].blank? render_cached_content([:cache_key], full_path, [:data]) end |
#render_pagination(data, template = nil) ⇒ String
Render pagination panel
118 119 120 121 |
# File 'lib/marfa/helpers/controller.rb', line 118 def render_pagination(data, template = nil) template ||= Marfa.config.pagination_template haml :"#{template}", locals: data end |
#render_static_block(options) ⇒ String Also known as: render_static_component
Render block from cache, return html without class eval
96 97 98 99 100 |
# File 'lib/marfa/helpers/controller.rb', line 96 def render_static_block() return get_cached_content([:cache_key]) unless [:cache_block].blank? full_path = "#{Marfa.config.block_templates_path}/#{options[:path]}" render_cached_content([:cache_key], full_path, [:data]) end |