mirror of
https://github.com/discourse/discourse.git
synced 2024-12-24 11:13:48 +08:00
f79dd5c8b5
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 ```
93 lines
3.3 KiB
Ruby
93 lines
3.3 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class Admin::Config::FlagsController < Admin::AdminController
|
|
def toggle
|
|
Flags::ToggleFlag.call(service_params) do
|
|
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
|
|
|
|
def new
|
|
end
|
|
|
|
def edit
|
|
end
|
|
|
|
def create
|
|
Flags::CreateFlag.call(service_params) do
|
|
on_success do |flag:|
|
|
Discourse.request_refresh!
|
|
render json: flag, serializer: FlagSerializer, used_flag_ids: Flag.used_flag_ids
|
|
end
|
|
on_failure { render(json: failed_json, status: 422) }
|
|
on_failed_policy(:invalid_access) { raise Discourse::InvalidAccess }
|
|
on_failed_policy(:unique_name) { render_json_error(I18n.t("flags.errors.unique_name")) }
|
|
on_failed_contract do |contract|
|
|
render(json: failed_json.merge(errors: contract.errors.full_messages), status: 400)
|
|
end
|
|
end
|
|
end
|
|
|
|
def update
|
|
Flags::UpdateFlag.call(service_params) do
|
|
on_success do |flag:|
|
|
Discourse.request_refresh!
|
|
render json: flag, serializer: FlagSerializer, used_flag_ids: Flag.used_flag_ids
|
|
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 }
|
|
on_failed_policy(:unique_name) { render_json_error(I18n.t("flags.errors.unique_name")) }
|
|
on_failed_contract do |contract|
|
|
render(json: failed_json.merge(errors: contract.errors.full_messages), status: 400)
|
|
end
|
|
end
|
|
end
|
|
|
|
def reorder
|
|
Flags::ReorderFlag.call(service_params) do
|
|
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
|
|
|
|
def destroy
|
|
Flags::DestroyFlag.call(service_params) do
|
|
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
|
|
end
|