User/Person models merge

This commit is contained in:
Stella Rouzi 2014-06-23 19:07:50 +03:00
parent 66b84e3660
commit 23bf55b66c
80 changed files with 1064 additions and 1221 deletions

View file

@ -45,4 +45,4 @@ class Comment < ActiveRecord::Base
def self.find_commentable(commentable_str, commentable_id)
commentable_str.constantize.find(commentable_id)
end
end
end

View file

@ -8,7 +8,7 @@ class Conference < ActiveRecord::Base
:start_date, :end_date, :rooms_attributes, :tracks_attributes, :dietary_choices_attributes,
:use_dietary_choices, :use_supporter_levels, :supporter_levels_attributes, :social_events_attributes,
:event_types_attributes, :registration_start_date, :registration_end_date, :logo,
:questions_attributes, :question_ids, :answers_attributes, :answer_ids,
:questions_attributes, :question_ids, :answers_attributes, :answer_ids,
:difficulty_levels_attributes, :use_difficulty_levels,
:use_vpositions, :use_vdays, :vdays_attributes, :vpositions_attributes, :use_volunteers,
:media_id, :media_type, :color, :description,
@ -114,9 +114,8 @@ class Conference < ActiveRecord::Base
# * +true+ - If the user isn't registered
def user_registered? user
return nil if user.nil?
return nil if user.person.nil?
if self.registrations.where(:person_id => user.person.id).count == 0
if self.registrations.where(:user_id => user.id).count == 0
logger.debug("User #{user.email} isn't registered to self.title")
return false
else
@ -303,28 +302,27 @@ class Conference < ActiveRecord::Base
end
##
# Returns a hash with person => submissions ordered by submissions for all conferences
# Returns a hash with user => submissions ordered by submissions for all conferences
#
# ====Returns
# * +hash+ -> person: submissions
# * +hash+ -> user: submissions
def self.get_top_submitter(limit = 5)
submitter = EventPerson.where('event_role = ?', 'submitter').limit(limit).group(:person_id)
submitter = EventUser.where('event_role = ?', 'submitter').limit(limit).group(:user_id)
counter = submitter.order('count_all desc').count
calculate_person_submission_hash(submitter, counter)
calculate_user_submission_hash(submitter, counter)
end
##
# Returns a hash with person => submissions ordered by submissions
# Returns a hash with user => submissions ordered by submissions
#
# ====Returns
# * +hash+ -> person: submissions
# * +hash+ -> user: submissions
def get_top_submitter(limit = 5)
submitter = EventPerson.joins(:event).
submitter = EventUser.joins(:event).
where('event_role = ? and conference_id = ?', 'submitter', id).
limit(limit).group(:person_id)
limit(limit).group(:user_id)
counter = submitter.order('count_all desc').count
Conference.calculate_person_submission_hash(submitter, counter)
Conference.calculate_user_submission_hash(submitter, counter)
end
##
@ -685,16 +683,16 @@ class Conference < ActiveRecord::Base
end
##
# Returns a hash with person => submissions ordered by submissions for all conferences
# Returns a hash with user => submissions ordered by submissions for all conferences
#
# ====Returns
# * +hash+ -> person: submissions
def self.calculate_person_submission_hash(submitters, counter)
# * +hash+ -> user: submissions
def self.calculate_user_submission_hash(submitters, counter)
result = ActiveSupport::OrderedHash.new
counter.each do |key, value|
submitter = submitters.where(person_id: key).first
submitter = submitters.where(user_id: key).first
if submitter
result[submitter.person] = value
result[submitter.user] = value
end
end
result
@ -720,9 +718,10 @@ class Conference < ActiveRecord::Base
# Creates a UID for the conference. Used as before_create.
#
def generate_guid
begin
guid = SecureRandom.urlsafe_base64
end while Person.where(:guid => guid).exists?
guid = SecureRandom.urlsafe_base64
# begin
# guid = SecureRandom.urlsafe_base64
# end # while User.where(:guid => guid).exists?
self.guid = guid
end

View file

@ -4,8 +4,8 @@ class DatatableSupporters < Datatable
items.each do |i|
item = []
if i.name.blank?
if !i.registration.nil? && !i.registration.person.nil?
item << i.registration.person.public_name
if !i.registration.nil? && !i.registration.user.nil?
item << i.registration.user.name
else
item << "Unknown"
end
@ -15,8 +15,8 @@ class DatatableSupporters < Datatable
end
if i.email.blank?
if !i.registration.nil? && !i.registration.person.nil?
item << i.registration.person.email
if !i.registration.nil? && !i.registration.user.nil?
item << i.registration.user.email
else
item << "Unknown"
end

