Module: MultiSite::ScopedModel::ScopedClassMethods

Defined in:
lib/multi_site/scoped_model.rb

Instance Method Summary collapse

Instance Method Details

#current_siteObject



96
97
98
# File 'lib/multi_site/scoped_model.rb', line 96

def current_site
  Page.current_site
end

#current_site!Object

Raises:

  • (ActiveRecord::SiteNotFound)


91
92
93
94
# File 'lib/multi_site/scoped_model.rb', line 91

def current_site!
  raise(ActiveRecord::SiteNotFound, "#{self} is site-scoped but current_site is #{self.current_site.inspect}", caller) if sites? && !self.current_site && !self.is_shareable?
  self.current_site
end

#find_initial_without_site(options) ⇒ Object



82
83
84
85
# File 'lib/multi_site/scoped_model.rb', line 82

def find_initial_without_site(options)
  options.update(:limit => 1)
  all_without_site(options).first
end

#find_without_site(*args) ⇒ Object

this only works with :all and :first and should only be used in odd cases like migration.



72
73
74
75
76
77
78
79
80
# File 'lib/multi_site/scoped_model.rb', line 72

def find_without_site(*args)
  options = args.extract_options!
  #set_readonly_option!(options)

  case args.first
    when :first then find_initial_without_site(options)     # defined here
    when :all   then all_without_site(options)       # already defined by the alias chain
  end
end

#is_shareable?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/multi_site/scoped_model.rb', line 119

def is_shareable?
  !!self.shareable
end

#is_site_scoped?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/multi_site/scoped_model.rb', line 115

def is_site_scoped?
  true
end

#paginate_with_site(options = {}) ⇒ Object



54
55
56
57
58
59
# File 'lib/multi_site/scoped_model.rb', line 54

def paginate_with_site(options={})
  return paginate_without_site(options) unless sites?
  where(site_scope_condition) do
    paginate_without_site(options)
  end
end

#plural_symbol_for_classObject



111
112
113
# File 'lib/multi_site/scoped_model.rb', line 111

def plural_symbol_for_class
  self.to_s.pluralize.underscore.intern
end

#site_scope_conditionObject



100
101
102
103
104
105
106
107
108
109
# File 'lib/multi_site/scoped_model.rb', line 100

def site_scope_condition
  if self.shareable
    condition = ""
    condition << "#{self.table_name}.site_id = #{self.current_site.id} OR " if self.current_site
    condition << "#{self.table_name}.site_id IS NULL" 
  else
    condition = "#{self.table_name}.site_id = #{self.current_site!.id}"
  end
  condition
end

#sites?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/multi_site/scoped_model.rb', line 87

def sites?
  Site.table_exists? && Site.several?
end