Class: NiftyServices::BaseCrudService

Inherits:
BaseService show all
Defined in:
lib/nifty_services/base_crud_service.rb

Constant Summary

Constants inherited from BaseService

NiftyServices::BaseService::CALLBACKS

Instance Attribute Summary collapse

Attributes inherited from BaseService

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseService

#add_error, #callback_fired?, concern, #default_logger, define_error_response_method, #execute, #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(record, options = {}) ⇒ BaseCrudService

Returns a new instance of BaseCrudService.



38
39
40
41
42
# File 'lib/nifty_services/base_crud_service.rb', line 38

def initialize(record, options = {})
  @record = record

  super(options)
end

Instance Attribute Details

#recordObject (readonly)

Returns the value of attribute record.



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

def record
  @record
end

Class Method Details

.get_whitelist_attributesObject



33
34
35
# File 'lib/nifty_services/base_crud_service.rb', line 33

def get_whitelist_attributes
  []
end

.record_type(record_type, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/nifty_services/base_crud_service.rb', line 7

def record_type(record_type, options = {})
  define_method :record_type do
    record_type
  end

  record_alias = options.delete(:alias_name) || record_type.to_s.underscore

  alias_method record_alias.to_sym, :record
end

.whitelist_attributes(*attrs) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/nifty_services/base_crud_service.rb', line 17

def whitelist_attributes(*attrs)
  raise "Invalid whitelist attributes(#{attrs}) array" unless attrs.is_a?(Array)

  attributes = *attrs.flatten.map(&:to_sym)

  define_method :record_attributes_whitelist do
    attributes
  end

  define_singleton_method :get_whitelist_attributes do
    attributes
  end

  attributes
end

Instance Method Details

#changed?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/nifty_services/base_crud_service.rb', line 48

def changed?
  changed_attributes.any?
end

#changed_attributesObject



44
45
46
# File 'lib/nifty_services/base_crud_service.rb', line 44

def changed_attributes
  []
end

#record_allowed_attributesObject Also known as: record_safe_attributes



64
65
66
# File 'lib/nifty_services/base_crud_service.rb', line 64

def record_allowed_attributes
  filter_hash(record_attributes_hash, record_attributes_whitelist)
end

#record_attributes_hashObject



56
57
58
# File 'lib/nifty_services/base_crud_service.rb', line 56

def record_attributes_hash
  not_implemented_exception(__method__)
end

#record_attributes_whitelistObject



60
61
62
# File 'lib/nifty_services/base_crud_service.rb', line 60

def record_attributes_whitelist
  not_implemented_exception(__method__)
end

#record_typeObject



52
53
54
# File 'lib/nifty_services/base_crud_service.rb', line 52

def record_type
  not_implemented_exception(__method__)
end