Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

T557 seeding dev data #563

Merged
merged 7 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
211 changes: 108 additions & 103 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,118 +1,123 @@
# reworking this to only create the necessary collections/roles/users for a minimally
# instance, and will move the seeding of works into a rake task
abort("The Rails environment is running in production mode!") if Rails.env.production?

def create_admin_role
@admin_role = Role.find_or_create_by(name: 'admin')
end
ActiveFedora::Cleaner.clean!

def create_default_admin_user
@admin_user = User.create!(email: ENV['DEV_ADMIN_USER_EMAIL'], password: ENV['DEV_ADMIN_USER_PASSWORD'])
admin_role = Role.find_or_create_by(name: 'admin')
admin_role.users << @admin_user
end
# users
admin_user = FactoryBot.create(:admin, email: "[email protected]")
content_admin_user = FactoryBot.create(:content_admin, email: "[email protected]")
non_admin_user = FactoryBot.create(:user, email: "[email protected]")

def create_default_admin_set
@default_admin_set = Hyrax::AdminSetCreateService.find_or_create_default_admin_set
end
# admin sets and collection types
default_admin_set_id = FactoryBot.create(:stored_default_admin_set_id)
admin_set_collection_type = FactoryBot.create(:admin_set_collection_type)
gw_etds_admin_set = FactoryBot.create(:admin_set, title: ["ETDs"],edit_users: [admin_user.user_key, content_admin_user.user_key])

def create_collection_type(machine_id, options)
coltype = Hyrax::CollectionType.find_by_machine_id(machine_id)
return coltype if coltype.present?
default_options = {
nestable: false, discoverable: false, sharable: false, allow_multiple_membership: false,
require_membership: false, assigns_workflow: false, assigns_visibility: false,
participants: [{ agent_type: Hyrax::CollectionTypeParticipant::GROUP_TYPE, agent_id: ::Ability.admin_group_name, access: Hyrax::CollectionTypeParticipant::MANAGE_ACCESS },
{ agent_type: Hyrax::CollectionTypeParticipant::GROUP_TYPE, agent_id: ::Ability.registered_group_name, access: Hyrax::CollectionTypeParticipant::CREATE_ACCESS }]
}
final_options = default_options.merge(options.except(:title))
Hyrax::CollectionTypes::CreateService.create_collection_type(machine_id: machine_id, title: options[:title], options: final_options)
end
permission_template = FactoryBot.create(:permission_template, source_id: gw_etds_admin_set.id, with_admin_set: true, with_active_workflow: true)

def create_admin_set_collection_type
admin_set_collection_type = Hyrax::CollectionType.find_or_create_admin_set_type
end
FactoryBot.create(:permission_template_access,
:deposit,
permission_template: permission_template,
agent_type: 'user',
agent_id: admin_user.user_key)

def create_user_collection_type
user_collection_type = Hyrax::CollectionType.find_or_create_default_collection_type
end
# collection to add works to
main_collection = FactoryBot.create(:public_collection, with_permission_template: permission_template)

def create_public_collection(user, type_gid, id, options)
options[:visibility] = Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_PUBLIC
create_collection(user, type_gid, id, options)
# other collections
4.times do
FactoryBot.create(:public_collection, with_permission_template: permission_template)
end

def create_etds_admin_set
etds_admin_set = Hyrax::AdministrativeSet.new(title: ['ETDs'])
etds_admin_set = Hyrax.persister.save(resource: etds_admin_set)
creating_user = User.where(email: ENV['DEV_ADMIN_USER_EMAIL']).first
Hyrax::AdminSetCreateService.call!(admin_set: etds_admin_set, creating_user: creating_user)
etds_admin_set
end

def create_collection(user, type_gid, id, options)
col = Collection.where(id: id)
return col.first if col.present?
col = Collection.new(id: id)
col.attributes = options.except(:visibility)
col.apply_depositor_metadata(user.user_key)
col.collection_type_gid = type_gid
col.visibility = options[:visibility]
col.save
Hyrax::Collections::PermissionsCreateService.create_default(collection: col, creating_user: user)
col
end


def create_etd(user, id, options)
work = GwEtd.where(id: id)
return work.first if work.present?
actor = Hyrax::CurationConcern.actor
attributes_for_actor = options
work = GwEtd.new(id: id)
actor_environment = Hyrax::Actors::Environment.new(work, Ability.new(user), attributes_for_actor)
actor.create(actor_environment)
work
# private works with specific configurations - will need to log in as admin to see them
FactoryBot.create(:work_with_image_files, user: admin_user, title: ["A Work with an Image File"])
FactoryBot.create(:embargoed_work, user: admin_user, title: ["An Embargoed Work"])
FactoryBot.create(:work_with_one_file, user: admin_user, title: ["A Work with a file"])
FactoryBot.create(:work_with_file_and_work, user: admin_user, title: ["A Work with a file and work"])
FactoryBot.create(:work_with_files, user: admin_user, title: ["A Work with files"])
FactoryBot.create(:work_with_one_child, user: admin_user, title: ["A Work with one child"])
FactoryBot.create(:work_with_two_children, user: admin_user, title: ["A Work with two children"])

# public GwWorks with metadata
5.times do |i|
FactoryBot.create(:public_work,
user: admin_user,
admin_set: gw_etds_admin_set,
title: ["Test Public ETD with metadata #{i}"],
gw_affiliation: ["Department of Testing", "Department of Quality Control", "Scholarly Technology Group"],
resource_type: [["Research Paper", "Article", "Archival Work"].sample],
creator: ["Professor Goodtests"],
contributor: ["Assistant Badtests", "Another Collaborator"],
description: ["A work for testing with metadata"],
abstract: Faker::Lorem.paragraphs(number: 1),
keyword: ["Testing", "Examining", "Prodding"],
license: ["http://www.europeana.eu/portal/rights/rr-r.html"],
rights_statement: ["http://rightsstatements.org/vocab/InC/1.0/"],
publisher: ["A Pretty Cool Publisher"],
date_created: [rand(1900..2010).to_s],
subject: ["Automated Testing"],
language: ["English"],
member_of_collections: [main_collection]
)
end

def create_public_etd(user, id, options)
options[:visibility] = Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_PUBLIC
create_etd(user, id, options)
# public GwJournalIssues
5.times do |i|
FactoryBot.create(:public_journal_issue,
user: admin_user,
admin_set: gw_etds_admin_set,
title: ["Test Public GW Journal Issue with Metadata #{i}"],
gw_affiliation: ["Department of Testing", "Department of Quality Control", "Scholarly Technology Group"],
resource_type: ["Journal Issue"],
creator: ["Professor Goodtests"],
contributor: ["Assistant Badtests", "Another Collaborator"],
description: ["A work for testing with metadata"],
abstract: Faker::Lorem.paragraphs(number: 1),
keyword: ["Testing", "Examining", "Prodding"],
license: ["http://www.europeana.eu/portal/rights/rr-r.html"],
rights_statement: ["http://rightsstatements.org/vocab/InC/1.0/"],
publisher: ["A Pretty Cool Publisher"],
date_created: [rand(1900..2010).to_s],
subject: ["Automated Testing"],
language: ["English"],
issue: [rand(1..3).to_s],
volume: [rand(1..3).to_s]
)
end

def create_private_etd(user, id, options)
options[:visibility] = Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_PRIVATE
create_etd(user, id, options)
# public ETDs
5.times do |i|
FactoryBot.create(:public_etd,
user: admin_user,
admin_set: gw_etds_admin_set,
title: ["Test Public GW Jouranl Issue with metadata #{i}"],
gw_affiliation: ["Department of Testing", "Department of Quality Control", "Scholarly Technology Group"],
resource_type: ["Journal Issue"],
creator: ["Professor Goodtests"],
contributor: ["Assistant Badtests", "Another Collaborator"],
description: ["A work for testing with metadata"],
abstract: Faker::Lorem.paragraphs(number: 1),
keyword: ["Testing", "Examining", "Prodding"],
license: ["http://www.europeana.eu/portal/rights/rr-r.html"],
rights_statement: ["http://rightsstatements.org/vocab/InC/1.0/"],
publisher: ["A Pretty Cool Publisher"],
date_created: [rand(1900..2010).to_s],
subject: ["Automated Testing"],
language: ["English"],
degree: "Ph.D",
advisor: ["Doctor Advisor"],
committee_member: ["Committee Member 1", "Committee Member 2"]
)
end

