Skip to content

Commit 4606cef

Browse files
authored
Amélioration des vérifications d'intégrité de migration (#3691)
* wip * wip * working * yes! * syntax * dry * fix * .with_deleted
1 parent a464fd1 commit 4606cef

16 files changed

Lines changed: 170 additions & 268 deletions

File tree

app/controllers/api/osuny/application_controller.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ def verify_app_token
1515
render_unauthorized unless @app
1616
end
1717

18-
def render_on_missing_migration_identifier
19-
render json: { error: 'Missing migration identifier.' }, status: :bad_request
20-
end
21-
2218
# Set API messages to English
2319
def switch_locale(&action)
2420
I18n.with_locale(:en, &action)

app/controllers/api/osuny/communication/websites/agenda/categories_controller.rb

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Api::Osuny::Communication::Websites::Agenda::CategoriesController < Api::Osuny::Communication::Websites::ApplicationController
2-
before_action :build_category, only: :create
3-
before_action :load_category, only: [:show, :update, :destroy]
2+
include Api::Osuny::HasResource
3+
include Api::Osuny::HasMigrationIdentifier
44

55
def index
66
@categories = paginate(website.agenda_categories.includes(:localizations))
@@ -10,6 +10,8 @@ def show
1010
end
1111

1212
def create
13+
@category = website.agenda_categories.build
14+
@category.assign_attributes(category_params)
1315
if @category.save
1416
render :show, status: :created
1517
else
@@ -31,7 +33,7 @@ def upsert
3133
category_params[:migration_identifier].present?
3234
}
3335
unless every_category_has_migration_identifier
34-
render_on_missing_migration_identifier
36+
render_missing_migration_identifier
3537
return
3638
end
3739

@@ -70,32 +72,14 @@ def destroy
7072

7173
protected
7274

73-
def build_category
74-
@category = website.agenda_categories.build
75-
@category.assign_attributes(category_params)
75+
def integrity_checker
76+
@integrity_checker ||= Osuny::Api::MigrationIdentifierIntegrityChecker.new(@category, category_params, website.agenda_categories)
7677
end
7778

78-
def load_category
79+
def load_resource
7980
@category = website.agenda_categories.find(params[:id])
8081
end
8182

82-
def load_migration_identifier
83-
@migration_identifier = category_params[:migration_identifier]
84-
render_on_missing_migration_identifier unless @migration_identifier.present?
85-
end
86-
87-
def ensure_same_migration_identifier
88-
if @category.migration_identifier != @migration_identifier
89-
render json: { error: 'Migration identifier does not match' }, status: :unprocessable_content
90-
end
91-
end
92-
93-
def ensure_migration_identifier_is_available
94-
if website.agenda_categories.where(migration_identifier: @migration_identifier).any?
95-
render json: { error: 'Migration identifier already used' }, status: :unprocessable_content
96-
end
97-
end
98-
9983
def l10n_permitted_keys
10084
[
10185
:migration_identifier, :language, :name, :meta_description,

app/controllers/api/osuny/communication/websites/agenda/events_controller.rb

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Api::Osuny::Communication::Websites::Agenda::EventsController < Api::Osuny::Communication::Websites::ApplicationController
2-
before_action :build_event, only: :create
3-
before_action :load_event, only: [:show, :update, :destroy]
2+
include Api::Osuny::HasResource
3+
include Api::Osuny::HasMigrationIdentifier
44

55
def index
66
@events = paginate(website.events.includes(:localizations))
@@ -10,6 +10,8 @@ def show
1010
end
1111

1212
def create
13+
@event = website.events.build
14+
@event.assign_attributes(event_params)
1315
if @event.save
1416
render :show, status: :created
1517
else
@@ -31,7 +33,7 @@ def upsert
3133
event_params[:migration_identifier].present?
3234
}
3335
unless every_event_has_migration_identifier
34-
render_on_missing_migration_identifier
36+
render_missing_migration_identifier
3537
return
3638
end
3739

@@ -70,32 +72,14 @@ def destroy
7072

7173
protected
7274

73-
def build_event
74-
@event = website.events.build
75-
@event.assign_attributes(event_params)
75+
def integrity_checker
76+
@integrity_checker ||= Osuny::Api::MigrationIdentifierIntegrityChecker.new(@event, event_params, website.events.with_deleted)
7677
end
7778

