module Ecoportal module API class V2 class Page class Component class ContractorEntitiesField < Page::Component passboolean :is_me_button passboolean :show_approval_status passboolean :singular passthrough :requires_number passarray :contractor_entities_ids passboolean :show_approval_status passboolean :grant_permissions_enabled passthrough :apply_attached_contractors_permissions_to passthrough :admin_access, :regular_access embeds_one :attached_contractors_permissions_flags, klass: "Ecoportal::API::V2::Page::PermissionFlags" embeds_one :attached_contractors_admin_permissions_flags, klass: "Ecoportal::API::V2::Page::PermissionFlags" def empty? contractor_entities_ids.empty? end # Attaches people def add(*ids) contractor_entities_ids << ids end # Deletes people def delete(*ids) contractor_entities_ids.reject! {|id| ids.include?(id)} end def to_s(delimiter: "\n") contractor_entities_ids.to_a.join(delimiter) end # Quick config helper # @param conf [Symbol, Array] # - `:snapshot` to set mode to `snapshot` # - `:live` to set mode to `live` # - `:me_button` to display `ME` button # - `:permits` to define the permissions # - `:all` for _entire page/all stages_ # - `:stages` for _all stages containing this field_ # - `:page` for _page only_ # - `:stage` for _only the stage containing this field when attached_ # - `:restructure` # - `:configure` # - `:can_permission` # - `:create_actions` # - `:admin_actions` # - `:subscribed` # - `:subscribed_to_tasks` # - `requires: number` to fine the number of required people to be attached def configure(*conf) conf.each_with_object([]) do |cnf, unused| case cnf when :me_button self.is_me_button = true when :singular self.singular = true when Hash supported = [:singular, :permits] unless (rest = hash_except(cnf.dup, *supported)).empty? unused.push(rest) end if cnf.key?(:singular) then self.singular = !!cnf[:singular] end if cnf.key?(:permits) if permits = cnf[:permits] self.regular_access = true configure_permits(*[permits].flatten.compact) else self.regular_access = false end end if cnf.key?(:admin_permits) if permits = cnf[:permits] self.admin_access = true configure_permits(*[permits].flatten.compact) else self.admin_access = false end end else unused.push(cnf) end end.yield_self do |unused| super(*unused) end end private def configure_permits(*conf, to: :regular) conf.each_with_object([]) do |cnf, flags| permits_scope = :apply_attached_contractors_permissions_to= case cnf when :all send(permits_scope, "page") when :stages send(permits_scope, "all_stages") when :page send(permits_scope, "page_only") when :stage send(permits_scope, "current_stage") else flags.push(cnf) end end.yield_self do |flags| target_flags = :attached_contractors_admin_permissions_flags if to == :admin target_flags ||= :attached_contractors_permissions_flags self.send(target_flags).configure *flags end end end end end end end end