Sha256: 504d9d7641590ebe1cd2e4833ac1897b444af79f4445561a9758825c2bb48b07
Contents?: true
Size: 1.88 KB
Versions: 33
Compression:
Stored size: 1.88 KB
Contents
module Saucy module Project extend ActiveSupport::Concern included do belongs_to :account has_many :permissions, :dependent => :destroy has_many :users, :through => :permissions validates_presence_of :account_id, :keyword, :name validates_uniqueness_of :keyword, :scope => :account_id validates_format_of :keyword, :with => %r{^[a-z0-9]+$}, :message => "must be only lower case letters." after_create :assign_default_memberships after_save :update_memberships # We have to define this here instead of mixing it in, # because ActiveRecord does the same def user_ids=(new_user_ids) @new_user_ids = new_user_ids.reject { |user_id| user_id.blank? } end end module ClassMethods def visible_to(user) where(['projects.id IN(?)', user.project_ids]) end def by_name order("projects.name") end end module InstanceMethods def to_param keyword end def has_member?(user) permissions. joins(:membership). exists?(:memberships => { :user_id => user.id }) end private def assign_default_memberships account.memberships.where(:admin => true).each do |membership| self.permissions.create(:membership => membership) end end def update_memberships if @new_user_ids removed_user_ids = self.user_ids - @new_user_ids added_user_ids = @new_user_ids - self.user_ids permissions.where(:user_id => removed_user_ids).destroy_all added_user_ids.each do |added_user_id| membership = account.memberships.where(:user_id => added_user_id).first permissions.create!(:membership => membership) end end end end end end
Version data entries
33 entries across 33 versions & 1 rubygems