lib/ecoportal/api/internal/account.rb in ecoportal-api-0.8.2 vs lib/ecoportal/api/internal/account.rb in ecoportal-api-0.8.3
- old
+ new
@@ -1,79 +1,56 @@
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
+ PROPERTIES = [
+ "user_id", "policy_group_ids", "default_tag", "prefilter",
+ "permissions_custom", "permissions_merged", "preferences",
+ "login_provider_ids", "starred_ids", "landing_page_id",
+ "accept_eula", "send_invites"
+ ]
+ passthrough *PROPERTIES.map(&:to_sym)
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 :perms_merged, key: "permissions_merged", klass: :permissions_class
embeds_one :preferences, klass: :preferences_class
# Sets the `policy_group_ids`
# @note it preserves the original order
# @param value [Array<String>] 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
+ set_uniq_array_keep_order("policy_group_ids", value)
end
# @return [Array<String>] 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
+ set_uniq_array_keep_order("login_provider_ids", value)
end
# @return [Array<String>] 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
+ set_uniq_array_keep_order("starred_ids", value)
end
# @return [Array<String>] 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)
@@ -86,16 +63,17 @@
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["permissions_custom"] = permissions.as_json
+ hash["permissions_merged"] = perms_merged.as_json
hash["preferences"] = preferences.as_json
end
+ end
+
+ def as_update(ref = :last, ignore: [])
+ super(ref, ignore: ignore | ["user_id", "permissions_merged", "prefilter"])
end
end
end
end