Module: Nested::JsUtil

Defined in:
lib/nested.rb

Class Method Summary collapse

Class Method Details

.function_arguments(resource) ⇒ Object



491
492
493
494
495
496
497
498
# File 'lib/nested.rb', line 491

def self.function_arguments(resource)
  resource
    .self_and_parents.select{|r| r.member?}
    .map{|r| r.name || r.parent.name}
    .map(&:to_s)
    .map(&:singularize)
    .reverse
end

.generate_function_name(resource, method, action) ⇒ Object



453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'lib/nested.rb', line 453

def self.generate_function_name(resource, method, action)
  arr = []

  arr << "update" if method == :put
  arr << "create" if method == :post
  arr << "destroy" if method == :delete

  parents = resource.parents
  parents.each_with_index do |p, idx|
    if p.collection? && method != :post && ((parents[idx + 1] && parents[idx + 1].singleton?) || parents.last == p)
      arr << p.name.to_s.send(:pluralize)
    else
      arr << p.name.to_s.send(:singularize)
    end
  end

  if resource.member?
    if resource.parent
      arr = arr.slice(0...-1)
      arr << resource.parent.name.to_s.send(:singularize)
    else
      arr << resource.name.to_s.send(:singularize)
    end
  elsif resource.singleton?
    arr << resource.name.to_s.send(:singularize)
  elsif resource.collection?
    if method == :post
      arr << resource.name.to_s.send(:singularize)
    else
      arr << resource.name.to_s.send(:pluralize)
    end
  end

  arr << action if action

  arr.map(&:to_s).join("_").camelcase(:lower)
end