OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
refactor: Simplify logic to validate params
Christophe Bliard0e634e24d911
diff --git a/app/controllers/journals_controller.rb b/app/controllers/journals_controller.rbindex 4d7d0418053..e5e37940199 100644--- a/app/controllers/journals_controller.rb+++ b/app/controllers/journals_controller.rb@@ -61,21 +61,14 @@ class JournalsController < ApplicationControllerenddef diff- field = params[:field].parameterize.underscore.to_sym+ return render_404 unless valid_field?- unless valid_diff?- return render_404+ unless @journal.details[field_param] in [from, to]+ return render_400 message: I18n.t(:error_journal_attribute_not_present, attribute: field_param)end- unless @journal.details[field].is_a?(Array)- return render_400 message: I18n.t(:error_journal_attribute_not_present, attribute: field)- end-- from = @journal.details[field][0]- to = @journal.details[field][1]-@diff = Redmine::Helpers::Diff.new(to, from)- @journable = @journal.journable+respond_to do |format|format.htmlformat.js do@@ -88,7 +81,8 @@ class JournalsController < ApplicationControllerdef find_journal@journal = Journal.find(params[:id])- @project = @journal.journable.project+ @journable = @journal.journable+ @project = @journable.projectrescue ActiveRecord::RecordNotFoundrender_404end@@ -102,17 +96,18 @@ class JournalsController < ApplicationControllerdo_authorize(permission)end- # Is this a valid field for diff'ing?- def valid_field?(field)- field.to_s.strip == 'description'+ def field_param+ @field_param ||= params[:field].parameterize.underscoreend- def valid_diff?- valid_field?(params[:field]) &&- (@journal.journable.instance_of?(WorkPackage) || @journal.journable.instance_of?(Project))+ # Is this a valid field for diff'ing?+ def valid_field?+ field_param == 'description'enddef journals_index_title- (@project ? @project.name : Setting.app_title) + ': ' + (@query.new_record? ? I18n.t(:label_changes_details) : @query.name)+ subject = @project ? @project.name : Setting.app_title+ query_name = @query.new_record? ? I18n.t(:label_changes_details) : @query.name+ "#{subject}: #{query_name}"endenddiff --git a/spec/controllers/journals_controller_spec.rb b/spec/controllers/journals_controller_spec.rbindex 02ec1e59785..0dc572cb271 100644--- a/spec/controllers/journals_controller_spec.rb+++ b/spec/controllers/journals_controller_spec.rb@@ -126,6 +126,17 @@ describe JournalsController doendend+ context 'for another field than description' do+ shared_let(:work_package) do+ create(:work_package, type: project.types.first,+ author: user,+ project:)+ end+ let(:params) { { id: work_package.last_journal.id.to_s, field: :another_field, format: 'js' } }++ it { expect(response).to have_http_status(:not_found) }+ end+context 'for other types, like forum message' doshared_let(:forum) { create(:forum, project:) }shared_let(:message) { create(:message, forum:, content: 'initial content') }