1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-03-21 12:29:03 +01:00

[ongoing] statistic profile

This commit is contained in:
Sylvain 2019-06-03 17:25:04 +02:00
parent 9ef2f142bd
commit 42e73e4315
6 changed files with 85 additions and 3 deletions

View File

@ -1,6 +1,7 @@
class Group < ActiveRecord::Base
has_many :plans
has_many :users
has_many :statistic_profiles
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

View File

@ -14,7 +14,8 @@ class Profile < ActiveRecord::Base
validates :birthday, presence: true
validates_numericality_of :phone, only_integer: true, allow_blank: false
after_save :update_invoicing_profile
after_save :update_invoicing_profile, if: invoicing_data_was_modified?
after_save :update_statistic_profile, if: statistic_data_was_modified?
def full_name
# if first_name or last_name is nil, the empty string will be used as a temporary replacement
@ -51,6 +52,14 @@ class Profile < ActiveRecord::Base
private
def invoicing_data_was_modified?
first_name_changed? or last_name_changed? or new_record?
end
def statistic_data_was_modified?
birthday_changed? or gender_changed? or new_record?
end
def update_invoicing_profile
if user.invoicing_profile.nil?
InvoicingProfile.create!(
@ -66,4 +75,19 @@ class Profile < ActiveRecord::Base
end
end
def update_statistic_profile
if statistic_profile.nil?
StatisticProfile.create!(
user: user,
birthday: birthday,
gender: gender
)
else
statistic_profile.update_attributes(
birthday: birthday,
gender: gender
)
end
end
end

View File

@ -0,0 +1,6 @@
class StatisticProfile < ActiveRecord::Base
belongs_to :user
belongs_to :group
# relations to reservations, trainings, subscriptions
end

View File

@ -24,6 +24,9 @@ class User < ActiveRecord::Base
has_one :invoicing_profile, dependent: :nullify
accepts_nested_attributes_for :invoicing_profile
has_one :statistic_profile, dependent: :nullify
accepts_nested_attributes_for :statistic_profile
has_many :my_projects, foreign_key: :author_id, class_name: 'Project', dependent: :destroy
has_many :project_users, dependent: :destroy
has_many :projects, through: :project_users
@ -63,7 +66,8 @@ class User < ActiveRecord::Base
after_commit :create_stripe_customer, on: [:create]
after_commit :notify_admin_when_user_is_created, on: :create
after_update :notify_group_changed, if: :group_id_changed?
after_save :update_invoicing_profile
after_save :update_invoicing_profile, if: invoicing_data_was_modified?
after_save :update_statistic_profile, if: statistic_data_was_modified?
attr_accessor :cgu
delegate :first_name, to: :profile
@ -362,10 +366,19 @@ class User < ActiveRecord::Base
attached_object: self
end
def invoicing_data_was_modified?
email_changed? or new_record?
end
def statistic_data_was_modified?
group_id_changed? or new_record?
end
def update_invoicing_profile
if invoicing_profile.nil?
InvoicingProfile.create!(
user: user,
user: self,
email: email
)
else
@ -374,4 +387,17 @@ class User < ActiveRecord::Base
)
end
end
def update_statistic_profile
if statistic_profile.nil?
StatisticProfile.create!(
user: self,
group_id: group_id
)
else
statistic_profile.update_attributes(
group_id: group_id
)
end
end
end

View File

@ -0,0 +1,10 @@
class CreateStatisticProfile < ActiveRecord::Migration
def change
create_table :statistic_profiles do |t|
t.boolean :gender
t.date :birthday
t.belongs_to :group, index: true, foreign_key: true
t.belongs_to :user, index: true, foreign_key: true
end
end
end

View File

@ -0,0 +1,15 @@
class FeedStatisticProfile < ActiveRecord::Migration
def change
User.all.each do |u|
p = u.profile
puts "WARNING: User #{u.id} has no profile" and next unless p
StatisticProfile.create!(
user: u,
group: u.group,
gender: p.gender,
birthday: p.birthday
)
end
end
end