Class: NiftyServices::BaseCreateService

Inherits:
BaseCrudService show all
Defined in:
lib/nifty_services/base_create_service.rb

Constant Summary

Constants inherited from BaseService

NiftyServices::BaseService::CALLBACKS

Instance Attribute Summary

Attributes inherited from BaseCrudService

#record

Attributes inherited from BaseService

#errors, #logger, #options, #response_status, #response_status_code

Instance Method Summary collapse

Methods inherited from BaseCrudService

#changed?, #changed_attributes, get_whitelist_attributes, #record_allowed_attributes, #record_attributes_hash, #record_attributes_whitelist, record_type, #record_type, whitelist_attributes

Methods inherited from BaseService

#add_error, #callback_fired?, concern, #default_logger, define_error_response_method, #executed?, #fail?, #option_disabled?, #option_enabled?, #option_exists?, #register_callback, register_callback, #register_callback_action, register_callback_action, register_error_response_method, #success?, #valid?

Constructor Details

#initialize(options = {}) ⇒ BaseCreateService

Returns a new instance of BaseCreateService.



4
5
6
# File 'lib/nifty_services/base_create_service.rb', line 4

def initialize(options = {})
  super(nil, options)
end

Instance Method Details

#executeObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/nifty_services/base_create_service.rb', line 8

def execute
  execute_action do
    with_before_and_after_callbacks(:create) do
      if can_execute_action?
        @record = with_before_and_after_callbacks(:build_record) { build_record }

        if try_to_save_record
          after_execute_success_response
        else
          errors = create_error_response(@record)
          after_error_response(errors)
        end
      end
    end
  end
end