Sha256: a89e381e19ceae29046b183da87b318b6bbd84c5f063a023ae69e523979adb9d

Contents?: true

Size: 790 Bytes

Versions: 23

Compression:

Stored size: 790 Bytes

Contents

module Activatable

  def self.included(controller)
    controller.extend(ClassMethods)
  end

  module ClassMethods

    def activatable
      include Activatable::InstanceMethods
    end

  end

  module InstanceMethods

    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

  end

end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
rails_core_extensions-0.15.0 lib/rails_core_extensions/activatable.rb
rails_core_extensions-0.14.0 lib/rails_core_extensions/activatable.rb
rails_core_extensions-0.13.2 lib/rails_core_extensions/activatable.rb
rails_core_extensions-0.13.1 lib/rails_core_extensions/activatable.rb
rails_core_extensions-0.13.0 lib/rails_core_extensions/activatable.rb
rails_core_extensions-0.12.0 lib/rails_core_extensions/activatable.rb
rails_core_extensions-0.11.3 lib/rails_core_extensions/activatable.rb
rails_core_extensions-0.11.2 lib/rails_core_extensions/activatable.rb
rails_core_extensions-0.11.1 lib/rails_core_extensions/activatable.rb
rails_core_extensions-0.11.0.beta.11 lib/rails_core_extensions/activatable.rb
rails_core_extensions-0.10.0 lib/rails_core_extensions/activatable.rb
rails_core_extensions-0.9.0 lib/rails_core_extensions/activatable.rb
rails_core_extensions-0.8.0 lib/rails_core_extensions/activatable.rb
rails_core_extensions-0.7.1 lib/rails_core_extensions/activatable.rb
rails_core_extensions-0.7.0 lib/rails_core_extensions/activatable.rb
rails_core_extensions-0.6.1 lib/rails_core_extensions/activatable.rb
rails_core_extensions-0.6.0 lib/rails_core_extensions/activatable.rb
rails_core_extensions-0.4.0 lib/rails_core_extensions/activatable.rb
rails_core_extensions-0.3.0 lib/rails_core_extensions/activatable.rb
rails_core_extensions-0.2.0 lib/rails_core_extensions/activatable.rb