Sha256: ef11123718cf457be237d88a7086fb8724084adec98c76679b3ad49d933a346a

Contents?: true

Size: 1.09 KB

Versions: 10

Compression:

Stored size: 1.09 KB

Contents

module Releaf::ActionController::Features
  extend ActiveSupport::Concern

  included do
    before_action :verify_feature_availability!
    helper_method :feature_available?
    rescue_from Releaf::FeatureDisabled, with: :access_denied
  end

  def verify_feature_availability!
    feature = action_feature(params[:action])
    raise Releaf::FeatureDisabled, feature.to_s if feature.present? && !feature_available?(feature)
  end

  def action_feature(action)
    action_features[action.to_sym]
  end

  def features
    [:edit, :create, :create_another, :destroy, :index, :toolbox, :search]
  end

  def action_features
    {
      index: :index,
      new: :create,
      create: :create,
      show: (feature_available?(:show) ? :show : :edit),
      edit: :edit,
      update: :edit,
      confirm_destroy: :destroy,
      destroy: :destroy
    }
  end

  def feature_available?(feature)
    return false if feature.blank?
    return false if feature == :create_another && !feature_available?(:create)
    return false if feature == :search && !feature_available?(:index)
    features.include? feature
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
releaf-core-3.1.0 app/lib/releaf/action_controller/features.rb
releaf-core-3.0.3 app/lib/releaf/action_controller/features.rb
releaf-core-3.0.2 app/lib/releaf/action_controller/features.rb
releaf-core-3.0.1 app/lib/releaf/action_controller/features.rb
releaf-core-3.0.0 app/lib/releaf/action_controller/features.rb
releaf-core-2.2.1 app/lib/releaf/action_controller/features.rb
releaf-core-2.2.0 app/lib/releaf/action_controller/features.rb
releaf-core-2.1.2 app/lib/releaf/action_controller/features.rb
releaf-core-2.1.1 app/lib/releaf/action_controller/features.rb
releaf-core-2.1.0 app/lib/releaf/action_controller/features.rb