Sha256: 28f515f18e115ce827011b224720e8627adfcbb09c314e2c2ec47d1719aadcfb

Contents?: true

Size: 1.23 KB

Versions: 33

Compression:

Stored size: 1.23 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: :feature_disabled
  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_disabled(exception)
    @feature = exception.message
    respond_with(nil, responder: action_responder(:feature_disabled))
  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

33 entries across 33 versions & 1 rubygems

Version Path
releaf-core-2.0.1 app/lib/releaf/action_controller/features.rb
releaf-core-2.0.0 app/lib/releaf/action_controller/features.rb
releaf-core-1.1.22 app/lib/releaf/action_controller/features.rb
releaf-core-1.1.21 app/lib/releaf/action_controller/features.rb
releaf-core-1.1.20 app/lib/releaf/action_controller/features.rb
releaf-core-1.1.19 app/lib/releaf/action_controller/features.rb
releaf-core-1.1.18 app/lib/releaf/action_controller/features.rb
releaf-core-1.1.17 app/lib/releaf/action_controller/features.rb
releaf-core-1.1.16 app/lib/releaf/action_controller/features.rb
releaf-core-1.1.15 app/lib/releaf/action_controller/features.rb
releaf-core-1.1.14 app/lib/releaf/action_controller/features.rb
releaf-core-1.1.13 app/lib/releaf/action_controller/features.rb
releaf-core-1.1.12 app/lib/releaf/action_controller/features.rb
releaf-core-1.1.11 app/lib/releaf/action_controller/features.rb
releaf-core-1.1.10 app/lib/releaf/action_controller/features.rb
releaf-core-1.1.9 app/lib/releaf/action_controller/features.rb
releaf-core-1.1.8 app/lib/releaf/action_controller/features.rb
releaf-core-1.1.7 app/lib/releaf/action_controller/features.rb
releaf-core-1.1.6 app/lib/releaf/action_controller/features.rb
releaf-core-1.1.5 app/lib/releaf/action_controller/features.rb