-
Notifications
You must be signed in to change notification settings - Fork 5
Implement remix endpoints #746
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,22 +14,70 @@ def show | |
| end | ||
|
|
||
| def create | ||
| render json: { status: 'ok', 'content-name': 'new-project-id' }, status: :ok | ||
| original_project = load_original_project(source_project_identifier) | ||
| return render json: { error: I18n.t('errors.admin.unauthorized') }, status: :unauthorized unless current_ability.can?(:show, original_project) | ||
|
|
||
| remix_params = create_params | ||
| return render json: { error: I18n.t('errors.project.remixing.invalid_params') }, status: :bad_request if remix_params[:scratch_component].blank? | ||
|
|
||
| remix_origin = request.origin || request.referer | ||
|
|
||
| result = Project::CreateRemix.call( | ||
| params: remix_params, | ||
| user_id: current_user.id, | ||
| original_project:, | ||
| remix_origin: | ||
| ) | ||
|
|
||
| if result.success? | ||
| render json: { status: 'ok', 'content-name': result[:project].identifier }, status: :ok | ||
| else | ||
| render json: { error: result[:error] }, status: :bad_request | ||
| end | ||
| end | ||
|
|
||
| def update | ||
| scratch_content = params.permit!.slice(:meta, :targets, :monitors, :extensions) | ||
| @project.scratch_component&.content = scratch_content.to_unsafe_h | ||
| @project.scratch_component&.content = scratch_content_params | ||
| @project.save! | ||
| render json: { status: 'ok' }, status: :ok | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def ensure_create_is_a_remix | ||
| return if params[:is_remix] == '1' | ||
| return if params[:is_remix] == '1' && params[:original_id].present? | ||
|
|
||
| render json: { error: I18n.t('errors.project.remixing.only_existing_allowed') }, status: :forbidden | ||
| end | ||
|
|
||
| def source_project_identifier | ||
| params[:original_id] | ||
| end | ||
|
|
||
| def create_params | ||
| {}.tap do |hash| | ||
| hash[:identifier] = source_project_identifier | ||
| scratch_content = scratch_content_params | ||
| hash[:scratch_component] = { content: scratch_content } if scratch_content.present? | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When would |
||
| end | ||
| end | ||
|
|
||
| def load_original_project(identifier) | ||
| project_loader = ProjectLoader.new(identifier, [params[:locale]]) | ||
| original_project = project_loader.load | ||
|
|
||
| raise ActiveRecord::RecordNotFound, I18n.t('errors.project.not_found') unless original_project | ||
| raise ActiveRecord::RecordNotFound, I18n.t('errors.project.not_found') unless valid_original_project?(original_project) | ||
|
|
||
| original_project | ||
| end | ||
|
|
||
| def scratch_content_params | ||
| params.slice(:meta, :targets, :monitors, :extensions).to_unsafe_h | ||
| end | ||
|
|
||
| render json: { error: 'Only remixing existing projects is allowed' }, status: :forbidden | ||
| def valid_original_project?(project) | ||
| project.project_type == Project::Types::CODE_EDITOR_SCRATCH | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small thing, but it would be nice to push this logic down into the Project model since it's also being used elsewhere. |
||
| end | ||
abcampo-iry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| end | ||
| end | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,7 +23,11 @@ def validate_params(response, params, user_id, original_project, remix_origin) | |
| end | ||
|
|
||
| def remix_project(response, params, user_id, original_project, remix_origin) | ||
| response[:project] = create_remix(original_project, params, user_id, remix_origin) | ||
| response[:project] = if scratch_project?(original_project) | ||
| create_scratch_remix(original_project, params, user_id, remix_origin) | ||
| else | ||
| create_remix(original_project, params, user_id, remix_origin) | ||
| end | ||
| response[:project].save! | ||
| response | ||
| end | ||
|
|
@@ -50,17 +54,29 @@ def create_remix(original_project, params, user_id, remix_origin) | |
| remix | ||
| end | ||
|
|
||
| def create_scratch_remix(original_project, params, user_id, remix_origin) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is so much easier to follow now, thanks for making this change. |
||
| remix = format_project(original_project, params, user_id, remix_origin) | ||
| scratch_component = params.fetch(:scratch_component) | ||
| remix.build_scratch_component(content: scratch_component[:content] || scratch_component['content']) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small thing - I saw 'content' is provided as a symbol in the controller, so why is it necessary to also accept strings here? If it is important to accept strings, does |
||
|
|
||
| remix | ||
| end | ||
|
|
||
| def format_project(original_project, params, user_id, remix_origin) | ||
| original_project.dup.tap do |proj| | ||
| proj.identifier = PhraseIdentifier.generate | ||
| proj.locale = nil | ||
| proj.name = params[:name] | ||
| proj.name = params[:name] || original_project.name | ||
| proj.user_id = user_id | ||
| proj.remixed_from_id = original_project.id | ||
| proj.remix_origin = remix_origin | ||
| proj.lesson_id = nil # Only the original can have a lesson id | ||
| end | ||
| end | ||
|
|
||
| def scratch_project?(project) | ||
| project.project_type == Project::Types::CODE_EDITOR_SCRATCH | ||
| end | ||
| end | ||
| end | ||
| end | ||
Uh oh!
There was an error while loading. Please reload this page.