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

Version Path
saucy-0.2.25 lib/saucy/project.rb
saucy-0.2.24 lib/saucy/project.rb
saucy-0.2.21 lib/saucy/project.rb
saucy-0.2.20 lib/saucy/project.rb
saucy-0.2.18 lib/saucy/project.rb
saucy-0.2.17 lib/saucy/project.rb
saucy-0.2.16 lib/saucy/project.rb
saucy-0.2.15 lib/saucy/project.rb
saucy-0.2.14 lib/saucy/project.rb
saucy-0.2.13 lib/saucy/project.rb
saucy-0.2.12 lib/saucy/project.rb
saucy-0.2.11 lib/saucy/project.rb
saucy-0.2.10 lib/saucy/project.rb
saucy-0.2.9 lib/saucy/project.rb
saucy-0.2.8.1 lib/saucy/project.rb
saucy-0.2.8 lib/saucy/project.rb
saucy-0.2.7 lib/saucy/project.rb
saucy-0.2.6.1 lib/saucy/project.rb
saucy-0.2.6 lib/saucy/project.rb
saucy-0.2.5 lib/saucy/project.rb