Advanced Rails - Building Industrial-Strength Web Apps in Record Time

(Tuis.) #1
Dependencies

ActiveSupport | 57

def test_load_save
bin = Binary.new
bin.data = @data

assert @data == bin.data, 'Newly assigned data differs from original'

bin.save
assert @data == bin.data, 'Data differs from original after save'

db_bin = Binary.find(bin.id)
assert @data == db_bin.data, 'Reloaded data differs from original'
end

Stay Current


Rails is a moving target: people continually contribute patches, and the core team is
always looking for ways to improve. The best way to stay on top of the fast-paced
changes is to monitor the Rails Trac timeline (http://dev.rubyonrails.org/timeline). An
RSS feed is available.


Also keep an eye on the ruby-core and rails-core mailing lists, which detail changes
being made to the Ruby and Rails core, respectively.


ActiveSupport


ActiveSupport is the library of utility methods that Rails uses. We examine them in
detail here for two reasons. First, they can be useful to our application code—we can
directly use many of these libraries and methods to our advantage when writing Rails
applications. Secondly, we can learn many things about Ruby programming by dis-
secting these parts. They are small and relatively easy to digest.


Dependencies dependencies.rb


Dependenciesautoloads missing constants by trying to find the file associated with the
constant. When you attempt to access a nonexistent constant, such asMessage,Dependencies
will try to find and loadmessage.rb from any directory inDependencies.load_paths.


DependenciesdefinesModule#const_missingandClass#const_missing, which both proxy to
Dependencies.load_missing_constant(const_parent, const_id). That method searches the
load paths for a file with the appropriate name; if found,Dependenciesloads the file and
ensures that it defined the appropriate constant.


Alternatively, Rails will create an empty module to satisfy nesting in the case of nested
models and controllers. If a directory namedapp/models/store/exists,Storewill be created
as an empty module, by the following process:



  1. Some piece of code references the undefined constantStore.

  2. Ruby callsconst_missing.

Free download pdf