Skip to content

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

Use create service

Oliver Günther473f6c77baaa
diff --git a/app/services/base_services/write.rb b/app/services/base_services/write.rb
index cc01e4e7a11..6012448a2a0 100644
--- a/app/services/base_services/write.rb
+++ b/app/services/base_services/write.rb
@@ -30,7 +30,7 @@
module BaseServices
class Write < BaseContracted
- private
+ protected
def persist(service_result)
service_result = super(service_result)
diff --git a/modules/ifc_models/app/contracts/ifc_models/base_contract.rb b/modules/ifc_models/app/contracts/ifc_models/base_contract.rb
new file mode 100644
index 00000000000..1686353cf79
--- /dev/null
+++ b/modules/ifc_models/app/contracts/ifc_models/base_contract.rb
@@ -0,0 +1,62 @@
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2017 the OpenProject Foundation (OPF)
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2017 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See doc/COPYRIGHT.rdoc for more details.
+#++
+
+module IFCModels
+ class BaseContract < ::ModelContract
+ delegate :project,
+ :new_record?,
+ to: :model
+
+ attribute :title
+ attribute :project
+ attribute :uploader
+
+ def self.model
+ ::IFCModels::IFCModel
+ end
+
+ def validate
+ user_allowed_to_manage
+ user_is_uploader
+
+ super
+ end
+
+ def user_allowed_to_manage
+ if model.project && !user.allowed_to?(:manage_ifc_models, model.project)
+ errors.add :base, :error_unauthorized
+ end
+ end
+
+ def user_is_uploader
+ unless model.uploader == user
+ errors.add :base, :error_unauthorized
+ end
+ end
+ end
+end
diff --git a/modules/ifc_models/app/contracts/ifc_models/create_contract.rb b/modules/ifc_models/app/contracts/ifc_models/create_contract.rb
new file mode 100644
index 00000000000..ce4894fe6bc
--- /dev/null
+++ b/modules/ifc_models/app/contracts/ifc_models/create_contract.rb
@@ -0,0 +1,32 @@
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2017 the OpenProject Foundation (OPF)
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2017 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See doc/COPYRIGHT.rdoc for more details.
+#++
+
+module IFCModels
+ class CreateContract < BaseContract
+ end
+end
diff --git a/modules/ifc_models/app/controllers/ifc_models/ifc_models_controller.rb b/modules/ifc_models/app/controllers/ifc_models/ifc_models_controller.rb
index 1d6c3494c30..09fdb77bbc0 100644
--- a/modules/ifc_models/app/controllers/ifc_models/ifc_models_controller.rb
+++ b/modules/ifc_models/app/controllers/ifc_models/ifc_models_controller.rb
@@ -47,7 +47,7 @@ module ::IFCModels
end
def new
- @ifc_model = @project.ifc_models.build(ifc_model_params)
+ @ifc_model = @project.ifc_models.build
end
def edit; end
@@ -55,17 +55,27 @@ module ::IFCModels
def show; end
def create
- @ifc_model = @project.ifc_models.build(uploader: User.current)
+ combined_params = permitted_model_params
+ .to_h
+ .reverse_merge(project: @project)
- if add_attachment && @ifc_model.save
+ call = ::IFCModels::CreateService
+ .new(user: current_user)
+ .call(combined_params)
+
+ @ifc_model = call.result
+
+ if call.success?
+ flash[:notice] = t(:notice_successful_create)
redirect_to action: :show, id: @ifc_model.id
else
+ @errors = call.errors
render action: :new
end
end
def update
- if @ifc_model.update(ifc_model_params)
+ if @ifc_model.update(permitted_params)
redirect_to action: :show, id: @ifc_model.id
else
render action: :edit
@@ -79,18 +89,10 @@ module ::IFCModels
private
- def add_attachment
- if (file = ifc_model_params['ifc_attachment']) && file && file.size.positive?
- @ifc_model.ifc_attachment = file
- @ifc_model.title = file.original_filename
- else
- flash[:error] = t('ifc_models.could_not_save_file')
- false
- end
- end
-
- def ifc_model_params
- params.fetch(:ifc_models_ifc_model, {}).permit('title', 'ifc_attachment')
+ def permitted_model_params
+ params
+ .fetch(:ifc_models_ifc_model, {})
+ .permit('title', 'ifc_attachment')
end
def find_ifc_model_object
diff --git a/modules/ifc_models/app/models/ifc_models/ifc_model.rb b/modules/ifc_models/app/models/ifc_models/ifc_model.rb
index 2bcf02c8e0b..3e869da2fbe 100644
--- a/modules/ifc_models/app/models/ifc_models/ifc_model.rb
+++ b/modules/ifc_models/app/models/ifc_models/ifc_model.rb
@@ -22,6 +22,10 @@ module IFCModels
end
end
+ def converted?
+ xkt_attachment && metadata_attachment
+ end
+
private
##
diff --git a/modules/ifc_models/app/services/ifc_models/create_service.rb b/modules/ifc_models/app/services/ifc_models/create_service.rb
new file mode 100644
index 00000000000..12add886508
--- /dev/null
+++ b/modules/ifc_models/app/services/ifc_models/create_service.rb
@@ -0,0 +1,62 @@
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2017 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#+
+
+module IFCModels
+ class CreateService < ::BaseServices::Create
+
+ protected
+
+ def before_perform(params)
+ ifc_attachment = params.delete('ifc_attachment')
+
+ super(params).tap do |result|
+ result.success = replace_attachment(result, ifc_attachment)
+ end
+ end
+
+ def instance(_params)
+ ::IFCModels::IFCModel.new
+ end
+
+ ##
+ # Replace the IFC attachment file after saving
+ def replace_attachment(result, file)
+ return unless result.success?
+
+ model = result.result
+ if file && file.size.positive?
+ model.ifc_attachment = file
+ model.title = file.original_filename
+ model.save
+ else
+ result.errors.add(:ifc_attachment, t('ifc_models.could_not_save_file'))
+ false
+ end
+ end
+ end
+end
diff --git a/modules/ifc_models/app/services/ifc_models/set_attributes_service.rb b/modules/ifc_models/app/services/ifc_models/set_attributes_service.rb
new file mode 100644
index 00000000000..d74bdd454d3
--- /dev/null
+++ b/modules/ifc_models/app/services/ifc_models/set_attributes_service.rb
@@ -0,0 +1,37 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2017 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+
+module IFCModels
+ class SetAttributesService < ::BaseServices::SetAttributes
+ def set_default_attributes(_params)
+ model.uploader = user
+ end
+ end
+end
diff --git a/modules/ifc_models/app/views/ifc_models/ifc_models/edit.html.erb b/modules/ifc_models/app/views/ifc_models/ifc_models/edit.html.erb
index d8d327fe7e1..4901c91bfd6 100644
--- a/modules/ifc_models/app/views/ifc_models/ifc_models/edit.html.erb
+++ b/modules/ifc_models/app/views/ifc_models/ifc_models/edit.html.erb
@@ -33,6 +33,7 @@ See doc/COPYRIGHT.rdoc for more details.
<%= toolbar title: IFCModels::IFCModel.model_name.human %>
<%= labelled_tabular_form_for @ifc_model, url: ifc_models_project_ifc_model_path(@project, @ifc_model), method: 'PATCH' do |f| -%>
+ <%= error_messages_for_contract @ifc_model, @errors %>
<%= render partial: 'form', locals: { f: f } %>
<%= styled_button_tag t(:button_save), class: "-highlight -with-icon icon-checkmark" %>
<% end %>
diff --git a/modules/ifc_models/app/views/ifc_models/ifc_models/new.html.erb b/modules/ifc_models/app/views/ifc_models/ifc_models/new.html.erb
index db0a64bdd16..0d1fd61bd75 100644
--- a/modules/ifc_models/app/views/ifc_models/ifc_models/new.html.erb
+++ b/modules/ifc_models/app/views/ifc_models/ifc_models/new.html.erb
@@ -32,6 +32,7 @@ See doc/COPYRIGHT.rdoc for more details.
<%= toolbar title: t('ifc_models.label_new_ifc_model') %>
<%= labelled_tabular_form_for @ifc_model, url: ifc_models_project_ifc_models_path(@project), html: { multipart: true } do |f| -%>
+ <%= error_messages_for_contract @ifc_model, @errors %>
<%= render :partial => 'form', locals: { f: f } %>
<%= styled_button_tag l(:button_create), class: "-highlight -with-icon icon-checkmark" %>
<% end %>
diff --git a/modules/ifc_models/app/views/ifc_models/ifc_models/show.html.erb b/modules/ifc_models/app/views/ifc_models/ifc_models/show.html.erb
index c8d29b391a5..3a4c0845a8c 100644
--- a/modules/ifc_models/app/views/ifc_models/ifc_models/show.html.erb
+++ b/modules/ifc_models/app/views/ifc_models/ifc_models/show.html.erb
@@ -54,9 +54,10 @@ See doc/COPYRIGHT.rdoc for more details.
<% end %>
<% end %>
+<% if @ifc_model.converted? %>
<div class="ifc-model-viewer-container">
<ifc-viewer-entry data-ifc-model-id="<%= @ifc_model.id %>"
data-xkt-file-url="<%= attachment_content_url(@ifc_model.xkt_attachment) %>"
data-metadata-file-url="<%= attachment_content_url(@ifc_model.metadata_attachment) %>"></ifc-viewer-entry>
</div>
-
+<% end %>