class Ecoportal::API::V2::Page::Component::ActionField

Public Instance Methods

add_task(name) { |task| ... } click to toggle source

Adds a task with `name` short description @return [Ecoportal::API::V2::Page::Component::Action]

# File lib/ecoportal/api/v2/page/component/action_field.rb, line 15
def add_task (name)
  task_doc = actions.items_class.new_doc
  actions.upsert!(task_doc) do |task|
    task.name = name
    if prev = previous_task(task)
      task.weight = prev.weight
    end
    yield(task) if block_given?
    fix_task_weights!
  end
end
ordered_tasks() click to toggle source
# File lib/ecoportal/api/v2/page/component/action_field.rb, line 27
def ordered_tasks
  actions.each_with_index.sort_by do |task, index|
    (task.weight >= 9999) ? [index, index] : [task.weight, index]
  end.map(&:first)
end

Private Instance Methods

fix_task_weights!() click to toggle source
# File lib/ecoportal/api/v2/page/component/action_field.rb, line 35
def fix_task_weights!
  ordered_tasks.each_with_index do |task, index|
    task.weight = index
  end
end
previous_task(value) click to toggle source
# File lib/ecoportal/api/v2/page/component/action_field.rb, line 41
def previous_task(value)
  tasks = ordered_tasks
  pos  = tasks.index(value) - 1
  return if pos < 0
  tasks[pos]
end