Module: ResourceHelper

Included in:
Admin::BaseController, BaseController
Defined in:
app/helpers/resource_helper.rb

Instance Method Summary collapse

Instance Method Details

#current_controller_namespaceObject



68
69
70
71
72
# File 'app/helpers/resource_helper.rb', line 68

def current_controller_namespace
  path = respond_to?(:controller_path) ? controller_path : controller.controller_path
  namespace = path.split('/')[0..-2].join('_')
  namespace.present? ? namespace : nil
end


52
53
54
# File 'app/helpers/resource_helper.rb', line 52

def links_to_actions(actions, *args)
  actions.map { |action| resource_link(action, *args) }.join("\n")
end


103
104
105
106
107
108
109
# File 'app/helpers/resource_helper.rb', line 103

def normalize_resource_link_options(options, action, type, resource)
  options[:class] ||= "#{action} #{type}"
  options[:id] ||= resource_link_id(action, type, resource)
  options[:title] ||= t(:"adva.titles.#{action}")
  options.reverse_merge!(resource_delete_options(type, options)) if action == :delete
  options
end


111
112
113
114
115
116
# File 'app/helpers/resource_helper.rb', line 111

def normalize_resource_link_text(text, action, type)
  type = type.to_s.gsub('/', '_').pluralize
  text ||= t(:"adva.#{type}.links.#{action}", :default => :"adva.resources.links.#{action}")
  text = t(text) if text.is_a?(Symbol)
  text
end

#normalize_resource_type(action, type, resource) ⇒ Object



56
57
58
59
60
61
62
# File 'app/helpers/resource_helper.rb', line 56

def normalize_resource_type(action, type, resource)
  type ||= resource.is_a?(Symbol) ? resource : resource.class.name
  type = 'section' if type.to_s.classify.constantize < Section
  type = type.to_s.tableize.gsub("/","_") if action == :index
  type = type.to_s.split("::").first == "Adva" ? type.to_s.underscore.gsub("/","_") : type.to_s.demodulize.underscore
  type
end

#resource_delete_options(type, options) ⇒ Object



118
119
120
121
122
123
124
# File 'app/helpers/resource_helper.rb', line 118

def resource_delete_options(type, options)
  type = type.to_s.gsub('/', '_').pluralize
  message = options.delete(:confirm)
  message ||= t(:"adva.#{type}.confirm_delete", :default => :"adva.resources.confirm_delete")
  message = t(message) if message.is_a?(Symbol)
  { :data => { :confirm => message }, :method => :delete }
end


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/helpers/resource_helper.rb', line 16

def resource_link(action, *args)
  action = action.to_sym
  url_options = args.extract_options!.dup
  options = url_options.slice!(:only_path, :namespace, :anchor, :cl) # FIXME rather slice out known link_to options here

  resource, text = *args.reverse
  type, resource = *resource.reverse if resource.is_a?(Array)

  type = normalize_resource_type(action, type, resource)
  text = normalize_resource_link_text(text, action, type)
  options = normalize_resource_link_options(options, action, type, resource)

  resource = [resource, type] if [:index, :new].include?(action)
  url = options.delete(:url) || resource_url(action, resource, url_options)

  link_to(text, url, options)
end


74
75
76
77
78
# File 'app/helpers/resource_helper.rb', line 74

def resource_link_id(action, type, resource)
  id = [action, type]
  id << resource.id if resource.is_a?(ActiveRecord::Base) && !action.in?(:index, :new)
  id.join('_')
end

#resource_owners(resource) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/helpers/resource_helper.rb', line 80

def resource_owners(resource)
  return [] if resource.nil? || resource.is_a?(Symbol)
  return resource.owners << resource if resource.respond_to?(:owners)

  owners = []
  if resource.respond_to?(:section)
    owners << resource.section
  elsif resource.respond_to?(:owner)
    owners << resource.owner
  end

  owners << resource
end

#resource_url(action, resource, options = {}) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
# File 'app/helpers/resource_helper.rb', line 2

def resource_url(action, resource, options = {})
  type, resource = *resource.reverse if resource.is_a?(Array)
  raise "can not generate a url for a new #{resource.class.name}" if resource.try(:new_record?)

  namespace = resource_url_namespace(options)
  type = normalize_resource_type(action, type, resource)
  options.reverse_merge!(:only_path => true)

  args = resource_owners(resource) << options
  args.shift unless namespace.try(:to_sym) == :admin

  send(resource_url_method(namespace, action, type, options), *args.uniq)
end

#resource_url_method(namespace, action, type, options) ⇒ Object



94
95
96
97
98
99
100
101
# File 'app/helpers/resource_helper.rb', line 94

def resource_url_method(namespace, action, type, options)
  method = [namespace, type].compact

  method << (options.delete(:only_path) ? 'path' : 'url')
  method.unshift(action) if [:new, :edit].include?(action.to_sym)

  method.compact.join('_').gsub('/', '_')
end

#resource_url_namespace(options) ⇒ Object



64
65
66
# File 'app/helpers/resource_helper.rb', line 64

def resource_url_namespace(options)
  options.key?(:namespace) ? options.delete(:namespace) : current_controller_namespace
end