Module: E9Rails::Helpers::ResourceLinks::HelperMethods

Defined in:
lib/e9_rails/helpers/resource_links.rb

Instance Method Summary collapse

Instance Method Details



87
88
89
# File 'lib/e9_rails/helpers/resource_links.rb', line 87

def link_to_collection(resource, options = {})
  link_to_resource(resource, options.merge(:action => :index))
end


83
84
85
# File 'lib/e9_rails/helpers/resource_links.rb', line 83

def link_to_destroy_resource(resource, options = {})
  link_to_resource(resource, options.merge(:action => :destroy))
end


75
76
77
# File 'lib/e9_rails/helpers/resource_links.rb', line 75

def link_to_edit_resource(resource, options = {})
  link_to_resource(resource, options.merge(:action => :edit))
end


79
80
81
# File 'lib/e9_rails/helpers/resource_links.rb', line 79

def link_to_new_resource(resource, options = {})
  link_to_resource(resource, options.merge(:action => :new))
end


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

  #
  # Mimic ActiveModel's lookup chain for attributes
  #
  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


71
72
73
# File 'lib/e9_rails/helpers/resource_links.rb', line 71

def link_to_show_resource(resource, options = {})
  link_to_resource(resource, options.merge(:action => :show))
end