1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-20 14:54:15 +01:00

[bug] create transverse plan: do not create one for the admins group

This commit is contained in:
Sylvain 2018-11-26 11:33:48 +01:00
parent 2efd7644d2
commit 56a56565ae
4 changed files with 53 additions and 36 deletions

View File

@ -1,2 +1,6 @@
Metrics/LineLength: Metrics/LineLength:
Max: 120 Max: 120
Metrics/MethodLength:
Max: 16
Style/BracesAroundHashParameters:
EnforcedStyle: context_dependent

View File

@ -5,6 +5,7 @@
- Migrated front-end application from CoffeeScript to ECMAScript 6 (JS) - Migrated front-end application from CoffeeScript to ECMAScript 6 (JS)
- Integration of Eslint and Rubocop coding rules - Integration of Eslint and Rubocop coding rules
- Fix a bug: on small screens, display of button "change group" overflows - Fix a bug: on small screens, display of button "change group" overflows
- Fix a bug: creating a transverse plan, create one for the hidden admins group
- Fix a security issue: dependency rack has a vulnerability as described in [CVE-2018-16471](https://nvd.nist.gov/vuln/detail/CVE-2018-16471) - Fix a security issue: dependency rack has a vulnerability as described in [CVE-2018-16471](https://nvd.nist.gov/vuln/detail/CVE-2018-16471)
- Fix a security issue: dependency loofah has a vulnerability as described in [CVE-2018-16468](https://github.com/flavorjones/loofah/issues/154) - Fix a security issue: dependency loofah has a vulnerability as described in [CVE-2018-16468](https://github.com/flavorjones/loofah/issues/154)
- Updated documentation - Updated documentation

View File

@ -5,6 +5,8 @@ class Group < ActiveRecord::Base
has_many :machines_prices, ->{ where(priceable_type: 'Machine') }, class_name: 'Price', dependent: :destroy has_many :machines_prices, ->{ where(priceable_type: 'Machine') }, class_name: 'Price', dependent: :destroy
has_many :spaces_prices, ->{ where(priceable_type: 'Space') }, class_name: 'Price', dependent: :destroy has_many :spaces_prices, ->{ where(priceable_type: 'Space') }, class_name: 'Price', dependent: :destroy
scope :all_except_admins, -> { where.not(slug: 'admins') }
extend FriendlyId extend FriendlyId
friendly_id :name, use: :slugged friendly_id :name, use: :slugged
@ -19,23 +21,24 @@ class Group < ActiveRecord::Base
end end
private private
def create_prices
create_trainings_pricings
create_machines_prices
create_spaces_prices
end
def create_trainings_pricings def create_prices
Training.all.each do |training| create_trainings_pricings
TrainingsPricing.create(group: self, training: training, amount: 0) create_machines_prices
end create_spaces_prices
end end
def create_machines_prices def create_trainings_pricings
Machine.all.each do |machine| Training.all.each do |training|
Price.create(priceable: machine, group: self, amount: 0) TrainingsPricing.create(group: self, training: training, amount: 0)
end
end end
end
def create_machines_prices
Machine.all.each do |machine|
Price.create(priceable: machine, group: self, amount: 0)
end
end
def create_spaces_prices def create_spaces_prices
Space.all.each do |space| Space.all.each do |space|
@ -45,13 +48,15 @@ class Group < ActiveRecord::Base
def create_statistic_subtype def create_statistic_subtype
user_index = StatisticIndex.find_by(es_type_key: 'user') user_index = StatisticIndex.find_by(es_type_key: 'user')
StatisticSubType.create!({statistic_types: user_index.statistic_types, key: self.slug, label: self.name}) StatisticSubType.create!( statistic_types: user_index.statistic_types, key: slug, label: name)
end end
def update_statistic_subtype def update_statistic_subtype
user_index = StatisticIndex.find_by(es_type_key: 'user') user_index = StatisticIndex.find_by(es_type_key: 'user')
subtype = StatisticSubType.joins(statistic_type_sub_types: :statistic_type).where(key: self.slug, statistic_types: { statistic_index_id: user_index.id }).first subtype = StatisticSubType.joins(statistic_type_sub_types: :statistic_type)
subtype.label = self.name .where(key: slug, statistic_types: { statistic_index_id: user_index.id })
.first
subtype.label = name
subtype.save! subtype.save!
end end
end end

View File

@ -27,19 +27,19 @@ class Plan < ActiveRecord::Base
validates :amount, :group, :base_name, presence: true validates :amount, :group, :base_name, presence: true
validates :interval_count, numericality: { only_integer: true, greater_than_or_equal_to: 1 } validates :interval_count, numericality: { only_integer: true, greater_than_or_equal_to: 1 }
validates :interval_count, numericality: { less_than: 12 }, if: Proc.new {|plan| plan.interval == 'month'} validates :interval_count, numericality: { less_than: 12 }, if: proc { |plan| plan.interval == 'month' }
validates :interval_count, numericality: { less_than: 52 }, if: Proc.new {|plan| plan.interval == 'week'} validates :interval_count, numericality: { less_than: 52 }, if: proc { |plan| plan.interval == 'week' }
validates :interval, inclusion: { in: %w(year month week) } validates :interval, inclusion: { in: %w[year month week] }
validates :base_name, :slug, presence: true validates :base_name, :slug, presence: true
def self.create_for_all_groups(plan_params) def self.create_for_all_groups(plan_params)
plans = [] plans = []
Group.all.each do |group| Group.all_except_admins.each do |group|
if plan_params[:type] == 'PartnerPlan' plan = if plan_params[:type] == 'PartnerPlan'
plan = PartnerPlan.new(plan_params.except(:group_id, :type)) PartnerPlan.new(plan_params.except(:group_id, :type))
else else
plan = Plan.new(plan_params.except(:group_id, :type)) Plan.new(plan_params.except(:group_id, :type))
end end
plan.group = group plan.group = group
if plan.save if plan.save
plans << plan plans << plan
@ -48,7 +48,7 @@ class Plan < ActiveRecord::Base
return false return false
end end
end end
return plans plans
end end
def destroyable? def destroyable?
@ -73,27 +73,34 @@ class Plan < ActiveRecord::Base
def human_readable_duration def human_readable_duration
i18n_key = "duration.#{interval}" i18n_key = "duration.#{interval}"
"#{I18n.t(i18n_key, count: interval_count)}" I18n.t(i18n_key, count: interval_count).to_s
end end
def human_readable_name(opts = {}) def human_readable_name(opts = {})
result = "#{base_name}" result = base_name.to_s
result += " - #{group.slug}" if opts[:group] result += " - #{group.slug}" if opts[:group]
result + " - #{human_readable_duration}" result + " - #{human_readable_duration}"
end end
# must be publicly accessible for the migration # must be publicly accessible for the migration
def create_statistic_type def create_statistic_type
stat_index = StatisticIndex.where({es_type_key: 'subscription'}) stat_index = StatisticIndex.where(es_type_key: 'subscription')
type = StatisticType.find_by(statistic_index_id: stat_index.first.id, key: self.duration.to_i) type = StatisticType.find_by(statistic_index_id: stat_index.first.id, key: duration.to_i)
if type == nil if type.nil?
type = StatisticType.create!({statistic_index_id: stat_index.first.id, key: self.duration.to_i, label: 'Durée : '+self.human_readable_duration, graph: true, simple: true}) type = StatisticType.create!(
statistic_index_id: stat_index.first.id,
key: duration.to_i,
label: "Durée : #{human_readable_duration}",
graph: true,
simple: true
)
end end
subtype = create_statistic_subtype subtype = create_statistic_subtype
create_statistic_association(type, subtype) create_statistic_association(type, subtype)
end end
private private
def create_stripe_plan def create_stripe_plan
stripe_plan = Stripe::Plan.create( stripe_plan = Stripe::Plan.create(
amount: amount, amount: amount,
@ -108,12 +115,12 @@ class Plan < ActiveRecord::Base
end end
def create_statistic_subtype def create_statistic_subtype
StatisticSubType.create!({key: self.slug, label: self.name}) StatisticSubType.create!(key: slug, label: name)
end end
def create_statistic_association(stat_type, stat_subtype) def create_statistic_association(stat_type, stat_subtype)
if stat_type != nil and stat_subtype != nil if !stat_type.nil? && !stat_subtype.nil?
StatisticTypeSubType.create!({statistic_type: stat_type, statistic_sub_type: stat_subtype}) StatisticTypeSubType.create!(statistic_type: stat_type, statistic_sub_type: stat_subtype)
else else
puts 'ERROR: Unable to create the statistics association for the new plan. '+ puts 'ERROR: Unable to create the statistics association for the new plan. '+
'Possible causes: the type or the subtype were not created successfully.' 'Possible causes: the type or the subtype were not created successfully.'