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
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(cache_key) ⇒ String, Nil
Render page from cache, store to cache, return html
49 50 51 52 |
# File 'lib/marfa/helpers/controller.rb', line 49 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
57 58 59 60 61 62 63 |
# File 'lib/marfa/helpers/controller.rb', line 57 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
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/marfa/helpers/controller.rb', line 70 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
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/marfa/helpers/controller.rb', line 130 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
24 25 26 27 28 29 |
# File 'lib/marfa/helpers/controller.rb', line 24 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 15 |
# File 'lib/marfa/helpers/controller.rb', line 12 def render_content(path, data) template_engine = Marfa.config.template_engine || :haml render(template_engine, :"#{path}", locals: data) end |
#render_page(options) ⇒ String
Render page from cache, return html
36 37 38 39 40 41 |
# File 'lib/marfa/helpers/controller.rb', line 36 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
119 120 121 122 123 |
# File 'lib/marfa/helpers/controller.rb', line 119 def render_pagination(data, template = nil) template ||= Marfa.config.pagination_template template_engine = Marfa.config.template_engine || :haml render(template_engine, :"#{template}", locals: data) end |
#render_static_block(options) ⇒ String Also known as: render_static_component
Render block from cache, return html without class eval
97 98 99 100 101 |
# File 'lib/marfa/helpers/controller.rb', line 97 def render_static_block() return get_cached_content([:cache_key]) unless [:cache_block].blank? full_path = "#{Marfa.config.block_templates_path}/#{[:path]}" render_cached_content([:cache_key], full_path, [:data]) end |