View file

@ -3,10 +3,10 @@ class EmailSettings < ActiveRecord::Base
:registration_email_template, :accepted_email_template, :rejected_email_template, :confirmed_email_template,
:registration_subject, :accepted_subject, :rejected_subject, :confirmed_without_registration_subject
def get_values(conference, person, event = nil)
def get_values(conference, user, event = nil)
h = {
"email" => person.email,
"name" => person.public_name,
"email" => user.email,
"name" => user.name,
"conference" => conference.title,
"registrationlink" => Rails.application.routes.url_helpers.register_conference_url(conference.short_title, :host => CONFIG["url_for_emails"])
}
@ -18,8 +18,8 @@ class EmailSettings < ActiveRecord::Base
h
end
def generate_registration_email(conference, person)
values = get_values(conference, person)
def generate_registration_email(conference, user)
values = get_values(conference, user)
template = self.registration_email_template
parse_template(template, values)
end

View file

@ -1,18 +1,18 @@
class Event < ActiveRecord::Base
include ActiveRecord::Transitions
has_paper_trail
attr_accessible :title, :subtitle, :abstract, :description, :event_type_id, :people_attributes, :person, :proposal_additional_speakers, :track_id, :media_id, :media_type, :require_registration, :difficulty_level_id
attr_accessible :title, :subtitle, :abstract, :description, :event_type_id, :users_attributes, :user, :proposal_additional_speakers, :track_id, :media_id, :media_type, :require_registration, :difficulty_level_id
acts_as_commentable
after_create :set_week
has_many :event_people, :dependent => :destroy
has_many :event_users, :dependent => :destroy
has_many :event_attachments, :dependent => :destroy
has_many :people, :through => :event_people
has_many :speakers, :through => :event_people, :source => :person
has_many :users, :through => :event_users
has_many :speakers, :through => :event_users, :source => :user
has_many :votes
has_many :voters, :through => :votes, :source => :person
has_many :voters, :through => :votes, :source => :user
belongs_to :event_type
has_and_belongs_to_many :registrations
@ -22,9 +22,9 @@ class Event < ActiveRecord::Base
belongs_to :difficulty_level
belongs_to :conference
accepts_nested_attributes_for :event_people, :allow_destroy => true
accepts_nested_attributes_for :event_users, :allow_destroy => true
accepts_nested_attributes_for :event_attachments, :allow_destroy => true, :reject_if => :all_blank
accepts_nested_attributes_for :people
accepts_nested_attributes_for :users
before_create :generate_guid
validate :abstract_limit
@ -63,10 +63,10 @@ class Event < ActiveRecord::Base
end
end
def voted?(event, person)
event.votes.where("person_id = ?", person).first
def voted?(event, user)
event.votes.where("user_id = ?", user).first
end
def average_rating
@total_rating = 0
self.votes.each do |vote|
@ -77,18 +77,18 @@ class Event < ActiveRecord::Base
end
def submitter
result = self.event_people.where(:event_role => "submitter").first
result = self.event_users.where(:event_role => "submitter").first
if !result.nil?
result.person
result.user
else
person = nil
# Perhaps the event_people haven't been saved, if this is a new proposal
self.event_people.each do |p|
user = nil
# Perhaps the event_users haven't been saved, if this is a new proposal
self.event_users.each do |p|
if p.event_role == "submitter"
person = p.person
user = p.user
end
end
person
user
end
end
@ -122,7 +122,7 @@ class Event < ActiveRecord::Base
def process_confirmation
if self.conference.email_settings.send_on_confirmed_without_registration?
if self.conference.registrations.where(:person_id => self.submitter.id).first.nil?
if self.conference.registrations.where(:user_id => self.submitter.id).first.nil?
Mailbot.confirm_reminder_mail(self).deliver
end
end
@ -194,7 +194,7 @@ class Event < ActiveRecord::Base
def biography_exists
if self.submitter.biography_word_count == 0
errors.add(:person_biography, "must be filled out")
errors.add(:user_biography, "must be filled out")
end
end

View file

@ -1,8 +1,8 @@
class EventPerson < ActiveRecord::Base
attr_accessible :event, :person, :person_id, :event_role
class EventUser < ActiveRecord::Base
attr_accessible :event, :user, :user_id, :event_role
# TODO Do we need these roles?
ROLES = [["Speaker","speaker"], ["Submitter","submitter"], ["Moderator","moderator"]]
belongs_to :event
belongs_to :person
belongs_to :user
end

View file