78-
def load_event
79+
def load_resource
7980
@event = website.events.find(params[:id])
8081
end
8182

82-
def load_migration_identifier
83-
@migration_identifier = event_params[:migration_identifier]
84-
render_on_missing_migration_identifier unless @migration_identifier.present?
85-
end
86-
87-
def ensure_same_migration_identifier
88-
if @event.migration_identifier != @migration_identifier
89-
render json: { error: 'Migration identifier does not match' }, status: :unprocessable_content
90-
end
91-
end
92-
93-
def ensure_migration_identifier_is_available
94-
if website.events.with_deleted.where(migration_identifier: @migration_identifier).any?
95-
render json: { error: 'Migration identifier already used' }, status: :unprocessable_content
96-
end
97-
end
98-
9983
def l10n_permitted_keys
10084
[
10185
:migration_identifier, :language, :title, :meta_description,
Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
class Api::Osuny::Communication::Websites::ApplicationController < Api::Osuny::ApplicationController
22
before_action :load_website
33

4-
before_action :load_migration_identifier, only: [:create, :update]
5-
before_action :ensure_migration_identifier_is_available, only: :create
6-
before_action :ensure_same_migration_identifier, only: :update
7-
84
protected
95

106
def load_website
@@ -15,16 +11,4 @@ def website
1511
@website
1612
end
1713

18-
def load_migration_identifier
19-
raise NoMethodError, "You must implement the `load_migration_identifier` method in #{self.class.name}"
20-
end
21-
22-
def ensure_migration_identifier_is_available
23-
raise NoMethodError, "You must implement the `ensure_migration_identifier_is_available` method in #{self.class.name}"
24-
end
25-
26-
def ensure_same_migration_identifier
27-
raise NoMethodError, "You must implement the `ensure_same_migration_identifier` method in #{self.class.name}"
28-
end
29-
3014
end

app/controllers/api/osuny/communication/websites/pages/categories_controller.rb

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Api::Osuny::Communication::Websites::Pages::CategoriesController < Api::Osuny::Communication::Websites::ApplicationController
2-
before_action :build_category, only: :create
3-
before_action :load_category, only: [:show, :update, :destroy]
2+
include Api::Osuny::HasResource
3+
include Api::Osuny::HasMigrationIdentifier
44

55
def index
66
@categories = paginate(website.page_categories.includes(:localizations))
@@ -10,6 +10,8 @@ def show
1010
end
1111

1212
def create
13+
@category = website.page_categories.build
14+
@category.assign_attributes(category_params)
1315
if @category.save
1416
render :show, status: :created
1517
else
@@ -31,7 +33,7 @@ def upsert
3133
category_params[:migration_identifier].present?
3234
}
3335
unless every_category_has_migration_identifier
34-
render_on_missing_migration_identifier
36+
render_missing_migration_identifier
3537
return
3638
end
3739

@@ -70,32 +72,14 @@ def destroy
7072

7173
protected
7274

73-
def build_category
74-
@category = website.page_categories.build
75-
@category.assign_attributes(category_params)
75+
def integrity_checker
76+
@integrity_checker ||= Osuny::Api::MigrationIdentifierIntegrityChecker.new(@category, category_params, website.page_categories)
7677
end
7778

78-
def load_category
79+
def load_resource
7980
@category = website.page_categories.find(params[:id])
8081
end
8182

82-
def load_migration_identifier
83-
@migration_identifier = category_params[:migration_identifier]
84-
render_on_missing_migration_identifier unless @migration_identifier.present?
85-
end
86-
87-
def ensure_same_migration_identifier
88-
if @category.migration_identifier != @migration_identifier
89-
render json: { error: 'Migration identifier does not match' }, status: :unprocessable_content
90-
end
91-
end
92-
93-
def ensure_migration_identifier_is_available
94-
if website.page_categories.where(migration_identifier: @migration_identifier).any?
95-
render json: { error: 'Migration identifier already used' }, status: :unprocessable_content
96-
end
97-
end
98-
9983
def l10n_permitted_keys
10084
[
10185
:migration_identifier, :language, :name, :meta_description,

app/controllers/api/osuny/communication/websites/pages_controller.rb

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Api::Osuny::Communication::Websites::PagesController < Api::Osuny::Communication::Websites::ApplicationController
2-
before_action :build_page, only: :create
3-
before_action :load_page, only: [:show, :update, :destroy]
2+
include Api::Osuny::HasResource
3+
include Api::Osuny::HasMigrationIdentifier
44

55
def index
66
@pages = paginate(website.pages.includes(:localizations))
@@ -10,6 +10,8 @@ def show
1010
end
1111

1212
def create
13+
@page = website.pages.build
14+
@page.assign_attributes(page_params)
1315
if @page.save
1416
render :show, status: :created
1517
else
@@ -31,7 +33,7 @@ def upsert
3133
page_params[:migration_identifier].present?
3234
}
3335
unless every_page_has_migration_identifier
34-
render_on_missing_migration_identifier
36+
render_missing_migration_identifier
3537
return
3638
end
3739

