Class | BigRecord::Model |
In: |
lib/big_record/model.rb
|
Parent: | Object |
ID_FIELD_SEPARATOR | = | '-' | Constants for special characters in generated IDs. An ID might then look like this: ‘United_States-Hawaii-Oahu-Honolulu-b9cef848-a4e0-11dc-a7ba-0018f3137ea8‘ | |
ID_WHITE_SPACE_CHAR | = | '_' |
respond_to? | -> | respond_to_without_attributes? |
For checking respond_to? without searching the attributes (which is faster). |
Specifies a white list of model attributes that can be set via mass-assignment, such as new(attributes), update_attributes(attributes), or attributes=(attributes)
This is the opposite of the attr_protected macro: Mass-assignment will only set attributes in this list, to assign to the rest of attributes you can use direct writer methods. This is meant to protect sensitive attributes from being overwritten by malicious users tampering with URLs or forms. If you‘d rather start from an all-open default and restrict attributes as needed, have a look at attr_protected.
class Customer < ActiveRecord::Base attr_accessible :name, :nickname end customer = Customer.new(:name => "David", :nickname => "Dave", :credit_rating => "Excellent") customer.credit_rating # => nil customer.attributes = { :name => "Jolly fellow", :credit_rating => "Superb" } customer.credit_rating # => nil customer.credit_rating = "Average" customer.credit_rating # => "Average"
Attributes listed as create_accessible work with mass assignment ONLY on creation. After that, any updates to that attribute will be protected from mass assignment. This differs from attr_readonly since that macro prevents attributes from ever being changed (even with the explicit setters) after the record is created.
class Customer < BigRecord::Base attr_create_accessible :name end customer = Customer.new(:name => "Greg") customer.name # => "Greg" customer.save # => true customer.attributes = { :name => "Nerd" } customer.name # => "Greg" customer.name = "Nerd" customer.name # => "Nerd"
Attributes named in this macro are protected from mass-assignment, such as new(attributes), update_attributes(attributes), or attributes=(attributes).
Mass-assignment to these attributes will simply be ignored, to assign to them you can use direct writer methods. This is meant to protect sensitive attributes from being overwritten by malicious users tampering with URLs or forms.
class Customer < ActiveRecord::Base attr_protected :credit_rating end customer = Customer.new("name" => David, "credit_rating" => "Excellent") customer.credit_rating # => nil customer.attributes = { "description" => "Jolly fellow", "credit_rating" => "Superb" } customer.credit_rating # => nil customer.credit_rating = "Average" customer.credit_rating # => "Average"
To start from an all-closed default and enable attributes as needed, have a look at attr_accessible.
Attributes listed as readonly can be set for a new record, but will be ignored in database updates afterwards.
Log and benchmark multiple statements in a single block.
@example
Project.benchmark("Creating project") do project = Project.create("name" => "stuff") project.create_manager("name" => "David") project.milestones << Milestone.find(:all) end
The benchmark is only recorded if the current level of the logger matches the log_level, which makes it easy to include benchmarking statements in production software that will remain inexpensive because the benchmark will only be conducted if the log level is low enough.
The logging of the multiple statements is turned off unless use_silence is set to false.
Macro for defining a new column for a model. Invokes {create_column} and adds the new column into the model‘s column hash.
@param type [Symbol, String] Column type as defined in the source of {ConnectionAdapters::Column#klass} @option options [true,false] :collection Whether this column is a collection. @option options [String] :alias Define an alias for the column that cannot be inferred. By default, ‘attribute:name’ will be aliased to ‘name’. @option options [String] :default Default value to set for this column.
@return [ConnectionAdapters::Column] The column object created.
Returns an array of all the attributes that have been specified as create_accessible.
New objects can be instantiated as either empty (pass no construction parameter) or pre-set with attributes but not yet saved (pass a hash with key names matching the associated table column names). In both instances, valid attribute keys are determined by the column names of the associated table — hence you can‘t have attributes that aren‘t part of the table columns.
@param [Hash] Optional hash argument consisting of keys that match the names of the columns, and their values.
Returns the class type of the record using the current module as a prefix. So descendents of MyApp::Business::Account would appear as MyApp::Business::AccountSubclass.
Returns true if the comparison_object is the same object, or is of the same type and has the same id.
Returns the value of the attribute identified by attr_name after it has been typecast (for example, "2004-12-12" in a data column is cast to a date object, like Date.new(2004, 12, 12)). (Alias for the protected read_attribute method).
Updates the attribute identified by attr_name with the specified value. (Alias for the protected write_attribute method).
Returns an array of names for the attributes available on this object sorted alphabetically.
Returns true if the specified attribute has been set by the user or by a database load and is neither nil nor empty? (the latter only applies to objects that respond to empty?, most notably Strings).
@return [String, Symbol] Name of an attribute. @return [true,false] Whether that attribute exists.
Allows you to set all the attributes at once by passing in a hash with keys matching the attribute names (which again matches the column names). Sensitive attributes can be protected from this form of mass-assignment by using the attr_protected macro. Or you can alternatively specify which attributes can be accessed in with the attr_accessible macro. Then all the attributes not included in that won‘t be allowed to be mass-assigned.
Overridden by FamilySpanColumns Returns the column object for the named attribute.
Deletes the record in the database and freezes this instance to reflect that no changes should be made (since they can‘t be persisted).
Just freeze the attributes hash, such that associations are still accessible even on destroyed records.
Returns true if the given attribute is in the attributes hash
@return [String, Symbol] Name of an attribute. @return [true,false] Whether that attribute exists in the attributes hash.
A model instance‘s primary key is always available as model.id whether you name it the default ‘id’ or set it to something else.
Returns true if this object hasn‘t been saved yet — that is, a record for the object doesn‘t yet exist in the data store.
Callback method meant to be overriden by subclasses if they need to preload some attributes before initializing the record. (usefull when using a meta model where the list of columns depends on the value of an attribute)
Records loaded through joins with piggy-back attributes will be marked as read only as they cannot be saved and return true to this query.
Reloads the attributes of this object from the database. The optional options argument is passed to find when reloading so you may do e.g. record.reload(:lock => true) to reload the same record with an exclusive row lock.
@return Itself with reloaded attributes.
A Person object with a name attribute can ask person.respond_to?("name"), person.respond_to?("name="), and person.respond_to?("name?") which will all return true.
Safe version of attributes= so that objects can be instantiated even if columns are removed.
@param [Hash] Attribute hash consisting of the column name and their values. @param [true, false] Pass the attributes hash through {remove_attributes_protected_from_mass_assignment}
Attempts to save the record, but instead of just returning false if it couldn‘t happen, it raises a RecordNotSaved exception
Updates a single attribute and saves the record. This is especially useful for boolean flags on existing records. Note: This method is overwritten by the Validation module that‘ll make sure that updates made with this method doesn‘t get subjected to validation checks. Hence, attributes can be updated even if the full object isn‘t valid.
Updates all the attributes from the passed-in Hash and saves the record. If the object is invalid, the saving will fail and false will be returned.
Updates an object just like Base.update_attributes but calls save! instead of save so an exception is raised if the record is invalid.
Instantiates objects for all attribute classes that needs more than one constructor parameter. This is done by calling new on the column type or aggregation type (through composed_of) object with these parameters. So having the pairs written_on(1) = "2004", written_on(2) = "6", written_on(3) = "24", will instantiate written_on (a date type) with Date.new("2004", "6", "24"). You can also specify a typecast character in the parentheses to have the parameters typecasted before they‘re used in the constructor. Use i for Fixnum, f for Float, s for String, and a for Array. If all the values for a given attribute are empty, the attribute will be set to nil.
Initializes the attributes array with keys matching the columns from the linked table and the values matching the corresponding default value of that column, so that a new instance, or one populated from a passed-in Hash, still has all the attributes that instances loaded from the database would.
The primary key and inheritance column can never be set by mass-assignment for security reasons.
Creates a record with values matching those of the instance attributes and returns its id. Generate a UUID as the row key.
Includes an ugly hack for Time.local instead of Time.new because the latter is reserved by Time itself.
Allows access to the object attributes, which are held in the @attributes hash, as were they first-class methods. So a Person class with a name attribute can use Person#name and Person#name= and never directly use the attributes hash — except for multiple assigns with ActiveRecord#attributes=. A Milestone class can also ask Milestone#completed? to test that the completed attribute is not nil or 0.
It‘s also possible to instantiate related objects, so a Client class belonging to the clients table with a master_id foreign key can instantiate master through Client#master.
Returns the value of the attribute identified by attr_name after it has been typecast (for example, "2004-12-12" in a data column is cast to a date object, like Date.new(2004, 12, 12)).
Removes any attributes from the argument hash that have been declared as protected from mass assignment. See the {attr_protected} and {attr_accessible} macros to define these attributes.
Updates the associated record with values matching those of the instance attributes. Returns the number of affected rows.
Update this record in hbase. Cannot be directly in the method ‘update’ because it would trigger callbacks and therefore weird behaviors.