Module: Card::Format::Render

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

Instance Method Summary collapse

Instance Method Details

#add_class(options, klass) ⇒ Object



131
132
133
# File 'lib/card/format/render.rb', line 131

def add_class options, klass
  options[:class] = [options[:class], klass].flatten.compact * ' '
end

#canonicalize_view(view) ⇒ Object



34
35
36
37
38
# File 'lib/card/format/render.rb', line 34

def canonicalize_view view
  return if view.blank?
  view_key = view.to_viewname.key.to_sym
  DEPRECATED_VIEWS[view_key] || view_key
end

#current_view(view) ⇒ Object



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

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

#default_item_viewObject



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

def default_item_view
  :name
end

#default_render_args(view, a = nil) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/card/format/render.rb', line 87

def default_render_args view, a=nil
  args =
    case a
    when nil then
      {}
    when Hash then
      a.clone
    when Array then
      a[0].merge a[1]
    else
      raise Card::Error, "bad render args: #{a}"
    end

  default_method = "default_#{view}_args"
  send default_method, args if respond_to?(default_method)
  args
end

#error_cardnameObject



123
124
125
# File 'lib/card/format/render.rb', line 123

def error_cardname
  card && card.name.present? ? card.name : 'unknown card'
end

#hidden_view?(view, args) ⇒ Boolean

Returns:

  • (Boolean)


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

def hidden_view? view, args
  args.delete(:optional) && !show_view?(view, args)
end

#id_counterObject



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

def id_counter
  return @parent.id_counter if @parent
  @id_counter ||= 0
  @id_counter += 1
end

#nest_arg_visibility(view, args) ⇒ Object



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

def nest_arg_visibility view, args
  [:show, :hide].each do |setting|
    return setting if parse_view_visibility(args[setting]).member?(view)
  end
  false
end

#page(view, slot_opts) ⇒ Object



4
5
6
7
8
# File 'lib/card/format/render.rb', line 4

def page view, slot_opts
  @card.run_callbacks :show_page do
    show view, slot_opts
  end
end

#parse_view_visibility(val) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/card/format/render.rb', line 74

def parse_view_visibility val
  case val
  when NilClass then
    []
  when Array then
    val
  when String then
    val.split(/[\s,]+/)
  else
    raise Card::Error, "bad show/hide argument: #{val}"
  end.map { |view| canonicalize_view view }
end

#render(view, args = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/card/format/render.rb', line 10

def render view, args={}
  view = canonicalize_view view
  return if hidden_view? view, args
  view = ok_view view, args
  current_view(view) do
    args = default_render_args view, args
    with_nest_mode view do
      Card::ViewCache.fetch(self, view, args) do
        method = view_method view, args
        method.arity == 0 ? method.call : method.call(args)
      end
    end
  end
rescue => e
  rescue_view e, view
end

#rendering_error(_exception, view) ⇒ Object



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

def rendering_error _exception, view
  "Error rendering: #{error_cardname} (#{view} view)"
end

#rescue_view(e, view) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/card/format/render.rb', line 105

def rescue_view e, view
  raise e if Rails.env =~ /^cucumber|test$/
  Rails.logger.info "\nError rendering #{error_cardname} / #{view}: "\
                  "#{e.class} : #{e.message}"
  Card::Error.current = e
  card.notable_exception_raised
  raise e if (debug = Card[:debugger]) && debug.content == 'on'
  rendering_error e, view
end

#show_view?(view, args) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/card/format/render.rb', line 48

def show_view? view, args
  default = args.delete(:default_visibility) || :show # FIXME: - ugly
  api_option = args["optional_#{view}".to_sym]
  case api_option
    # permanent visibility specified in code
  when :always then true
  when  :never then false
  else
    # wagneer can override code settings
    contextual_setting = nest_arg_visibility(view, args) || api_option
    case contextual_setting
    when :show then true
    when :hide then false
    else
      default == :show
    end
  end
end

#unique_idObject



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

def unique_id
  "#{card.key}-#{id_counter}"
end

#view_method(view, args) ⇒ Object



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

def view_method view, args
  method "_view_#{view}"
rescue
  args[:unsupported_view] = view
  method '_view_unsupported_view'
end