Module: Pageflow::RenderJsonHelper

Included in:
ConfigHelper, PageTypesHelper
Defined in:
app/helpers/pageflow/render_json_helper.rb

Instance Method Summary collapse

Instance Method Details

#render_html_partial(*args) ⇒ Object


24
25
26
27
28
# File 'app/helpers/pageflow/render_json_helper.rb', line 24

def render_html_partial(*args)
  render_with_format(:html) do
    render(*args)
  end
end

#render_json_partial(*args) ⇒ Object


16
17
18
19
20
21
22
# File 'app/helpers/pageflow/render_json_helper.rb', line 16

def render_json_partial(*args)
  render_with_format(:json) do
    JbuilderTemplate.encode(self) do |json|
      json.partial!(*args)
    end.html_safe
  end
end

#render_json_seed(records, options = {}) ⇒ Object

Renders `image_files/_image_file.json.jbuilder` when given a collection of `ImageFile` records.


5
6
7
8
9
10
11
12
13
14
# File 'app/helpers/pageflow/render_json_helper.rb', line 5

def render_json_seed(records, options = {})
  if records.respond_to?(:to_model)
    name = records.to_model.class.model_name.to_s.split('::').last.underscore.downcase
    render_json_partial(['pageflow/editor', name.pluralize, name.singularize] * '/', name.singularize.to_sym => records)
  else
    return '[]'.html_safe if records.empty?
    name = records.first.to_model.class.model_name.to_s.split('::').last.underscore.downcase
    render_json_partial(['pageflow/editor', name.pluralize, name.singularize] * '/', :collection => records, :as => name.singularize.to_sym)
  end
end

#render_with_format(format, &block) ⇒ Object


30
31
32
33
34
35
36
# File 'app/helpers/pageflow/render_json_helper.rb', line 30

def render_with_format(format, &block)
  old_formats = formats
  self.formats = [format] # hack so partials resolve with json not html format
  yield
ensure
  self.formats = old_formats
end