Commit 2d81578d authored by Igor Drozdov's avatar Igor Drozdov

Merge branch 'fj-refactor-snippet-input-action' into 'master'

Refactor SnippetInputAction to accept actions as symbols

See merge request gitlab-org/gitlab!34451
parents 735618a4 6e102b0f
......@@ -3,7 +3,7 @@
class SnippetInputAction
include ActiveModel::Validations
ACTIONS = %w[create update delete move].freeze
ACTIONS = %i[create update delete move].freeze
ACTIONS.each do |action_const|
define_method "#{action_const}_action?" do
......@@ -20,7 +20,7 @@ class SnippetInputAction
validate :ensure_same_file_path_and_previous_path, if: :update_action?
def initialize(action: nil, previous_path: nil, file_path: nil, content: nil)
@action = action
@action = action&.to_sym
@previous_path = previous_path
@file_path = file_path
@content = content
......@@ -28,7 +28,7 @@ class SnippetInputAction
def to_commit_action
{
action: action&.to_sym,
action: action,
previous_path: build_previous_path,
file_path: file_path,
content: content
......@@ -44,6 +44,7 @@ class SnippetInputAction
end
def ensure_same_file_path_and_previous_path
return if previous_path.blank? || file_path.blank?
return if previous_path == file_path
errors.add(:file_path, "can't be different from the previous_path attribute")
......
......@@ -7,6 +7,11 @@ describe SnippetInputAction do
using RSpec::Parameterized::TableSyntax
where(:action, :file_path, :content, :previous_path, :is_valid, :invalid_field) do
:create | 'foobar' | 'foobar' | 'foobar' | true | nil
:move | 'foobar' | 'foobar' | 'foobar' | true | nil
:delete | 'foobar' | 'foobar' | 'foobar' | true | nil
:update | 'foobar' | 'foobar' | 'foobar' | true | nil
:foo | 'foobar' | 'foobar' | 'foobar' | false | :action
'create' | 'foobar' | 'foobar' | 'foobar' | true | nil
'move' | 'foobar' | 'foobar' | 'foobar' | true | nil
'delete' | 'foobar' | 'foobar' | 'foobar' | true | nil
......@@ -14,14 +19,17 @@ describe SnippetInputAction do
'foo' | 'foobar' | 'foobar' | 'foobar' | false | :action
nil | 'foobar' | 'foobar' | 'foobar' | false | :action
'' | 'foobar' | 'foobar' | 'foobar' | false | :action
'move' | 'foobar' | 'foobar' | nil | false | :previous_path
'move' | 'foobar' | 'foobar' | '' | false | :previous_path
'create' | 'foobar' | nil | 'foobar' | false | :content
'create' | 'foobar' | '' | 'foobar' | false | :content
'create' | nil | 'foobar' | 'foobar' | false | :file_path
'create' | '' | 'foobar' | 'foobar' | false | :file_path
'update' | 'foobar' | nil | 'foobar' | false | :content
'update' | 'other' | 'foobar' | 'foobar' | false | :file_path
:move | 'foobar' | 'foobar' | nil | false | :previous_path
:move | 'foobar' | 'foobar' | '' | false | :previous_path
:create | 'foobar' | nil | 'foobar' | false | :content
:create | 'foobar' | '' | 'foobar' | false | :content
:create | nil | 'foobar' | 'foobar' | false | :file_path
:create | '' | 'foobar' | 'foobar' | false | :file_path
:update | 'foobar' | nil | 'foobar' | false | :content
:update | 'foobar' | '' | 'foobar' | false | :content
:update | 'other' | 'foobar' | 'foobar' | false | :file_path
:update | 'foobar' | 'foobar' | nil | true | nil
:update | 'foobar' | 'foobar' | '' | true | nil
end
with_them do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment