Module: Wallaby::LinksHelper

Included in:
BaseHelper
Defined in:
lib/helpers/wallaby/links_helper.rb

Overview

Links helper

Instance Method Summary collapse

Instance Method Details

Return link to cancel action

Parameters:

Yields:

  • block to return the link label

Returns:

  • (String)

    anchor link of cancel action



141
142
143
144
# File 'lib/helpers/wallaby/links_helper.rb', line 141

def cancel_link(html_options: {}, &block)
  block ||= -> { wt 'links.cancel' }
  link_to 'javascript:history.back()', html_options, &block
end

Return link to delete action for a given model class.

Parameters:

Options Hash (options:):

  • :url (String)

    url/path for the link

  • :is_resource (Boolean)

    to tell Urlable#edit_path if it is a resource

Yields:

  • block to return the link label

Returns:

  • (String, nil)

    anchor element

  • (nil)

    if user’s not authorized



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/helpers/wallaby/links_helper.rb', line 121

def delete_link(resource, options: {}, url_params: {}, html_options: {}, &block)
  return if unauthorized?(:destroy, extract(resource)) || resource.try(:readonly?)

  html_options, block = LinkOptionsNormalizer.normalize(
    html_options, block, class: 'resource__destroy', block: -> { wt 'links.delete' }
  )

  html_options[:method] ||= :delete
  (html_options[:data] ||= {})[:confirm] ||= wt 'links.confirm.delete'

  url = options[:url] || show_path(resource.itself, url_params: url_params)
  link_to url, html_options, &block
end

Return link to edit page for a given model class.

Parameters:

Options Hash (options:):

  • :url (String)

    url/path for the link

  • :readonly (Boolean)

    readonly and therefore output the label

  • :is_resource (Boolean)

    to tell Urlable#edit_path if it is a resource

Yields:

  • block to return the link label

Returns:

  • (String)

    anchor element / text

  • (nil)

    if user’s not authorized



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/helpers/wallaby/links_helper.rb', line 95

def edit_link(resource, options: {}, url_params: {}, html_options: {}, &block)
  html_options, block = LinkOptionsNormalizer.normalize(
    html_options, block,
    class: 'resource__update',
    block: -> { "#{wt 'links.edit'} #{decorate(resource).to_label}" }
  )

  default = (options[:readonly] && block.call) || nil
  return default if unauthorized?(:edit, extract(resource)) || resource.try(:readonly?)

  url = options[:url] || edit_path(resource.itself, url_params: url_params)
  link_to url, html_options, &block
end

Return link to index page for a given model class

Parameters:

  • model_class (Class)
  • options (Hash) (defaults to: {})
  • url_params (Hash, ActionController::Parameters) (defaults to: {})
  • html_options (Hash) (defaults to: {})

Options Hash (options:):

  • :url (String)

    url/path for the link

Yields:

  • block to return the link label

Returns:

  • (String)

    anchor link to index page for a given model class

  • (nil)

    if user’s not authorized



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/helpers/wallaby/links_helper.rb', line 17

def index_link(model_class, options: {}, url_params: {}, html_options: {}, &block)
  return if unauthorized?(:index, model_class)

  html_options, block = LinkOptionsNormalizer.normalize(
    html_options, block,
    block: -> { to_model_label model_class }
  )

  url = options[:url] || index_path(model_class, url_params: url_params)
  link_to url, html_options, &block
end

Return link to create page for a given model class

Parameters:

  • model_class (Class)
  • options (Hash) (defaults to: {})
  • url_params (Hash, ActionController::Parameters) (defaults to: {})
  • html_options (Hash) (defaults to: {})

Options Hash (options:):

  • :url (String)

    url/path for the link

Yields:

  • block to return the link label

Returns:

  • (String, nil)

    anchor element

  • (nil)

    if user’s not authorized



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/helpers/wallaby/links_helper.rb', line 40

def new_link(model_class, options: {}, url_params: {}, html_options: {}, &block)
  return if unauthorized?(:new, model_class) || decorator_of(model_class).readonly?

  html_options, block = LinkOptionsNormalizer.normalize(
    html_options, block,
    class: 'resource__create',
    block: -> { wt 'links.new', model: to_model_label(model_class) }
  )

  url = options[:url] || new_path(model_class, url_params: url_params)
  link_to url, html_options, &block
end

Return link to show page for a given model class.

Parameters:

Options Hash (options:):

  • :url (String)

    url/path for the link

  • :readonly (Boolean)

    readonly and therefore output the label

  • :is_resource (Boolean)

    to tell Urlable#show_path if it is a resource

Yields:

  • block to return the link label

Returns:

  • (String)

    anchor element / text

  • (nil)

    if user’s not authorized



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/helpers/wallaby/links_helper.rb', line 66

def show_link(resource, options: {}, url_params: {}, html_options: {}, &block)
  # NOTE: to_s is a must
  # if a block is returning integer (e.g. `{ 1 }`)
  # `link_to` will render blank text note inside hyper link
  html_options, block = LinkOptionsNormalizer.normalize(
    html_options, block,
    block: -> { decorate(resource).to_label.to_s }
  )

  default = (options[:readonly] && block.call) || nil
  return default if unauthorized?(:show, extract(resource))

  url = options[:url] || show_path(resource.itself, url_params: url_params)
  link_to url, html_options, &block
end