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.



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

def initialize
end

Class Method Details

.after(callback, *args) ⇒ Object



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

def self.after(callback, *args)
  options = extract_callback_options(args, "!halted && value")

  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



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

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

  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

Instance Method Details

#admin_save(obj) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/application_service.rb', line 87

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



35
36
37
# File 'lib/application_service.rb', line 35

def current_object
  @obj
end

#current_object=(obj) ⇒ Object



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

def current_object=(obj)
  @obj = obj
end

#destroy(obj) ⇒ Object



80
81
82
83
84
85
# File 'lib/application_service.rb', line 80

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

#save(obj) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/application_service.rb', line 43

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



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/application_service.rb', line 55

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) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/application_service.rb', line 67

def update_attributes(obj, params)
  @obj = obj
  @obj.assign_attributes(params)

  yield if block_given?

  run_callbacks :save do
    run_callbacks :update do
       @obj.save
    end
  end
end