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

Store ID of parent project when remixing #22

Merged
merged 13 commits into from
Mar 4, 2022
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@
/config/master.key

.rubocop-https*

/coverage
13 changes: 9 additions & 4 deletions app/concepts/project/operation/create_remix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@ def validate_params(response, params, user_id)
response[:error] = 'Invalid parameters' unless valid
end

def remix_project(response, params, user_id)
original_project = Project.find_by!(identifier: params[:phrase_id])

response[:project] = original_project.dup.tap do |proj|
def create_remix(original_project, user_id)
original_project.dup.tap do |proj|
proj.user_id = user_id
proj.components = original_project.components.map(&:dup)
proj.remixed_from_id = original_project.id
end
end

def remix_project(response, params, user_id)
original_project = Project.find_by!(identifier: params[:phrase_id])

response[:project] = create_remix(original_project, user_id)

response[:error] = 'Unable to create project' unless response[:project].save
response
Expand Down
2 changes: 2 additions & 0 deletions app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
class Project < ApplicationRecord
before_validation :check_unique_not_null, on: :create
validates :identifier, presence: true, uniqueness: true
belongs_to :parent, class_name: 'Project', foreign_key: 'remixed_from_id', optional: true, inverse_of: :children
has_many :components, -> { order(:index) }, dependent: :destroy, inverse_of: :project
has_many :children, class_name: 'Project', foreign_key: 'remixed_from_id', dependent: :nullify, inverse_of: :parent

private

Expand Down
2 changes: 2 additions & 0 deletions app/views/api/projects/show.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

json.call(@project, :identifier, :project_type, :name)

json.parent(@project.parent, :name, :identifier) if @project.parent

json.components @project.components, :id, :name, :extension, :content
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AssociateRemixedProjectsWithTheirParentProject < ActiveRecord::Migration[7.0]
def change
add_reference :projects, :remixed_from, type: :uuid, references: :projects
end
end
4 changes: 3 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions spec/models/project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
RSpec.describe Project, type: :model do
describe 'associations' do
it { is_expected.to have_many(:components) }
it { is_expected.to have_many(:children) }
it { is_expected.to belong_to(:parent).optional(true) }
end

describe 'identifier not nil' do
Expand Down