task_group.rb
Hipposoft 2008
Base class for things which group tasks, such as Projects or Customers. See below for more details.
07-Mar-2008 (ADH): Created from project.rb.
Define default sort order for caller convenience.
Want to do this, for future compatibility with Rails 4:
scope :active, -> { where( :active => true ) } scope :inactive, -> { where( :active => false ) }
…however it breaks:
https://github.com/rails/rails/issues/10658
Thus, bypass the syntactic sugar and just write the methods.
# File app/models/task_group.rb, line 34 def self.active; where( :active => true ); end
Find all objects which the given user is allowed to see. A conditions hash may be passed to further restrict the search (that is, the “{…}” in “find( :all, :conditions => {…})”).
# File app/models/task_group.rb, line 64 def self.find_permitted( user, conditions = nil ) # Can't see any items if no user is given. Can see all items if # the user is unrestricted. return [] unless user items = find( :all, { :conditions => conditions } ) return items if user.privileged? allowed = [] items.each do | item | allowed.push( item ) if item.is_permitted_for?( user ) end return allowed end
# File app/models/task_group.rb, line 35 def self.inactive; where( :active => false ); end
Return the object’s title. This is done for loose compatibility with a Task object during report generation.
# File app/models/task_group.rb, line 56 def augmented_title return self.title end
Is the given user permitted to update this object? Restricted users cannot modify things. Administrators always can. Managers only can if the object is still active.
# File app/models/task_group.rb, line 87 def can_be_modified_by?( user ) return false if ( user.restricted? ) return true if ( user.admin? ) return self.active end
Is permission granted for the given user to see this object? See also find_permitted. Returns ‘true’ if permitted, else ‘false’.
# File app/models/task_group.rb, line 96 def is_permitted_for?( user ) return true if user.privileged? # User is restricted. User can only see this object if it # has at least one task associated with it and at least one # of those associated tasks appears in the user's permitted # task list, so check the intersection of the two arrays. return ( self.tasks & user.tasks ).length > 0 end