Commit addb45c3 authored by Douwe Maan's avatar Douwe Maan

Refactor Namespace#feature_available_in_plan?

parent 7206b29c
...@@ -92,6 +92,13 @@ class License < ActiveRecord::Base ...@@ -92,6 +92,13 @@ class License < ActiveRecord::Base
EARLY_ADOPTER_PLAN => EARLY_ADOPTER_FEATURES EARLY_ADOPTER_PLAN => EARLY_ADOPTER_FEATURES
}.freeze }.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. # Add on codes that may occur in legacy licenses that don't have a plan yet.
FEATURES_FOR_ADD_ONS = { FEATURES_FOR_ADD_ONS = {
'GitLab_Auditor_User' => :auditor_user, 'GitLab_Auditor_User' => :auditor_user,
...@@ -135,6 +142,18 @@ class License < ActiveRecord::Base ...@@ -135,6 +142,18 @@ class License < ActiveRecord::Base
FEATURES_BY_PLAN.fetch(plan, []) FEATURES_BY_PLAN.fetch(plan, [])
end 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 def current
if RequestStore.active? if RequestStore.active?
RequestStore.fetch(:current_license) { load_license } RequestStore.fetch(:current_license) { load_license }
...@@ -149,14 +168,6 @@ class License < ActiveRecord::Base ...@@ -149,14 +168,6 @@ class License < ActiveRecord::Base
RequestStore.delete(:current_license) RequestStore.delete(:current_license)
end 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 def load_license
return unless self.table_exists? return unless self.table_exists?
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
.billing-plan-header.content-block.center .billing-plan-header.content-block.center
.billing-plan-logo .billing-plan-logo
- if Namespace::EE_PLANS.keys.include?(plan.code) - if Namespace::PLANS.include?(plan.code)
= render "shared/billings/plans/#{plan.code}.svg" = render "shared/billings/plans/#{plan.code}.svg"
- elsif plan.free? - elsif plan.free?
= render "shared/billings/plans/free.svg" = render "shared/billings/plans/free.svg"
......
...@@ -3,6 +3,6 @@ require './spec/support/sidekiq' ...@@ -3,6 +3,6 @@ require './spec/support/sidekiq'
Plan.create!(name: EE::Namespace::FREE_PLAN, Plan.create!(name: EE::Namespace::FREE_PLAN,
title: EE::Namespace::FREE_PLAN.titleize) 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) Plan.create!(name: plan, title: plan.titleize)
end end
...@@ -13,13 +13,16 @@ module EE ...@@ -13,13 +13,16 @@ module EE
GOLD_PLAN = 'gold'.freeze GOLD_PLAN = 'gold'.freeze
EARLY_ADOPTER_PLAN = 'early_adopter'.freeze EARLY_ADOPTER_PLAN = 'early_adopter'.freeze
EE_PLANS = { NAMESPACE_PLANS_TO_LICENSE_PLANS = {
BRONZE_PLAN => License::STARTER_PLAN, BRONZE_PLAN => License::STARTER_PLAN,
SILVER_PLAN => License::PREMIUM_PLAN, SILVER_PLAN => License::PREMIUM_PLAN,
GOLD_PLAN => License::ULTIMATE_PLAN, GOLD_PLAN => License::ULTIMATE_PLAN,
EARLY_ADOPTER_PLAN => License::EARLY_ADOPTER_PLAN EARLY_ADOPTER_PLAN => License::EARLY_ADOPTER_PLAN
}.freeze }.freeze
LICENSE_PLANS_TO_NAMESPACE_PLANS = NAMESPACE_PLANS_TO_LICENSE_PLANS.invert.freeze
PLANS = NAMESPACE_PLANS_TO_LICENSE_PLANS.keys.freeze
prepended do prepended do
belongs_to :plan belongs_to :plan
...@@ -33,6 +36,12 @@ module EE ...@@ -33,6 +36,12 @@ module EE
validate :validate_plan_name validate :validate_plan_name
end 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 def root_ancestor
ancestors.reorder(nil).find_by(parent_id: nil) ancestors.reorder(nil).find_by(parent_id: nil)
end end
...@@ -70,7 +79,7 @@ module EE ...@@ -70,7 +79,7 @@ module EE
def feature_available_in_plan?(feature) def feature_available_in_plan?(feature)
@features_available_in_plan ||= Hash.new do |h, 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 end
@features_available_in_plan[feature] @features_available_in_plan[feature]
...@@ -126,7 +135,7 @@ module EE ...@@ -126,7 +135,7 @@ module EE
private private
def validate_plan_name 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') errors.add(:plan, 'is not included in the list')
end end
end end
...@@ -142,11 +151,11 @@ module EE ...@@ -142,11 +151,11 @@ module EE
end end
def plans def plans
@ancestors_plans ||= @plans ||=
if parent_id 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 else
[plan] Array(plan)
end end
end 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