Sha256: 259f12a7a5a342d22c7f74e9f0c045769b431b5a555fa8b723b76ad0709b7c35

Contents?: true

Size: 1.45 KB

Versions: 19

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

require_dependency "renalware/snippets"

#
# This controller handles copying snippets (when I am the owner) and cloning them (when I am not).
# The ownership drives what suffix is appended to the snippet title to keep it unique.
#
module Renalware
  module Snippets
    class SnippetClonesController < BaseController
      COPY_SUFFIX = "[COPY]".freeze
      CLONE_SUFFIX = "[CLONE]".freeze

      def create
        snippet = duplicate_current_snippet
        authorize snippet
        snippet.save!
        redirect_to edit_snippet_path(snippet)
      end

      private

      def duplicate_current_snippet
        title_suffix = title_suffix_to_apply_to_the_duplicate(current_snippet)
        current_snippet.dup.tap do |snippet|
          snippet.author = Snippets.cast_user(current_user)
          snippet.title = snippet_title_generator_for(snippet, title_suffix: title_suffix).call
          snippet.last_used_on = nil
          snippet.times_used = 0
        end
      end

      def current_snippet
        Snippet.find(params[:snippet_id])
      end

      def title_suffix_to_apply_to_the_duplicate(snippet)
        snippet_is_mine?(snippet) ? COPY_SUFFIX : CLONE_SUFFIX
      end

      def snippet_title_generator_for(snippet, title_suffix:)
        DuplicateSnippetTitleGenerator.new(snippet, title_suffix: title_suffix)
      end

      def snippet_is_mine?(snippet)
        snippet.author_id == current_user.id
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
renalware-core-2.0.37 app/controllers/renalware/snippets/snippet_clones_controller.rb
renalware-core-2.0.36 app/controllers/renalware/snippets/snippet_clones_controller.rb
renalware-core-2.0.35 app/controllers/renalware/snippets/snippet_clones_controller.rb
renalware-core-2.0.34 app/controllers/renalware/snippets/snippet_clones_controller.rb
renalware-core-2.0.33 app/controllers/renalware/snippets/snippet_clones_controller.rb
renalware-core-2.0.32 app/controllers/renalware/snippets/snippet_clones_controller.rb
renalware-core-2.0.31 app/controllers/renalware/snippets/snippet_clones_controller.rb
renalware-core-2.0.30 app/controllers/renalware/snippets/snippet_clones_controller.rb
renalware-core-2.0.28 app/controllers/renalware/snippets/snippet_clones_controller.rb
renalware-core-2.0.27 app/controllers/renalware/snippets/snippet_clones_controller.rb
renalware-core-2.0.26 app/controllers/renalware/snippets/snippet_clones_controller.rb
renalware-core-2.0.25 app/controllers/renalware/snippets/snippet_clones_controller.rb
renalware-core-2.0.24 app/controllers/renalware/snippets/snippet_clones_controller.rb
renalware-core-2.0.23 app/controllers/renalware/snippets/snippet_clones_controller.rb
renalware-core-2.0.22 app/controllers/renalware/snippets/snippet_clones_controller.rb
renalware-core-2.0.21 app/controllers/renalware/snippets/snippet_clones_controller.rb
renalware-core-2.0.20 app/controllers/renalware/snippets/snippet_clones_controller.rb
renalware-core-2.0.18 app/controllers/renalware/snippets/snippet_clones_controller.rb
renalware-core-2.0.17 app/controllers/renalware/snippets/snippet_clones_controller.rb