Skip to content
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
6 changes: 6 additions & 0 deletions app/models/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class Collection < ApplicationRecord
validates :name, uniqueness: {case_sensitive: false}
validates :public_id, multimodel_uniqueness: {case_sensitive: false, check: FederailsCommon::FEDIVERSE_USERNAMES}

before_validation :publish_creator, if: :will_be_public?

validate :validate_publishable

after_create_commit :after_create
Expand Down Expand Up @@ -121,4 +123,8 @@ def validate_publishable
errors.add :collection, :private if collection && !collection.public?
end
end

def publish_creator
creator&.update!(caber_relations_attributes: [{permission: "view", subject: nil}]) unless creator&.public?
end
end
4 changes: 4 additions & 0 deletions app/models/concerns/caber_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ def assign_default_permissions
grant_permission_to("own", owner) if owner
end
end

def will_be_public?
caber_relations.find { |it| it.subject.nil? }
end
end
17 changes: 10 additions & 7 deletions app/models/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Model < ApplicationRecord
acts_as_taggable_on :tags

before_validation :strip_separators_from_path, if: :path_changed?

before_validation :publish_creator, if: :will_be_public?
before_validation :normalize_license
# In Rails 7.1 we will be able to do this instead:
# normalizes :license, with: -> license { license.blank? ? nil : license }
Expand Down Expand Up @@ -336,11 +336,14 @@ def noteworthy_change?

def validate_publishable
# If the model will be public
if caber_relations.find { |it| it.subject.nil? }
# Check required fields
errors.add :license, :blank if license.nil?
errors.add :creator, :blank if creator.nil?
errors.add :creator, :private if creator && !creator.public?
end
return unless will_be_public?
# Check required fields
errors.add :license, :blank if license.nil?
errors.add :creator, :blank if creator.nil?
errors.add :creator, :private if creator && !creator.public?
end

def publish_creator
creator&.update!(caber_relations_attributes: [{permission: "view", subject: nil}]) unless creator&.public?
end
end
4 changes: 2 additions & 2 deletions spec/models/collection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
collection.validate
end

it "requires creator to be public if set" do
expect(collection.errors[:creator]).to include "must be public"
it "makes creator public automatically" do
expect(collection.creator).to be_public
end

it "requires collection to be public if set" do
Expand Down
7 changes: 4 additions & 3 deletions spec/models/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -516,9 +516,10 @@
expect(model.errors[:creator]).to include "can't be blank"
end

it "requires a public creator" do
model.update(creator: create(:creator))
expect(model.errors[:creator]).to include "must be public"
it "make creators public" do
new_creator = create(:creator)
model.update!(creator: new_creator, license: "MIT")
expect(new_creator).to be_public
end

it "requires a license" do
Expand Down
14 changes: 14 additions & 0 deletions spec/requests/models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,20 @@
expect(tags[2]).to eq "d"
end

it "auto-publishes creator if being made public", :as_moderator do # rubocop:todo RSpec/ExampleLength, RSpec/MultipleExpectations
private_creator = create(:creator)
private_model = create(:model, creator: private_creator)
put "/models/#{private_model.to_param}", params: {
model: {
creator_id: private_creator.id,
caber_relations_attributes: {"0" => {subject: "role::public", permission: "view"}}
}
}
expect(response).to have_http_status(:redirect)
expect(private_model.reload).to be_public
expect(private_creator.reload).to be_public
end

it "is denied to non-moderators", :as_contributor do
put "/models/#{library.models.first.to_param}"
expect(response).to have_http_status(:forbidden)
Expand Down
Loading