Guest User

Untitled

a guest
Jan 22nd, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. class PolcoGroup
  2. include Mongoid::Document
  3. field :name, :type => String
  4. field :type, :type => Symbol, :default => :custom
  5. field :description, :type => String
  6. index :name
  7. index :type
  8. field :follower_count
  9. field :member_count
  10.  
  11. belongs_to :owner, :class_name => "User", :inverse_of => :custom_groups
  12.  
  13. has_and_belongs_to_many :members, :class_name => "User", :inverse_of => :joined_groups
  14. has_and_belongs_to_many :followers, :class_name => "User", :inverse_of => :followed_groups
  15.  
  16. # some validations
  17. validates_uniqueness_of :name, :scope => :type
  18. validates_inclusion_of :type, :in => [:custom, :state, :district, :common, :country], :message => 'Only valid groups are custom, state, district, common, country'
  19.  
  20. scope :states, where(type: :state)
  21. scope :districts, where(type: :district)
  22. scope :customs, where(type: :custom)
  23.  
  24. # time to create the ability to follow
  25. #has_many :followers, :class_name => "User", :inverse_of =>
  26.  
  27. def followers_count
  28. self.follower_ids.count
  29. end
  30.  
  31. def members_count
  32. self.member_ids.count
  33. end
  34.  
  35. def the_rep
  36. if self.type == :district
  37. Legislator.all.select{|l| l.district_name == self.name}.first
  38. else
  39. "Only districts can have a representative"
  40. end
  41. end
  42. end
Add Comment
Please, Sign In to add comment