From c383bcdc2828c665db2df81c997bdcd00eef6099 Mon Sep 17 00:00:00 2001 From: Larissa Reis Date: Wed, 13 Apr 2016 03:59:18 -0300 Subject: [PATCH] Creates concerns for concealing common attributes for different types of person --- app/models/concerns/human.rb | 33 +++++++++++++++++++++++++++++++++ app/models/concerns/profile_entity.rb | 33 +++++++++++++++++++++++++++++++++ app/models/external_person.rb | 7 +++++++ app/models/person.rb | 29 ++--------------------------- app/models/profile.rb | 31 ++++--------------------------- 5 files changed, 79 insertions(+), 54 deletions(-) create mode 100644 app/models/concerns/human.rb create mode 100644 app/models/concerns/profile_entity.rb create mode 100644 app/models/external_person.rb diff --git a/app/models/concerns/human.rb b/app/models/concerns/human.rb new file mode 100644 index 0000000..3f5f59c --- /dev/null +++ b/app/models/concerns/human.rb @@ -0,0 +1,33 @@ +module Human + extend ActiveSupport::Concern + + included do + has_many :comments, :foreign_key => :author_id + has_many :abuse_reports, :foreign_key => 'reporter_id', :dependent => :destroy + + scope :abusers, -> { + joins(:abuse_complaints).where('tasks.status = 3').distinct.select('profiles.*') + } + scope :non_abusers, -> { + distinct.select("profiles.*"). + joins("LEFT JOIN tasks ON profiles.id = tasks.requestor_id AND tasks.type='AbuseComplaint'"). + where("tasks.status != 3 OR tasks.id is NULL") + } + end + + def already_reported?(profile) + abuse_reports.any? { |report| report.abuse_complaint.reported == profile && report.abuse_complaint.opened? } + end + + def register_report(abuse_report, profile) + AbuseComplaint.create!(:reported => profile, :target => profile.environment) if !profile.opened_abuse_complaint + abuse_report.abuse_complaint = profile.opened_abuse_complaint + abuse_report.reporter = self + abuse_report.save! + end + + def abuser? + AbuseComplaint.finished.where(:requestor_id => self).count > 0 + end + +end diff --git a/app/models/concerns/profile_entity.rb b/app/models/concerns/profile_entity.rb new file mode 100644 index 0000000..e39aab7 --- /dev/null +++ b/app/models/concerns/profile_entity.rb @@ -0,0 +1,33 @@ +module ProfileEntity + extend ActiveSupport::Concern + + included do + attr_accessible :name, :identifier, :environment, :redirection_after_login + + validates_presence_of :identifier, :name + validates_inclusion_of :redirection_after_login, :in => Environment.login_redirection_options.keys, :allow_nil => true + + belongs_to :environment + has_many :search_terms, :as => :context + has_many :abuse_complaints, :foreign_key => 'requestor_id', :dependent => :destroy + + end + + def disable + self.visible = false + self.save + end + + def enable + self.visible = true + self.save + end + + def preferred_login_redirection + redirection_after_login.blank? ? environment.redirection_after_login : redirection_after_login + end + + def opened_abuse_complaint + abuse_complaints.opened.first + end +end diff --git a/app/models/external_person.rb b/app/models/external_person.rb new file mode 100644 index 0000000..43f86b9 --- /dev/null +++ b/app/models/external_person.rb @@ -0,0 +1,7 @@ +# A pseudo profile is a person from a remote network +class ExternalPerson + + include Human + include ProfileEntity + +end diff --git a/app/models/person.rb b/app/models/person.rb index 323e5b6..61a6c18 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -1,6 +1,8 @@ # A person is the profile of an user holding all relationships with the rest of the system class Person < Profile + include Human + attr_accessible :organization, :contact_information, :sex, :birth_date, :cell_phone, :comercial_phone, :jabber_id, :personal_website, :nationality, :address_reference, :district, :schooling, :schooling_status, :formation, :custom_formation, :area_of_study, :custom_area_of_study, :professional_activity, :organization_website, :following_articles SEARCH_FILTERS = { @@ -88,7 +90,6 @@ class Person < Profile memberships.where('role_assignments.role_id = ?', role.id) end - has_many :comments, :foreign_key => :author_id has_many :article_followers, :dependent => :destroy has_many :following_articles, :class_name => 'Article', :through => :article_followers, :source => :article has_many :friendships, :dependent => :destroy @@ -100,8 +101,6 @@ class Person < Profile has_many :requested_tasks, :class_name => 'Task', :foreign_key => :requestor_id, :dependent => :destroy - has_many :abuse_reports, :foreign_key => 'reporter_id', :dependent => :destroy - has_many :mailings has_many :scraps_sent, :class_name => 'Scrap', :foreign_key => :sender_id, :dependent => :destroy @@ -123,15 +122,6 @@ class Person < Profile scope :more_popular, -> { order 'friends_count DESC' } - scope :abusers, -> { - joins(:abuse_complaints).where('tasks.status = 3').distinct.select('profiles.*') - } - scope :non_abusers, -> { - distinct.select("profiles.*"). - joins("LEFT JOIN tasks ON profiles.id = tasks.requestor_id AND tasks.type='AbuseComplaint'"). - where("tasks.status != 3 OR tasks.id is NULL") - } - scope :admins, -> { joins(:role_assignments => :role).where('roles.key = ?', 'environment_administrator') } scope :activated, -> { joins(:user).where('users.activation_code IS NULL AND users.activated_at IS NOT NULL') } scope :deactivated, -> { joins(:user).where('NOT (users.activation_code IS NULL AND users.activated_at IS NOT NULL)') } @@ -517,21 +507,6 @@ class Person < Profile leave_hash.to_json end - def already_reported?(profile) - abuse_reports.any? { |report| report.abuse_complaint.reported == profile && report.abuse_complaint.opened? } - end - - def register_report(abuse_report, profile) - AbuseComplaint.create!(:reported => profile, :target => profile.environment) if !profile.opened_abuse_complaint - abuse_report.abuse_complaint = profile.opened_abuse_complaint - abuse_report.reporter = self - abuse_report.save! - end - - def abuser? - AbuseComplaint.finished.where(:requestor_id => self).count > 0 - end - def control_panel_settings_button {:title => _('Edit Profile'), :icon => 'edit-profile'} end diff --git a/app/models/profile.rb b/app/models/profile.rb index b0f75db..f5c7e20 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -3,9 +3,10 @@ # which by default is the one returned by Environment:default. class Profile < ApplicationRecord - attr_accessible :name, :identifier, :public_profile, :nickname, :custom_footer, :custom_header, :address, :zip_code, :contact_phone, :image_builder, :description, :closed, :template_id, :environment, :lat, :lng, :is_template, :fields_privacy, :preferred_domain_id, :category_ids, :country, :city, :state, :national_region_code, :email, :contact_email, :redirect_l10n, :notification_time, - :redirection_after_login, :custom_url_redirection, - :email_suggestions, :allow_members_to_invite, :invite_friends_only, :secret, :profile_admin_mail_notification + include ProfileEntity + + attr_accessible :public_profile, :nickname, :custom_footer, :custom_header, :address, :zip_code, :contact_phone, :image_builder, :description, :closed, :template_id, :lat, :lng, :is_template, :fields_privacy, :preferred_domain_id, :category_ids, :country, :city, :state, :national_region_code, :email, :contact_email, :redirect_l10n, :notification_time, + :custom_url_redirection, :email_suggestions, :allow_members_to_invite, :invite_friends_only, :secret, :profile_admin_mail_notification # use for internationalizable human type names in search facets # reimplement on subclasses @@ -224,8 +225,6 @@ class Profile < ApplicationRecord welcome_page && welcome_page.published ? welcome_page.body : nil end - has_many :search_terms, :as => :context - def scraps(scrap=nil) scrap = scrap.is_a?(Scrap) ? scrap.id : scrap scrap.nil? ? Scrap.all_scraps(self) : Scrap.all_scraps(self).find(scrap) @@ -278,7 +277,6 @@ class Profile < ApplicationRecord has_many :domains, :as => :owner belongs_to :preferred_domain, :class_name => 'Domain', :foreign_key => 'preferred_domain_id' - belongs_to :environment has_many :articles, :dependent => :destroy belongs_to :home_page, :class_name => Article.name, :foreign_key => 'home_page_id' @@ -305,8 +303,6 @@ class Profile < ApplicationRecord has_many :profile_categorizations_including_virtual, :class_name => 'ProfileCategorization' has_many :categories_including_virtual, :through => :profile_categorizations_including_virtual, :source => :category - has_many :abuse_complaints, :foreign_key => 'requestor_id', :dependent => :destroy - has_many :profile_suggestions, :foreign_key => :suggestion_id, :dependent => :destroy def top_level_categorization @@ -401,7 +397,6 @@ class Profile < ApplicationRecord self.all end - validates_presence_of :identifier, :name validates_length_of :nickname, :maximum => 16, :allow_nil => true validate :valid_template validate :valid_identifier @@ -1042,20 +1037,6 @@ private :generate_url, :url_options end end - def opened_abuse_complaint - abuse_complaints.opened.first - end - - def disable - self.visible = false - self.save - end - - def enable - self.visible = true - self.save - end - def control_panel_settings_button {:title => _('Edit Profile'), :icon => 'edit-profile'} end @@ -1122,10 +1103,6 @@ private :generate_url, :url_options display_private_info_to?(current_person) || (public_fields.include?(field) && public?) end - validates_inclusion_of :redirection_after_login, :in => Environment.login_redirection_options.keys, :allow_nil => true - def preferred_login_redirection - redirection_after_login.blank? ? environment.redirection_after_login : redirection_after_login - end settings_items :custom_url_redirection, type: String, default: nil def remove_from_suggestion_list(person) -- libgit2 0.21.2