Module: ResourceHelper
- Included in:
- Admin::BaseController, BaseController
- Defined in:
- app/helpers/resource_helper.rb
Instance Method Summary collapse
- #current_controller_namespace ⇒ Object
- #links_to_actions(actions, *args) ⇒ Object
- #normalize_resource_link_options(options, action, type, resource) ⇒ Object
- #normalize_resource_link_text(text, action, type) ⇒ Object
- #normalize_resource_type(action, type, resource) ⇒ Object
- #resource_delete_options(type, options) ⇒ Object
- #resource_link(action, *args) ⇒ Object
- #resource_link_id(action, type, resource) ⇒ Object
- #resource_owners(resource) ⇒ Object
- #resource_url(action, resource, options = {}) ⇒ Object
- #resource_url_method(namespace, action, type, options) ⇒ Object
- #resource_url_namespace(options) ⇒ Object
Instance Method Details
#current_controller_namespace ⇒ Object
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 |
#links_to_actions(actions, *args) ⇒ Object
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 |
#normalize_resource_link_options(options, action, type, resource) ⇒ Object
103 104 105 106 107 108 109 |
# File 'app/helpers/resource_helper.rb', line 103 def (, action, type, resource) [:class] ||= "#{action} #{type}" [:id] ||= resource_link_id(action, type, resource) [:title] ||= action.to_s.titleize .reverse_merge!((type, )) if action == :delete end |
#normalize_resource_link_text(text, action, type) ⇒ Object
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 ||= action.capitalize 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 |
# File 'app/helpers/resource_helper.rb', line 118 def (type, ) type = type.to_s.gsub('/', '_').pluralize = .delete(:confirm) ||= "Are you sure you want to delete this #{type}?" { :data => { :confirm => }, :method => :delete } end |
#resource_link(action, *args) ⇒ Object
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 = args..dup = .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) = (, action, type, resource) resource = [resource, type] if [:index, :new].include?(action) url = .delete(:url) || resource_url(action, resource, ) link_to(text, url, ) end |
#resource_link_id(action, type, resource) ⇒ Object
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) && !%i[index name].include?(action) 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, = {}) 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() type = normalize_resource_type(action, type, resource) .reverse_merge!(:only_path => true) args = resource_owners(resource) << args.shift unless namespace.try(:to_sym) == :admin send(resource_url_method(namespace, action, type, ), *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, ) method = [namespace, type].compact method << (.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() .key?(:namespace) ? .delete(:namespace) : current_controller_namespace end |