Skip to content

Instantly share code, notes, and snippets.

@lmon
Created August 16, 2018 17:02
Show Gist options
  • Select an option

  • Save lmon/a403f0045da28944b8e9ecd735e8ae84 to your computer and use it in GitHub Desktop.

Select an option

Save lmon/a403f0045da28944b8e9ecd735e8ae84 to your computer and use it in GitHub Desktop.
copy_story_blocks
# copy all the blocks in a story to another story
def copy_story_blocks(params, replace_all_blocks = false)
HashParamsChecker.check_params!({ :user_id => 22,
:story_id => 44,
:revision_id => 55,
:block_id => 66,
:from_story_id => 2,
:to_story_id => 1 },
params
)
from_story = Story.find(params[:from_story_id])
to_story = Story.find(params[:to_story_id])
from_story_blocks = from_story.draft_story_blocks
new_revision_id = to_story.draft_revision_id
# remove all the blocks from the to_story
if replace_all_blocks
to_story_blocks = to_story.draft_story_blocks
blocks_to_remove = to_story_blocks.map(&:id)
params = {
params[:user_id],,
:story_id => to_story.id,
:revision_id => new_revision_id,
:block_ids => blocks_to_remove
}
new_revision = remove_blocks(params)
new_revision_id = new_revision[:revision_id]
end
from_story_blocks.each_with_index do |sb, index|
new_revision = StoryRevision.create!(
:user_id => params[:user_id],
:story_id => params[:from_story_id],
:block_id => params[:block_id],
:action => OperationType::Update
)
# get all the blocks from from_story
# for each block call Cms::StoryEditingService.add_block(block_data),
# where we are adding the block to the to_story, using
block_data = {
:user_id => sb[:user_id],#,
:story_id => to_story.id,
:revision_id => new_revision_id,
:block_schema_type => sb[:block_schema_type],
:position => sb[:position], <----- what happens if this position is already taken?
:block_data => sb[:block_data]
}
add_block(block_data)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment