Module: PowerResource::ActionHelper
- Defined in:
- app/helpers/power_resource/action_helper.rb
Instance Method Summary collapse
-
#resource_action_human_name(action_name, options = {}) ⇒ Object
Returns humanized name for an action.
-
#resource_action_title(action_name, options = {}) ⇒ Object
Returns a title for a current resource based on an action.
-
#resource_link_to(action_name, resource_instance = nil) ⇒ Object
Returns a link for a current resource based on an action.
Instance Method Details
#resource_action_human_name(action_name, options = {}) ⇒ Object
Returns humanized name for an action
Customization using I18n API:
power_resource:
actions:
new: 'New %{resource_name}'
edit: 'Edit %{resource_name}'
index: '%{collection_name}'
Variables available in I18n:
resource_name # => Post
downcased_resource_name # => post
collection_name # => Posts
Examples:
resource_action_human_name(:new)
# => New Post
resource_action_human_name(:edit)
# => Edit Post
resource_action_human_name(:duplicate)
# => Duplicate Post
resource_action_human_name(:index)
# => Posts
30 31 32 33 34 35 36 37 38 39 |
# File 'app/helpers/power_resource/action_helper.rb', line 30 def resource_action_human_name(action_name, = {}) I18n.t("power_resource.resource_actions.#{action_name.to_s}", { resource_name: resource_human_name, downcased_resource_name: resource_human_name.downcase, collection_name: collection_human_name, default: (action_name == :index ? collection_human_name : "#{action_name.to_s.humanize} #{resource_human_name}") }.merge()) end |
#resource_action_title(action_name, options = {}) ⇒ Object
Returns a title for a current resource based on an action
Customization using I18n API:
power_resource:
titles:
post:
new: 'New %{resource_name}'
edit: 'Edit %{resource_name}'
delete: 'Delete %{downcased_resource_name}'
index: '%{collection_name}'
Variables available in I18n:
resource_name # => Post
downcased_resource_name # => post
collection_name # => Posts
Examples:
resource_action_title(:new)
# => New Post
resource_action_title(:edit)
# => Edit Post
resource_action_title(:duplicate)
# => Duplicate Post
resource_action_title(:index)
# => Posts
70 71 72 73 74 75 76 77 78 |
# File 'app/helpers/power_resource/action_helper.rb', line 70 def resource_action_title(action_name, = {}) I18n.t("power_resource.titles.#{resource_name}.#{action_name.to_s}", { resource_name: resource_human_name, downcased_resource_name: resource_human_name.downcase, collection_name: collection_human_name, default: resource_action_human_name(action_name, ) }.merge()) end |
#resource_link_to(action_name, resource_instance = nil) ⇒ Object
Returns a link for a current resource based on an action
Customization using I18n API:
power_resource:
links:
post:
new: 'New %{resource_name}'
edit: 'Edit'
index: '%{collection_name}'
Variables available in I18n:
resource_name # => Post
downcased_resource_name # => post
collection_name # => Posts
Examples:
resource_link_to(:new)
# => <a href="/posts/new">New Post</a>
resource_link_to(:edit, resource)
# => <a href="/posts/1/edit">Edit</a>
resource_link_to(:index)
# => <a href="/posts/">Posts</a>
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'app/helpers/power_resource/action_helper.rb', line 106 def resource_link_to(action_name, resource_instance = nil) if resource_instance human_name = resource_human_name_for(resource_instance.class.name) end case action_name when :show, :edit, :delete default_text = I18n.t("power_resource.actions.#{action_name.to_s}", default: action_name.to_s.humanize) else default_text = resource_action_human_name(action_name) end text = I18n.t("power_resource.links.#{resource_name}.#{action_name.to_s}", { resource_name: human_name || resource_human_name, downcased_resource_name: human_name ? human_name.downcase : resource_human_name.downcase, collection_name: collection_human_name, default: default_text }) case action_name when :index link_to(text, collection_path) when :show link_to(text, resource_path(resource_instance || resource)) when :new link_to(text, new_resource_path) when :edit link_to(text, edit_resource_path(resource_instance || resource), class: ) when :delete link_to(text, resource_path(resource_instance || resource), class: , method: :delete, data: { confirm: I18n.t('power_resource.confirmations.delete', default: 'Are you sure?') }) end end |