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



107
108
109
# File 'lib/card/format/nest.rb', line 107

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

#field_nest(field, opts = {}) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/card/format/nest.rb', line 20

def field_nest field, opts={}
  if field.is_a?(Card)
    nest field, opts
  else
    prepare_nest opts.merge(inc_name: card.cardname.field(field))
  end
end

#field_subformat(field) ⇒ Object



15
16
17
18
# File 'lib/card/format/nest.rb', line 15

def field_subformat field
  field = card.cardname.field(field) unless field.is_a?(Card)
  subformat field
end

#get_nest_content(cardname) ⇒ Object



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

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



139
140
141
# File 'lib/card/format/nest.rb', line 139

def get_nest_defaults _nested_card
  { view: :name }
end

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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/card/format/nest.rb', line 64

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

#nest_defaults(nested_card) ⇒ Object



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

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



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/card/format/nest.rb', line 111

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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/card/format/nest.rb', line 42

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



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/card/format/nest.rb', line 28

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



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

def wrap_main content
  content # no wrapping in base format
end