diff --git a/app/models/group.rb b/app/models/group.rb index a7ad99f8c..ee2abfaad 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -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 diff --git a/app/models/profile.rb b/app/models/profile.rb index 57e2cfdb3..c9aaa6e13 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -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 diff --git a/app/models/statistic_profile.rb b/app/models/statistic_profile.rb new file mode 100644 index 000000000..2c88d7bfa --- /dev/null +++ b/app/models/statistic_profile.rb @@ -0,0 +1,6 @@ +class StatisticProfile < ActiveRecord::Base + belongs_to :user + belongs_to :group + + # relations to reservations, trainings, subscriptions +end diff --git a/app/models/user.rb b/app/models/user.rb index 574b4c9b7..d18e7fea9 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/db/migrate/20190603150642_create_statistic_profile.rb b/db/migrate/20190603150642_create_statistic_profile.rb new file mode 100644 index 000000000..68106a700 --- /dev/null +++ b/db/migrate/20190603150642_create_statistic_profile.rb @@ -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 diff --git a/db/migrate/20190603151142_feed_statistic_profile.rb b/db/migrate/20190603151142_feed_statistic_profile.rb new file mode 100644 index 000000000..71ceafac0 --- /dev/null +++ b/db/migrate/20190603151142_feed_statistic_profile.rb @@ -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