1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-29 10:24:20 +01:00
fab-manager/app/models/group.rb

63 lines
1.8 KiB
Ruby
Raw Normal View History

2015-05-05 03:10:25 +02:00
class Group < ActiveRecord::Base
has_many :plans
2016-03-23 18:39:41 +01:00
has_many :users
2015-05-05 03:10:25 +02:00
has_many :trainings_pricings, 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
2016-03-23 18:39:41 +01:00
scope :all_except_admins, -> { where.not(slug: 'admins') }
2016-03-23 18:39:41 +01:00
extend FriendlyId
friendly_id :name, use: :slugged
validates :name, :slug, presence: true
after_create :create_prices
after_create :create_statistic_subtype
after_update :update_statistic_subtype, if: :name_changed?
def destroyable?
users.empty? and plans.empty?
end
private
def create_prices
create_trainings_pricings
create_machines_prices
create_spaces_prices
end
def create_trainings_pricings
Training.all.each do |training|
TrainingsPricing.create(group: self, training: training, amount: 0)
2016-03-23 18:39:41 +01:00
end
end
2016-03-23 18:39:41 +01:00
def create_machines_prices
Machine.all.each do |machine|
Price.create(priceable: machine, group: self, amount: 0)
2016-03-23 18:39:41 +01:00
end
end
2016-03-23 18:39:41 +01:00
def create_spaces_prices
Space.all.each do |space|
Price.create(priceable: space, group: self, amount: 0)
end
end
2016-03-23 18:39:41 +01:00
def create_statistic_subtype
user_index = StatisticIndex.find_by(es_type_key: 'user')
StatisticSubType.create!( statistic_types: user_index.statistic_types, key: slug, label: name)
2016-03-23 18:39:41 +01:00
end
2015-05-05 03:10:25 +02:00
2016-03-23 18:39:41 +01:00
def update_statistic_subtype
user_index = StatisticIndex.find_by(es_type_key: 'user')
subtype = StatisticSubType.joins(statistic_type_sub_types: :statistic_type)
.where(key: slug, statistic_types: { statistic_index_id: user_index.id })
.first
subtype.label = name
2016-03-23 18:39:41 +01:00
subtype.save!
end
2015-05-05 03:10:25 +02:00
end