team.rb revision b2dbef8c73faa240d208f3ff9fb49b21d9a894a5
e8838207238c3ffb22159e51deb37ec24ba89c76Till Mossakowskiclass Team < ActiveRecord::Base
60c9fc413e77110e71b7e8c41ffbc645b4ed90b4Julian Kornberger
fb0fc9c7ffa0a722fec20358a243cc42af57a4fbhenning mueller include Common::Scopes
5c4c13ac875d79def115ce3d8443d4ec87b3252aJulian Kornberger has_many :team_users
5c4c13ac875d79def115ce3d8443d4ec87b3252aJulian Kornberger has_many :users, :through => :team_users
5c4c13ac875d79def115ce3d8443d4ec87b3252aJulian Kornberger has_many :permissions, :as => :subject
ee5342a8882c2fc7631fcffb5497e6597747887cTim Reddehase
db7b1bfd41eb01238f00c7df022f4c11d6414e8cEugen Kuksa # create admin user after team creation
0af513ab5680818fba780f05ef7cf16e0c0cb0d8henning mueller attr_accessor :admin_user
827e98b08be3acd813ab859061f7e6318b7f8abdDaniel Couto Vale attr_accessible :admin_user
b17c7a521e12ece55af76387a9668acb5f830d43Sascha Graef after_create :create_admin_user
827e98b08be3acd813ab859061f7e6318b7f8abdDaniel Couto Vale
7152ae731eaa6fb83bc8147a463fae3a342d6360Sascha Graef attr_accessible :name
ee5342a8882c2fc7631fcffb5497e6597747887cTim Reddehase
4c72e6358f909d53733ce548ee694a4c17923a4aSascha Graef strip_attributes :only => :name
5c4c13ac875d79def115ce3d8443d4ec87b3252aJulian Kornberger
5c4c13ac875d79def115ce3d8443d4ec87b3252aJulian Kornberger scope :autocomplete_search, ->(query) {
88d63c94a94060017febcabb0862b11ed3685f6aDaniel Couto Vale where("name LIKE ?", "%" << query << "%")
f0cc21da871d51c6e8c2dafaf4b076cc8b45ec0bTim Reddehase }
f0cc21da871d51c6e8c2dafaf4b076cc8b45ec0bTim Reddehase
2bc45485d0fe7200960bd17f1b81ea477bf16befTim Reddehase validates :name,
268e4c0f7fc6630f88604079ab4ede1b8963ec6aSascha Graef :presence => true,
d327eb79653cf7820fe583473ab35816d44cd757Eugen Kuksa :length => { :minimum => 3, :maximum => 50 },
e276f66fed51be31fe120ad73f369f497aebdabbEileen Bolloff :uniqueness => { :case_sensitive => false }
d327eb79653cf7820fe583473ab35816d44cd757Eugen Kuksa
aa6a6b29584351c27210c27d29151fccbba61212Tim Reddehase def to_s
d327eb79653cf7820fe583473ab35816d44cd757Eugen Kuksa name
aa6a6b29584351c27210c27d29151fccbba61212Tim Reddehase end
d327eb79653cf7820fe583473ab35816d44cd757Eugen Kuksa
aa6a6b29584351c27210c27d29151fccbba61212Tim Reddehase # does the given user have admin-privileges in this team?
d327eb79653cf7820fe583473ab35816d44cd757Eugen Kuksa def admin?(user)
aa6a6b29584351c27210c27d29151fccbba61212Tim Reddehase user && (user.admin? || team_users.admin.find_by_user_id(user.id))
d327eb79653cf7820fe583473ab35816d44cd757Eugen Kuksa end
aa6a6b29584351c27210c27d29151fccbba61212Tim Reddehase
1d405fd1ac68b72d526b1637cb34d507ae24c56ahenning mueller protected
0af513ab5680818fba780f05ef7cf16e0c0cb0d8henning mueller
8e48b4c76e2fa30469470d03d6e7e403e8419ac4Sascha Graef # create admin user after team-creation
86ef19586a07f694886961e298f793803b8f4d54henning mueller def create_admin_user
41292ec9bfaafec2c0592a3ac0092e4c412ee7e8henning mueller if admin_user
19c33b67e5f1d783f812f85d2f681949484f925bEugen Kuksa team_users.create! \
0af513ab5680818fba780f05ef7cf16e0c0cb0d8henning mueller admin: true,
0af513ab5680818fba780f05ef7cf16e0c0cb0d8henning mueller user: admin_user
00c175adc24043fbcd4cd5e3769c9d13e31ff831Model Renamer end
00c175adc24043fbcd4cd5e3769c9d13e31ff831Model Renamer end
00c175adc24043fbcd4cd5e3769c9d13e31ff831Model Renamer
00c175adc24043fbcd4cd5e3769c9d13e31ff831Model Renamerend
00c175adc24043fbcd4cd5e3769c9d13e31ff831Model Renamer