Module: Marfa::Helpers::Controller

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

Instance Method Summary collapse

Instance Method Details

#csrf_tagString

CSRF-tag

Returns:



136
137
138
# File 'lib/marfa/helpers/controller.rb', line 136

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

#csrf_tokenString

Generate CSRF token

Returns:



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

Examples:

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

Parameters:

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

  • path (String)
    • URL

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

Returns:

  • (String)

    data from cache

  • (Nil)


56
57
58
59
60
# File 'lib/marfa/helpers/controller.rb', line 56

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)
  nil
end

#get_html(options) ⇒ String

Get HTML from cache or render new

Examples:

get_html({ path: 'index', tags: ['tag1', 'tag2'], data: {} })

Parameters:

  • options (Hash)
    • params

Returns:



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(options)
  cache_time = options[: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, options[:path], options[:tags])
    html = render_page(options) if html.nil?
  else
    html = render_page(options)
  end
  html
end

#query_to_tags(query) ⇒ Array

convert query json to tags

Parameters:

  • query (Hash)
    • hash of params

Returns:

  • (Array)

    of strings key-value or []



65
66
67
68
69
70
71
# File 'lib/marfa/helpers/controller.rb', line 65

def query_to_tags(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

Examples:

render_block({ path: 'index/index', tags: ['tag1', 'tag2'] })

Parameters:

  • options (Hash)
    • options hash

Returns:



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(options)
  # TODO: Improve caching with parameters
  cache_time = options[:cache_time] || Marfa.config.cache[:expiration_time]
  tags = options[:tags] || []

  kind = 'block'
  kind += "-#{@device}" if Marfa.config.cache[:use_device]
  tags += query_to_tags(options[:query])

  if cache_time > 0
    content = get_cached_content(kind, options[:path], tags)
    return content unless content.nil?
  end

  classname = options[:class_name] || (options[:path].to_class_name + 'Block')
  return unless Object.const_defined?(classname)

  attrs = {
    user_data: @user_data || {},
    query: options[:query] || {}
  }

  block = Object.const_get(classname).new
  data = block.get_data(attrs)
  data = data.merge(options[:locals]) unless options[:locals].nil?

  full_path = Marfa.config.block_templates_path + '/' + options[:path]

  return render_content(full_path, data) if cache_time == 0

  cache_key = Marfa.cache.create_key(kind, options[:path], tags)
  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

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



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

Examples:

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

Parameters:

  • path (String)
    • URL

  • data (Hash)
    • options hash

Returns:

  • (String)

    rendered 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

Examples:

render_page({ path: 'index', tags: ['tag1', 'tag2'], data: {} })

Parameters:

  • options (Hash)
    • options hash

Returns:

  • (String)

    rendered content



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/marfa/helpers/controller.rb', line 35

def render_page(options)
  cache_time = options[:cache_time] || Marfa.config.cache[:expiration_time]

  kind = 'page'
  kind += "-#{@device}" if Marfa.config.cache[:use_device]

  full_path = 'pages/' + options[:path]
  return render_content(full_path, options[:data]) if cache_time == 0

  cache_key = Marfa.cache.create_key(kind, options[:path], options[:tags])
  render_cached_content(cache_key, full_path, options[:data])
end

#render_pagination(data, _template = nil) ⇒ String

Render pagination panel

Parameters:

  • data (Hash)
    • pages info data

  • _template (String) (defaults to: nil)
    • template to render

Returns:



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

Examples:

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

Parameters:

  • path (String)
    • URL

  • data (Hash) (defaults to: {})
    • data to render

Returns:



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