Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
636 changes: 636 additions & 0 deletions GRAPHITI_MIGRATION.md

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
gem 'icalendar', '~> 2.11', '>= 2.11.2'
gem 'improvmx', '~> 0.2.1'
gem 'isbn_validation', '~> 1.2', '>= 1.2.2'
gem 'jsonapi-authorization', '~> 3.0', '>= 3.0.2'
gem 'jsonapi-resources', '~> 0.9.1'
gem 'graphiti', '~> 1.7'

Check failure on line 21 in Gemfile

View workflow job for this annotation

GitHub Actions / Lint

[Correctable] Bundler/OrderedGems: Gems should be sorted in an alphabetical order within their section of the Gemfile. Gem graphiti should appear before isbn_validation.
gem 'graphiti-rails', '~> 0.4'
gem 'kaminari', '~> 1.2' # Pagination for Graphiti
gem 'message_bus', '~> 4.4', '>= 4.4.1'
gem 'mini_magick', '~> 5.3'
gem 'paper_trail', '~> 16.0'
Expand Down
59 changes: 50 additions & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,23 @@ GEM
dotenv (= 3.1.8)
railties (>= 6.1)
drb (2.2.1)
dry-core (1.1.0)
concurrent-ruby (~> 1.0)
logger
zeitwerk (~> 2.6)
dry-inflector (1.2.0)
dry-logic (1.6.0)
bigdecimal
concurrent-ruby (~> 1.0)
dry-core (~> 1.1)
zeitwerk (~> 2.6)
dry-types (1.8.3)
bigdecimal (~> 3.0)
concurrent-ruby (~> 1.0)
dry-core (~> 1.0)
dry-inflector (~> 1.0)
dry-logic (~> 1.4)
zeitwerk (~> 2.6)
erubi (1.13.1)
et-orbi (1.2.11)
tzinfo
Expand Down Expand Up @@ -172,6 +189,19 @@ GEM
ruby-progressbar (~> 1.4)
globalid (1.2.1)
activesupport (>= 6.1)
graphiti (1.8.2)
activesupport (>= 5.2)
concurrent-ruby (>= 1.2, < 2.0)
dry-types (>= 0.15.0, < 2.0)
graphiti_errors (~> 1.1.0)
jsonapi-renderer (~> 0.2, >= 0.2.2)
jsonapi-serializable (~> 0.3.0)
graphiti-rails (0.4.1)
graphiti (~> 1.2)
railties (>= 5.0)
rescue_registry (~> 1.0)
graphiti_errors (1.1.2)
jsonapi-serializable (~> 0.1)
guard (2.19.1)
formatador (>= 0.2.4)
listen (>= 2.7, < 4.0)
Expand Down Expand Up @@ -223,13 +253,21 @@ GEM
jar-dependencies (0.5.5)
json (2.10.2)
json (2.10.2-java)
jsonapi-authorization (3.0.2)
jsonapi-resources (~> 0.9.0)
pundit (>= 1.0.0, < 3.0.0)
jsonapi-resources (0.9.12)
activerecord (>= 4.1)
concurrent-ruby
railties (>= 4.1)
jsonapi-renderer (0.2.2)
jsonapi-serializable (0.3.1)
jsonapi-renderer (~> 0.2.0)
kaminari (1.2.2)
activesupport (>= 4.1.0)
kaminari-actionview (= 1.2.2)
kaminari-activerecord (= 1.2.2)
kaminari-core (= 1.2.2)
kaminari-actionview (1.2.2)
actionview
kaminari-core (= 1.2.2)
kaminari-activerecord (1.2.2)
activerecord
kaminari-core (= 1.2.2)
kaminari-core (1.2.2)
language_server-protocol (3.17.0.4)
lint_roller (1.1.0)
listen (3.9.0)
Expand Down Expand Up @@ -396,6 +434,8 @@ GEM
io-console (~> 0.5)
request_store (1.7.0)
rack (>= 1.4)
rescue_registry (1.0.0)
activesupport (>= 5.0)
rest-client (2.1.0)
http-accept (>= 1.7.0, < 2.0)
http-cookie (>= 1.0.2, < 2.0)
Expand Down Expand Up @@ -594,14 +634,15 @@ DEPENDENCIES
faker (~> 3.5, >= 3.5.2)
friendly_id (~> 5.5, >= 5.5.1)
fuubar (~> 2.5, >= 2.5.1)
graphiti (~> 1.7)
graphiti-rails (~> 0.4)
guard-rspec (~> 4.7, >= 4.7.3)
http (~> 5.3, >= 5.3.1)
iban-tools (~> 1.2.1)
icalendar (~> 2.11, >= 2.11.2)
improvmx (~> 0.2.1)
isbn_validation (~> 1.2, >= 1.2.2)
jsonapi-authorization (~> 3.0, >= 3.0.2)
jsonapi-resources (~> 0.9.1)
kaminari (~> 1.2)
listen (~> 3.9)
message_bus (~> 4.4, >= 4.4.1)
mina (~> 1.2, >= 1.2.5)
Expand Down
12 changes: 3 additions & 9 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
class ApplicationController < JSONAPI::ResourceController
# frozen_string_literal: true

