Module: Nested::JsUtil

Defined in:
lib/nested.rb

Class Method Summary collapse

Class Method Details

.function_arguments(resource) ⇒ Object



390
391
392
393
394
395
396
397
# File 'lib/nested.rb', line 390

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



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/nested.rb', line 352

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