module Ecoportal module API class V2 class Page class Component class PeopleField < Page::Component passboolean :is_me_button passthrough :attach_mode passthrough :person_schema_id embeds_many :viewable_fields, klass: "Ecoportal::API::V2::Page::Component::PeopleViewableField" passboolean :singular passthrough :requires_number passarray :people_ids pass_reader :cached_people passboolean :attached_people_permissions_enabled, :attached_people_permissions_editable passthrough :apply_attached_people_permissions_to embeds_one :attached_people_permissions_flags, klass: "Ecoportal::API::V2::Page::PermissionFlags" # Attaches people def add(*ids) people_ids << ids end # Deletes people def delete(*ids) people_ids.reject! {|id| ids.include?(id)} end # Adds a field to the `viewable_fields` def add_viewable(field_id, pos: NOT_USED, before: NOT_USED, after: NOT_USED) viewable_fields.upsert!({"id" => field_id}, pos: pos, before: before, after: after) end # Deletes a field from the `viewable_fields` def delete_viewable(field_id) viewable_fields.delete!(field_id) 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 :snapshot self.attach_mode = "snapshot" when :live self.attach_mode = "live" when :me_button self.is_me_button = true when :singular self.singular = true when Hash supported = [:singular, :permits, :requires] 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.attached_people_permissions_enabled = true configure_permits(*[permits].flatten.compact) else self.attached_people_permissions_enabled = false end end if cnf.key?(:requires) self.singular = false if requires = cnf[:requires] self.required = true self.requires_number = requires else self.required = false self.requires_number = nil end end else unused.push(cnf) end end.yield_self do |unused| super(*unused) end end private def configure_permits(*conf) conf.each_with_object([]) do |cnf, flags| case cnf when :all self.apply_attached_people_permissions_to = "page" when :stages self.apply_attached_people_permissions_to = "all_stages" when :page self.apply_attached_people_permissions_to = "page_only" when :stage self.apply_attached_people_permissions_to = "current_stage" when :can_edit self.attached_people_permissions_editable = true else flags.push(cnf) end end.yield_self do |flags| self.attached_people_permissions_flags.configure *flags end end end end end end end end require 'ecoportal/api/v2/page/component/people_viewable_field'