class ApplicationController < ActionController::API
include Pundit::Authorization

before_action :set_paper_trail_whodunnit
before_action :set_sentry_context
protect_from_forgery with: :null_session
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized

# Disable content_type check, since it makes testing practically impossible
# and does not add any layers of usefulness or security.
# TODO: Remove when JR docs include instructions on rspec with this header
def verify_content_type_header
true
end

def pundit_user
current_user || current_application
end
Expand Down
82 changes: 82 additions & 0 deletions app/controllers/concerns/graphiti_crud.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# frozen_string_literal: true

# Provides standard Graphiti CRUD actions for controllers
# Include this module to get index, show, create, update, destroy actions
module GraphitiCrud
extend ActiveSupport::Concern

included do
class_attribute :resource_class
end

class_methods do
def graphiti_resource(klass)
self.resource_class = klass
end
end

def index
Graphiti.with_context(context, action_name.to_sym) do
resources = resource_class.all(params)
render json: resources.to_jsonapi
end
end

def show
Graphiti.with_context(context, action_name.to_sym) do
resource = resource_class.find(params)
render json: resource.to_jsonapi
end
end

def create
Graphiti.with_context(context, action_name.to_sym) do
resource = resource_class.build(params)

if resource.save
render json: resource.to_jsonapi, status: :created
else
render json: resource.errors.to_jsonapi, status: :unprocessable_entity
end
end
end

def update
Graphiti.with_context(context, action_name.to_sym) do
resource = resource_class.find(params)

if resource.update_attributes
render json: resource.to_jsonapi
else
render json: resource.errors.to_jsonapi, status: :unprocessable_entity
end
end
end

def destroy
Graphiti.with_context(context, action_name.to_sym) do
resource = resource_class.find(params)

if resource.destroy
head :no_content
else
render json: resource.errors.to_jsonapi, status: :unprocessable_entity
end
end
end

private

def resource_class
self.class.resource_class || infer_resource_class
end

def infer_resource_class
# Infer from controller name: V1::UsersController -> V1::UserResource
controller_name = self.class.name
resource_name = controller_name

Check failure on line 77 in app/controllers/concerns/graphiti_crud.rb

View workflow job for this annotation

GitHub Actions / Lint

Style/StringConcatenation: Prefer string interpolation to string concatenation. (https://rubystyle.guide#string-interpolation)
.sub(/Controller$/, '')
.singularize + 'Resource'
resource_name.constantize
end
end
5 changes: 5 additions & 0 deletions app/controllers/v1/activities_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
require 'icalendar/tzinfo'

class V1::ActivitiesController < V1::ApplicationController
include GraphitiCrud

before_action :doorkeeper_authorize!, except: %i[index show ical]
before_action :set_model, only: %i[generate_alias]

graphiti_resource V1::ActivityResource

def generate_alias
authorize @model

Expand Down Expand Up @@ -98,6 +102,7 @@
return false unless @user

@user.permission?(:read, Activity)

Check failure on line 105 in app/controllers/v1/activities_controller.rb

View workflow job for this annotation

GitHub Actions / Lint

[Correctable] Layout/EmptyLinesAroundMethodBody: Extra empty line detected at method body end. (https://rubystyle.guide#empty-lines-around-bodies)
end

def ical_add_birthdays?(requested_categories)
Expand Down
17 changes: 5 additions & 12 deletions app/controllers/v1/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# frozen_string_literal: true

class V1::ApplicationController < ApplicationController
before_action :doorkeeper_authorize!
rescue_from AmberError::NotMemberOfGroupError, with: :user_is_not_member_of_group_error

# Graphiti context - passed to resources
def context
{ user: current_user, application: current_application, action: params[:action] }
end
Expand All @@ -17,20 +20,10 @@ def set_model
head :not_found
end

def base_url
# When request is incoming via Ember proxy you want to add /api in the base_url
result = super
return result if result.include? '3000'

"#{result}/api"
end

def permitted_serializable_attributes
@permitted_serializable_attributes ||= begin
permitted_serializable_attributes = resource_klass.new(model_class.new,
context).fetchable_fields
attrs = params[:attrs].presence || 'id'
permitted_serializable_attributes & attrs.split(',').map(&:to_sym)
attrs.split(',').map(&:to_sym)
end
end

Expand All @@ -43,7 +36,7 @@ def user_is_not_member_of_group_error # rubocop:disable Metrics/MethodLength
source: {
pointer: '/data/relationships/group'
},
status: 422
status: '422'
}]
}, status: :unprocessable_entity
end
Expand Down
8 changes: 7 additions & 1 deletion app/controllers/v1/article_comments_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# frozen_string_literal: true