@@ -70,32 +72,14 @@ def destroy
7072

7173
protected
7274

73-
def build_page
74-
@page = website.pages.build
75-
@page.assign_attributes(page_params)
75+
def integrity_checker
76+
@integrity_checker ||= Osuny::Api::MigrationIdentifierIntegrityChecker.new(@page, page_params, website.pages.with_deleted)
7677
end
7778

78-
def load_page
79+
def load_resource
7980
@page = website.pages.find(params[:id])
8081
end
8182

82-
def load_migration_identifier
83-
@migration_identifier = page_params[:migration_identifier]
84-
render_on_missing_migration_identifier unless @migration_identifier.present?
85-
end
86-
87-
def ensure_same_migration_identifier
88-
if @page.migration_identifier != @migration_identifier
89-
render json: { error: 'Migration identifier does not match' }, status: :unprocessable_content
90-
end
91-
end
92-
93-
def ensure_migration_identifier_is_available
94-
if website.pages.with_deleted.where(migration_identifier: @migration_identifier).any?
95-
render json: { error: 'Migration identifier already used' }, status: :unprocessable_content
96-
end
97-
end
98-
9983
def l10n_permitted_keys
10084
[
10185
:migration_identifier, :language, :title, :breadcrumb_title, :meta_description,

app/controllers/api/osuny/communication/websites/portfolio/categories_controller.rb

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Api::Osuny::Communication::Websites::Portfolio::CategoriesController < Api::Osuny::Communication::Websites::ApplicationController
2-
before_action :build_category, only: :create
3-
before_action :load_category, only: [:show, :update, :destroy]
2+
include Api::Osuny::HasResource
3+
include Api::Osuny::HasMigrationIdentifier
44

55
def index
66
@categories = paginate(website.portfolio_categories.includes(:localizations))
@@ -10,6 +10,8 @@ def show
1010
end
1111

1212
def create
13+
@category = website.portfolio_categories.build
14+
@category.assign_attributes(category_params)
1315
if @category.save
1416
render :show, status: :created
1517
else
@@ -31,7 +33,7 @@ def upsert
3133
category_params[:migration_identifier].present?
3234
}
3335
unless every_category_has_migration_identifier
34-
render_on_missing_migration_identifier
36+
render_missing_migration_identifier
3537
return
3638
end
3739

@@ -70,32 +72,14 @@ def destroy
7072

7173
protected
7274

73-
def build_category
74-
@category = website.portfolio_categories.build
75-
@category.assign_attributes(category_params)
75+
def integrity_checker
76+
@integrity_checker ||= Osuny::Api::MigrationIdentifierIntegrityChecker.new(@category, category_params, website.portfolio_categories)
7677
end
7778

78-
def load_category
79+
def load_resource
7980
@category = website.portfolio_categories.find(params[:id])
8081
end
8182

82-
def load_migration_identifier
83-
@migration_identifier = category_params[:migration_identifier]
84-
render_on_missing_migration_identifier unless @migration_identifier.present?
85-
end
86-
87-
def ensure_same_migration_identifier
88-
if @category.migration_identifier != @migration_identifier
89-
render json: { error: 'Migration identifier does not match' }, status: :unprocessable_content
90-
end
91-
end
92-
93-
def ensure_migration_identifier_is_available
94-
if website.portfolio_categories.where(migration_identifier: @migration_identifier).any?
95-
render json: { error: 'Migration identifier already used' }, status: :unprocessable_content
96-
end
97-
end
98-
9983
def l10n_permitted_keys
10084
[
10185
:migration_identifier, :language, :name, :meta_description,

0 commit comments

Comments
 (0)