Sha256: 73f4a11c1a241595950f058569cbf2d9dc231fb21ae13aae4dfea65edcdc711f

Contents?: true

Size: 1.18 KB

Versions: 10

Compression:

Stored size: 1.18 KB

Contents

class TeamUser < ActiveRecord::Base

  belongs_to :user
  belongs_to :team
  has_many :projects, through: :team

  validates :user_id, presence: true
  validates :team, presence: true
  validate :roles_should_all_be_defined_by_config



  Houston.config.roles.each do |role|
    method_name = role.downcase.gsub(' ', '_')
    class_eval <<-RUBY
    def #{method_name}?
      name == "#{role}"
    end

    def self.#{method_name.pluralize}
      with_role("#{role}")
    end
    RUBY
  end



  class << self
    def to_users
      User.where(id: all.select(:user_id))
    end

    def to_projects
      Project.where(team_id: all.select(:team_id))
    end

    def with_role(role)
      where ["? = ANY(roles)", role]
    end

    def for_user(user_or_id)
      user_id = user_or_id.is_a?(User) ? user_or_id.id : user_or_id
      where user_id: user_id
    end

    def any?
      count > 0
    end
  end



  def roles
    super || []
  end



private

  def roles_should_all_be_defined_by_config
    roles.each do |role|
      next if Houston.config.roles.member?(role)
      errors.add :roles, "includes #{role.inspect} which is not a defined role (#{Houston.config.roles.join(", ")})"
    end
  end

end

Version data entries

10 entries across 10 versions & 1 rubygems

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