@ -1,97 +0,0 @@
class Person < ActiveRecord::Base
include Gravtastic
gravtastic :size => 32
attr_accessible :email, :first_name, :last_name, :public_name, :biography, :company, :avatar, :irc_nickname, :mobile, :tshirt, :languages, :volunteer_experience
belongs_to :user, :inverse_of => :person
has_many :event_people, :dependent => :destroy
has_many :events, -> { uniq }, :through => :event_people
has_many :registrations, :dependent => :destroy
has_many :votes, :dependent => :destroy
has_many :voted_events, :through => :votes, :source => :events
validates :first_name, :presence => true
validates :last_name, :presence => true
validates :email, :presence => true
validate :biography_limit
before_create :generate_guid
before_save :set_public_name
has_attached_file :avatar,
:styles => {:tiny => "16x16>", :small => "32x32>", :large => "128x128>"},
:default_url => "person_:style.png"
validates_attachment_content_type :avatar, :content_type => [/jpg/, /jpeg/, /png/, /gif/]
alias_attribute :affiliation, :company
def to_s
if self.public_name.empty?
self.first_name + " " + self.last_name
else
self.public_name
end
end
def attending_conference? conference
Registration.where(:conference_id => conference.id,
:person_id => self.id).count
end
def proposals conference
events.where('conference_id = ? AND event_people.event_role=?', conference.id, 'submitter')
end
def proposal_count conference
proposals(conference).count
end
def biography_word_count
if self.biography.nil?
0
else
self.biography.split.size
end
end
def self.find_person_by_user_id user_id
Person.where(:user_id => user_id).first
end
def confirmed?
if User.exists?(self.user_id)
user = User.find(self.user_id)
if user.confirmed?
true
else
false
end
else
false
end
end
private
def biography_limit
if !self.biography.nil? && self.biography.split.size > 150
errors.add(:abstract, "cannot have more than 150 words")
end
end
def set_public_name
if public_name.blank?
self.public_name = ""
self.public_name = "#{first_name} #{last_name}" if !first_name.blank? && !last_name.blank?
end
end
def generate_guid
begin
guid = SecureRandom.urlsafe_base64
end while Person.where(:guid => guid).exists?
self.guid = guid
end
end

View file

@ -1,5 +1,5 @@
class Registration < ActiveRecord::Base
belongs_to :person
belongs_to :user
belongs_to :conference
belongs_to :dietary_choice
@ -10,27 +10,21 @@ class Registration < ActiveRecord::Base
has_and_belongs_to_many :qanswers
has_and_belongs_to_many :vchoices
attr_accessible :person_id, :conference_id, :attending_social_events, :attending_with_partner,
:using_affiliated_lodging, :arrival, :departure, :person_attributes, :other_dietary_choice, :dietary_choice_id,
attr_accessible :user_id, :conference_id, :attending_social_events, :attending_with_partner,
:using_affiliated_lodging, :arrival, :departure, :user_attributes, :other_dietary_choice, :dietary_choice_id,
:handicapped_access_required, :supporter_registration_attributes, :social_event_ids, :other_special_needs,
:event_ids, :attended, :volunteer, :vchoice_ids,
:qanswer_ids, :qanswers_attributes
accepts_nested_attributes_for :person
accepts_nested_attributes_for :user
accepts_nested_attributes_for :supporter_registration
accepts_nested_attributes_for :social_events
accepts_nested_attributes_for :qanswers
delegate :first_name, :to => :person
delegate :last_name, :to => :person
delegate :public_name, :to => :person
delegate :email, :to => :person
delegate :irc_nickname, :to => :person
delegate :company, :to => :person
delegate :mobile, :to => :person
delegate :languages, :to => :person
delegate :volunteer_experience, :to => :person
delegate :tshirt, :to => :person
delegate :name, :to => :user
delegate :email, :to => :user
delegate :nickname, :to => :user
delegate :affiliation, :to => :user
alias_attribute :other_needs, :other_special_needs

View file

@ -9,9 +9,10 @@ class Room < ActiveRecord::Base
private
def generate_guid
begin
guid = SecureRandom.urlsafe_base64
end while Person.where(:guid => guid).exists?
guid = SecureRandom.urlsafe_base64
# begin
# guid = SecureRandom.urlsafe_base64
# end while Person.where(:guid => guid).exists?
self.guid = guid
end

View file

