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 an action

Parameters:

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

    (@see ActionView::Helpers::UrlHelper#link_to)

Returns:

  • (String)

    HTML anchor element



110
111
112
113
# File 'lib/helpers/wallaby/links_helper.rb', line 110

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

Return link to delete action by a given model class

If user’s not authorized, nil will be returned

Parameters:

  • resource (Object, Wallaby::ResourceDecorator)

    model class

  • options (Hash)
  • html_options (Hash) (defaults to: {})

    (@see ActionView::Helpers::UrlHelper#link_to)

Returns:

  • (String, nil)

    anchor element



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/helpers/wallaby/links_helper.rb', line 91

def delete_link(resource, html_options: {}, &block)
  return if cannot? :destroy, extract(resource)

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

  html_options[:method] ||= :delete
  html_options[:data] ||= {}
  html_options[:data][:confirm] ||= t 'links.confirm.delete'

  link_to show_path(resource), html_options, &block
end

Return link to edit page by a given model class If user’s not authorized, resource label will be returned

Parameters:

  • resource (Object, Wallaby::ResourceDecorator)

    model class

  • options (Hash) (defaults to: {})
  • html_options (Hash) (defaults to: {})

    (@see ActionView::Helpers::UrlHelper#link_to)

Returns:

  • (String)

    anchor element / text



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/helpers/wallaby/links_helper.rb', line 71

def edit_link(resource, options: {}, html_options: {}, &block)
  default = options[:readonly] && decorate(resource).to_label || nil
  return default if cannot? :edit, extract(resource)

  html_options, block = LinkOptionsNormalizer.normalize(
    html_options, block,
    class: 'resource__update',
    block: -> { "#{t 'links.edit'} #{decorate(resource).to_label}" }
  )

  link_to edit_path(resource), html_options, &block
end

#edit_path(resource) ⇒ String

Url for edit form page of given resource

Parameters:

  • resource (Object)

Returns:

  • (String)


148
149
150
151
152
153
# File 'lib/helpers/wallaby/links_helper.rb', line 148

def edit_path(resource)
  decorated = decorate resource
  return unless decorated.primary_key_value
  wallaby_engine.edit_resource_path \
    decorated.resources_name, decorated.primary_key_value
end

Return link to index page by a given model class

If user’s not authorized, nil will be returned

Parameters:

  • model_class (Class)

    model class

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

    (@see ActionView::Helpers::UrlHelper#link_to)

Returns:

  • (String, nil)

    anchor element



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

def index_link(model_class, url_params: {}, html_options: {}, &block)
  return if cannot? :index, model_class
  html_options, block = LinkOptionsNormalizer.normalize(
    html_options, block,
    block: -> { to_model_label model_class }
  )

  path = index_path model_class, url_params: url_params
  link_to path, html_options, &block
end

#index_paramsActionController::Parameters

Returns whitelisted params used by Wallaby.

Returns:

  • (ActionController::Parameters)

    whitelisted params used by Wallaby



5
6
7
# File 'lib/helpers/wallaby/links_helper.rb', line 5

def index_params
  params.except(:resources, :utf8).permit(:filter, :page, :per, :q, :sort)
end

#index_path(model_class, url_params: {}) ⇒ String

Url for index page

Parameters:

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

Returns:

  • (String)


119
120
121
122
123
124
125
126
# File 'lib/helpers/wallaby/links_helper.rb', line 119

def index_path(model_class, url_params: {})
  if url_params.is_a?(::ActionController::Parameters) \
    && !url_params.permitted?
    url_params = {}
  end
  wallaby_engine.resources_path \
    to_resources_name(model_class), url_params.to_h
end

Return link to create page by a given model class

If user’s not authorized, nil will be returned

Parameters:

  • model_class (Class)

    model class

  • options (Hash)
  • html_options (Hash) (defaults to: {})

    (@see ActionView::Helpers::UrlHelper#link_to)

Returns:

  • (String, nil)

    anchor element



34
35
36
37
38
39
40
41
42
43
# File 'lib/helpers/wallaby/links_helper.rb', line 34

def new_link(model_class, html_options: {}, &block)
  return if cannot? :new, model_class
  html_options, block = LinkOptionsNormalizer.normalize(
    html_options, block,
    class: 'resource__create',
    block: -> { t 'links.new', model: to_model_label(model_class) }
  )

  link_to new_path(model_class), html_options, &block
end

#new_path(model_class) ⇒ String

Url for new resource form page

Parameters:

  • model_class (Class)

Returns:

  • (String)


131
132
133
# File 'lib/helpers/wallaby/links_helper.rb', line 131

def new_path(model_class)
  wallaby_engine.new_resource_path to_resources_name(model_class)
end

Return link to show page by a given model class If user’s not authorized, resource label will be returned

Parameters:

  • resource (Object, Wallaby::ResourceDecorator)

    model class

  • options (Hash) (defaults to: {})
  • html_options (Hash) (defaults to: {})

    (@see ActionView::Helpers::UrlHelper#link_to)

Returns:

  • (String)

    anchor element / text



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/helpers/wallaby/links_helper.rb', line 51

def show_link(resource, options: {}, 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 cannot? :show, extract(resource)
  link_to show_path(resource), html_options, &block
end

#show_path(resource) ⇒ String

Url for show page of given resource

Parameters:

  • resource (Object)

Returns:

  • (String)


138
139
140
141
142
143
# File 'lib/helpers/wallaby/links_helper.rb', line 138

def show_path(resource)
  decorated = decorate resource
  return unless decorated.primary_key_value
  wallaby_engine.resource_path \
    decorated.resources_name, decorated.primary_key_value
end