Class: Rails::Configuration

Inherits:
Object show all
Defined in:
lib/initializer.rb

Overview

The Configuration class holds all the parameters for the Initializer and ships with defaults that suites most Rails applications. But it’s possible to overwrite everything. Usually, you’ll create an Configuration file implicitly through the block running on the Initializer, but it’s also possible to create the Configuration instance in advance and pass it in like this:

config = Rails::Configuration.new
Rails::Initializer.run(:process, config)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Create a new Configuration instance, initialized with the default values.



676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
# File 'lib/initializer.rb', line 676

def initialize
  set_root_path!

  self.frameworks                   = default_frameworks
  self.load_paths                   = default_load_paths
  self.load_once_paths              = default_load_once_paths
  self.log_path                     = default_log_path
  self.log_level                    = default_log_level
  self.view_path                    = default_view_path
  self.controller_paths             = default_controller_paths
  self.cache_classes                = default_cache_classes
  self.whiny_nils                   = default_whiny_nils
  self.plugins                      = default_plugins
  self.plugin_paths                 = default_plugin_paths
  self.plugin_locators              = default_plugin_locators
  self.plugin_loader                = default_plugin_loader
  self.database_configuration_file  = default_database_configuration_file
  self.routes_configuration_file    = default_routes_configuration_file
  self.gems                         = default_gems

  for framework in default_frameworks
    self.send("#{framework}=", Rails::OrderedOptions.new)
  end
  self.active_support = Rails::OrderedOptions.new
end

Instance Attribute Details

#action_controllerObject

A stub for setting options on ActionController::Base.



512
513
514
# File 'lib/initializer.rb', line 512

def action_controller
  @action_controller
end

#action_mailerObject

A stub for setting options on ActionMailer::Base.



515
516
517
# File 'lib/initializer.rb', line 515

def action_mailer
  @action_mailer
end

#action_viewObject

A stub for setting options on ActionView::Base.



518
519
520
# File 'lib/initializer.rb', line 518

def action_view
  @action_view
end

#active_recordObject

A stub for setting options on ActiveRecord::Base.



521
522
523
# File 'lib/initializer.rb', line 521

def active_record
  @active_record
end

#active_resourceObject

A stub for setting options on ActiveRecord::Base.



524
525
526
# File 'lib/initializer.rb', line 524

def active_resource
  @active_resource
end

#active_supportObject

A stub for setting options on ActiveSupport.



527
528
529
# File 'lib/initializer.rb', line 527

def active_support
  @active_support
end

#cache_classesObject

Whether or not classes should be cached (set to false if you want application classes to be reloaded on each request)



531
532
533
# File 'lib/initializer.rb', line 531

def cache_classes
  @cache_classes
end

#cache_storeObject

The specific cache store to use. By default, the ActiveSupport::Cache::Store will be used.



575
576
577
# File 'lib/initializer.rb', line 575

def cache_store
  @cache_store
end

#controller_pathsObject

The list of paths that should be searched for controllers. (Defaults to app/controllers and components.)



535
536
537
# File 'lib/initializer.rb', line 535

def controller_paths
  @controller_paths
end

#database_configuration_fileObject

The path to the database configuration file to use. (Defaults to config/database.yml.)



539
540
541
# File 'lib/initializer.rb', line 539

def database_configuration_file
  @database_configuration_file
end

#frameworksObject

The list of rails framework components that should be loaded. (Defaults to :active_record, :action_controller, :action_view, :action_mailer, and :active_resource).



549
550
551
# File 'lib/initializer.rb', line 549

def frameworks
  @frameworks
end

#gemsObject

An array of gems that this rails application depends on. Rails will automatically load these gems during installation, and allow you to install any missing gems with:

rake gems:install

You can add gems with the #gem method.



642
643
644
# File 'lib/initializer.rb', line 642

def gems
  @gems
end

#load_once_pathsObject

An array of paths from which Rails will automatically load from only once. All elements of this array must also be in load_paths.



557
558
559
# File 'lib/initializer.rb', line 557

def load_once_paths
  @load_once_paths
end

#load_pathsObject

An array of additional paths to prepend to the load path. By default, all app, lib, vendor and mock paths are included in this list.



553
554
555
# File 'lib/initializer.rb', line 553

def load_paths
  @load_paths
end

#log_levelObject

