Module: ContextualLinkHelpers

Included in:
ListLinkHelpers
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', 'destroy'
    "trash"
  when "index", "list"
    "list-alt"
  when "update"
    "refresh"
  when "copy"
    "repeat"
  else
    action
  end
end


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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/helpers/contextual_link_helpers.rb', line 51

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_sym

  # Resource and Model setup
  # Use controller name to guess resource or model if not specified
  case action
  when :new, :index
    default_model = controller_name.singularize.camelize.constantize
    model = resource_or_model || default_model
    explicit_resource_or_model = default_model != model
  when :show, :edit, :delete, :destroy
    default_resource = instance_variable_get("@#{controller_name.singularize}")
    resource = resource_or_model || default_resource
    model = resource.class
    explicit_resource_or_model = default_resource != resource
  end
  model_name = model.to_s.underscore

  unless resource_or_model.is_a?(String)
    # 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)
  end

  # Option generation
  case action
  when :delete, :destroy
    options.merge!(:confirm => t_confirm_delete(resource), :method => :delete)
  end

  begin
    if resource_or_model.is_a?(String)
      path = resource_or_model
    else
      # Path generation
      case action
      when :index
        if explicit_resource_or_model
          path = polymorphic_path(model)
        else
          path = url_for(:action => nil)
        end
      when :delete, :destroy
        if explicit_resource_or_model
          path = polymorphic_path(resource)
        else
          path = url_for(:action => :destroy)
        end
      else
        if explicit_resource_or_model
          path = polymorphic_path(resource_or_model, :action => action)
        else
          path = url_for(:action => action)
        end
      end
    end

    return icon_link_to(action, path, options)

  rescue ActionController::UrlGenerationError
    # This handles cases where we did exclude crud actions in the routing map.
  end
end


142
143
144
145
146
147
148
149
150
151
# File 'app/helpers/contextual_link_helpers.rb', line 142

def contextual_links(action = nil, resource_or_model = nil, options = {}, &block)
  ('div', :class => I18nRailsHelpers.contextual_class) do
    content = contextual_links_for(action, resource_or_model, options)
    if block_given?
      additional_content = capture(&block)
      content += ("\n" + additional_content).html_safe unless additional_content.nil?
    end
    content
  end
end


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/helpers/contextual_link_helpers.rb', line 118

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_sym

  actions = []
  case action
  when :new, :create
    actions << :index
  when :show
    actions += [:edit, :destroy, :index]
  when :edit, :update
    actions += [:show, :destroy, :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
40
41
42
43
44
45
46
47
48
49
# File 'app/helpers/contextual_link_helpers.rb', line 24

def icon_link_to(action, url = nil, options = {})
  classes = []
  if class_options = options.delete(:class)
    classes << class_options.split(' ')
  end

  classes << I18nRailsHelpers.contextual_link_class

  if action.is_a? Symbol
    url ||= {:action => action}
    title = t_action(action)
  else
    title = action
  end

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

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

  options.merge!(:class => classes.join(" "))
  link_to(url_for(url), options) do
    boot_icon(action_to_icon(icon)) + " " + title
  end
end