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
configure(*conf) click to toggle source

Quick config helper @param conf [Symbol, Array<Symbol>]

- `:requires` required number of completed actions
  - `:all`
  - `#Number`
# File lib/ecoportal/api/v2/page/component/action_field.rb, line 38
def configure(*conf)
  self.create_actions = true
  conf.each_with_object([]) do |cnf, unused|
    case cnf
    when Hash
      supported = [:requires]
      unless (rest = hash_except(cnf.dup, *supported)).empty?
        unused.push(rest)
      end
      if cnf.key?(:requires) then configure_required(cnf[:requires]) end
    else
      unused.push(cnf)
    end
  end.yield_self do |unused|
    super(*unused)
  end
end
ordered_tasks() click to toggle source
# File lib/ecoportal/api/v2/page/component/action_field.rb, line 27
def ordered_tasks
  actions.sort_by.with_index do |task, index|
    [task.weight, index]
  end
end

Private Instance Methods

configure_required(req) click to toggle source
# File lib/ecoportal/api/v2/page/component/action_field.rb, line 58
def configure_required(req)
  self.required = true
  case req
  when :all, NilClass
    self.required_number_of_completed_actions = "all"
  when Numeric
    self.required_number_of_completed_actions = req
  else
    # Unsupported
  end
end
fix_task_weights!() click to toggle source
# File lib/ecoportal/api/v2/page/component/action_field.rb, line 70
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 76
def previous_task(value)
  tasks = ordered_tasks
  pos  = tasks.index(value) - 1
  return if pos < 0
  tasks[pos]
end