cubesystems/releaf

View on GitHub
releaf-core/app/lib/releaf/action_controller/features.rb

Summary

Maintainability
A
25 mins
Test Coverage
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