Skip to content

Commit

Permalink
Add API endpoints for news
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverguenther committed Jun 22, 2024
1 parent ef0d225 commit 664efc1
Show file tree
Hide file tree
Showing 35 changed files with 1,645 additions and 7 deletions.
9 changes: 9 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,15 @@ RSpec/DescribeClass:
- 'spec/features/**/*.rb'
- 'modules/*/spec/features/**/*.rb'

# Allow number HTTP status codes in specs
RSpecRails/HttpStatus:
Enabled: false

# have_http_status does not exist on MockResponse
# using it would yield "expected a response object, but an instance of Rack::MockResponse was received"
RSpecRails/HaveHttpStatus:
Enabled: false

# dynamic finders cop clashes with capybara ID cop
Rails/DynamicFindBy:
Enabled: true
Expand Down
50 changes: 50 additions & 0 deletions app/contracts/news/base_contract.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2024 the OpenProject GmbH
#
# 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-2013 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 COPYRIGHT and LICENSE files for more details.
#++

class News::BaseContract < ModelContract
include Attachments::ValidateReplacements

validate :allowed_to_manage

def self.model
News
end

attribute :project
attribute :title
attribute :summary
attribute :description

def allowed_to_manage
return if model.project.nil?

unless user.allowed_in_project?(:manage_news, model.project)
errors.add :base, :error_unauthorized
end
end
end
30 changes: 30 additions & 0 deletions app/contracts/news/create_contract.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2024 the OpenProject GmbH
#
# 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-2013 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 COPYRIGHT and LICENSE files for more details.
#++

class News::CreateContract < News::BaseContract
end
31 changes: 31 additions & 0 deletions app/contracts/news/delete_contract.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2024 the OpenProject GmbH
#
# 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-2013 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 COPYRIGHT and LICENSE files for more details.
#++

class News::DeleteContract < ::DeleteContract
delete_permission :manage_news
end
30 changes: 30 additions & 0 deletions app/contracts/news/update_contract.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2024 the OpenProject GmbH
#
# 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-2013 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 COPYRIGHT and LICENSE files for more details.
#++

class News::UpdateContract < News::BaseContract
end
29 changes: 22 additions & 7 deletions app/controllers/news_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,29 +73,44 @@ def new
def edit; end

def create
@news = News.new(project: @project, author: User.current)
@news.attributes = permitted_params.news
if @news.save
call = News::CreateService
.new(user: current_user)
.call(permitted_params.news.merge(project: @project))

if call.success?
flash[:notice] = I18n.t(:notice_successful_create)
redirect_to controller: "/news", action: "index", project_id: @project
else
@news = call.result
render action: "new"
end
end

def update
@news.attributes = permitted_params.news
if @news.save
call = News::UpdateService
.new(model: @news, user: current_user)
.call(permitted_params.news.merge(project: @project))

if call.success?
flash[:notice] = I18n.t(:notice_successful_update)
redirect_to action: "show", id: @news
else
@news = call.result
render action: "edit"
end
end

def destroy
@news.destroy
flash[:notice] = I18n.t(:notice_successful_delete)
call = News::DeleteService
.new(model: @news, user: current_user)
.call

if call.success?
flash[:notice] = I18n.t(:notice_successful_delete)
else
call.apply_flash_message!(flash)
end

redirect_to action: "index", project_id: @project
end

Expand Down
6 changes: 6 additions & 0 deletions app/models/news.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class News < ApplicationRecord
order(:created_at)
}, as: :commented, dependent: :delete_all

validates :project, presence: true
validates :title, presence: true
validates :title, length: { maximum: 256 }
validates :summary, length: { maximum: 255 }
Expand All @@ -46,6 +47,11 @@ class News < ApplicationRecord
references: :projects,
date_column: "#{table_name}.created_at"

acts_as_attachable view_permission: :view_news,
add_on_new_permission: :manage_news,
add_on_persisted_permission: :manage_news,
delete_permission: :manage_news

acts_as_watchable

after_create :add_author_as_watcher
Expand Down
31 changes: 31 additions & 0 deletions app/services/news/create_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2024 the OpenProject GmbH
#
# 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-2013 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 COPYRIGHT and LICENSE files for more details.
#++

class News::CreateService < BaseServices::Create
include Attachments::ReplaceAttachments
end
30 changes: 30 additions & 0 deletions app/services/news/delete_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2024 the OpenProject GmbH
#
# 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-2013 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 COPYRIGHT and LICENSE files for more details.
#++

class News::DeleteService < BaseServices::Delete
end
43 changes: 43 additions & 0 deletions app/services/news/set_attributes_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2024 the OpenProject GmbH
#
# 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-2013 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 COPYRIGHT and LICENSE files for more details.
#++

class News::SetAttributesService < BaseServices::SetAttributes
include Attachments::SetReplacements

private

def set_default_attributes(*)
set_default_author
end

def set_default_author
model.change_by_system do
model.author = user
end
end
end
31 changes: 31 additions & 0 deletions app/services/news/update_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2024 the OpenProject GmbH
#
# 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-2013 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 COPYRIGHT and LICENSE files for more details.
#++

class News::UpdateService < BaseServices::Update
include Attachments::ReplaceAttachments
end
Loading

0 comments on commit 664efc1

Please sign in to comment.