Skip to content

Commit

Permalink
Merge pull request #562 from cortex-cms/topic/refactor-v2
Browse files Browse the repository at this point in the history
Railway-Oriented Error Handling
  • Loading branch information
toastercup authored Aug 8, 2018
2 parents 7c21751 + 9bc2cb8 commit d05e4ff
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 57 deletions.
64 changes: 37 additions & 27 deletions app/controllers/cortex/content_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,47 +28,57 @@ def edit
@content_item = content_type.content_items.find_by_tenant(current_user.active_tenant).find_by_id(params[:id])
@wizard = wizard_decorator(@content_item.content_type)

title = @content_item.field_items.find { |field_item| field_item.field.name == 'Title' }.data['text'] # TODO: refactor this hardcoded Field reference
title = @content_item.field_items.find {|field_item| field_item.field.name == 'Title'}.data['text'] # TODO: refactor this hardcoded Field reference
add_breadcrumb content_type.name.pluralize, :content_type_content_items_path
add_breadcrumb title
add_breadcrumb 'Edit'
end

def update
begin
update_content_item
rescue ActiveRecord::RecordInvalid => e
flash[:warning] = validation_message(e.message)
@content_item = content_item_reload(content_type.content_items.find_by_id(params[:id]))
@wizard = wizard_decorator(@content_item.content_type)
update_content_item
.with_step_args(
execute_content_item_state_change: [state: params[:content_item][:state]]
)
.call(id: params[:id], content_type: content_type,
content_item_params: content_item_params, current_user: current_user) do |m|
m.success do |content_item|
flash[:success] = "#{content_type.name} successfully updated"
redirect_to content_type_content_items_path
end

title = @content_item.field_items.find { |field_item| field_item.field.name == 'Title' }.data['text'] # TODO: refactor this hardcoded Field reference
add_breadcrumb content_type.name.pluralize, :content_type_content_items_path
add_breadcrumb title
add_breadcrumb 'Edit'
m.failure :persist_content_item do |errors|
flash[:warning] = clean_error_messages(errors.full_messages)
render_update_content_item_error
end

render :edit
else
flash[:success] = "Hooray! #{content_type.name} Updated!"
redirect_to content_type_content_items_path
m.failure do |error|
flash[:warning] = ['Unknown System Error']
render_update_content_item_error
end
end
end

def create
begin
create_content_item
rescue ActiveRecord::RecordInvalid => e
flash[:warning] = validation_message(e.message)
@content_item = content_item_reload(content_type.content_items.new)
@wizard = wizard_decorator(@content_item.content_type)
create_content_item
.with_step_args(
execute_content_item_state_change: [state: params[:content_item][:state]]
)
.call(id: params[:id], content_type: content_type,
content_item_params: content_item_params, current_user: current_user) do |m|
m.success do |content_item|
flash[:success] = "#{content_type.name} successfully created"
redirect_to content_type_content_items_path
end

add_breadcrumb content_type.name.pluralize, :content_type_content_items_path
add_breadcrumb 'New'
m.failure :persist_content_item do |errors|
flash[:warning] = clean_error_messages(errors.full_messages)
render_create_content_item_error
end

render :new
else
flash[:success] = "Hooray! #{content_type.name} Created!"
redirect_to content_type_content_items_path
m.failure do |error|
flash[:warning] = ['Unknown System Error']
render_update_content_item_error
end
end
end
end
Expand Down
39 changes: 24 additions & 15 deletions app/helpers/cortex/content_item_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,32 @@ def content_type

def create_content_item
CreateContentItemTransaction.new
.with_step_args(
execute_content_item_state_change: [state: params[:content_item][:state]]
)
.call(id: params[:id], content_type: content_type,
content_item_params: content_item_params, current_user: current_user)
.value!
end

def update_content_item
UpdateContentItemTransaction.new
.with_step_args(
execute_content_item_state_change: [state: params[:content_item][:state]]
)
.call(id: params[:id], content_type: content_type,
content_item_params: content_item_params, current_user: current_user)
.value!
end

def render_create_content_item_error
@content_item = content_item_reload(content_type.content_items.new)
@wizard = wizard_decorator(@content_item.content_type)

add_breadcrumb content_type.name.pluralize, :content_type_content_items_path
add_breadcrumb 'New'

render :new
end

def render_update_content_item_error
@content_item = content_item_reload(content_type.content_items.find_by_id(params[:id]))
@wizard = wizard_decorator(@content_item.content_type)

title = @content_item.field_items.find { |field_item| field_item.field.name == 'Title' }.data['text'] # TODO: refactor this hardcoded Field reference
add_breadcrumb content_type.name.pluralize, :content_type_content_items_path
add_breadcrumb title
add_breadcrumb 'Edit'

render :edit
end

def content_item_reload(content_item)
Expand Down Expand Up @@ -98,9 +108,8 @@ def field_item_param_data(field_item_params)
params_hash['data'] || {}
end

def validation_message(base_message)
msg_array = base_message.gsub('Validation failed:', '').gsub('Field items', '').split(',')
msg_array.map {|message| message.strip.titleize}
def clean_error_messages(messages)
messages.map {|message| message.gsub('Field items', '').strip.titleize}
end
end
end
13 changes: 5 additions & 8 deletions app/operations/cortex/database_transact_operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ module Cortex

result = nil

begin
ActiveRecord::Base.transaction do
result = block.(Success(input))
raise ActiveRecord::Rollback if result.failure?
result
end
rescue ActiveRecord::Rollback
result
ActiveRecord::Base.transaction do
result = block.(Success(input))
raise ActiveRecord::Rollback if result.failure?
end

result
}
end
7 changes: 5 additions & 2 deletions app/operations/cortex/persist_content_item_operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ class PersistContentItemOperation
include Dry::Transaction::Operation

def call(input)
input.save!
Success(input)
if input.save
Success(input)
else
Failure(input.errors)
end
end
end
end
10 changes: 5 additions & 5 deletions app/views/cortex/content_items/_form.html.haml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
= form_for [@content_type, @content_item], html: { multipart: true } do |form|
= form.hidden_field :creator_id, value: current_user.id
= form.hidden_field :content_type_id, value: @content_type.id
-# TODO: remove conditional once cortex plugins fields are properly converted
- if @content_type.name == 'Media'
= react_component('CortexApp', props: { temporary_render: 'Wizard' })
- else
= cell(Cortex::WizardCell, @wizard, context: { content_item: @content_item, form: form }).()
-# TODO: remove conditional once fields are properly converted to React
-# if @content_type.name == 'Media'
=# react_component('CortexApp', props: { temporary_render: 'Wizard' })
-# else
= cell(Cortex::WizardCell, @wizard, context: { content_item: @content_item, form: form }).()

%footer.mdl-grid
.mdl-cell.mdl-cell--12-col
Expand Down

0 comments on commit d05e4ff

Please sign in to comment.