Module: Card::Format::Render

Included in:
Card::Format
Defined in:
lib/card/format/render.rb

Overview

View rendering methods.

Instance Method Summary collapse

Instance Method Details

#add_debug_info(view, method, rendered) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/card/format/render.rb', line 97

def add_debug_info view, method, rendered
  return rendered unless show_debug_info?

  <<-HTML
    <view-debug view='#{safe_name}##{view}' src='#{pretty_path method.source_location}' module='#{method.owner}'/>
    #{rendered}
  HTML
end

#before_view(view) ⇒ Object



69
70
71
# File 'lib/card/format/render.rb', line 69

def before_view view
  try "_before_#{view}"
end

#current_view(view) ⇒ Object



176
177
178
179
180
181
182
# File 'lib/card/format/render.rb', line 176

def current_view view
  old_view = @current_view
  @current_view = view
  yield
ensure
  @current_view = old_view
end

#expand_stubs(cached_content) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/card/format/render.rb', line 149

def expand_stubs cached_content
  return cached_content unless cached_content.is_a? String

  conto = Card::Content.new cached_content, self, chunk_list: :stub
  conto.process_chunks

  if conto.pieces.size == 1
    # for stubs in json format this converts a single stub back
    # to it's original type (e.g. a hash)
    conto.pieces.first.to_s
  else
    conto.to_s
  end
end

#final_render(view) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/card/format/render.rb', line 83

def final_render view
  current_view(view) do
    with_wrapper do
      method = view_method view
      rendered = final_render_call method
      add_debug_info view, method, rendered
    end
  end
end

#final_render_call(method) ⇒ Object



93
94
95
# File 'lib/card/format/render.rb', line 93

def final_render_call method
  method.call
end

#prepare_stub_nest(stub_hash) {|stub_card, view_opts| ... } ⇒ Object

Yields:

  • (stub_card, view_opts)


139
140
141
142
143
144
145
146
147
# File 'lib/card/format/render.rb', line 139

def prepare_stub_nest stub_hash
  stub_card = Card.fetch_from_cast stub_hash[:cast]
  view_opts = stub_hash[:view_opts]
  voo.normalize_special_options! view_opts
  if stub_card&.key.present? && stub_card.key == card.key
    view_opts[:nest_name] ||= "_self"
  end
  yield stub_card, view_opts
end

#pretty_path(source_location) ⇒ Object



110
111
112
113
# File 'lib/card/format/render.rb', line 110

def pretty_path source_location
  source_location.first.gsub(%r{^.+mod\d+-([^/]+)}, '\1: ') + ":" +
    source_location.second.to_s
end

#raise_wrap_error(wrapper) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/card/format/render.rb', line 61

def raise_wrap_error wrapper
  if wrapper.is_a? String
    raise Card::Error::UserError, "unknown layout card: #{wrapper}"
  else
    raise ArgumentError, "unknown wrapper: #{wrapper}"
  end
end

#render!(view, view_options = {}) ⇒ Object

view=open&layout=simple



7
8
9
10
11
12
13
14
15
16
# File 'lib/card/format/render.rb', line 7

def render! view, view_options={}
  voo = View.new self, view, view_options, @voo
  with_voo voo do
    voo.process do |final_view|
      final_render final_view
    end
  end
rescue StandardError => e
  rescue_view e, view
end

#render_with_card_layout(mark) ⇒ Object



55
56
57
58
59
# File 'lib/card/format/render.rb', line 55

def render_with_card_layout mark
  return unless Card::Layout.card_layout? mark

  Card::Layout::CardLayout.new(mark, self).render
end

#render_with_wrapper(wrapper, opts) ⇒ Object



51
52
53
# File 'lib/card/format/render.rb', line 51

def render_with_wrapper wrapper, opts
  try("wrap_with_#{wrapper}", opts) { @rendered }
end

#show_debug_info?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/card/format/render.rb', line 106

def show_debug_info?
  Env.params[:debug] == "view"
end

#show_view?(view, default_viz = :show) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
# File 'lib/card/format/render.rb', line 77

def show_view? view, default_viz=:show
  voo.process_visibility # trigger viz processing
  visibility = voo.viz_hash[view] || default_viz
  visibility == :show
end

#stub_debuggingObject



130
131
132
133
134
135
136
137
# File 'lib/card/format/render.rb', line 130

def stub_debugging
  result = yield
  if Rails.env.development? && result =~ /stub/
    Rails.logger.debug "STUB IN RENDERED VIEW: #{card.name}: " \
                      "#{voo.ok_view}\n#{result}"
  end
  result
end

#stub_nest(stub_hash) ⇒ Object



184
185
186
187
188
# File 'lib/card/format/render.rb', line 184

def stub_nest stub_hash
  prepare_stub_nest(stub_hash) do |stub_card, view_opts|
    nest stub_card, view_opts, stub_hash[:format_opts]
  end
end

#stub_render(cached_content) ⇒ Object



124
125
126
127
128
# File 'lib/card/format/render.rb', line 124

def stub_render cached_content
  stub_debugging do
    expand_stubs cached_content
  end
end

#supports_view?(view) ⇒ Boolean

Returns:

  • (Boolean)


172
173
174
# File 'lib/card/format/render.rb', line 172

def supports_view? view
  respond_to? Card::Set::Format.view_method_name(view)
end

#view_cache_setting(view) ⇒ Object

:standard, :always, :never



116
117
118
# File 'lib/card/format/render.rb', line 116

def view_cache_setting view
  voo&.cache || view_setting(:cache, view) || :standard
end

#view_method(view) ⇒ Object



164
165
166
167
168
169
170
# File 'lib/card/format/render.rb', line 164

def view_method view
  unless supports_view? view
    raise Card::Error::UserError, unsupported_view_error_message(view)
  end

  method Card::Set::Format.view_method_name(view)
end

#view_setting(setting_name, view) ⇒ Object



120
121
122
# File 'lib/card/format/render.rb', line 120

def view_setting setting_name, view
  try Card::Set::Format.view_setting_method_name(view, setting_name)
end

#vooObject



73
74
75
# File 'lib/card/format/render.rb', line 73

def voo
  @voo ||= View.new self, nil, {}
end

#with_voo(voo) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/card/format/render.rb', line 18

def with_voo voo
  old_voo = @voo
  @voo = voo
  yield
ensure
  @voo = old_voo
end

#with_wrapperObject



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/card/format/render.rb', line 26

def with_wrapper
  if voo.layout.present?
    voo.wrap ||= []
    layout = voo.layout.to_name.key
    # don't wrap twice with modals or overlays
    # this can happen if the view is wrapped with modal
    # and is requested with layout=modal param
    voo.wrap.unshift layout unless voo.wrap.include? layout.to_sym
  end

  @rendered = yield
  wrap_with_wrapper
end

#wrap_with_wrapperObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/card/format/render.rb', line 40

def wrap_with_wrapper
  return @rendered unless voo.wrap.present?

  voo.wrap.reverse.each do |wrapper, opts|
    @rendered = render_with_wrapper(wrapper, opts) ||
                render_with_card_layout(wrapper) ||
                raise_wrap_error(wrapper)
  end
  @rendered
end