|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +#-- copyright |
| 4 | +# OpenProject is an open source project management software. |
| 5 | +# Copyright (C) the OpenProject GmbH |
| 6 | +# |
| 7 | +# This program is free software; you can redistribute it and/or |
| 8 | +# modify it under the terms of the GNU General Public License version 3. |
| 9 | +# |
| 10 | +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: |
| 11 | +# Copyright (C) 2006-2013 Jean-Philippe Lang |
| 12 | +# Copyright (C) 2010-2013 the ChiliProject Team |
| 13 | +# |
| 14 | +# This program is free software; you can redistribute it and/or |
| 15 | +# modify it under the terms of the GNU General Public License |
| 16 | +# as published by the Free Software Foundation; either version 2 |
| 17 | +# of the License, or (at your option) any later version. |
| 18 | +# |
| 19 | +# This program is distributed in the hope that it will be useful, |
| 20 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | +# GNU General Public License for more details. |
| 23 | +# |
| 24 | +# You should have received a copy of the GNU General Public License |
| 25 | +# along with this program; if not, write to the Free Software |
| 26 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 27 | +# |
| 28 | +# See COPYRIGHT and LICENSE files for more details. |
| 29 | +#++ |
| 30 | + |
| 31 | +class InplaceEditFieldsController < ApplicationController |
| 32 | + include OpTurbo::ComponentStream |
| 33 | + |
| 34 | + before_action :find_model |
| 35 | + before_action :set_attribute |
| 36 | + no_authorization_required! :update |
| 37 | + |
| 38 | + def update |
| 39 | + handler = OpenProject::InplaceEdit::UpdateRegistry.fetch(@model) |
| 40 | + |
| 41 | + success = handler.call( |
| 42 | + model: @model, |
| 43 | + attribute: @attribute, |
| 44 | + params: permitted_params, |
| 45 | + user: current_user |
| 46 | + ) |
| 47 | + |
| 48 | + if success |
| 49 | + render_success_flash_message_via_turbo_stream( |
| 50 | + message: I18n.t(:notice_successful_update) |
| 51 | + ) |
| 52 | + end |
| 53 | + |
| 54 | + replace_via_turbo_stream( |
| 55 | + component:, |
| 56 | + status: success ? :ok : :unprocessable_entity |
| 57 | + ) |
| 58 | + |
| 59 | + respond_with_turbo_streams |
| 60 | + end |
| 61 | + |
| 62 | + private |
| 63 | + |
| 64 | + def find_model |
| 65 | + @model = |
| 66 | + params[:model] |
| 67 | + .constantize |
| 68 | + .find(params[:id]) |
| 69 | + rescue NameError, ActiveRecord::RecordNotFound |
| 70 | + head :not_found |
| 71 | + end |
| 72 | + |
| 73 | + def set_attribute |
| 74 | + @attribute = params[:attribute].to_sym |
| 75 | + end |
| 76 | + |
| 77 | + def permitted_params |
| 78 | + params |
| 79 | + .expect(@model.model_name.param_key => [@attribute]) |
| 80 | + end |
| 81 | + |
| 82 | + def component |
| 83 | + OpenProject::Common::InplaceEditFieldComponent.new( |
| 84 | + model: @model, |
| 85 | + attribute: @attribute |
| 86 | + ) |
| 87 | + end |
| 88 | +end |
0 commit comments