2024-05-29 12:39:58 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Admin::Config::FlagsController < Admin::AdminController
|
|
|
|
def toggle
|
2024-10-02 22:07:57 +08:00
|
|
|
Flags::ToggleFlag.call(service_params) do
|
2024-05-29 12:39:58 +08:00
|
|
|
on_success do
|
|
|
|
Discourse.request_refresh!
|
|
|
|
render(json: success_json)
|
|
|
|
end
|
|
|
|
on_failure { render(json: failed_json, status: 422) }
|
|
|
|
on_model_not_found(:message) { raise Discourse::NotFound }
|
|
|
|
on_failed_policy(:invalid_access) { raise Discourse::InvalidAccess }
|
|
|
|
on_failed_contract do |contract|
|
|
|
|
render(json: failed_json.merge(errors: contract.errors.full_messages), status: 400)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def index
|
|
|
|
end
|
2024-06-05 11:27:06 +08:00
|
|
|
|
2024-07-03 06:45:37 +08:00
|
|
|
def new
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2024-10-02 22:07:57 +08:00
|
|
|
Flags::CreateFlag.call(service_params) do
|
DEV: Stop injecting a service result object in the caller object
Currently, when calling a service with its block form, a `#result`
method is automatically created on the caller object. Even if it never
clashed so far, this could happen.
This patch removes that method, and instead use a more classical way of
doing things: the result object is now provided as an argument to the
main block. This means if we need to access the result object in an
outcome block, it will be done like this from now on:
```ruby
MyService.call(params) do |result|
on_success do
# do something with the result object
do_something(result)
end
end
```
In the same vein, this patch introduces the ability to match keys from
the result object in the outcome blocks, like we already do with step
definitions in a service. For example:
```ruby
on_success do |model:, contract:|
do_something(model, contract)
end
```
Instead of
```ruby
on_success do
do_something(result.model, result.contract)
end
```
2024-10-21 21:37:02 +08:00
|
|
|
on_success do |flag:|
|
2024-07-03 06:45:37 +08:00
|
|
|
Discourse.request_refresh!
|
DEV: Stop injecting a service result object in the caller object
Currently, when calling a service with its block form, a `#result`
method is automatically created on the caller object. Even if it never
clashed so far, this could happen.
This patch removes that method, and instead use a more classical way of
doing things: the result object is now provided as an argument to the
main block. This means if we need to access the result object in an
outcome block, it will be done like this from now on:
```ruby
MyService.call(params) do |result|
on_success do
# do something with the result object
do_something(result)
end
end
```
In the same vein, this patch introduces the ability to match keys from
the result object in the outcome blocks, like we already do with step
definitions in a service. For example:
```ruby
on_success do |model:, contract:|
do_something(model, contract)
end
```
Instead of
```ruby
on_success do
do_something(result.model, result.contract)
end
```
2024-10-21 21:37:02 +08:00
|
|
|
render json: flag, serializer: FlagSerializer, used_flag_ids: Flag.used_flag_ids
|
2024-07-03 06:45:37 +08:00
|
|
|
end
|
|
|
|
on_failure { render(json: failed_json, status: 422) }
|
|
|
|
on_failed_policy(:invalid_access) { raise Discourse::InvalidAccess }
|
2024-09-30 07:17:19 +08:00
|
|
|
on_failed_policy(:unique_name) { render_json_error(I18n.t("flags.errors.unique_name")) }
|
2024-07-03 06:45:37 +08:00
|
|
|
on_failed_contract do |contract|
|
|
|
|
render(json: failed_json.merge(errors: contract.errors.full_messages), status: 400)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2024-10-02 22:07:57 +08:00
|
|
|
Flags::UpdateFlag.call(service_params) do
|
DEV: Stop injecting a service result object in the caller object
Currently, when calling a service with its block form, a `#result`
method is automatically created on the caller object. Even if it never
clashed so far, this could happen.
This patch removes that method, and instead use a more classical way of
doing things: the result object is now provided as an argument to the
main block. This means if we need to access the result object in an
outcome block, it will be done like this from now on:
```ruby
MyService.call(params) do |result|
on_success do
# do something with the result object
do_something(result)
end
end
```
In the same vein, this patch introduces the ability to match keys from
the result object in the outcome blocks, like we already do with step
definitions in a service. For example:
```ruby
on_success do |model:, contract:|
do_something(model, contract)
end
```
Instead of
```ruby
on_success do
do_something(result.model, result.contract)
end
```
2024-10-21 21:37:02 +08:00
|
|
|
on_success do |flag:|
|
2024-07-03 06:45:37 +08:00
|
|
|
Discourse.request_refresh!
|
DEV: Stop injecting a service result object in the caller object
Currently, when calling a service with its block form, a `#result`
method is automatically created on the caller object. Even if it never
clashed so far, this could happen.
This patch removes that method, and instead use a more classical way of
doing things: the result object is now provided as an argument to the
main block. This means if we need to access the result object in an
outcome block, it will be done like this from now on:
```ruby
MyService.call(params) do |result|
on_success do
# do something with the result object
do_something(result)
end
end
```
In the same vein, this patch introduces the ability to match keys from
the result object in the outcome blocks, like we already do with step
definitions in a service. For example:
```ruby
on_success do |model:, contract:|
do_something(model, contract)
end
```
Instead of
```ruby
on_success do
do_something(result.model, result.contract)
end
```
2024-10-21 21:37:02 +08:00
|
|
|
render json: flag, serializer: FlagSerializer, used_flag_ids: Flag.used_flag_ids
|
2024-07-03 06:45:37 +08:00
|
|
|
end
|
|
|
|
on_failure { render(json: failed_json, status: 422) }
|
|
|
|
on_model_not_found(:message) { raise Discourse::NotFound }
|
|
|
|
on_failed_policy(:not_system) { render_json_error(I18n.t("flags.errors.system")) }
|
|
|
|
on_failed_policy(:not_used) { render_json_error(I18n.t("flags.errors.used")) }
|
|
|
|
on_failed_policy(:invalid_access) { raise Discourse::InvalidAccess }
|
2024-09-30 07:17:19 +08:00
|
|
|
on_failed_policy(:unique_name) { render_json_error(I18n.t("flags.errors.unique_name")) }
|
2024-07-03 06:45:37 +08:00
|
|
|
on_failed_contract do |contract|
|
|
|
|
render(json: failed_json.merge(errors: contract.errors.full_messages), status: 400)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-06-05 11:27:06 +08:00
|
|
|
def reorder
|
2024-10-02 22:07:57 +08:00
|
|
|
Flags::ReorderFlag.call(service_params) do
|
2024-06-05 11:27:06 +08:00
|
|
|
on_success do
|
|
|
|
Discourse.request_refresh!
|
|
|
|
render(json: success_json)
|
|
|
|
end
|
|
|
|
on_failure { render(json: failed_json, status: 422) }
|
|
|
|
on_model_not_found(:message) { raise Discourse::NotFound }
|
|
|
|
on_failed_policy(:invalid_access) { raise Discourse::InvalidAccess }
|
|
|
|
on_failed_policy(:invalid_move) { render_json_error(I18n.t("flags.errors.wrong_move")) }
|
|
|
|
on_failed_contract do |contract|
|
|
|
|
render(json: failed_json.merge(errors: contract.errors.full_messages), status: 400)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2024-07-03 06:45:37 +08:00
|
|
|
|
|
|
|
def destroy
|
2024-10-02 22:07:57 +08:00
|
|
|
Flags::DestroyFlag.call(service_params) do
|
2024-07-03 06:45:37 +08:00
|
|
|
on_success do
|
|
|
|
Discourse.request_refresh!
|
|
|
|
render(json: success_json)
|
|
|
|
end
|
|
|
|
on_failure { render(json: failed_json, status: 422) }
|
|
|
|
on_failed_policy(:not_system) { render_json_error(I18n.t("flags.errors.system")) }
|
|
|
|
on_failed_policy(:not_used) { render_json_error(I18n.t("flags.errors.used")) }
|
|
|
|
on_failed_policy(:invalid_access) { raise Discourse::InvalidAccess }
|
|
|
|
on_failed_contract do |contract|
|
|
|
|
render(json: failed_json.merge(errors: contract.errors.full_messages), status: 400)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2024-05-29 12:39:58 +08:00
|
|
|
end
|