2024-07-03 06:45:37 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Flags::UpdateFlag
|
|
|
|
include Service::Base
|
|
|
|
|
DEV: Have `contract` take a block in services
Currently in services, the `contract` step is only used to define where
the contract will be called in the execution flow. Then, a `Contract`
class has to be defined with validations in it.
This patch allows the `contract` step to take a block containing
validations, attributes, etc. directly. No need to then open a
`Contract` class later in the service.
It also has a nice side effect, as it’s now easy to define multiples
contracts inside the same service. Before, we had the `class_name:`
option, but it wasn’t really useful as you had to redefine a complete
new contract class.
Now, when using a name for the contract other than `default`, a new
contract will be created automatically using the provided name.
Example:
```ruby
contract(:user) do
attribute :user_id, :integer
validates :user_id, presence: true
end
```
This will create a `UserContract` class and use it, also putting the
resulting contract in `context[:user_contract]`.
2024-10-01 23:17:14 +08:00
|
|
|
contract do
|
2024-10-17 23:15:35 +08:00
|
|
|
attribute :id, :integer
|
2024-07-03 06:45:37 +08:00
|
|
|
attribute :name, :string
|
|
|
|
attribute :description, :string
|
2024-07-18 08:10:22 +08:00
|
|
|
attribute :require_message, :boolean
|
2024-07-03 06:45:37 +08:00
|
|
|
attribute :enabled, :boolean
|
|
|
|
attribute :applies_to
|
DEV: Have `contract` take a block in services
Currently in services, the `contract` step is only used to define where
the contract will be called in the execution flow. Then, a `Contract`
class has to be defined with validations in it.
This patch allows the `contract` step to take a block containing
validations, attributes, etc. directly. No need to then open a
`Contract` class later in the service.
It also has a nice side effect, as it’s now easy to define multiples
contracts inside the same service. Before, we had the `class_name:`
option, but it wasn’t really useful as you had to redefine a complete
new contract class.
Now, when using a name for the contract other than `default`, a new
contract will be created automatically using the provided name.
Example:
```ruby
contract(:user) do
attribute :user_id, :integer
validates :user_id, presence: true
end
```
This will create a `UserContract` class and use it, also putting the
resulting contract in `context[:user_contract]`.
2024-10-01 23:17:14 +08:00
|
|
|
|
2024-10-17 23:15:35 +08:00
|
|
|
validates :id, presence: true
|
2024-07-03 06:45:37 +08:00
|
|
|
validates :name, presence: true
|
|
|
|
validates :description, presence: true
|
|
|
|
validates :name, length: { maximum: Flag::MAX_NAME_LENGTH }
|
|
|
|
validates :description, length: { maximum: Flag::MAX_DESCRIPTION_LENGTH }
|
2024-07-23 09:47:50 +08:00
|
|
|
validates :applies_to, inclusion: { in: -> { Flag.valid_applies_to_types } }, allow_nil: false
|
2024-07-03 06:45:37 +08:00
|
|
|
end
|
DEV: Have `contract` take a block in services
Currently in services, the `contract` step is only used to define where
the contract will be called in the execution flow. Then, a `Contract`
class has to be defined with validations in it.
This patch allows the `contract` step to take a block containing
validations, attributes, etc. directly. No need to then open a
`Contract` class later in the service.
It also has a nice side effect, as it’s now easy to define multiples
contracts inside the same service. Before, we had the `class_name:`
option, but it wasn’t really useful as you had to redefine a complete
new contract class.
Now, when using a name for the contract other than `default`, a new
contract will be created automatically using the provided name.
Example:
```ruby
contract(:user) do
attribute :user_id, :integer
validates :user_id, presence: true
end
```
This will create a `UserContract` class and use it, also putting the
resulting contract in `context[:user_contract]`.
2024-10-01 23:17:14 +08:00
|
|
|
model :flag
|
|
|
|
policy :not_system
|
|
|
|
policy :not_used
|
|
|
|
policy :invalid_access
|
|
|
|
policy :unique_name
|
|
|
|
transaction do
|
|
|
|
step :update
|
|
|
|
step :log
|
|
|
|
end
|
2024-07-03 06:45:37 +08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2024-10-17 23:15:35 +08:00
|
|
|
def fetch_flag(contract:)
|
|
|
|
Flag.find_by(id: contract.id)
|
2024-07-03 06:45:37 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def not_system(flag:)
|
|
|
|
!flag.system?
|
|
|
|
end
|
|
|
|
|
|
|
|
def not_used(flag:)
|
|
|
|
!flag.used?
|
|
|
|
end
|
|
|
|
|
|
|
|
def invalid_access(guardian:, flag:)
|
|
|
|
guardian.can_edit_flag?(flag)
|
|
|
|
end
|
|
|
|
|
2024-10-17 23:15:35 +08:00
|
|
|
def unique_name(contract:)
|
|
|
|
!Flag.custom.where(name: contract.name).where.not(id: contract.id).exists?
|
|
|
|
end
|
|
|
|
|
|
|
|
def update(flag:, contract:)
|
|
|
|
flag.update!(contract.attributes)
|
2024-07-03 06:45:37 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def log(guardian:, flag:)
|
|
|
|
StaffActionLogger.new(guardian.user).log_custom(
|
|
|
|
"update_flag",
|
2024-10-17 23:15:35 +08:00
|
|
|
flag.slice(:name, :description, :applies_to, :require_message, :enabled),
|
2024-07-03 06:45:37 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|