Skip to content

Commit

Permalink
Merge pull request #22 from /issues/17-Store_ID_of_parent_project_whe…
Browse files Browse the repository at this point in the history
…n_remixing

Store ID of parent project when remixing
  • Loading branch information
loiswells97 authored Mar 4, 2022
2 parents 7619884 + 9ca50de commit 8cf6501
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 5 deletions.
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 @@ -26,14 +26,19 @@ def validate_params(response, params, user_id)
def remix_project(response, params, user_id)
original_project = Project.find_by!(identifier: params[:phrase_id])

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

response[:error] = 'Unable to create project' unless response[:project].save
response
end

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
end
end
end
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

0 comments on commit 8cf6501

Please sign in to comment.