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

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

Instance Method Summary collapse

Instance Method Details



85
86
87
# File 'lib/e9_rails/helpers/resource_links.rb', line 85

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


81
82
83
# File 'lib/e9_rails/helpers/resource_links.rb', line 81

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


73
74
75
# File 'lib/e9_rails/helpers/resource_links.rb', line 73

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


77
78
79
# File 'lib/e9_rails/helpers/resource_links.rb', line 77

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


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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/e9_rails/helpers/resource_links.rb', line 14

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


69
70
71
# File 'lib/e9_rails/helpers/resource_links.rb', line 69

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

#parentObject



10
11
12
# File 'lib/e9_rails/helpers/resource_links.rb', line 10

def parent
  controller.parent rescue nil
end