The log level to use for the default Rails logger. In production mode, this defaults to :info. In development mode, it defaults to :debug.



562
563
564
# File 'lib/initializer.rb', line 562

def log_level
  @log_level
end

#log_pathObject

The path to the log file to use. Defaults to log/##environment.log (e.g. log/development.log or log/production.log).



566
567
568
# File 'lib/initializer.rb', line 566

def log_path
  @log_path
end

#loggerObject

The specific logger to use. By default, a logger will be created and initialized using #log_path and #log_level, but a programmer may specifically set the logger to use via this accessor and it will be used directly.



572
573
574
# File 'lib/initializer.rb', line 572

def logger
  @logger
end

#plugin_loaderObject

The class that handles loading each plugin. Defaults to Rails::Plugin::Loader, but a sub class would have access to fine grained modification of the loading behavior. See the implementation of Rails::Plugin::Loader for more details.



605
606
607
# File 'lib/initializer.rb', line 605

def plugin_loader
  @plugin_loader
end

#plugin_locatorsObject

The classes that handle finding the desired plugins that you’d like to load for your application. By default it is the Rails::Plugin::FileSystemLocator which finds plugins to load in vendor/plugins. You can hook into gem location by subclassing Rails::Plugin::Locator and adding it onto the list of plugin_locators.



600
601
602
# File 'lib/initializer.rb', line 600

def plugin_locators
  @plugin_locators
end

#plugin_pathsObject

The path to the root of the plugins directory. By default, it is in vendor/plugins.



594
595
596
# File 'lib/initializer.rb', line 594

def plugin_paths
  @plugin_paths
end

#pluginsObject

The list of plugins to load. If this is set to nil, all plugins will be loaded. If this is set to [], no plugins will be loaded. Otherwise, plugins will be loaded in the order specified.



587
588
589
# File 'lib/initializer.rb', line 587

def plugins
  @plugins
end

#reload_pluginsObject

Enables or disables plugin reloading. You can get around this setting per plugin. If reload_plugins? is false, add this to your plugin’s init.rb to make it reloadable:

ActiveSupport::Dependencies.load_once_paths.delete lib_path

If reload_plugins? is true, add this to your plugin’s init.rb to only load it once:

ActiveSupport::Dependencies.load_once_paths << lib_path


618
619
620
# File 'lib/initializer.rb', line 618

def reload_plugins
  @reload_plugins
end

#root_pathObject (readonly)

The application’s base directory.



509
510
511
# File 'lib/initializer.rb', line 509

def root_path
  @root_path
end

#routes_configuration_fileObject

The path to the routes configuration file to use. (Defaults to config/routes.rb.)



543
544
545
# File 'lib/initializer.rb', line 543

def routes_configuration_file
  @routes_configuration_file
end

#template_engineObject

Default Template Engine to use erb or haml If you use haml, will be added a gem dependency, so you can do

rake gems:install

For install it!



631
632
633
# File 'lib/initializer.rb', line 631

def template_engine
  @template_engine
end

#time_zoneObject

Sets the default time_zone. Setting this will enable time_zone awareness for Active Record models and set the Active Record default timezone to :utc.



672
673
674
# File 'lib/initializer.rb', line 672

def time_zone
  @time_zone
end

#view_pathObject

The root of the application’s views. (Defaults to app/views.)



578
579
580
# File 'lib/initializer.rb', line 578

def view_path
  @view_path
end

#whiny_nilsObject

Set to true if you want to be warned (noisily) when you try to invoke any method of nil. Set to false for the standard Ruby behavior.



582
583
584
# File 'lib/initializer.rb', line 582

def whiny_nils
  @whiny_nils
end

Instance Method Details

#after_initialize(&after_initialize_block) ⇒ Object

Adds a block which will be executed after rails has been fully initialized. Useful for per-environment configuration which depends on the framework being fully initialized.



745
746
747
# File 'lib/initializer.rb', line 745

def after_initialize(&after_initialize_block)
  after_initialize_blocks << after_initialize_block if after_initialize_block
end

#after_initialize_blocksObject

Returns the blocks added with Configuration#after_initialize



750
751
752
# File 'lib/initializer.rb', line 750

def after_initialize_blocks
  @after_initialize_blocks ||= []
end

#breakpoint_server(_ = nil) ⇒ Object Also known as: breakpoint_server=

