forked from visualitypl/hotwire-kanban
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement card services for create, update, and destroy actions
- Loading branch information
Showing
7 changed files
with
123 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# frozen_string_literal: true | ||
|
||
module Cards | ||
class CreateCardService | ||
attr_reader :card | ||
|
||
def initialize(params:) | ||
@params = params | ||
end | ||
|
||
def call | ||
@card = Card.new(params) | ||
|
||
ActiveRecord::Base.transaction do | ||
card.save! | ||
end | ||
true | ||
rescue ActiveRecord::RecordInvalid | ||
false | ||
end | ||
|
||
private | ||
|
||
attr_reader :params | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
module Cards | ||
class DestroyCardService | ||
attr_reader :card | ||
|
||
def initialize(card:) | ||
@card = card | ||
end | ||
|
||
def call | ||
ActiveRecord::Base.transaction do | ||
card.destroy! | ||
end | ||
true | ||
rescue ActiveRecord::RecordInvalid | ||
false | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
module Cards | ||
class UpdateCardService | ||
attr_reader :card | ||
|
||
def initialize(card:, params:) | ||
@card = card | ||
@params = params | ||
end | ||
|
||
def call | ||
ActiveRecord::Base.transaction do | ||
card.update!(params) | ||
end | ||
true | ||
rescue ActiveRecord::RecordInvalid | ||
false | ||
end | ||
|
||
private | ||
|
||
attr_reader :params | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Cards::CreateCardService, type: :service do | ||
describe '#call' do | ||
it 'creates a new card' do | ||
board = create(:board) | ||
board_column = create(:board_column, board: board) | ||
params = { title: 'Card title', description: 'Card description', board_column_id: board_column.id } | ||
service = described_class.new(params: params) | ||
|
||
expect { service.call }.to change { Card.count }.by(1) | ||
expect(service.card.title).to eq('Card title') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Cards::DestroyCardService, type: :service do | ||
describe '#call' do | ||
it 'destroys a card' do | ||
card = create(:card) | ||
service = described_class.new(card: card) | ||
|
||
expect { service.call }.to change { Card.count }.by(-1) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Cards::UpdateCardService, type: :service do | ||
describe '#call' do | ||
it 'updates a card' do | ||
board = create(:board) | ||
board_column1 = create(:board_column, board: board) | ||
board_column2 = create(:board_column, board: board) | ||
card = create(:card, title: 'Old title', description: 'Old description', board_column: board_column1) | ||
params = { title: 'New title', description: 'New description', board_column_id: board_column2.id } | ||
service = described_class.new(card: card, params: params) | ||
|
||
expect { service.call }.to change { card.reload.title }.from('Old title').to('New title') | ||
expect(card.description).to eq('New description') | ||
expect(card.board_column).to eq(board_column2) | ||
end | ||
end | ||
end |