Module: ContextualLinkHelpers

Defined in:
app/helpers/contextual_link_helpers.rb

Instance Method Summary collapse

Instance Method Details

#action_to_icon(action) ⇒ Object

CRUD helpers



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/helpers/contextual_link_helpers.rb', line 3

def action_to_icon(action)
  case action.to_s
  when 'new'
    "plus"
  when 'show'
    "eye-open"
  when 'edit'
    "edit"
  when 'delete'
    "trash"
  when "index", "list"
    "list-alt"
  when "update"
    "refresh"
  when "copy"
    "repeat"
  else
    action
  end
end


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/helpers/contextual_link_helpers.rb', line 41

def contextual_link_to(action, resource_or_model = nil, link_options = {})
  # We don't want to change the passed in link_options
  options = link_options.dup

  # Handle both symbols and strings
  action = action.to_s
  
  # Resource and Model setup
  # Use controller name to guess resource or model if not specified
  case action
  when 'new', 'index'
    model = resource_or_model || controller_name.singularize.camelize.constantize
  when 'show', 'edit', 'delete'
    resource = 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?(:cannot?) and cannot?(action.to_sym, model)
  
  # Option generation
  case action
  when 'delete'
    options.merge!(:confirm => t_confirm_delete(resource), :method => :delete)
  end

  # Path generation
  case action
  when 'index'
    if model
      path = polymorphic_path(model)
    else
      path = url_for(:action => nil)
    end
  when 'show', 'delete'
    if resource
      path = polymorphic_path(resource)
    else
      return
    end
  else
    if resource_or_model
      path = polymorphic_path(resource_or_model, :action => action)
    else
      path = url_for(:action => action)
    end
  end

  return icon_link_to(action, path, options)
end


117
118
119
120
121
# File 'app/helpers/contextual_link_helpers.rb', line 117

def contextual_links(action = nil, resource_or_model = nil, options = {})
  ('div', :class => 'contextual') do
    contextual_links_for(action, resource_or_model, options)
  end
end


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/helpers/contextual_link_helpers.rb', line 93

def contextual_links_for(action = nil, resource_or_model = nil, options = {})
  # 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, options)}
  
  return links.join("\n").html_safe
end


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/helpers/contextual_link_helpers.rb', line 24

def icon_link_to(action, url = nil, options = {})
  classes = ["btn"]

  url ||= {:action => action}

  icon = options.delete(:icon)
  icon ||= action

  type = options.delete(:type)
  classes << "btn-#{type}"

  options.merge!(:class => classes.join(" "))
  link_to(url_for(url), options) do 
    (:i, "", :class => "icon-#{action_to_icon(icon)}") + " " + t_action(action)
  end
end