module Ecoportal module API class Internal class Account < Common::BaseModel passthrough :policy_group_ids, :landing_page_id, :permissions_preset, :permissions_custom, :preferences, :prefilter, :login_provider_ids, :starred_ids, :accept_eula, :send_invites, :default_tag class_resolver :preferences_class, "Ecoportal::API::Internal::Preferences" class_resolver :permissions_class, "Ecoportal::API::Internal::Permissions" embeds_one :permissions, key: "permissions_custom", klass: :permissions_class embeds_one :preferences, klass: :preferences_class # Sets the `policy_group_ids` # @note it preserves the original order # @param value [Array] the policy group ids to be set. def policy_group_ids=(value) unless value.is_a?(Array) raise "policy_group_ids= needs to be passed an Array, got #{value.class}" end value.uniq! ini_ids = (original_doc && original_doc["policy_group_ids"]) || [] # preserve original order to avoid false updates doc["policy_group_ids"] = (ini_ids & value) + (value - ini_ids) doc["policy_group_ids"].compact! end # @return [Array] the policy group ids of this user. def policy_group_ids doc["policy_group_ids"] ||= [] end # Sets the `login_provider_ids` def login_provider_ids=(value) unless value.is_a?(Array) raise "login_provider_ids= needs to be passed an Array, got #{value.class}" end doc["login_provider_ids"] = value.compact! end # @return [Array] the login provider ids of this user. def login_provider_ids doc["login_provider_ids"] ||= [] end # Sets the `starred_ids` def starred_ids=(value) unless value.is_a?(Array) raise "starred_ids= needs to be passed an Array, got #{value.class}" end doc["starred_ids"] = value.compact! end # @return [Array] the starred page ids of this user. def starred_ids doc["starred_ids"] ||= [] end # Sets the `permissions_preset`. # @note basically the same as `permissions_preset=` but when `"custom"`, it's changed to `nil` # @param value [nil, String] preset name. def preset=(value) self.permissions_preset = value == "custom" ? nil : value end # Gets the `permissions_preset`. # @note basically the same as `permissions_preset` but when 'nil', it returns `"custom"` instead # @return [nil, String] preset name. def preset self.permissions_preset.nil? ? "custom" : self.permissions_preset end # It preserves the values of keys that are not defined in `value`. # @param value [Hash] the abilities that you want to update. def permissions_custom=(value) doc["permissions_custom"] ||= {} doc["permissions_custom"].merge!(value) end # It preserves the values of keys that are not defined in `value`. # @param value [Hash] the preferences that you want to update. def preferences=(value) doc["preferences"] ||= {} doc["preferences"].merge!(value) end def as_json super.tap do |hash| if preset == "custom" hash["permissions_custom"] = permissions.as_json else hash.delete "permissions_custom" end hash["preferences"] = preferences.as_json end end end end end end require 'ecoportal/api/internal/permissions' require 'ecoportal/api/internal/preferences'