Module: Wallaby::LinksHelper
- Included in:
- BaseHelper
- Defined in:
- lib/helpers/wallaby/links_helper.rb
Overview
Links helper
Instance Method Summary collapse
-
#cancel_link(html_options = {}, &block) ⇒ String
Return link to cancel an action.
-
#delete_link(resource, html_options: {}, &block) ⇒ String?
Return link to delete action by a given model class.
-
#edit_link(resource, options: {}, html_options: {}, &block) ⇒ String
Return link to edit page by a given model class If user’s not authorized, resource label will be returned.
-
#edit_path(resource) ⇒ String
Url for edit form page of given resource.
-
#index_link(model_class, url_params: {}, html_options: {}, &block) ⇒ String?
Return link to index page by a given model class.
-
#index_params ⇒ ActionController::Parameters
Whitelisted params used by Wallaby.
-
#index_path(model_class, url_params: {}) ⇒ String
Url for index page.
-
#new_link(model_class, html_options: {}, &block) ⇒ String?
Return link to create page by a given model class.
-
#new_path(model_class) ⇒ String
Url for new resource form page.
-
#show_link(resource, options: {}, html_options: {}, &block) ⇒ String
Return link to show page by a given model class If user’s not authorized, resource label will be returned.
-
#show_path(resource) ⇒ String
Url for show page of given resource.
Instance Method Details
#cancel_link(html_options = {}, &block) ⇒ String
Return link to cancel an action
110 111 112 113 |
# File 'lib/helpers/wallaby/links_helper.rb', line 110 def cancel_link( = {}, &block) block ||= -> { t 'links.cancel' } link_to :back, , &block end |
#delete_link(resource, html_options: {}, &block) ⇒ String?
Return link to delete action by a given model class
If user’s not authorized, nil will be returned
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) , block = LinkOptionsNormalizer.normalize( , block, class: 'resource__destroy', block: -> { t 'links.delete' } ) [:method] ||= :delete [:data] ||= {} [:data][:confirm] ||= t 'links.confirm.delete' link_to show_path(resource), , &block end |
#edit_link(resource, options: {}, html_options: {}, &block) ⇒ String
Return link to edit page by a given model class If user’s not authorized, resource label will be returned
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 = [:readonly] && decorate(resource).to_label || nil return default if cannot? :edit, extract(resource) , block = LinkOptionsNormalizer.normalize( , block, class: 'resource__update', block: -> { "#{t 'links.edit'} #{decorate(resource).to_label}" } ) link_to edit_path(resource), , &block end |
#edit_path(resource) ⇒ String
Url for edit form page of given resource
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 |
#index_link(model_class, url_params: {}, html_options: {}, &block) ⇒ String?
Return link to index page by a given model class
If user’s not authorized, nil will be returned
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 , block = LinkOptionsNormalizer.normalize( , block, block: -> { to_model_label model_class } ) path = index_path model_class, url_params: url_params link_to path, , &block end |
#index_params ⇒ ActionController::Parameters
Returns 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
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 |
#new_link(model_class, html_options: {}, &block) ⇒ String?
Return link to create page by a given model class
If user’s not authorized, nil will be returned
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 , block = LinkOptionsNormalizer.normalize( , block, class: 'resource__create', block: -> { t 'links.new', model: to_model_label(model_class) } ) link_to new_path(model_class), , &block end |
#new_path(model_class) ⇒ String
Url for new resource form page
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 |
#show_link(resource, options: {}, html_options: {}, &block) ⇒ String
Return link to show page by a given model class If user’s not authorized, resource label will be returned
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 , block = LinkOptionsNormalizer.normalize( , block, block: -> { decorate(resource).to_label.to_s } ) default = [:readonly] && block.call || nil return default if cannot? :show, extract(resource) link_to show_path(resource), , &block end |
#show_path(resource) ⇒ String
Url for show page of given resource
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 |