Module: Railsmith::BaseService::CrudActions Private

Included in:
Railsmith::BaseService
Defined in:
lib/railsmith/base_service/crud_actions.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Default ‘create`/`update`/`destroy` action implementations.

Instance Method Summary collapse

Instance Method Details

#createObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



8
9
10
11
12
13
# File 'lib/railsmith/base_service/crud_actions.rb', line 8

def create
  with_model_transaction do |model_klass|
    record = build_record(model_klass, sanitize_attributes(attributes_params))
    write_with_nested_support(record, write_method: :save, nested_mode: :create)
  end
end

#destroyObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/railsmith/base_service/crud_actions.rb', line 26

def destroy
  with_model_transaction do |model_klass|
    record_result = find_record(model_klass, record_id)
    next record_result if record_result.failure?

    record = record_result.value
    cascade_result = cascade_destroy_if_needed(record)
    next cascade_result if cascade_result.failure?

    persist_write(record, method_name: :destroy)
  end
end

#findObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



39
40
41
42
43
44
# File 'lib/railsmith/base_service/crud_actions.rb', line 39

def find
  model_klass = model_class
  return missing_model_class_result unless model_klass

  find_record(model_klass, record_id)
end

#listObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



46
47
48
49
50
51
52
53
# File 'lib/railsmith/base_service/crud_actions.rb', line 46

def list
  model_klass = model_class
  return missing_model_class_result unless model_klass

  Result.success(value: base_scope(model_klass).all)
rescue StandardError => e
  Result.failure(error: map_exception_to_error(e))
end

#updateObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



15
16
17
18
19
20
21
22
23
24
# File 'lib/railsmith/base_service/crud_actions.rb', line 15

def update
  with_model_transaction do |model_klass|
    record_result = find_record(model_klass, record_id)
    next record_result if record_result.failure?

    record = record_result.value
    assign_attributes(record, sanitize_attributes(attributes_params))
    write_with_nested_support(record, write_method: :save, nested_mode: :update)
  end
end