Module: Marfa::Helpers::Controller

Defined in:
lib/marfa/helpers/controller.rb

Instance Method Summary collapse

Instance Method Details

#csrf_tagString

CSRF-tag

Returns:



108
109
110
# File 'lib/marfa/helpers/controller.rb', line 108

def csrf_tag
  Rack::Csrf.csrf_tag(env)
end

#csrf_tokenString

Generate CSRF token

Returns:



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

Examples:

get_cached_content('page', 'index/index', ['tag1', 'tag2'])

Parameters:

  • kind (String)
    • kind (block, page)

  • path (String)
    • URL

  • tags (Array)
    • tag list

Returns:

  • (String)

    data from cache

  • (Nil)


39
40
41
42
# File 'lib/marfa/helpers/controller.rb', line 39

def get_cached_content(kind, path, tags)
  cache_key = Marfa.cache.create_key(kind, path, tags)
  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

Examples:

get_html('index/index', ['tag1', 'tag2'], {})

Parameters:

  • path (String)
    • URL

  • tags (Array)
    • tag list

  • data (Hash)
    • data to render

Returns:



119
120
121
122
123
# File 'lib/marfa/helpers/controller.rb', line 119

def get_html(path, tags, data)
  html = get_cached_content('page', path, tags)
  html = render_page(path, tags, data) if html.nil?
  html
end

#render_block(path, tags) ⇒ String

Render block from cache, return html DEPRECATED

Examples:

render_block('index/index', ['tag1', 'tag2'])

Parameters:

  • path (String)
    • URL

  • tags (Array)
    • tag list

Returns:



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, tags)
  # TODO: Improve caching with parameters
  content = get_cached_content('block', path, tags)
  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, tags)
  full_path = 'blocks/' + path

  render_cached_content(cache_key, full_path, data)
end

#render_cached_content(cache_key, path, data = {}) ⇒ String

Rendering cached content

Examples:

render_cached_content('some_key', 'path/url', {})

Parameters:

  • cache_key (String)

    key

  • path (String)
    • URL

  • data (Hash) (defaults to: {})
    • options hash

Returns:

  • (String)

    rendered 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

Examples:

render_block('index/index', ['tag1', 'tag2'])

Parameters:

  • path (String)
    • URL

  • tags (Array) (defaults to: [])
    • tag list

Returns:



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, tags = [], query = {})
  # TODO: Improve caching with parameters
  content = get_cached_content('block', path, tags)
  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, tags)
  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

Examples:

render_page('index', ['tag1', 'tag2'], {})

Parameters:

  • path (String)
    • URL

  • tags (Array)
    • tag list

  • data (Hash)
    • options hash

Returns:

  • (String)

    rendered content



25
26
27
28
29
# File 'lib/marfa/helpers/controller.rb', line 25

def render_page(path, tags, data)
  cache_key = Marfa.cache.create_key('page', path, tags)
  full_path = 'pages/' + path
  render_cached_content(cache_key, full_path, data)
end