Class: ApplicationService

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Callbacks
Defined in:
lib/application_service.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApplicationService

Returns a new instance of ApplicationService.



8
9
# File 'lib/application_service.rb', line 8

def initialize
end

Class Method Details

.after(callback, *args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/application_service.rb', line 22

def self.after(callback, *args)
  options = extract_callback_options(args)

  conditional = ActiveSupport::Callbacks::Conditionals::Value.new { |v|
    v != false
  }
  options[:if] = Array(options[:if]) << conditional

  args.each do |arg|
    set_callback callback, :after, arg, options
    if options.fetch(:skip_on_admin_save, false)
      skip_callback callback, :after, arg, if: -> { @on_admin_save }
    end
  end
end

.before(callback, *args) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/application_service.rb', line 11

def self.before(callback, *args)
  options = extract_callback_options(args)

  args.each do |arg|
    set_callback callback, :before, arg, options
    if options.fetch(:skip_on_admin_save, false)
      skip_callback callback, :before, arg, if: -> { @on_admin_save }
    end
  end
end

.extract_callback_options(args) ⇒ Object



107
108
109
110
111
# File 'lib/application_service.rb', line 107

def self.extract_callback_options(args)
  options = args.last.is_a?(Hash) ? args.pop : {}

  options
end

Instance Method Details

#admin_save(obj) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/application_service.rb', line 90

def admin_save(obj)
  @obj = obj

  @on_admin_save = true
  save_method = @obj.respond_to?(:admin_save) ? :admin_save : :save
  run_callbacks :save do
    if @obj.new_record?
      result = create(save_method)
    else
      result = update(save_method)
    end
    result
  end
ensure
  @on_admin_save = false
end

#current_objectObject



38
39
40
# File 'lib/application_service.rb', line 38

def current_object
  @obj
end

#current_object=(obj) ⇒ Object



42
43
44
# File 'lib/application_service.rb', line 42

def current_object=(obj)
  @obj = obj
end

#destroy(obj) ⇒ Object



83
84
85
86
87
88
# File 'lib/application_service.rb', line 83

def destroy(obj)
  @obj = obj
  run_callbacks :destroy do
    @obj.destroy
  end
end

#save(obj) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/application_service.rb', line 46

def save(obj)
  @obj = obj
  run_callbacks :save do
    if @obj.new_record?
      result = create
    else
      result = update
    end
    result
  end
end

#save!(obj) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/application_service.rb', line 58

def save!(obj)
  @obj = obj
  run_callbacks :save do
    if @obj.new_record?
      result = create!
    else
      result = update!
    end
    result
  end
end

#update_attributes(obj, params, with_admin_save: false) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/application_service.rb', line 70

def update_attributes(obj, params, with_admin_save: false)
  @obj = obj
  @obj.assign_attributes(params)

  yield if block_given?

  if with_admin_save
    self.admin_save(obj)
  else
    self.save(obj)
  end
end