Sha256: b390e6ba1164ef614b911f77cf785fc1d2c0a27fc5052f68a2bfd3a90b0e0d23

Contents?: true

Size: 1.11 KB

Versions: 10

Compression:

Stored size: 1.11 KB

Contents

class Team < ActiveRecord::Base
  include Houston::Props

  has_and_belongs_to_many :users, readonly: true
  has_many :projects
  has_many :roles, -> { joins(:user).merge(User.unretired) }, class_name: "TeamUser", dependent: :destroy, validate: false

  default_scope -> { order(name: :asc) }

  accepts_nested_attributes_for :roles, :allow_destroy => true, # <-- !todo: authorized access only
    reject_if: proc { |attrs| attrs[:user_id].blank? }



  Houston.config.roles.each do |role|
    collection_name = role.downcase.gsub(' ', '_').pluralize
    class_eval <<-RUBY
      has_many :#{collection_name}, -> { where(["? = ANY(teams_users.roles)", "#{role}"]) }, class_name: "User", through: :roles, source: :user
    RUBY
  end



  class << self
    def projects
      Project.where(team_id: all.select(:id))
    end

    def project_ids
      projects.ids
    end
  end



  def add_teammate(user_or_id, *desired_roles)
    teammate = roles.find_or_initialize_by((user_or_id.is_a?(User) ? :user : :user_id) => user_or_id)
    teammate.team = self
    teammate.roles = teammate.roles | desired_roles
    teammate.save!
  end

end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
houston-core-0.9.2 app/models/team.rb
houston-core-0.9.1 app/models/team.rb
houston-core-0.9.0 app/models/team.rb
houston-core-0.9.0.rc1 app/models/team.rb
houston-core-0.8.4 app/models/team.rb
houston-core-0.8.3 app/models/team.rb
houston-core-0.8.2 app/models/team.rb
houston-core-0.8.1 app/models/team.rb
houston-core-0.8.0 app/models/team.rb
houston-core-0.8.0.pre2 app/models/team.rb