Module: ListLinkHelpers

Includes:
ContextualLinkHelpers
Defined in:
app/helpers/list_link_helpers.rb

Instance Method Summary collapse

Methods included from ContextualLinkHelpers

#action_to_icon, #contextual_link_to, #contextual_links, #contextual_links_for, #icon_link_to

Instance Method Details



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/helpers/list_link_helpers.rb', line 14

def list_link_for(action, resource_or_model = nil, options = {})
  # Handle both symbols and strings
  action = action.to_s

  # Resource and Model setup
  # Support nested resources
  if resource_or_model.is_a? Array
    main_resource_or_model = resource_or_model.last
  else
    main_resource_or_model = resource_or_model
  end

  # Use controller name to guess resource or model if not specified
  case action
  when 'new', 'index'
    model = main_resource_or_model || controller_name.singularize.camelize.constantize
  when 'show', 'edit', 'delete'
    resource = main_resource_or_model || instance_variable_get("@#{controller_name.singularize}")
    model = resource.class
  end
  model_name = model.to_s.underscore

  # No link if CanCan is used and current user isn't authorized to call this action
  return if respond_to?(:can?) and cannot?(action.to_sym, model)

  # Link generation
  case action
  when 'index', 'show'
    path = polymorphic_path(resource_or_model)
  when 'delete'
    path = polymorphic_path(resource_or_model)
    options.merge!(:confirm => t_confirm_delete(main_resource_or_model), :method => :delete)
  else
    path = polymorphic_path(resource_or_model, :action => action)
  end

  return list_link_to(action, path, options)
end

List link helpers



5
6
7
8
9
10
11
12
# File 'app/helpers/list_link_helpers.rb', line 5

def list_link_to(action, url, options = {})
  icon = options.delete(:icon)
  icon ||= action

  link_to(url_for(url), options) do
    boot_icon action_to_icon(icon)
  end
end


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/helpers/list_link_helpers.rb', line 53

def list_links_for(action = nil, resource_or_model = nil)
  # Use current action if not specified
  action ||= action_name

  # Handle both symbols and strings
  action = action.to_s

  actions = []
  case action
  when 'new', 'create'
    actions << 'index'
  when 'show'
    actions += ['edit', 'delete', 'index']
  when 'edit', 'update'
    actions += ['show', 'delete', 'index']
  when 'index'
    actions << 'new'
  end

  links = actions.map{|link_for| contextual_link_to(link_for, resource_or_model)}

  return links.join("\n").html_safe
end