Class: ApplicationService
- Inherits:
-
Object
- Object
- ApplicationService
- Includes:
- ActiveSupport::Callbacks
- Defined in:
- lib/application_service.rb
Class Method Summary collapse
Instance Method Summary collapse
- #admin_save(obj) ⇒ Object
- #current_object ⇒ Object
- #current_object=(obj) ⇒ Object
- #destroy(obj) ⇒ Object
-
#initialize ⇒ ApplicationService
constructor
A new instance of ApplicationService.
- #save(obj) ⇒ Object
- #save!(obj) ⇒ Object
- #update_attributes(obj, params) ⇒ Object
Constructor Details
#initialize ⇒ ApplicationService
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
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/application_service.rb', line 23 def self.after(callback, *args) = (args) args.each do |arg| set_callback callback, :after, arg, if .fetch(:skip_on_admin_save, false) skip_callback callback, :after, arg, if: -> { @on_admin_save } end end end |
.before(callback, *args) ⇒ Object
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/application_service.rb', line 12 def self.before(callback, *args) = (args) args.each do |arg| set_callback callback, :before, arg, if .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
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/application_service.rb', line 86 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_object ⇒ Object
34 35 36 |
# File 'lib/application_service.rb', line 34 def current_object @obj end |
#current_object=(obj) ⇒ Object
38 39 40 |
# File 'lib/application_service.rb', line 38 def current_object=(obj) @obj = obj end |
#destroy(obj) ⇒ Object
79 80 81 82 83 84 |
# File 'lib/application_service.rb', line 79 def destroy(obj) @obj = obj run_callbacks :destroy do @obj.destroy end end |
#save(obj) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/application_service.rb', line 42 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
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/application_service.rb', line 54 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
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/application_service.rb', line 66 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 |