Rails Flashcards

1
Q

Asset Pipeline

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Asset Path Helper

A

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: “” });

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Manifest Files

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Directives (as in Manifest Files)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

CollectionProxy (Class)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

The ‘request’ Object

A

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? , ..

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which methods trigger validations, and will save the object to the database only if the object is valid? What do they return?

A
  • 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Which methods don’t trigger validations and should be used carefully therefore?

A
  • decrement!
  • decrement_counter
  • increment!
  • increment_counter
  • toggle!
  • touch
  • update_all
  • update_attribute
  • update_column
  • update_columns
  • update_counters
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

The ‘errors.messages’ instance method

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

The errors[] class

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

The validates_associated helper

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Fixtures

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Nesting (Constants, Autoloading)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What for is the ‘sprockets-rails’ gem?

A

The asset pipeline is implemented in the ‘sprockets-rails’ gem.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are ‘Active Record Nested Attributes’

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Why is it possible that AR objects who are invalid, don’t show any errors?

A

Note that an object instantiated with new will not report errors even if it’s technically invalid, because validations are not run when using new. Sending a #valid? will trigger the validations and generate the errors.

17
Q

What is the #constantize method for?

A

constantize tries to find a declared constant with the name specified in the string. It raises a NameError when the name is not in CamelCase or is not initialized. See ActiveSupport::Inflector.constantize

18
Q

What is ‘mattr_accessor’ for ?

A

Rails extends Ruby with both mattr_accessor (Module accessor) and cattr_accessor (as well as _reader/_writer versions). As Ruby’s attr_accessor generates getter/setter methods for instances, cattr/mattr_accessor provide getter/setter methods at the class or module level. Thus: