Module: Card::Set::All::Links::Format

Extended by:
Card::Set::AbstractFormat
Defined in:
card/tmpsets/set/mod008-card-mod-format/all/links.rb,
card/tmpsets/set/mod007-card-mod-format/all/links.rb

Overview

The #link_to methods support smart formatting of links in multiple formats.

Instance Method Summary collapse

Instance Method Details

Creates a “link”, the meaning of which depends upon the format. In this base format, the link looks like [text][absolute path]

Parameters:

  • text (String) (defaults to: nil)

    optional string associated with link

  • opts (Hash) (defaults to: {})

    optional Hash. In simple formats, :path is usually the only key



17
18
19
20
21
22
23
24
# File 'card/tmpsets/set/mod008-card-mod-format/all/links.rb', line 17

def link_to text=nil, opts={}
  path = path((opts.delete(:path) || {}))
  if text && path != text
    "#{text}[#{path}]"
  else
    path
  end
end

link to a card other than the current card.

Parameters:

  • cardish (Integer, Symbol, String, Card)

    a card identifier

  • text (String) (defaults to: nil)
  • opts (Hash) (defaults to: {})


39
40
41
42
# File 'card/tmpsets/set/mod008-card-mod-format/all/links.rb', line 39

def link_to_card cardish, text=nil, opts={}
  add_to_path opts, mark: Card::Name[cardish]
  link_to text, opts
end

a “resource” is essentially a reference to something that decko doesn’t recognize to be a card. Can be a remote url, a local url (that decko hasn’t parsed) or a local path.

Parameters:

  • resource (String)
  • text (String) (defaults to: nil)
  • opts (Hash) (defaults to: {})


50
51
52
53
# File 'card/tmpsets/set/mod008-card-mod-format/all/links.rb', line 50

def link_to_resource resource, text=nil, opts={}
  resource = clean_resource resource, resource_type(resource)
  link_to text, opts.merge(path: resource)
end

link to a different view of the current card

Parameters:

  • view (Symbol, String)
  • text (String) (defaults to: nil)
  • opts (Hash) (defaults to: {})


30
31
32
33
# File 'card/tmpsets/set/mod008-card-mod-format/all/links.rb', line 30

def link_to_view view, text=nil, opts={}
  add_to_path opts, view: view unless view == :home
  link_to text, opts
end

smart_link_to is wrapper method for #link_to, #link_to_card, #link_to_view, and #link_to_resource. If the opts argument contains :view, :related, :card, or :resource, it will use the respective method to render a link.

This is usually most useful when writing views that generate many different kinds of links.



61
62
63
64
65
66
67
# File 'card/tmpsets/set/mod008-card-mod-format/all/links.rb', line 61

def smart_link_to text, opts={}
  if (linktype = %i[view card resource].find { |key| opts[key] })
    send "link_to_#{linktype}", opts.delete(linktype), text, opts
  else
    send :link_to, text, opts
  end
end