Refactor SnippetInputAction to accept actions as symbols

parent 33ddf71a
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
class SnippetInputAction class SnippetInputAction
include ActiveModel::Validations include ActiveModel::Validations
ACTIONS = %w[create update delete move].freeze ACTIONS = %i[create update delete move].freeze
ACTIONS.each do |action_const| ACTIONS.each do |action_const|
define_method "#{action_const}_action?" do define_method "#{action_const}_action?" do
...@@ -20,7 +20,7 @@ class SnippetInputAction ...@@ -20,7 +20,7 @@ class SnippetInputAction
validate :ensure_same_file_path_and_previous_path, if: :update_action? validate :ensure_same_file_path_and_previous_path, if: :update_action?
def initialize(action: nil, previous_path: nil, file_path: nil, content: nil) def initialize(action: nil, previous_path: nil, file_path: nil, content: nil)
@action = action @action = action&.to_sym
@previous_path = previous_path @previous_path = previous_path
@file_path = file_path @file_path = file_path
@content = content @content = content
...@@ -28,7 +28,7 @@ class SnippetInputAction ...@@ -28,7 +28,7 @@ class SnippetInputAction
def to_commit_action def to_commit_action
{ {
action: action&.to_sym, action: action,
previous_path: build_previous_path, previous_path: build_previous_path,
file_path: file_path, file_path: file_path,
content: content content: content
...@@ -44,6 +44,7 @@ class SnippetInputAction ...@@ -44,6 +44,7 @@ class SnippetInputAction
end end
def ensure_same_file_path_and_previous_path def ensure_same_file_path_and_previous_path
return if previous_path.blank? || file_path.blank?
return if previous_path == file_path return if previous_path == file_path
errors.add(:file_path, "can't be different from the previous_path attribute") errors.add(:file_path, "can't be different from the previous_path attribute")
......
...@@ -7,6 +7,11 @@ describe SnippetInputAction do ...@@ -7,6 +7,11 @@ describe SnippetInputAction do
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
where(:action, :file_path, :content, :previous_path, :is_valid, :invalid_field) do 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 'create' | 'foobar' | 'foobar' | 'foobar' | true | nil
'move' | 'foobar' | 'foobar' | 'foobar' | true | nil 'move' | 'foobar' | 'foobar' | 'foobar' | true | nil
'delete' | 'foobar' | 'foobar' | 'foobar' | true | nil 'delete' | 'foobar' | 'foobar' | 'foobar' | true | nil
...@@ -14,14 +19,17 @@ describe SnippetInputAction do ...@@ -14,14 +19,17 @@ describe SnippetInputAction do
'foo' | 'foobar' | 'foobar' | 'foobar' | false | :action 'foo' | 'foobar' | 'foobar' | 'foobar' | false | :action
nil | 'foobar' | 'foobar' | 'foobar' | false | :action nil | 'foobar' | 'foobar' | 'foobar' | false | :action
'' | 'foobar' | 'foobar' | 'foobar' | false | :action '' | 'foobar' | 'foobar' | 'foobar' | false | :action
'move' | 'foobar' | 'foobar' | nil | false | :previous_path :move | 'foobar' | 'foobar' | nil | false | :previous_path
'move' | 'foobar' | 'foobar' | '' | false | :previous_path :move | 'foobar' | 'foobar' | '' | false | :previous_path
'create' | 'foobar' | nil | 'foobar' | false | :content :create | 'foobar' | nil | 'foobar' | false | :content
'create' | 'foobar' | '' | 'foobar' | false | :content :create | 'foobar' | '' | 'foobar' | false | :content
'create' | nil | 'foobar' | 'foobar' | false | :file_path :create | nil | 'foobar' | 'foobar' | false | :file_path
'create' | '' | 'foobar' | 'foobar' | false | :file_path :create | '' | 'foobar' | 'foobar' | false | :file_path
'update' | 'foobar' | nil | 'foobar' | false | :content :update | 'foobar' | nil | 'foobar' | false | :content
'update' | 'other' | 'foobar' | 'foobar' | false | :file_path :update | 'foobar' | '' | 'foobar' | false | :content
:update | 'other' | 'foobar' | 'foobar' | false | :file_path
:update | 'foobar' | 'foobar' | nil | true | nil
:update | 'foobar' | 'foobar' | '' | true | nil
end end
with_them do 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