Module: Marfa::Helpers::Controller

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

Instance Method Summary collapse

Instance Method Details

#csrf_tagString

CSRF-tag

Returns:



111
112
113
# File 'lib/marfa/helpers/controller.rb', line 111

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

#csrf_tokenString

Generate CSRF token

Returns:



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

Examples:

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

Parameters:

  • cache_key (String)
    • cache key

Returns:

  • (String)

    data from cache

  • (Nil)


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

Parameters:

  • query (Hash)
    • hash of params

Returns:



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

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

Examples:

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

Parameters:

  • options (Hash)
    • options hash

Returns:



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(options)
  cache_block = options[:cache_block]

  if cache_block
    content = get_cached_content(options[:cache_key])
    return content unless content.nil?
  end

  model_name = options[:model]
  return unless Object.const_defined?(model_name)

  model = Object.const_get(model_name)
  return unless model.respond_to? options[:method].to_sym

  data = model.send(options[:method].to_sym, options[:params])
  data = data.merge(options[:locals]) unless options[:locals].nil?

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

  return render_content(full_path, data) unless cache_block

  render_cached_content(options[:cache_key], full_path, data)
end

#render_block_with_data(options) ⇒ String

Render block with data from cache, return html

Examples:

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

Parameters:

  • options (Hash)
    • options hash

Returns:



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(options)
  cache_block = options[:cache_block]

  # tags += query_to_tags(options[:query])
  if cache_block
    content = get_cached_content(options[:cache_key])
    return content unless content.nil?
  end

  data = options[:data]
  data = data.merge(options[:locals]) unless options[:locals].nil?

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

  return render_content(full_path, data) unless cache_block

  render_cached_content(options[: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



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

Examples:

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

Parameters:

  • path (String)
    • URL

  • data (Hash)
    • options hash

Returns:

  • (String)

    rendered 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

Examples:

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

Parameters:

  • options (Hash)
    • options hash

Returns:

  • (String)

    rendered content



36
37
38
39
40
41
# File 'lib/marfa/helpers/controller.rb', line 36

def render_page(options)
  full_path = 'pages/' + options[:path]
  return render_content(full_path, options[:data]) if options[:cache_page].blank? || options[:cache_key].blank?

  render_cached_content(options[: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:



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

Parameters:

  • options (Hash)
    • params

Returns:



97
98
99
100
101
# File 'lib/marfa/helpers/controller.rb', line 97

def render_static_block(options)
  return get_cached_content(options[:cache_key]) unless options[:cache_block].blank?
  full_path = "#{Marfa.config.block_templates_path}/#{options[:path]}"
  render_cached_content(options[:cache_key], full_path, options[:data])
end