Skip to content

Commit

Permalink
Merge pull request #18 from nickel/more-about-pictos
Browse files Browse the repository at this point in the history
Enabling/disabling pictos
  • Loading branch information
nickel authored Mar 27, 2024
2 parents 41696c9 + 5f9d716 commit bc25493
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/packages/planning/models/picto.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ class Picto < ApplicationRecord
has_one_attached :image

def to_struct
CustomStruct.new(attributes)
CustomStruct.new(attributes.merge(enabled?: enabled))
end
end
4 changes: 2 additions & 2 deletions app/packages/planning/operations/picto/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Form
attribute :keyword, :string
attribute :external_id, :integer
attribute :external_source, :string
attribute :active, :boolean
attribute :enabled, :boolean, default: true

attribute :image
attribute :data
Expand All @@ -17,7 +17,7 @@ class Form

def call
Picto
.new(keyword:, external_id:, external_source:, active:, data:)
.new(keyword:, external_id:, external_source:, enabled:, data:)
.save_with_response
.and_then do |picto|
picto.image.attach(io: File.open(image), filename: "image.png") if image
Expand Down
24 changes: 24 additions & 0 deletions app/packages/planning/operations/picto/disable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

class Picto::Disable < CommandHandler::Command
class Form
include CommandHandler::Form

attribute :picto_id, :integer
end

delegate(*Form.new.attributes.keys, to: :form)

def call
Picto::Find
.call(picto_id:)
.and_then do
Picto
.find_by(id: picto_id)
.update_with_response(enabled: false)
.and_then do |picto|
Response.success(picto.to_struct)
end
end
end
end
24 changes: 24 additions & 0 deletions app/packages/planning/operations/picto/enable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

class Picto::Enable < CommandHandler::Command
class Form
include CommandHandler::Form

attribute :picto_id, :integer
end

delegate(*Form.new.attributes.keys, to: :form)

def call
Picto::Find
.call(picto_id:)
.and_then do
Picto
.find_by(id: picto_id)
.update_with_response(enabled: true)
.and_then do |picto|
Response.success(picto.to_struct)
end
end
end
end
22 changes: 22 additions & 0 deletions app/packages/planning/operations/picto/find.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

class Picto::Find < CommandHandler::Command
class Form
include CommandHandler::Form

attribute :picto_id, :integer
end

delegate(*Form.new.attributes.keys, to: :form)

def call
if (picto = Picto.find_by(id: picto_id))
Response.success(picto.to_struct)
else
Response.failure(
Errors::RecordNotFoundError
.build(form:)
)
end
end
end
2 changes: 1 addition & 1 deletion db/migrate/20240326153956_create_pictos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def change

t.jsonb :data

t.boolean :active, default: true
t.boolean :enabled, default: true

t.timestamps
end
Expand Down
2 changes: 1 addition & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions test/packages/planning/operations/picto/disable_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

require "test_helper"

class Picto::DisableTest < ActiveSupport::TestCase
test "picto should be disabled" do
picto = Factory.generate_picto(enabled: true)

assert picto.enabled?

response = Picto::Disable.call(picto_id: picto.id)

assert response.success?
refute response.value.enabled?
end
end
16 changes: 16 additions & 0 deletions test/packages/planning/operations/picto/enable_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

require "test_helper"

class Picto::EnableTest < ActiveSupport::TestCase
test "picto should be enabled" do
picto = Factory.generate_picto(enabled: false)

refute picto.enabled?

response = Picto::Enable.call(picto_id: picto.id)

assert response.success?
assert response.value.enabled?
end
end
8 changes: 8 additions & 0 deletions test/support/factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,12 @@ def generate_plan(**input)
name: "This is a plan"
).value!
end

def generate_picto(**input)
Picto::Create
.call(
keyword: "Picto #{Time.now.to_f}",
enabled: input.fetch(:enabled, true)
).value!
end
end

0 comments on commit bc25493

Please sign in to comment.