Module: Card::Format::Nest

Includes:
Fetch, Main, Subformat, View
Included in:
Card::Format
Defined in:
lib/card/format/nest.rb,
lib/card/format/nest/main.rb,
lib/card/format/nest/view.rb,
lib/card/format/nest/fetch.rb,
lib/card/format/nest/subformat.rb

Defined Under Namespace

Modules: ClassMethods, Fetch, Main, Subformat, View

Constant Summary

Constants included from View

View::NEST_MODES

Instance Method Summary collapse

Methods included from View

#nest_render, #with_nest_mode

Methods included from Subformat

#field_subformat, #subformat

Methods included from Main

#wrap_main

Methods included from Fetch

#fetch_nested_card

Instance Method Details

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

Main difference compared to #nest is that you can use codename symbols to get nested fields

Examples:

home = Card['home'].format
home.nest :self         # => nest for '*self'
home.field_nest :self   # => nest for 'Home+*self'


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

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

#get_nest_defaults(_nested_card) ⇒ Object



70
71
72
# File 'lib/card/format/nest.rb', line 70

def get_nest_defaults _nested_card
  { view: :name }
end

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



9
10
11
12
13
14
# File 'lib/card/format/nest.rb', line 9

def nest name_or_card_or_opts, opts={}
  nested_card = fetch_nested_card name_or_card_or_opts, opts
  opts = name_or_card_or_opts if name_or_card_or_opts.is_a? Hash
  opts[:inc_name] ||= nested_card.name
  nest_card nested_card, opts
end

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



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/card/format/nest.rb', line 49

def nest_card 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)

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

#nest_defaults(nested_card) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/card/format/nest.rb', line 62

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

#process_nest(opts) ⇒ Object Also known as: prepare_nest



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

def process_nest opts
  opts ||= {}

  if opts.key?(:comment)
    # commented nest
    opts[:comment]
  elsif content_out_of_view?
    ""
  elsif main_nest_within_layout? opts
    main_nest opts
  else
    # standard nest
    count_chars { nest opts }
  end
end