From 7b24cf0391ed8916bf12781a179b4ac5995e1690 Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Mon, 13 May 2013 11:39:09 -0400 Subject: emphasize that callbacks are called in destroy_all Cleaned up rdoc a bit emphasizing that callbacks are called. Also removed the stress on the fact that records are always removed. If callbacks return false then records will not be deleted. --- .../lib/active_record/associations/collection_association.rb | 8 ++++---- activerecord/lib/active_record/associations/collection_proxy.rb | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index 2a00ac1386..efd7ecb97c 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -237,11 +237,11 @@ module ActiveRecord end end - # Destroy +records+ and remove them from this association calling - # +before_remove+ and +after_remove+ callbacks. + # Deletes the +records+ and removes them from this association calling + # +before_remove+ , +after_remove+ , +before_destroy+ and +after_destroy+ callbacks. # - # Note that this method will _always_ remove records from the database - # ignoring the +:dependent+ option. + # Note that this method removes records from the database ignoring the + # +:dependent+ option. def destroy(*records) records = find(records) if records.any? { |record| record.kind_of?(Fixnum) || record.kind_of?(String) } delete_or_destroy(records, :destroy) diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb index 71b64de5ea..e82c195335 100644 --- a/activerecord/lib/active_record/associations/collection_proxy.rb +++ b/activerecord/lib/active_record/associations/collection_proxy.rb @@ -422,9 +422,9 @@ module ActiveRecord @association.delete_all end - # Deletes the records of the collection directly from the database. - # This will _always_ remove the records ignoring the +:dependent+ - # option. + # Deletes the records of the collection directly from the database + # ignoring the +:dependent+ option. It invokes +before_remove+, + # +after_remove+ , +before_destroy+ and +after_destroy+ callbacks. # # class Person < ActiveRecord::Base # has_many :pets -- cgit v1.2.3 From 87d71b02b85a44ce461e533666ef5f0e43a46531 Mon Sep 17 00:00:00 2001 From: aditya-kapoor Date: Tue, 14 May 2013 00:27:52 +0530 Subject: Added documentation for model migration generation --- .../lib/rails/generators/active_record/model/model_generator.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'activerecord') diff --git a/activerecord/lib/rails/generators/active_record/model/model_generator.rb b/activerecord/lib/rails/generators/active_record/model/model_generator.rb index 40e134e626..821df0e0d6 100644 --- a/activerecord/lib/rails/generators/active_record/model/model_generator.rb +++ b/activerecord/lib/rails/generators/active_record/model/model_generator.rb @@ -12,6 +12,9 @@ module ActiveRecord class_option :parent, :type => :string, :desc => "The parent class for the generated model" class_option :indexes, :type => :boolean, :default => true, :desc => "Add indexes for references and belongs_to columns" + + # creates the migration file for the model first followed by the model file itself + def create_migration_file return unless options[:migration] && options[:parent].nil? attributes.each { |a| a.attr_options.delete(:index) if a.reference? && !a.has_index? } if options[:indexes] == false @@ -39,6 +42,7 @@ module ActiveRecord protected + # Used by the migration template to determine the parent name of the model def parent_class_name options[:parent] || "ActiveRecord::Base" end -- cgit v1.2.3 From ddf07d21834f4ad88c25334829348f5e4000fea0 Mon Sep 17 00:00:00 2001 From: Anton Kalyaev Date: Tue, 14 May 2013 10:15:32 +0400 Subject: improved doc for ActiveRecord#find_by_sql method (Refs #10599) [ci skip] --- activerecord/lib/active_record/querying.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/querying.rb b/activerecord/lib/active_record/querying.rb index f78ccb01aa..3d85898c41 100644 --- a/activerecord/lib/active_record/querying.rb +++ b/activerecord/lib/active_record/querying.rb @@ -14,7 +14,7 @@ module ActiveRecord # Executes a custom SQL query against your database and returns all the results. The results will # be returned as an array with columns requested encapsulated as attributes of the model you call # this method from. If you call Product.find_by_sql then the results will be returned in - # a Product object with the attributes you specified in the SQL query. + # a +Product+ object with the attributes you specified in the SQL query. # # If you call a complicated SQL query which spans multiple tables the columns specified by the # SELECT will be attributes of the model, whether or not they are columns of the corresponding @@ -29,9 +29,10 @@ module ActiveRecord # Post.find_by_sql "SELECT p.title, c.author FROM posts p, comments c WHERE p.id = c.post_id" # # => [#"Ruby Meetup", "first_name"=>"Quentin"}>, ...] # - # # You can use the same string replacement techniques as you can with ActiveRecord#find + # You can use the same string replacement techniques as you can with ActiveRecord::QueryMethods#where: + # # Post.find_by_sql ["SELECT title FROM posts WHERE author = ? AND created > ?", author_id, start_date] - # # => [#"The Cheap Man Buys Twice"}>, ...] + # Post.find_by_sql ["SELECT body FROM comments WHERE author = :user_id OR approved_by = :user_id", { :user_id => user_id }] def find_by_sql(sql, binds = []) result_set = connection.select_all(sanitize_sql(sql), "#{name} Load", binds) column_types = {} -- cgit v1.2.3 From 0fbf31b2000e467f18ebba90efb3a88fa1a4a7c9 Mon Sep 17 00:00:00 2001 From: aditya-kapoor Date: Wed, 15 May 2013 12:28:08 +0530 Subject: Added documentation for ActiveRecord::Associations::Builder::Association class --- activerecord/lib/active_record/associations/builder/association.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/associations/builder/association.rb b/activerecord/lib/active_record/associations/builder/association.rb index 5c37f42794..34684fd71a 100644 --- a/activerecord/lib/active_record/associations/builder/association.rb +++ b/activerecord/lib/active_record/associations/builder/association.rb @@ -1,3 +1,7 @@ +# This is the parent Association class which defines certain class variables (valid_options) and +# instance variables (model, name, scope, options, reflection) which would be common across all the associations that we known today in Rails.. +# Every association need to have the values of these variables set and they are used at multiple places + module ActiveRecord::Associations::Builder class Association #:nodoc: class << self -- cgit v1.2.3 From da8fff528ddcf3ea92f5c6cc402105e0403bb74a Mon Sep 17 00:00:00 2001 From: aditya-kapoor Date: Wed, 15 May 2013 15:07:55 +0530 Subject: Added some more documentation for ActiveRecord::Associations::Builder::Association class --- .../lib/active_record/associations/builder/association.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/associations/builder/association.rb b/activerecord/lib/active_record/associations/builder/association.rb index 34684fd71a..456041cce8 100644 --- a/activerecord/lib/active_record/associations/builder/association.rb +++ b/activerecord/lib/active_record/associations/builder/association.rb @@ -1,6 +1,17 @@ # This is the parent Association class which defines certain class variables (valid_options) and # instance variables (model, name, scope, options, reflection) which would be common across all the associations that we known today in Rails.. # Every association need to have the values of these variables set and they are used at multiple places +# The heirarchy is defined as follows: +# Association +# - SingularAssociation +# - BelongsTo +# - HasOne +# - CollectionAssociation +# - HasMany +# - HasAndBelongsToMany +# +# The HasMany :Through association is a special case of HasMany association with the :through option set for it +# module ActiveRecord::Associations::Builder class Association #:nodoc: -- cgit v1.2.3 From 90e60aa4694be408b299085cad8b8a6be64df0ac Mon Sep 17 00:00:00 2001 From: aditya-kapoor Date: Wed, 15 May 2013 15:31:53 +0530 Subject: Added some more documentation for define_readers and define_writer of the Association and its inherited classes --- activerecord/lib/active_record/associations/builder/association.rb | 7 +++++++ .../active_record/associations/builder/collection_association.rb | 4 ++++ .../lib/active_record/associations/builder/singular_association.rb | 4 ++++ 3 files changed, 15 insertions(+) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/associations/builder/association.rb b/activerecord/lib/active_record/associations/builder/association.rb index 456041cce8..4b72846ef6 100644 --- a/activerecord/lib/active_record/associations/builder/association.rb +++ b/activerecord/lib/active_record/associations/builder/association.rb @@ -73,6 +73,13 @@ module ActiveRecord::Associations::Builder def validate_options options.assert_valid_keys(valid_options) end + + # Defines the setter and getter methods for the association + # class Post < ActiveRecord::Base + # has_many :comments + # end + # + # Post.first.comments and Post.first.comments= methods are defined by this method... def define_accessors define_readers diff --git a/activerecord/lib/active_record/associations/builder/collection_association.rb b/activerecord/lib/active_record/associations/builder/collection_association.rb index fdead16761..9c6690b721 100644 --- a/activerecord/lib/active_record/associations/builder/collection_association.rb +++ b/activerecord/lib/active_record/associations/builder/collection_association.rb @@ -1,3 +1,5 @@ +# This class is inherited by the has_many and has_many_and_belongs_to_many association classes + require 'active_record/associations' module ActiveRecord::Associations::Builder @@ -66,6 +68,8 @@ module ActiveRecord::Associations::Builder model.send("#{full_callback_name}=", Array(options[callback_name.to_sym])) end + # Defines the setter and getter methods for the collection_singular_ids. + def define_readers super diff --git a/activerecord/lib/active_record/associations/builder/singular_association.rb b/activerecord/lib/active_record/associations/builder/singular_association.rb index f06426a09d..96ccbeb8a3 100644 --- a/activerecord/lib/active_record/associations/builder/singular_association.rb +++ b/activerecord/lib/active_record/associations/builder/singular_association.rb @@ -1,3 +1,5 @@ +# This class is inherited by the has_one and belongs_to association classes + module ActiveRecord::Associations::Builder class SingularAssociation < Association #:nodoc: def valid_options @@ -13,6 +15,8 @@ module ActiveRecord::Associations::Builder define_constructors if constructable? end + # Defines the (build|create)_association methods for belongs_to or has_one association + def define_constructors mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1 def build_#{name}(*args, &block) -- cgit v1.2.3 From 7f24d3d6956a7775771302d143e3b09de681d12f Mon Sep 17 00:00:00 2001 From: Vijay Dev Date: Sun, 19 May 2013 21:32:17 +0530 Subject: copy edits[ci skip] --- .../active_record/associations/builder/association.rb | 19 ++++++++----------- .../generators/active_record/model/model_generator.rb | 2 +- 2 files changed, 9 insertions(+), 12 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/associations/builder/association.rb b/activerecord/lib/active_record/associations/builder/association.rb index 4b72846ef6..6a3658f328 100644 --- a/activerecord/lib/active_record/associations/builder/association.rb +++ b/activerecord/lib/active_record/associations/builder/association.rb @@ -1,17 +1,14 @@ -# This is the parent Association class which defines certain class variables (valid_options) and -# instance variables (model, name, scope, options, reflection) which would be common across all the associations that we known today in Rails.. -# Every association need to have the values of these variables set and they are used at multiple places -# The heirarchy is defined as follows: +# This is the parent Association class which defines the variables +# used by all associations. +# +# The hierarchy is defined as follows: # Association # - SingularAssociation -# - BelongsTo -# - HasOne +# - BelongsToAssociation +# - HasOneAssociation # - CollectionAssociation -# - HasMany -# - HasAndBelongsToMany -# -# The HasMany :Through association is a special case of HasMany association with the :through option set for it -# +# - HasManyAssociation +# - HasAndBelongsToManyAssociation module ActiveRecord::Associations::Builder class Association #:nodoc: diff --git a/activerecord/lib/rails/generators/active_record/model/model_generator.rb b/activerecord/lib/rails/generators/active_record/model/model_generator.rb index 821df0e0d6..7e8d68ce69 100644 --- a/activerecord/lib/rails/generators/active_record/model/model_generator.rb +++ b/activerecord/lib/rails/generators/active_record/model/model_generator.rb @@ -13,7 +13,7 @@ module ActiveRecord class_option :indexes, :type => :boolean, :default => true, :desc => "Add indexes for references and belongs_to columns" - # creates the migration file for the model first followed by the model file itself + # creates the migration file for the model. def create_migration_file return unless options[:migration] && options[:parent].nil? -- cgit v1.2.3