lib/spontaneous/plugins/application/state.rb in spontaneous-0.2.0.alpha7 vs lib/spontaneous/plugins/application/state.rb in spontaneous-0.2.0.beta1
- old
+ new
@@ -1,22 +1,51 @@
# encoding: UTF-8
module Spontaneous::Plugins::Application
module State
- extend ActiveSupport::Concern
+ extend Spontaneous::Concern
module ClassMethods
def init(options={})
# return false if loaded?
self.environment = (options.delete(:environment) || ENV["SPOT_ENV"] || :development)
self.mode = options.delete(:mode) || ENV["SPOT_MODE"] || :back
root = options.delete(:root) || ENV["SPOT_ROOT"] || Dir.pwd
site = Spontaneous::Site.instantiate(root, environment, mode)
+ lib = File.expand_path(File.join(root, "lib"))
+ $:.push(lib) unless $:.include?(lib)
Spontaneous::Logger.setup(:log_level => options[:log_level], :logfile => options[:logfile], :cli => options[:cli])
site.initialize!
site.schema.validate! if self.mode == :console
logger.warn "Auto login is enabled and set to '#{site.config.auto_login}'. Please ensure this is disabled in production mode by removing the 'auto_login' setting from your environment file." if site.config.auto_login and mode == :back
Thread.current[:spontaneous_loaded] = true
+ end
+
+ # This is called after definition of the Content model.
+ #
+ # Site = Spontaneous.site(Content)
+ #
+ # It is a safe way to define the content model that should be used
+ # globally as it checks for its existance before overwriting.
+ #
+ # I could do the assignment of Spontaneous::Content automatically
+ # after creation of the first content model, but this method provides
+ # a nice way to create the ::Site constant in the user/site code
+ def site(content_model)
+ site!(content_model) unless defined?(Spontaneous::Content)
+ Spontaneous::Site
+ end
+
+ # This forces the assignment of Spontaneous::Content, overwriting any
+ # previous value.
+ #
+ # Used in tests.
+ #
+ def site!(content_model)
+ spot = ::Spontaneous
+ Spontaneous.send :remove_const, :Content if defined?(Spontaneous::Content)
+ Spontaneous.const_set(:Content, content_model)
+ Spontaneous::Site
end
def loaded?
Thread.current[:spontaneous_loaded]
end