Module: MultiSite::ScopedModel::ClassMethods

Defined in:
lib/multi_site/scoped_model.rb

Instance Method Summary collapse

Instance Method Details

#is_site_scoped(options = {}) ⇒ Object

only option at the moment is :shareable, which we take to mean that sites are optional: if true it causes us not to set the site automatically or to validate its presence, and to extend the scoping conditions so that objects with no site are returned as well as objects with the specified site that is, anything without a site is considered to be shared among all sites the default is false



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/multi_site/scoped_model.rb', line 20

def is_site_scoped(options={})
  return if is_site_scoped?
  
  options = {
    :shareable => false
  }.merge(options)

  class_eval <<-EO
    default_scope {where(site_scope_condition)}
    extend MultiSite::ScopedModel::ScopedClassMethods
    include MultiSite::ScopedModel::ScopedInstanceMethods
  EO
  
  belongs_to :site
  Site.send(:has_many, plural_symbol_for_class)

  before_validation :set_site
  validates_presence_of :site unless options[:shareable]

  class << self
    attr_accessor :shareable
    alias_method_chain :paginate, :site
    %w{count average minimum maximum sum}.each do |getter|
      alias_method_chain getter.intern, :site
    end
  end
  
  self.shareable = options[:shareable]
end

#is_site_scoped?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/multi_site/scoped_model.rb', line 9

def is_site_scoped?
  false
end