Rails Flashcards
Asset Pipeline
The asset pipeline provides a framework to concatenate and minify or compress JavaScript and CSS assets. It also adds the ability to write these assets in other languages and pre-processors such as CoffeeScript, Sass and ERB.
Asset Path Helper
If you add an erb extension to a JavaScript asset, making it something such as application.js.erb, you can then use the asset_path helper in your JavaScript code:
$(‘#logo’).attr({ src: “” });
Manifest Files
Sprockets uses manifest files to determine which assets to include and serve. These manifest files contain directives - instructions that tell Sprockets which files to require in order to build a single CSS or JavaScript file. With these directives, Sprockets loads the files specified, processes them if necessary, concatenates them into one single file and then compresses them (if Rails.application.config.assets.compress is true).
Directives (as in Manifest Files)
Directives are processed top to bottom, but the order in which files are included by require_tree is unspecified. You should not rely on any particular order among those. If you need to ensure some particular JavaScript ends up above some other in the concatenated file, require the prerequisite file first in the manifest. Note that the family of require directives prevents files from being included twice in the output.
CollectionProxy (Class)
Association proxies in Active Record are middlemen between the object that holds the association, known as the @owner, and the actual associated object, known as the @target. The kind of association any proxy is about is available in @reflection. That’s an instance of the class ActiveRecord::Reflection::AssociationReflection.
The ‘request’ Object
The request object contains a lot of useful information about the request coming in from the client. To get a full list of the available methods, refer to the API documentation. Among the properties that you can access on this object are: host, domain, url, method, get?, patch? , ..
Which methods trigger validations, and will save the object to the database only if the object is valid? What do they return?
- save / save!
- create / create!
- update / update!
The bang versions raise an exception if the record is invalid. Save and Update return false, Create just returns the object.
Which methods don’t trigger validations and should be used carefully therefore?
- decrement!
- decrement_counter
- increment!
- increment_counter
- toggle!
- touch
- update_all
- update_attribute
- update_column
- update_columns
- update_counters
The ‘errors.messages’ instance method
After Active Record has performed validations, any errors found can be accessed through the errors.messages instance method, which returns a collection of errors. By definition, an object is valid if this collection is empty after running validations.
The errors[] class
To verify whether or not a particular attribute of an object is valid, you can use errors[:attribute]. It returns an array of all the errors for :attribute. If there are no errors on the specified attribute, an empty array is returned.
The validates_associated helper
You should use this helper when your model has associations with other models and they also need to be validated. When you try to save your object, valid? will be called upon each one of the associated objects.
class Library < ActiveRecord::Base
has_many :books
validates_associated :books
end
Fixtures
Fixtures are a way of organizing test data; they reside in the fixtures folder. Fixtures is a fancy word for sample data. Fixtures allow you to populate your testing database with predefined data before your tests run. Fixtures are database independent written in YAML. There is one file per model.
Nesting (Constants, Autoloading)
The nesting at any given place is the collection of enclosing nested class and module objects outwards. The nesting at any given place can be inspected with Module.nesting.
What for is the ‘sprockets-rails’ gem?
The asset pipeline is implemented in the ‘sprockets-rails’ gem.
What are ‘Active Record Nested Attributes’
Nested attributes allow you to save attributes on associated records through the parent. By default nested attribute updating is turned off and you can enable it using the #accepts_nested_attributes_for class method. When you enable nested attributes an attribute writer is defined on the model.
The attribute writer is named after the association, which means that in the following example, two new methods are added to your model:
author_attributes=(attributes) and pages_attributes=(attributes).
class Book < ActiveRecord::Base
has_one :author
has_many :pages
accepts_nested_attributes_for :author, :pages
end