Module: JSONAPI::ResourceActions

Extended by:
ActiveSupport::Concern
Includes:
ActiveStorageSupport, CrudHelpers, FieldValidation, FilterValidation, Pagination, Preloading, ResourceLoading, Serialization, TypeValidation
Included in:
BaseController
Defined in:
lib/json_api/controllers/concerns/resource_actions.rb,
lib/json_api/controllers/concerns/resource_actions/pagination.rb,
lib/json_api/controllers/concerns/resource_actions/preloading.rb,
lib/json_api/controllers/concerns/resource_actions/crud_helpers.rb,
lib/json_api/controllers/concerns/resource_actions/serialization.rb,
lib/json_api/controllers/concerns/resource_actions/type_validation.rb,
lib/json_api/controllers/concerns/resource_actions/field_validation.rb,
lib/json_api/controllers/concerns/resource_actions/resource_loading.rb,
lib/json_api/controllers/concerns/resource_actions/filter_validation.rb

Defined Under Namespace

Modules: CrudHelpers, FieldValidation, FilterValidation, Pagination, Preloading, ResourceLoading, Serialization, TypeValidation

Instance Method Summary collapse

Methods included from TypeValidation

#validate_resource_id!, #validate_resource_type!

Methods included from Serialization

#serialize_collection, #serialize_resource

Methods included from Preloading

#preload_includes

Methods included from FieldValidation

#validate_fields_param, #validate_include_param, #validate_sort_param

Methods included from FilterValidation

#validate_filter_param

Methods included from ActiveStorageSupport

#active_storage_attachment?, #append_only_enabled?, #attach_active_storage_files, #extract_active_storage_params_from_hash, #filter_active_storage_from_includes, #filter_polymorphic_from_includes, #find_blob_by_signed_id, #find_relationship_definition, #process_active_storage_attachment, #purge_on_nil_enabled?, #resolve_model_class_for_attachment, #serialize_active_storage_relationship, #serialize_blob_identifier

Instance Method Details

#build_resource_for_createObject



61
62
63
64
65
# File 'lib/json_api/controllers/concerns/resource_actions.rb', line 61

def build_resource_for_create
  sti_class = determine_sti_class
  params_hash, @create_attachments = prepare_create_params(sti_class)
  sti_class.new(params_hash)
end

#createObject



50
51
52
53
54
55
56
57
58
59
# File 'lib/json_api/controllers/concerns/resource_actions.rb', line 50

def create
  resource = build_resource_for_create
  authorize_resource_action!(resource, action: :create)
  attach_active_storage_files(resource, @create_attachments, resource_class: determine_sti_resource_class)
  save_created(resource)
rescue ArgumentError => e
  render_create_error(e)
rescue JSONAPI::Exceptions::ParameterNotAllowed, ActiveSupport::MessageVerifier::InvalidSignature => e
  handle_create_exception(e)
end

#destroyObject



86
87
88
89
90
91
# File 'lib/json_api/controllers/concerns/resource_actions.rb', line 86

def destroy
  authorize_resource_action!(@resource, action: :destroy)
  @resource.destroy
  emit_resource_event(:deleted, @resource)
  head :no_content
end

#handle_create_exception(error) ⇒ Object



67
68
69
70
71
72
# File 'lib/json_api/controllers/concerns/resource_actions.rb', line 67

def handle_create_exception(error)
  case error
  when JSONAPI::Exceptions::ParameterNotAllowed then render_parameter_not_allowed_error(error)
  when ActiveSupport::MessageVerifier::InvalidSignature then render_signed_id_error(error)
  end
end

#indexObject



36
37
38
39
40
41
42
# File 'lib/json_api/controllers/concerns/resource_actions.rb', line 36

def index
  scope = apply_authorization_scope(@preloaded_resources || model_class.all, action: :index)
  query = build_query(scope)
  @total_count = query.total_count
  @pagination_applied = query.pagination_applied
  render json: serialize_collection(query.scope), status: :ok
end

#showObject



44
45
46
47
48
# File 'lib/json_api/controllers/concerns/resource_actions.rb', line 44

def show
  resource = @preloaded_resource || @resource
  authorize_resource_action!(resource, action: :show)
  render json: serialize_resource(resource), status: :ok
end

#updateObject



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/json_api/controllers/concerns/resource_actions.rb', line 74

def update
  authorize_resource_action!(@resource, action: :update)
  params_hash, attachments = prepare_update_params
  save_updated(params_hash, attachments)
rescue ArgumentError => e
  render_invalid_relationship_error(e)
rescue JSONAPI::Exceptions::ParameterNotAllowed => e
  render_parameter_not_allowed_error(e)
rescue ActiveSupport::MessageVerifier::InvalidSignature => e
  render_signed_id_error(e)
end