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



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

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



24
25
26
# File 'lib/card/format/render.rb', line 24

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

#current_view(view) ⇒ Object



133
134
135
136
137
138
139
# File 'lib/card/format/render.rb', line 133

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

#expand_stubs(cached_content) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/card/format/render.rb', line 106

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



38
39
40
41
42
# File 'lib/card/format/render.rb', line 38

def final_render view
  method = view_method view
  rendered = final_render_call method
  add_debug_info view, method, rendered
end

#final_render_call(method) ⇒ Object



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

def final_render_call method
  method.call
end

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

Yields:

  • (stub_card, view_opts)


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

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



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

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

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

view=open&layout=simple



7
8
9
10
11
12
13
14
# 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|
      wrap_and_render final_view
    end
  end
end

#show_debug_info?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/card/format/render.rb', line 63

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

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

Returns:

  • (Boolean)


32
33
34
35
36
# File 'lib/card/format/render.rb', line 32

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



87
88
89
90
91
92
93
94
# File 'lib/card/format/render.rb', line 87

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



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

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



81
82
83
84
85
# File 'lib/card/format/render.rb', line 81

def stub_render cached_content
  stub_debugging do
    expand_stubs cached_content
  end
end

#supports_view?(view) ⇒ Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/card/format/render.rb', line 129

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

#view_cache_setting(view) ⇒ Object

:standard, :always, :never



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

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

#view_method(view) ⇒ Object



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

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



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

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

#vooObject



28
29
30
# File 'lib/card/format/render.rb', line 28

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

#with_voo(voo) ⇒ Object



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

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

#wrap_and_render(view) ⇒ Object



44
45
46
47
48
# File 'lib/card/format/render.rb', line 44

def wrap_and_render view
  current_view(view) { with_wrapper { final_render view } }
rescue StandardError => e
  rescue_view e, view
end