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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/e9_rails/helpers/resource_links.rb', line 16
def link_to_resource(resource, options = {})
options.symbolize_keys!
path_options = options.slice!(:scope, :action, :method, :remote, :confirm, :class)
klass = resource.is_a?(Class) ? resource : resource.class
action = (options.delete(:action) || :show).to_sym
if klass == resource && ![:index, :new].member?(action)
action = :index
end
scopes = [*(options[:scope] || @route_scope), parent].compact
path = case action
when :new; new_polymorphic_path(scopes << klass, path_options)
when :edit; edit_polymorphic_path(scopes << resource, path_options)
when :index; polymorphic_path(scopes << klass, path_options)
else polymorphic_path(scopes << resource, path_options)
end
mn = klass.model_name
translation_options = {
:model => mn.human,
:models => mn.human.pluralize,
:collection => mn.collection,
:element => mn.element
}
if action == :destroy
defaults = klass.lookup_ancestors.map {|k|
:"#{klass.i18n_scope}.links.#{k.model_name.underscore}.confirm_destroy"
} << :"#{klass.i18n_scope}.links.confirm_destroy"
options[:method] = :delete
options.reverse_merge!({
:remote => true,
:confirm => I18n.t(defaults.shift, translation_options.merge(:default => defaults))
})
end
defaults = klass.lookup_ancestors.map do |k|
:"#{klass.i18n_scope}.links.#{k.model_name.underscore}.#{action}"
end
defaults << :"#{klass.i18n_scope}.links.#{action}"
defaults << action.to_s.humanize
link_to I18n.t(defaults.shift, translation_options.merge(:default => defaults)), path, options
end
|