2024-05-29 00:39:58 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-07-02 18:45:37 -04:00
|
|
|
class Flags::ToggleFlag
|
2024-05-29 00:39:58 -04:00
|
|
|
include Service::Base
|
|
|
|
|
2024-10-17 11:15:35 -04:00
|
|
|
policy :invalid_access
|
2024-11-28 10:51:43 -05:00
|
|
|
|
DEV: Replace `params` by the contract object in services
This patch replaces the parameters provided to a service through
`params` by the contract object.
That way, it allows better consistency when accessing input params. For
example, if you have a service without a contract, to access a
parameter, you need to use `params[:my_parameter]`. But with a contract,
you do this through `contract.my_parameter`. Now, with this patch,
you’ll be able to access it through `params.my_parameter` or
`params[:my_parameter]`.
Some methods have been added to the contract object to better mimic a
Hash. That way, when accessing/using `params`, you don’t have to think
too much about it:
- `params.my_key` is also accessible through `params[:my_key]`.
- `params.my_key = value` can also be done through `params[:my_key] =
value`.
- `#slice` and `#merge` are available.
- `#to_hash` has been implemented, so the contract object will be
automatically cast as a hash by Ruby depending on the context. For
example, with an AR model, you can do this: `user.update(**params)`.
2024-10-23 11:57:48 -04:00
|
|
|
params do
|
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 11:17:14 -04:00
|
|
|
attribute :flag_id, :integer
|
|
|
|
|
|
|
|
validates :flag_id, presence: true
|
|
|
|
end
|
2024-11-28 10:51:43 -05:00
|
|
|
|
2024-05-29 00:39:58 -04:00
|
|
|
model :flag
|
2024-11-28 10:51:43 -05:00
|
|
|
|
2024-05-29 00:39:58 -04:00
|
|
|
transaction do
|
|
|
|
step :toggle
|
|
|
|
step :log
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def invalid_access(guardian:)
|
|
|
|
guardian.can_toggle_flag?
|
|
|
|
end
|
|
|
|
|
DEV: Replace `params` by the contract object in services
This patch replaces the parameters provided to a service through
`params` by the contract object.
That way, it allows better consistency when accessing input params. For
example, if you have a service without a contract, to access a
parameter, you need to use `params[:my_parameter]`. But with a contract,
you do this through `contract.my_parameter`. Now, with this patch,
you’ll be able to access it through `params.my_parameter` or
`params[:my_parameter]`.
Some methods have been added to the contract object to better mimic a
Hash. That way, when accessing/using `params`, you don’t have to think
too much about it:
- `params.my_key` is also accessible through `params[:my_key]`.
- `params.my_key = value` can also be done through `params[:my_key] =
value`.
- `#slice` and `#merge` are available.
- `#to_hash` has been implemented, so the contract object will be
automatically cast as a hash by Ruby depending on the context. For
example, with an AR model, you can do this: `user.update(**params)`.
2024-10-23 11:57:48 -04:00
|
|
|
def fetch_flag(params:)
|
2024-10-28 12:21:59 -04:00
|
|
|
Flag.find_by(id: params.flag_id)
|
2024-10-17 11:15:35 -04:00
|
|
|
end
|
|
|
|
|
2024-05-29 00:39:58 -04:00
|
|
|
def toggle(flag:)
|
|
|
|
flag.update!(enabled: !flag.enabled)
|
|
|
|
end
|
|
|
|
|
|
|
|
def log(guardian:, flag:)
|
|
|
|
StaffActionLogger.new(guardian.user).log_custom(
|
|
|
|
"toggle_flag",
|
|
|
|
{ flag: flag.name, enabled: flag.enabled },
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|