@ -1,13 +1,13 @@
class SupporterRegistration < ActiveRecord::Base
belongs_to :supporter_level
belongs_to :registration
before_save :set_attributes_from_person
before_save :set_attributes_from_user
attr_accessible :registration, :supporter_level_id, :name, :email, :supporter_level, :code, :code_is_valid, :conference_id
def set_attributes_from_person
self.name ||= registration.try(:person).try(:public_name)
self.email ||= registration.try(:person).try(:email)
def set_attributes_from_user
self.name ||= registration.try(:user).try(:name)
self.email ||= registration.try(:user).try(:email)
true
end
end

View file

@ -8,9 +8,10 @@ class Track < ActiveRecord::Base
private
def generate_guid
begin
guid = SecureRandom.urlsafe_base64
end while Person.where(:guid => guid).exists?
guid = SecureRandom.urlsafe_base64
# begin
# guid = SecureRandom.urlsafe_base64
# end while Person.where(:guid => guid).exists?
self.guid = guid
end

View file

@ -1,4 +1,7 @@
class User < ActiveRecord::Base
include Gravtastic
gravtastic :size => 32
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
@ -8,18 +11,22 @@ class User < ActiveRecord::Base
:omniauthable, omniauth_providers: [:novell, :google, :facebook]
has_and_belongs_to_many :roles
has_one :person, :inverse_of => :user
has_many :openids
attr_accessible :email, :password, :password_confirmation, :remember_me, :role_id, :role_ids,
:person_attributes
accepts_nested_attributes_for :person
:name, :email_public, :biography, :nickname, :affiliation
has_many :event_users, :dependent => :destroy
has_many :events, -> { uniq }, :through => :event_users
has_many :registrations, :dependent => :destroy
has_many :votes, :dependent => :destroy
has_many :voted_events, :through => :votes, :source => :events
accepts_nested_attributes_for :roles
before_create :setup_role
before_create :create_person
delegate :last_name, :first_name, :public_name, to: :person
validates :name, presence: true
# Searches for user based on email. Returns found user or new user.
# ====Returns
@ -33,6 +40,7 @@ class User < ActiveRecord::Base
if user.new_record?
user.email = auth.info.email
user.name = auth.info.name
user.password = Devise.friendly_token[0, 20]
user.skip_confirmation!
end
@ -50,33 +58,70 @@ class User < ActiveRecord::Base
end
def setup_role
roles << Role.find_by(name: 'Admin') if User.count == 0
roles << Role.find_by(name: 'Participant') if roles.empty?
roles << Role.where(name: 'Admin') if User.count == 0
roles << Role.where(name: 'Participant') if roles.empty?
end
def popup_details
details = "<b>Sign-in Count</b><br>"
details += "#{self.sign_in_count}<br>"
details += "<b>Current Sign-in</b><br>"
details += "#{self.current_sign_in_at}<br>"
details += "<b>Last Sign-in</b><br>"
details += "#{self.last_sign_in_at}<br>"
details += "<b>Current Sign-in IP</b><br>"
details += "#{self.current_sign_in_ip}<br>"
details += "<b>Last Sign-in IP</b><br>"
details += "#{self.last_sign_in_ip}<br>"
details += "<b>Created at</b><br>"
details += "#{self.created_at}<br>"
def self.prepare(params)
email = params['email']
user = User.where(email: email).first_or_initialize
# If there is a new user, add the necessary attributes
if user.new_record?
user.password = Devise.friendly_token[0,20]
user.skip_confirmation!
user.attributes = params
end
user
end
def registered
registrations = self.registrations
if registrations.count == 0
'None'
else
registrations.map { |r| r.conference.title }.join ', '
end
end
def attended
registrations_attended = self.registrations.where(attended: true)
if registrations_attended.count == 0
'None'
else
registrations_attended.map { |r| r.conference.title }.join ', '
end
end
def confirmed?
!confirmed_at.nil?
end
private
def create_person
# TODO Search people for existing email address, add to their account
build_person(email: email) if person.nil?
true
def attending_conference? conference
Registration.where(:conference_id => conference.id,
:user_id => self.id).count
end
def proposals conference
events.where('conference_id = ? AND event_users.event_role=?', conference.id, 'submitter')
end
def proposal_count conference
proposals(conference).count
end
def biography_word_count
if self.biography.nil?
0
else
self.biography.split.size
end
end
private
def biography_limit
if !self.biography.nil? && self.biography.split.size > 150
errors.add(:abstract, "cannot have more than 150 words")
end
end
end

View file

@ -1,10 +1,8 @@
class Vote < ActiveRecord::Base
attr_accessible :rating
belongs_to :person
belongs_to :user
belongs_to :event
delegate :first_name, :to => :person
delegate :last_name, :to => :person
delegate :public_name, :to => :person
end
delegate :name, :to => :user
end