2024-06-05 11:27:06 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-07-03 06:45:37 +08:00
|
|
|
class Flags::ReorderFlag
|
2024-06-05 11:27:06 +08:00
|
|
|
include Service::Base
|
|
|
|
|
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 23:57:48 +08: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 23:17:14 +08:00
|
|
|
attribute :flag_id, :integer
|
|
|
|
attribute :direction, :string
|
|
|
|
|
|
|
|
validates :flag_id, presence: true
|
2024-10-17 23:15:35 +08:00
|
|
|
validates :direction, inclusion: { in: %w[up down] }
|
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
|
|
|
end
|
2024-11-28 23:51:43 +08:00
|
|
|
|
2024-06-05 11:27:06 +08:00
|
|
|
model :flag
|
|
|
|
policy :invalid_access
|
2024-10-17 23:15:35 +08:00
|
|
|
model :all_flags
|
2024-06-05 11:27:06 +08:00
|
|
|
policy :invalid_move
|
2024-11-28 23:51:43 +08:00
|
|
|
|
2024-06-05 11:27:06 +08:00
|
|
|
transaction do
|
|
|
|
step :move
|
|
|
|
step :log
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
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 23:57:48 +08:00
|
|
|
def fetch_flag(params:)
|
2024-10-29 00:21:59 +08:00
|
|
|
Flag.find_by(id: params.flag_id)
|
2024-06-05 11:27:06 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def invalid_access(guardian:, flag:)
|
|
|
|
guardian.can_reorder_flag?(flag)
|
|
|
|
end
|
|
|
|
|
2024-10-17 23:15:35 +08:00
|
|
|
def fetch_all_flags
|
|
|
|
Flag.where.not(name_key: "notify_user").order(:position).to_a
|
2024-06-05 11:27:06 +08:00
|
|
|
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 23:57:48 +08:00
|
|
|
def invalid_move(flag:, params:, all_flags:)
|
2024-10-29 00:21:59 +08:00
|
|
|
return false if all_flags.first == flag && params.direction == "up"
|
|
|
|
return false if all_flags.last == flag && params.direction == "down"
|
2024-06-05 11:27:06 +08:00
|
|
|
true
|
|
|
|
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 23:57:48 +08:00
|
|
|
def move(flag:, params:, all_flags:)
|
2024-06-05 11:27:06 +08:00
|
|
|
old_position = flag.position
|
|
|
|
index = all_flags.index(flag)
|
2024-10-29 00:21:59 +08:00
|
|
|
target_flag = all_flags[params.direction == "up" ? index - 1 : index + 1]
|
2024-06-05 11:27:06 +08:00
|
|
|
|
|
|
|
flag.update!(position: target_flag.position)
|
|
|
|
target_flag.update!(position: old_position)
|
|
|
|
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 23:57:48 +08:00
|
|
|
def log(guardian:, flag:, params:)
|
2024-06-05 11:27:06 +08:00
|
|
|
StaffActionLogger.new(guardian.user).log_custom(
|
|
|
|
"move_flag",
|
2024-10-29 00:21:59 +08:00
|
|
|
{ flag: flag.name, direction: params.direction },
|
2024-06-05 11:27:06 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|