Module: Activatable::InstanceMethods

Defined in:
lib/rails_core_extensions/activatable.rb

Instance Method Summary collapse

Instance Method Details

#activate(success_block = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rails_core_extensions/activatable.rb', line 17

def activate(success_block = nil)
  resource.active = params[:active].presence || false
  action = resource.active ? 'activate' : 'inactivate'

  resource.save!

  success_block ||= -> {
    flash[:success] = "#{resource} #{action}d"
    redirect_to(collection_path)
  }

  success_block.call

rescue ActiveRecord::ActiveRecordError => e
  resource.errors.add(:base, "Failed to #{action}: " + e.message)
  flash[:error] = resource.errors.full_messages.to_sentence
  redirect_to(collection_path)
end