Module: Card::Format::Nest

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

Instance Method Summary collapse

Instance Method Details

#fetch_nested_card(options) ⇒ Object



96
97
98
# File 'lib/card/format/nest.rb', line 96

def fetch_nested_card options
  Card.fetch options[:inc_name], new: nest_new_args(options)
end

#get_nest_content(cardname) ⇒ Object



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

def get_nest_content cardname
  content = params[cardname.to_s.tr('+', '_')]

  # CLEANME This is a hack so plus cards re-populate on failed signups
  p = params['subcards']
  if p && (card_params = p[cardname.to_s])
    content = card_params['content']
  end
  content if content.present? # returns nil for empty string
end

#get_nest_defaults(_nested_card) ⇒ Object



128
129
130
# File 'lib/card/format/nest.rb', line 128

def get_nest_defaults _nested_card
  { view: :name }
end

#nest(nested_card, opts = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/card/format/nest.rb', line 51

def nest nested_card, opts={}
  # ActiveSupport::Notifications.instrument('card', message:
  # "nest: #{nested_card.name}, #{opts}") do
  opts.delete_if { |_k, v| v.nil? }
  opts.reverse_merge! nest_defaults(nested_card)

  sub = nil
  if opts[:inc_name] =~ /^_(self)?$/
    sub = self
  else
    sub = subformat nested_card
    sub.nest_opts = opts[:items] ? opts[:items].clone : {}
  end

  view = canonicalize_view opts.delete :view
  opts[:home_view] = [:closed, :edit].member?(view) ? :open : view
  # FIXME: special views should be represented in view definitions

  view =
    case @mode
    when :edit then
      view_in_edit_mode(view, nested_card)
    when :template then
      :template_rule
    when :closed then
      view_in_closed_mode(view, nested_card)
    else
      view
    end

  sub.optional_render view, opts
  # end
end

#nest_defaults(nested_card) ⇒ Object



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

def nest_defaults nested_card
  @nest_defaults ||= begin
    defaults = get_nest_defaults(nested_card).clone
    defaults.merge! @nest_opts if @nest_opts
    defaults
  end
end

#nest_new_args(options) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/card/format/nest.rb', line 100

def nest_new_args options
  args = { name: options[:inc_name], type: options[:type], supercard: card }
  args.delete(:supercard) if options[:inc_name].strip.blank?
  # special case.  gets absolutized incorrectly. fix in smartname?
  if options[:inc_name] =~ /^_main\+/
    # FIXME: this is a rather hacky (and untested) way to get @superleft
    # to work on new cards named _main+whatever
    args[:name] = args[:name].gsub(/^_main\+/, '+')
    args[:supercard] = root.card
  end
  if (content = get_nest_content options[:inc_name])
    args[:content] = content
  end
  args
end

#prepare_nest(opts) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/card/format/nest.rb', line 29

def prepare_nest opts
  @char_count ||= 0
  opts ||= {}

  case
  when opts.key?(:comment)
    # commented nest
    opts[:comment]
  when @mode == :closed && @char_count > Card.config.max_char_count
    # move on; content out of view
    ''
  when opts[:inc_name] == '_main' && show_layout? && @depth == 0
    # the main card within a layout
    expand_main opts
  else
    # standard nest
    result = nest fetch_nested_card(opts), opts
    @char_count += result.length if @mode == :closed && result
    result
  end
end

#subformat(subcard) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/card/format/nest.rb', line 4

def subformat subcard
  subcard = Card.fetch(subcard, new: {}) if subcard.is_a?(String)
  self.class.new subcard,
                 parent: self, depth: @depth + 1, root: @root,
                 # FIXME: - the following four should not be hard-coded
                 # here.  need a generalized mechanism
                 # for attribute inheritance
                 context_names: @context_names, mode: @mode,
                 mainline: @mainline, form: @form
end

#with_nest_mode(mode) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/card/format/nest.rb', line 15

def with_nest_mode mode
  if (switch_mode = INCLUSION_MODES[mode]) && @mode != switch_mode
    old_mode = @mode
    @mode = switch_mode
    @nest_defaults = nil
  end
  result = yield
  if old_mode
    @nest_defaults = nil
    @mode = old_mode
  end
  result
end

#wrap_main(content) ⇒ Object



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

def wrap_main content
  content # no wrapping in base format
end