Method: Jets::UrlHelper#_handle_array

Defined in:
lib/jets/overrides/rails/url_helper.rb

#_handle_array(array) ⇒ Object

Convention is that the model class name is the method name. Doesnt work if user is using as.



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
# File 'lib/jets/overrides/rails/url_helper.rb', line 37

def _handle_array(array)
  contains_nil = !array.select(&:nil?).empty?
  if contains_nil
    raise "ERROR: You passed a nil value in the Array. #{array.inspect}."
  end

  last_persisted = nil
  items = array.map do |x|
    if x.is_a?(ActiveRecord::Base)
      last_persisted = x.persisted?
      x.persisted? ? x.model_name.singular_route_key : x.model_name.route_key
    else
      x
    end
  end
  meth = items.join('_') + "_path"

  args = array.clone
  args.shift if args.first.is_a?(Symbol) # drop the first element if its a symbol
  args = last_persisted ? args : args[0..-2]

  # post_comment_path(post_id) - keep all args - for update
  # post_comments_path - drop last arg - for create
  send(meth, *args)
end