class V1::ArticleCommentsController < V1::ApplicationController
before_action :doorkeeper_authorize!, except: %i[index show get_related_resources]
include GraphitiCrud

before_action :doorkeeper_authorize!, except: %i[index show]

graphiti_resource V1::ArticleCommentResource
end
6 changes: 6 additions & 0 deletions app/controllers/v1/articles_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# frozen_string_literal: true

class V1::ArticlesController < V1::ApplicationController
include GraphitiCrud

before_action :doorkeeper_authorize!, except: %i[index show]

graphiti_resource V1::ArticleResource
end
5 changes: 5 additions & 0 deletions app/controllers/v1/board_room_presences_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
# frozen_string_literal: true

class V1::BoardRoomPresencesController < V1::ApplicationController
include GraphitiCrud

graphiti_resource V1::BoardRoomPresenceResource
end
6 changes: 6 additions & 0 deletions app/controllers/v1/books_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# frozen_string_literal: true

class V1::BooksController < V1::ApplicationController
include GraphitiCrud

graphiti_resource V1::BookResource

def isbn_lookup # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
authorize Book

Expand Down
16 changes: 13 additions & 3 deletions app/controllers/v1/debit/collections_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# frozen_string_literal: true

module V1::Debit
class CollectionsController < V1::ApplicationController
include GraphitiCrud

before_action :set_model, only: %i[sepa]

graphiti_resource V1::Debit::CollectionResource

def sepa
authorize Debit::Collection
return head :not_found unless @model.transactions.any?
Expand Down Expand Up @@ -34,9 +40,13 @@ def send_compressed_sepa_files(sepa_files)
end

def error_response
resource = V1::Debit::CollectionResource.new(@model, {})
errors = JSONAPI::Exceptions::ValidationErrors.new(resource).errors
JSONAPI::ErrorsOperationResult.new(422, errors)
{
errors: [{
title: 'Validation Error',
detail: 'Collection has validation errors',
status: '422'
}]
}
end
end
end
5 changes: 5 additions & 0 deletions app/controllers/v1/debit/mandates_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# frozen_string_literal: true

module V1::Debit
class MandatesController < V1::ApplicationController
include GraphitiCrud

graphiti_resource V1::Debit::MandateResource
end
end
5 changes: 5 additions & 0 deletions app/controllers/v1/debit/transactions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# frozen_string_literal: true

module V1::Debit
class TransactionsController < V1::ApplicationController
include GraphitiCrud

graphiti_resource V1::Debit::TransactionResource
end
end
5 changes: 5 additions & 0 deletions app/controllers/v1/form/closed_question_answers_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# frozen_string_literal: true

module V1::Form
class ClosedQuestionAnswersController < V1::ApplicationController
include GraphitiCrud

graphiti_resource V1::Form::ClosedQuestionAnswerResource
end
end
5 changes: 5 additions & 0 deletions app/controllers/v1/form/closed_question_options_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# frozen_string_literal: true

module V1::Form
class ClosedQuestionOptionsController < V1::ApplicationController
include GraphitiCrud

graphiti_resource V1::Form::ClosedQuestionOptionResource
end
end
Loading
Loading