[ Pobierz całość w formacie PDF ]
.each do |key, value|7 send("#{key}=", value)8 end9 end10 endattr_accessibleDefines a list of attributes that can be set via mass assignment, all others will be protected by default.1 class User2 include ActiveModel::MassAssignmentSecurity3 attr_accessible :first_name, :last_name45 def attributes=(props)6 sanitize_for_mass_assignment(props).each do |key, value|7 send("#{key}=", value)8 end9 end10 endNameextendsStringand wraps a bunch of logic around name information about your object so that it can be usedwith Rails.How much name information could there be? Take a look atName s constructor.1 def initialize(klass, namespace = nil)2 super(klass.name)3 @unnamespaced = self.sub(/^#{namespace.name}::/, '') if namespace4 @klass = klass5 @singular = _singularize(self).freeze6 @plural = ActiveSupport::Inflector.pluralize(@singular).freeze7 @element = ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.d\8 emodulize(self)).freeze9 @human = ActiveSupport::Inflector.humanize(@element).freeze10 @collection = ActiveSupport::Inflector.tableize(self).freeze11 @partial_path = "#{@collection}/#{@element}".freezeActive Model API Reference 53412 @param_key = (namespace ? _singularize(@unnamespaced) : @singular).freeze13 @route_key = (namespace ? ActiveSupport::Inflector.pluralize(@param_key) \14 : @plural).freeze15 endAll of this information is calculated and stored at initialization-time, presumably since it s used all over Rails.active_model/naming.rbcollectionReturns an underscored plural version of the model name.elementReturns an underscored version of the model name.humanReturns a translated human readable version of the model name using I18n.The basic recipe is to capitalizedthe first word of the name.1 BlogPost.model_name.human # => "Blog post"partial_pathReturns.pluralReturns a pluralized version of the model name.singularReturns a singularized version of the model name.Namingis the module that you include in your class to get name type information for your model.active_model/naming.rbmodel_nameReturns an for the object.Used by Action Pack to determine routingActive Model API Reference 535Observeris the class to inherit from when creating your own observer to hook into the lifecycle of your models.Inorder for it to work properly, the model to observe must include , and the observers must be set up in aninitializer or similar.1 class LoggingObserver @first_name, 'last_name' => @last_name }7 end8 endActive Model API Reference 537active_model/serialization.rbserializable_hash(options = nil)Returns the serializable hash representation of your model.Options provided can be of the following::except Do not include these attributes in the XML.:methods Only include these methods in the XML.:only Only include the supplied attributes.Serializers::JSONis a module to include in your models when you want to provide a JSON representation of your object.Itautmatically includes the module and depends on the and methods to be present.1 class User2 include ActiveModel::Serializers::JSON3 attr_accessor :first_name, :last_name45 def attributes6 { 'first_name' => @first_name, 'last_name' => @last_name }7 end89 def attributes=(attrs)10 @first_name = attrs['first_name']11 @last_name = attrs['last_name']12 end13 endactive_model/serializers/json.rbas_json(options = nil)Returns a hash to convert to JSON for the model attributes.from_json(json)Decodes the supplied JSON, sets the attributes on the model, and returns self.Serializers::Xmlis a module to include in your models when you want to provide an XML representation of your object.Itautmatically includes the module and depends on the and methods to be present.Active Model API Reference 5381 class Pet2 include ActiveModel::Serializers::XML3 attr_accessor :name45 def attributes6 { 'name' => @name }7 end89 def attributes=(attrs)10 @name = attrs['name']11 end12 endactive_model/serializers/xml.rbto_xml(options = {}, &block)Returns an XML representation of the object.Available options are::builder Supply a custom builder to generate the markup.:except Do not include these attributes in the XML.:indent Number of spaces to indent the XML.:methods Only include these methods in the XML.:namespace Set the XMLNS.:only Only include the supplied attributes.:skip_instruct Skip processing instructions.:skip_types Skip typing.:type Add a type to the XML tags.from_xml(xml)Decodes the supplied XML, sets the attributes on the model, and returns self.Translationprovides the ability to add internationalization support to your modelActive Model API Reference 5391 class User2 extend ActiveModel::Translation3 endactive_model/translation.rbi18n_scopeReturns :activemodel, you can override if you want a custom namespace.lookup_ancestorsGets all ancestors of this class that support i18n.human_attribute_name(attribute, options = {}Translates attribute names into a human readable format with options.:default The default text for the attribute name.Validationsadds a fully-featured validations framework to your model.This includes the means to validate the followingtypes of scenarios plus the ability to create custom validators.Acceptance of a field.Confirmation of a field.Exclusion of a field from a set of values.Format of a field against a regular expression.Inclusion of a field in a set of values.Length of a field.Numericality of a field.Presence of a field
[ Pobierz całość w formacie PDF ]