Module: RepositoryManager::HasRepository::ClassMethods

Defined in:
lib/repository_manager/has_repository.rb

Instance Method Summary collapse

Instance Method Details

#has_repository(options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/repository_manager/has_repository.rb', line 6

def has_repository(options = {})

  has_many :sharings, through: :sharings_members, class_name: 'RepositoryManager::Sharing'
  has_many :sharings_members, class_name: 'RepositoryManager::SharingsMember', as: :member, dependent: :destroy
  has_many :sharings_owners, as: :owner, class_name: 'RepositoryManager::Sharing'

  # The own repo_items
  has_many :repo_items, as: :owner, class_name: 'RepositoryManager::RepoItem' #, dependent: :destroy
  # The sharing repo_items
  has_many :shared_repo_items, through: :sharings, source: :repo_item, class_name: 'RepositoryManager::RepoItem'

  if Rails::VERSION::MAJOR == 4
    has_many :root_repo_items, -> { where ancestry: nil }, as: :owner, class_name: 'RepositoryManager::RepoItem'
    has_many :root_shared_repo_items,  -> { where ancestry: nil }, through: :sharings, source: :repo_item, class_name: 'RepositoryManager::RepoItem'
  else
    has_many :root_repo_items, where(ancestry: nil), as: :owner, class_name: 'RepositoryManager::RepoItem'
    has_many :root_shared_repo_items, where(ancestry: nil), through: :sharings, source: :repo_item, class_name: 'RepositoryManager::RepoItem'
  end

  #scope :all_repo_items, -> { self.repo_items.shared_repo_items }

  #All repo_items (own and sharings)
  #has_many :all_repo_items

  include RepositoryManager::HasRepository::LocalInstanceMethods
end