module Ecoportal module API class V2 class Page class Stage < Common::Content::DoubleModel passkey :id passforced :patch_ver, default: 1 passthrough :name, :ordering passarray :subtags, order_matters: false passarray :section_ids passthrough :status passboolean :complete, :lock_after_completion embeds_many :permits, klass: "Ecoportal::API::V2::Page::Permit" passboolean :disable_direct_permissions passboolean :creator_enabled, :creator_editable embeds_one :creator_flags, klass: "Ecoportal::API::V2::Page::PermissionFlags" passthrough :can def ooze self._parent.ooze end # @yield [section] do some stuff with section. # @yieldparam section [Ecoportal::API::V2::Page::Section] one of the sections of this stage # @return [Array] sections attached to this stage. def sections sec_ids = section_ids.to_a root.sections.values_at(*sec_ids).select.with_index do |sec, i| puts "Warning: section #{id} points to missing section #{sec_ids[i]}" if !sec fld && (!block_given? || yield(sec)) end.sort_by.with_index {|sec, index| [sec.weight, index]} end # Check if a section belongs to a stage # @raise [ArgumentError] if none of the valid types of `sec_or_id` are used # @param sec_or_id [String, Ecoportal::API::V2::Page::Section] # @return [Boolean] whether or not the section belongs to the stage def section?(sec_or_id) case sec_or_id when Ecoportal::API::V2::Page::Section section?(sec_or_id.id) when String self.section_ids.include?(id) else raise ArgumentError.new("sec_or_id must be either a Section or a String. Given: #{sec_or_id.class}") end end # Adds one or more sections to this stage # @raise [ArgumentError] if any of the elements is not a Section # @raise [Exception] if any of the sections does not belong to ooze.sections # @param secs [Array] sections to be added to this stage. def add_section(*secs) secs.each do |sec| unless sec.is_a?(Ecoportal::API::V2::Page::Section) msg = "Expected Ecoportal::API::V2::Page::Section. Given: #{sec.class}" raise ArgumentError.new(msg) end unless ooze.sections.include?(sec) msg = "The section '#{sec.heading}' (#{sec.id}) is not present in ooze.sections.\n" msg += "Review your script (i.e. @var where you store previous ooze runs)." raise msg end section_ids.insert_one(sec.id) end self end end end end end end