def create_authenticated_etd(user, id, options)
options[:visibility] = Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_AUTHENTICATED
create_etd(user, id, options)
end

def create_content_admin_role
@content_admin_role = Role.find_or_create_by(name: 'content-admin')
end

def create_content_admin_user
@content_admin_user = User.create!(email: "[email protected]", password: "password")
content_admin_role = Role.find_or_create_by(name: "content-admin")
content_admin_role.users << @content_admin_user
end

# --------------

# -- Creating admin role and user
create_admin_role
create_default_admin_user

# -- Creating default admin sets, admin set collection type, and user collection type --
create_default_admin_set
create_admin_set_collection_type
create_user_collection_type

# -- Creating the ETDs admin set
@etds_admin_set = create_etds_admin_set

# -- Creating a content-admin role and user
create_content_admin_role
create_content_admin_user
# content blocks
ContentBlock.find_or_create_by(name: "header_background_color").update!(value: "#FFFFFF")
ContentBlock.find_or_create_by(name: "header_text_color").update!(value: "#444444")
ContentBlock.find_or_create_by(name: "link_color").update!(value: "#28659A")
ContentBlock.find_or_create_by(name: "footer_link_color").update!(value: "#FFFFFF")
ContentBlock.find_or_create_by(name: "primary_button_background_color").update!(value: "#28659A")
ContentBlock.find_or_create_by(name: "featured_researcher").update!(value: File.open("#{Rails.root}/spec/fixtures/content_blocks/featured_researcher.html").read)
ContentBlock.find_or_create_by(name: "about_page").update!(value: File.open("#{Rails.root}/spec/fixtures/content_blocks/about_page.html").read)
ContentBlock.find_or_create_by(name: "help_page").update!(value: File.open("#{Rails.root}/spec/fixtures/content_blocks/help_page.html").read)
ContentBlock.find_or_create_by(name: "share_page").update!(value: File.open("#{Rails.root}/spec/fixtures/content_blocks/share_page.html").read)

# bundle exec rails db:drop ; bundle exec rails db:create ; bundle exec rails db:migrate ; bundle exec rails db:seed
12 changes: 12 additions & 0 deletions spec/factories/access_control.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true
FactoryBot.define do
factory :access_control, class: Hyrax::AccessControl do
permissions { build(:permission) }

trait :with_target do
access_to { valkyrie_create(:hyrax_resource).id }

permissions { build(:permission, access_to: access_to) }
end
end
end
13 changes: 10 additions & 3 deletions spec/factories/admin_sets.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# from https://github.com/samvera/hyrax/blob/v2.9.6/spec/factories/admin_sets.rb

# frozen_string_literal: true
FactoryBot.define do
factory :admin_set do
sequence(:title) { |n| ["Title #{n}"] }
Expand All @@ -12,6 +11,7 @@
after(:create) do |admin_set, evaluator|
if evaluator.with_permission_template
attributes = { source_id: admin_set.id }
attributes = evaluator.permission_template_attributes.merge(attributes) if evaluator.permission_template_attributes.respond_to?(:merge)
attributes = evaluator.with_permission_template.merge(attributes) if evaluator.with_permission_template.respond_to?(:merge)
# There is a unique constraint on permission_templates.source_id; I don't want to
# create a permission template if one already exists for this admin_set
Expand All @@ -22,6 +22,13 @@
transient do
# false, true, or Hash with keys for permission_template
with_permission_template { false }
permission_template_attributes { {} }
end

factory :complete_admin_set do
alternative_title { ['alternative admin set title'] }
creator { ['moomin', 'snufkin'] }
description { ['Before a revolution happens', 'it is perceived as impossible'] }
end
end
end
end
Loading
Loading