lib/ecoportal/api/v2/page/component/action_field.rb in ecoportal-api-v2-0.8.8 vs lib/ecoportal/api/v2/page/component/action_field.rb in ecoportal-api-v2-0.8.9

- old
+ new

@@ -2,13 +2,13 @@ module API class V2 class Page class Component class ActionField < Page::Component - passthrough :create_actions + passboolean :create_actions passthrough :required_number_of_completed_actions - passthrough :permits_and_rules_integration, :add_subscribed, :add_subscribed_to_tasks + passboolean :permits_and_rules_integration, :add_subscribed, :add_subscribed_to_tasks embeds_many :actions, klass: "Ecoportal::API::V2::Page::Component::Action", order_key: :weight # Adds a task with `name` short description # @return [Ecoportal::API::V2::Page::Component::Action] @@ -28,10 +28,45 @@ actions.each_with_index.sort_by do |task, index| (task.weight >= 9999) ? [index, index] : [task.weight, index] end.map(&:first) end + # Quick config helper + # @param conf [Symbol, Array<Symbol>] + # - `:requires` required number of completed actions + # - `:all` + # - `#Number` + 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 + private + + 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 def fix_task_weights! ordered_tasks.each_with_index do |task, index| task.weight = index end