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



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

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

#expand_stubs(cached_content) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/card/format/render.rb', line 154

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_each_chunk do |stub_hash|
    yield(stub_hash)
  end

  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)


144
145
146
147
148
149
150
151
152
# File 'lib/card/format/render.rb', line 144

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_options # trigger viz processing
  visibility = voo.viz_hash[view] || default_viz
  visibility == :show
end

#stub_render(cached_content) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/card/format/render.rb', line 131

def stub_render cached_content
  result = expand_stubs cached_content do |stub_hash|
    prepare_stub_nest(stub_hash) do |stub_card, view_opts|
      nest stub_card, view_opts, stub_hash[:format_opts]
    end
  end
  if result =~ /stub/
    Rails.logger.info "STUB IN RENDERED VIEW: #{card.name}: " \
                      "#{voo.ok_view}\n#{result}"
  end
  result
end

#supports_view?(view) ⇒ Boolean

Returns:

  • (Boolean)


179
180
181
# File 'lib/card/format/render.rb', line 179

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

#view_cache_setting(view) ⇒ Object

setting (:alway, :never, :nested) designated in view definition



116
117
118
119
120
121
122
123
124
# File 'lib/card/format/render.rb', line 116

def view_cache_setting view
  coded_setting = view_setting(:cache, view) || :standard
  # method = self.class.view_cache_setting_method view
  # coded_setting = respond_to?(method) ? send(method) : :standard
  return :never if coded_setting == :never

  # seems unwise to override a hard-coded "never"
  (voo&.cache) || coded_setting
end

#view_method(view) ⇒ Object



171
172
173
174
175
176
177
# File 'lib/card/format/render.rb', line 171

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



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

def view_setting setting_name, view
  method = self.class.view_setting_method_name view, setting_name
  try method
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.push 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