Deprecated options:



660
661
662
663
664
665
666
# File 'lib/initializer.rb', line 660

def breakpoint_server(_ = nil)
  $stderr.puts %(
  *******************************************************************
  * config.breakpoint_server has been deprecated and has no effect. *
  *******************************************************************
  )
end

#builtin_directoriesObject



765
766
767
768
# File 'lib/initializer.rb', line 765

def builtin_directories
  # Include builtins only in the development environment.
  (environment == 'development') ? Dir["#{RAILTIES_PATH}/builtin/*/"] : []
end

#database_configurationObject

Loads and returns the contents of the #database_configuration_file. The contents of the file are processed via ERB before being sent through YAML::load.



725
726
727
728
# File 'lib/initializer.rb', line 725

def database_configuration
  require 'erb'
  YAML::load(ERB.new(IO.read(database_configuration_file)).result)
end

#environmentObject

Return the currently selected environment. By default, it returns the value of the RAILS_ENV constant.



738
739
740
# File 'lib/initializer.rb', line 738

def environment
  ::RAILS_ENV
end

#environment_pathObject

The path to the current environment’s file (development.rb, etc.). By default the file is at config/environments/#{environment}.rb.



732
733
734
# File 'lib/initializer.rb', line 732

def environment_path
  "#{root_path}/config/environments/#{environment}.rb"
end

#framework_pathsObject



770
771
772
773
774
775
776
777
778
779
# File 'lib/initializer.rb', line 770

def framework_paths
  paths = %w(erailties erailties/lib activesupport/lib)
  paths << 'eactionpack/lib' if frameworks.include? :action_controller or frameworks.include? :action_view
  
  [:active_record, :action_mailer, :active_resource, :action_web_service].each do |framework|
    paths << "#{framework.to_s.gsub('_', '')}/lib" if frameworks.include? framework
  end
  
  paths.map { |dir| "#{framework_root_path}/#{dir}" }.select { |dir| File.directory?(dir) }
end

#gem(name, options = {}) ⇒ Object

Adds a single Gem dependency to the rails application. By default, it will require the library with the same name as the gem. Use :lib to specify a different name.

# gem 'aws-s3', '>= 0.4.0'
# require 'aws/s3'
config.gem 'aws-s3', :lib => 'aws/s3', :version => '>= 0.4.0', \
  :source => "http://code.whytheluckystiff.net"

To require a library be installed, but not attempt to load it, pass :lib => false

config.gem 'qrp', :version => '0.4.1', :lib => false


655
656
657
# File 'lib/initializer.rb', line 655

def gem(name, options = {})
  @gems << Rails::GemDependency.new(name, options)
end

#reload_plugins?Boolean

Returns true if plugin reloading is enabled.

Returns:

  • (Boolean)


621
622
623
# File 'lib/initializer.rb', line 621

def reload_plugins?
  !!@reload_plugins
end

#set_root_path!Object

Set the root_path to RAILS_ROOT and canonicalize it.



703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
# File 'lib/initializer.rb', line 703

def set_root_path!
  raise 'RAILS_ROOT is not set' unless defined?(::RAILS_ROOT)
  raise 'RAILS_ROOT is not a directory' unless File.directory?(::RAILS_ROOT)

  @root_path =
    # Pathname is incompatible with Windows, but Windows doesn't have
    # real symlinks so File.expand_path is safe.
    if RUBY_PLATFORM =~ /(:?mswin|mingw)/
      File.expand_path(::RAILS_ROOT)

    # Otherwise use Pathname#realpath which respects symlinks.
    else
      Pathname.new(::RAILS_ROOT).realpath.to_s
    end
  
  Object.const_set(:RELATIVE_RAILS_ROOT, ::RAILS_ROOT.dup) unless defined?(::RELATIVE_RAILS_ROOT)
  ::RAILS_ROOT.replace @root_path
end

#to_prepare(&callback) ⇒ Object

Add a preparation callback that will run before every request in development mode, or before the first request in production.

See Dispatcher#to_prepare.



758
759
760
761
762
763
# File 'lib/initializer.rb', line 758

def to_prepare(&callback)
  after_initialize do 
    require 'dispatcher' unless defined?(::Dispatcher)
    Dispatcher.to_prepare(&callback)
  end
end