Commit 6bdbe7c0 authored by Sean McGivern's avatar Sean McGivern

Merge branch 'dm-namespace-license-plans' into 'master'

Refactor Namespace#feature_available_in_plan?

See merge request gitlab-org/gitlab-ee!3249
parents 1146d154 addb45c3
......@@ -92,6 +92,13 @@ class License < ActiveRecord::Base
EARLY_ADOPTER_PLAN => EARLY_ADOPTER_FEATURES
}.freeze
PLANS_BY_FEATURE = FEATURES_BY_PLAN.each_with_object({}) do |(plan, features), hash|
features.each do |feature|
hash[feature] ||= []
hash[feature] << plan
end
end.freeze
# Add on codes that may occur in legacy licenses that don't have a plan yet.
FEATURES_FOR_ADD_ONS = {
'GitLab_Auditor_User' => :auditor_user,
......@@ -135,6 +142,18 @@ class License < ActiveRecord::Base
FEATURES_BY_PLAN.fetch(plan, [])
end
def plans_with_feature(feature)
if GLOBAL_FEATURES.include?(feature)
raise ArgumentError, "Use `License.feature_available?` for features that cannot be restricted to only a subset of projects or namespaces"
end
PLANS_BY_FEATURE.fetch(feature, [])
end
def plan_includes_feature?(plan, feature)
plans_with_feature(feature).include?(plan)
end
def current
if RequestStore.active?
RequestStore.fetch(:current_license) { load_license }
......@@ -149,14 +168,6 @@ class License < ActiveRecord::Base
RequestStore.delete(:current_license)
end
def plan_includes_feature?(plan, feature)
if GLOBAL_FEATURES.include?(feature)
raise ArgumentError, "Use `License.feature_available?` for features that cannot be restricted to only a subset of projects or namespaces"
end
features_for_plan(plan).include?(feature)
end
def load_license
return unless self.table_exists?
......
......@@ -2,7 +2,7 @@
.billing-plan-header.content-block.center
.billing-plan-logo
- if Namespace::EE_PLANS.keys.include?(plan.code)
- if Namespace::PLANS.include?(plan.code)
= render "shared/billings/plans/#{plan.code}.svg"
- elsif plan.free?
= render "shared/billings/plans/free.svg"
......
......@@ -3,6 +3,6 @@ require './spec/support/sidekiq'
Plan.create!(name: EE::Namespace::FREE_PLAN,
title: EE::Namespace::FREE_PLAN.titleize)
EE::Namespace::EE_PLANS.each_key do |plan|
EE::Namespace::NAMESPACE_PLANS_TO_LICENSE_PLANS.each_key do |plan|
Plan.create!(name: plan, title: plan.titleize)
end
......@@ -13,13 +13,16 @@ module EE
GOLD_PLAN = 'gold'.freeze
EARLY_ADOPTER_PLAN = 'early_adopter'.freeze
EE_PLANS = {
NAMESPACE_PLANS_TO_LICENSE_PLANS = {
BRONZE_PLAN => License::STARTER_PLAN,
SILVER_PLAN => License::PREMIUM_PLAN,
GOLD_PLAN => License::ULTIMATE_PLAN,
EARLY_ADOPTER_PLAN => License::EARLY_ADOPTER_PLAN
}.freeze
LICENSE_PLANS_TO_NAMESPACE_PLANS = NAMESPACE_PLANS_TO_LICENSE_PLANS.invert.freeze
PLANS = NAMESPACE_PLANS_TO_LICENSE_PLANS.keys.freeze
prepended do
belongs_to :plan
......@@ -33,6 +36,12 @@ module EE
validate :validate_plan_name
end
module ClassMethods
def plans_with_feature(feature)
LICENSE_PLANS_TO_NAMESPACE_PLANS.values_at(*License.plans_with_feature(feature))
end
end
def root_ancestor
ancestors.reorder(nil).find_by(parent_id: nil)
end
......@@ -70,7 +79,7 @@ module EE
def feature_available_in_plan?(feature)
@features_available_in_plan ||= Hash.new do |h, feature|
h[feature] = plans.any? { |plan| License.plan_includes_feature?(EE_PLANS[plan&.name], feature) }
h[feature] = (plans.map(&:name) & self.class.plans_with_feature(feature)).any?
end
@features_available_in_plan[feature]
......@@ -126,7 +135,7 @@ module EE
private
def validate_plan_name
if @plan_name.present? && EE_PLANS.keys.exclude?(@plan_name)
if @plan_name.present? && PLANS.exclude?(@plan_name)
errors.add(:plan, 'is not included in the list')
end
end
......@@ -142,11 +151,11 @@ module EE
end
def plans
@ancestors_plans ||=
@plans ||=
if parent_id
Plan.where(id: ancestors.with_plan.reorder(nil).select('plan_id')) + [plan]
Plan.where(id: self_and_ancestors.with_plan.reorder(nil).select(:plan_id))
else
[plan]
Array(plan)
end
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment