# # A Collection is a conceptual and administrative entity containing a set of items. # class Collection < Ddr::Models::Base include Hydra::AdminPolicyBehavior include Ddr::Models::HasChildren include Ddr::Models::HasAttachments has_attributes :default_license_title, datastream: Ddr::Datastreams::DEFAULT_RIGHTS, at: [:license, :title], multiple: false has_attributes :default_license_description, datastream: Ddr::Datastreams::DEFAULT_RIGHTS, at: [:license, :description], multiple: false has_attributes :default_license_url, datastream: Ddr::Datastreams::DEFAULT_RIGHTS, at: [:license, :url], multiple: false has_many :children, property: :is_member_of_collection, class_name: 'Item' has_many :targets, property: :is_external_target_for, class_name: 'Target' alias_method :items, :children alias_method :item_ids, :child_ids after_create :set_admin_policy validates_presence_of :title # Returns the SolrDocuments for Components associated with the Collection. # # @return A lazy enumerator of SolrDocuments. def components_from_solr query = "#{Ddr::IndexFields::COLLECTION_URI}:#{RSolr.solr_escape(internal_uri)}" filter = ActiveFedora::SolrService.construct_query_for_rel(:has_model => Component.to_class_uri) results = ActiveFedora::SolrService.query(query, fq: filter, rows: 100000) results.lazy.map {|doc| SolrDocument.new(doc)} end # Returns the license attributes provided as default values for objects # governed by the Collection. # # @return [Hash] the attributes, `:title`, `:description`, and `:url`. def default_license if default_license_title.present? or default_license_description.present? or default_license_url.present? {title: default_license_title, description: default_license_description, url: default_license_url} end end # Sets the default license attributes for objects governed by the Collection. def default_license=(new_license) raise ArgumentError unless new_license.is_a?(Hash) # XXX don't do this - not duck-typeable l = new_license.with_indifferent_access self.default_license_title = l[:title] self.default_license_description = l[:description] self.default_license_url = l[:url] end # Returns a list of entities (either users or groups) having a default access # level on objects governed by the Collection. # # @param type [String] the type of entity, "user" or "group". # @param access [String] the default access level, "discover", "read", or "edit". # @return [Array] the entities (users or groups) def default_entities_for_permission(type, access) default_permissions.collect { |p| p[:name] if p[:type] == type and p[:access] == access }.compact end ["discover", "read", "edit"].each do |access| ["user", "group"].each do |type| define_method("default_#{access}_#{type}s") { default_entities_for_permission(type, access) } end end def set_policy_roles_from_legacy_data roles.revoke *(roles.in_policy_scope) roles.grant *(legacy_default_permissions.to_policy_roles) end alias_method :set_policy_roles, :set_policy_roles_from_legacy_data def legacy_default_permissions Ddr::Auth::LegacyPermissions.new(default_permissions) end def grant_roles_to_creator(creator) roles.grant type: Ddr::Auth::Roles::CURATOR, agent: creator.agent, scope: Ddr::Auth::Roles::RESOURCE_SCOPE roles.grant type: Ddr::Auth::Roles::CURATOR, agent: creator.agent, scope: Ddr::Auth::Roles::POLICY_SCOPE end private def set_admin_policy self.admin_policy = self self.save end end