activerecord_authorails 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +3043 -0
- data/README +360 -0
- data/RUNNING_UNIT_TESTS +64 -0
- data/Rakefile +226 -0
- data/examples/associations.png +0 -0
- data/examples/associations.rb +87 -0
- data/examples/shared_setup.rb +15 -0
- data/examples/validation.rb +85 -0
- data/install.rb +30 -0
- data/lib/active_record.rb +85 -0
- data/lib/active_record/acts/list.rb +244 -0
- data/lib/active_record/acts/nested_set.rb +211 -0
- data/lib/active_record/acts/tree.rb +89 -0
- data/lib/active_record/aggregations.rb +191 -0
- data/lib/active_record/associations.rb +1637 -0
- data/lib/active_record/associations/association_collection.rb +190 -0
- data/lib/active_record/associations/association_proxy.rb +158 -0
- data/lib/active_record/associations/belongs_to_association.rb +56 -0
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +50 -0
- data/lib/active_record/associations/has_and_belongs_to_many_association.rb +169 -0
- data/lib/active_record/associations/has_many_association.rb +210 -0
- data/lib/active_record/associations/has_many_through_association.rb +247 -0
- data/lib/active_record/associations/has_one_association.rb +80 -0
- data/lib/active_record/attribute_methods.rb +75 -0
- data/lib/active_record/base.rb +2164 -0
- data/lib/active_record/calculations.rb +270 -0
- data/lib/active_record/callbacks.rb +367 -0
- data/lib/active_record/connection_adapters/abstract/connection_specification.rb +279 -0
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +130 -0
- data/lib/active_record/connection_adapters/abstract/quoting.rb +58 -0
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +343 -0
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +310 -0
- data/lib/active_record/connection_adapters/abstract_adapter.rb +161 -0
- data/lib/active_record/connection_adapters/db2_adapter.rb +228 -0
- data/lib/active_record/connection_adapters/firebird_adapter.rb +728 -0
- data/lib/active_record/connection_adapters/frontbase_adapter.rb +861 -0
- data/lib/active_record/connection_adapters/mysql_adapter.rb +414 -0
- data/lib/active_record/connection_adapters/openbase_adapter.rb +350 -0
- data/lib/active_record/connection_adapters/oracle_adapter.rb +689 -0
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +584 -0
- data/lib/active_record/connection_adapters/sqlite_adapter.rb +407 -0
- data/lib/active_record/connection_adapters/sqlserver_adapter.rb +591 -0
- data/lib/active_record/connection_adapters/sybase_adapter.rb +662 -0
- data/lib/active_record/deprecated_associations.rb +104 -0
- data/lib/active_record/deprecated_finders.rb +44 -0
- data/lib/active_record/fixtures.rb +628 -0
- data/lib/active_record/locking/optimistic.rb +106 -0
- data/lib/active_record/locking/pessimistic.rb +77 -0
- data/lib/active_record/migration.rb +394 -0
- data/lib/active_record/observer.rb +178 -0
- data/lib/active_record/query_cache.rb +64 -0
- data/lib/active_record/reflection.rb +222 -0
- data/lib/active_record/schema.rb +58 -0
- data/lib/active_record/schema_dumper.rb +149 -0
- data/lib/active_record/timestamp.rb +51 -0
- data/lib/active_record/transactions.rb +136 -0
- data/lib/active_record/validations.rb +843 -0
- data/lib/active_record/vendor/db2.rb +362 -0
- data/lib/active_record/vendor/mysql.rb +1214 -0
- data/lib/active_record/vendor/simple.rb +693 -0
- data/lib/active_record/version.rb +9 -0
- data/lib/active_record/wrappers/yaml_wrapper.rb +15 -0
- data/lib/active_record/wrappings.rb +58 -0
- data/lib/active_record/xml_serialization.rb +308 -0
- data/test/aaa_create_tables_test.rb +59 -0
- data/test/abstract_unit.rb +77 -0
- data/test/active_schema_test_mysql.rb +31 -0
- data/test/adapter_test.rb +87 -0
- data/test/adapter_test_sqlserver.rb +81 -0
- data/test/aggregations_test.rb +95 -0
- data/test/all.sh +8 -0
- data/test/ar_schema_test.rb +33 -0
- data/test/association_inheritance_reload.rb +14 -0
- data/test/associations/callbacks_test.rb +126 -0
- data/test/associations/cascaded_eager_loading_test.rb +138 -0
- data/test/associations/eager_test.rb +393 -0
- data/test/associations/extension_test.rb +42 -0
- data/test/associations/join_model_test.rb +497 -0
- data/test/associations_test.rb +1809 -0
- data/test/attribute_methods_test.rb +49 -0
- data/test/base_test.rb +1586 -0
- data/test/binary_test.rb +37 -0
- data/test/calculations_test.rb +219 -0
- data/test/callbacks_test.rb +377 -0
- data/test/class_inheritable_attributes_test.rb +32 -0
- data/test/column_alias_test.rb +17 -0
- data/test/connection_test_firebird.rb +8 -0
- data/test/connections/native_db2/connection.rb +25 -0
- data/test/connections/native_firebird/connection.rb +26 -0
- data/test/connections/native_frontbase/connection.rb +27 -0
- data/test/connections/native_mysql/connection.rb +24 -0
- data/test/connections/native_openbase/connection.rb +21 -0
- data/test/connections/native_oracle/connection.rb +27 -0
- data/test/connections/native_postgresql/connection.rb +23 -0
- data/test/connections/native_sqlite/connection.rb +34 -0
- data/test/connections/native_sqlite3/connection.rb +34 -0
- data/test/connections/native_sqlite3/in_memory_connection.rb +18 -0
- data/test/connections/native_sqlserver/connection.rb +23 -0
- data/test/connections/native_sqlserver_odbc/connection.rb +25 -0
- data/test/connections/native_sybase/connection.rb +23 -0
- data/test/copy_table_sqlite.rb +64 -0
- data/test/datatype_test_postgresql.rb +52 -0
- data/test/default_test_firebird.rb +16 -0
- data/test/defaults_test.rb +60 -0
- data/test/deprecated_associations_test.rb +396 -0
- data/test/deprecated_finder_test.rb +151 -0
- data/test/empty_date_time_test.rb +25 -0
- data/test/finder_test.rb +504 -0
- data/test/fixtures/accounts.yml +28 -0
- data/test/fixtures/author.rb +99 -0
- data/test/fixtures/author_favorites.yml +4 -0
- data/test/fixtures/authors.yml +7 -0
- data/test/fixtures/auto_id.rb +4 -0
- data/test/fixtures/bad_fixtures/attr_with_numeric_first_char +1 -0
- data/test/fixtures/bad_fixtures/attr_with_spaces +1 -0
- data/test/fixtures/bad_fixtures/blank_line +3 -0
- data/test/fixtures/bad_fixtures/duplicate_attributes +3 -0
- data/test/fixtures/bad_fixtures/missing_value +1 -0
- data/test/fixtures/binary.rb +2 -0
- data/test/fixtures/categories.yml +14 -0
- data/test/fixtures/categories/special_categories.yml +9 -0
- data/test/fixtures/categories/subsubdir/arbitrary_filename.yml +4 -0
- data/test/fixtures/categories_ordered.yml +7 -0
- data/test/fixtures/categories_posts.yml +23 -0
- data/test/fixtures/categorization.rb +5 -0
- data/test/fixtures/categorizations.yml +17 -0
- data/test/fixtures/category.rb +20 -0
- data/test/fixtures/column_name.rb +3 -0
- data/test/fixtures/comment.rb +23 -0
- data/test/fixtures/comments.yml +59 -0
- data/test/fixtures/companies.yml +55 -0
- data/test/fixtures/company.rb +107 -0
- data/test/fixtures/company_in_module.rb +59 -0
- data/test/fixtures/computer.rb +3 -0
- data/test/fixtures/computers.yml +4 -0
- data/test/fixtures/course.rb +3 -0
- data/test/fixtures/courses.yml +7 -0
- data/test/fixtures/customer.rb +55 -0
- data/test/fixtures/customers.yml +17 -0
- data/test/fixtures/db_definitions/db2.drop.sql +32 -0
- data/test/fixtures/db_definitions/db2.sql +231 -0
- data/test/fixtures/db_definitions/db22.drop.sql +2 -0
- data/test/fixtures/db_definitions/db22.sql +5 -0
- data/test/fixtures/db_definitions/firebird.drop.sql +63 -0
- data/test/fixtures/db_definitions/firebird.sql +304 -0
- data/test/fixtures/db_definitions/firebird2.drop.sql +2 -0
- data/test/fixtures/db_definitions/firebird2.sql +6 -0
- data/test/fixtures/db_definitions/frontbase.drop.sql +32 -0
- data/test/fixtures/db_definitions/frontbase.sql +268 -0
- data/test/fixtures/db_definitions/frontbase2.drop.sql +1 -0
- data/test/fixtures/db_definitions/frontbase2.sql +4 -0
- data/test/fixtures/db_definitions/mysql.drop.sql +32 -0
- data/test/fixtures/db_definitions/mysql.sql +234 -0
- data/test/fixtures/db_definitions/mysql2.drop.sql +2 -0
- data/test/fixtures/db_definitions/mysql2.sql +5 -0
- data/test/fixtures/db_definitions/openbase.drop.sql +2 -0
- data/test/fixtures/db_definitions/openbase.sql +302 -0
- data/test/fixtures/db_definitions/openbase2.drop.sql +2 -0
- data/test/fixtures/db_definitions/openbase2.sql +7 -0
- data/test/fixtures/db_definitions/oracle.drop.sql +65 -0
- data/test/fixtures/db_definitions/oracle.sql +325 -0
- data/test/fixtures/db_definitions/oracle2.drop.sql +2 -0
- data/test/fixtures/db_definitions/oracle2.sql +6 -0
- data/test/fixtures/db_definitions/postgresql.drop.sql +37 -0
- data/test/fixtures/db_definitions/postgresql.sql +263 -0
- data/test/fixtures/db_definitions/postgresql2.drop.sql +2 -0
- data/test/fixtures/db_definitions/postgresql2.sql +5 -0
- data/test/fixtures/db_definitions/schema.rb +60 -0
- data/test/fixtures/db_definitions/sqlite.drop.sql +32 -0
- data/test/fixtures/db_definitions/sqlite.sql +215 -0
- data/test/fixtures/db_definitions/sqlite2.drop.sql +2 -0
- data/test/fixtures/db_definitions/sqlite2.sql +5 -0
- data/test/fixtures/db_definitions/sqlserver.drop.sql +34 -0
- data/test/fixtures/db_definitions/sqlserver.sql +243 -0
- data/test/fixtures/db_definitions/sqlserver2.drop.sql +2 -0
- data/test/fixtures/db_definitions/sqlserver2.sql +5 -0
- data/test/fixtures/db_definitions/sybase.drop.sql +34 -0
- data/test/fixtures/db_definitions/sybase.sql +218 -0
- data/test/fixtures/db_definitions/sybase2.drop.sql +4 -0
- data/test/fixtures/db_definitions/sybase2.sql +5 -0
- data/test/fixtures/default.rb +2 -0
- data/test/fixtures/developer.rb +52 -0
- data/test/fixtures/developers.yml +21 -0
- data/test/fixtures/developers_projects.yml +17 -0
- data/test/fixtures/developers_projects/david_action_controller +3 -0
- data/test/fixtures/developers_projects/david_active_record +3 -0
- data/test/fixtures/developers_projects/jamis_active_record +2 -0
- data/test/fixtures/edge.rb +5 -0
- data/test/fixtures/edges.yml +6 -0
- data/test/fixtures/entrant.rb +3 -0
- data/test/fixtures/entrants.yml +14 -0
- data/test/fixtures/fk_test_has_fk.yml +3 -0
- data/test/fixtures/fk_test_has_pk.yml +2 -0
- data/test/fixtures/flowers.jpg +0 -0
- data/test/fixtures/funny_jokes.yml +10 -0
- data/test/fixtures/joke.rb +6 -0
- data/test/fixtures/keyboard.rb +3 -0
- data/test/fixtures/legacy_thing.rb +3 -0
- data/test/fixtures/legacy_things.yml +3 -0
- data/test/fixtures/migrations/1_people_have_last_names.rb +9 -0
- data/test/fixtures/migrations/2_we_need_reminders.rb +12 -0
- data/test/fixtures/migrations/3_innocent_jointable.rb +12 -0
- data/test/fixtures/migrations_with_decimal/1_give_me_big_numbers.rb +15 -0
- data/test/fixtures/migrations_with_duplicate/1_people_have_last_names.rb +9 -0
- data/test/fixtures/migrations_with_duplicate/2_we_need_reminders.rb +12 -0
- data/test/fixtures/migrations_with_duplicate/3_foo.rb +7 -0
- data/test/fixtures/migrations_with_duplicate/3_innocent_jointable.rb +12 -0
- data/test/fixtures/migrations_with_missing_versions/1000_people_have_middle_names.rb +9 -0
- data/test/fixtures/migrations_with_missing_versions/1_people_have_last_names.rb +9 -0
- data/test/fixtures/migrations_with_missing_versions/3_we_need_reminders.rb +12 -0
- data/test/fixtures/migrations_with_missing_versions/4_innocent_jointable.rb +12 -0
- data/test/fixtures/mixed_case_monkey.rb +3 -0
- data/test/fixtures/mixed_case_monkeys.yml +6 -0
- data/test/fixtures/mixin.rb +63 -0
- data/test/fixtures/mixins.yml +127 -0
- data/test/fixtures/movie.rb +5 -0
- data/test/fixtures/movies.yml +7 -0
- data/test/fixtures/naked/csv/accounts.csv +1 -0
- data/test/fixtures/naked/yml/accounts.yml +1 -0
- data/test/fixtures/naked/yml/companies.yml +1 -0
- data/test/fixtures/naked/yml/courses.yml +1 -0
- data/test/fixtures/order.rb +4 -0
- data/test/fixtures/people.yml +3 -0
- data/test/fixtures/person.rb +4 -0
- data/test/fixtures/post.rb +58 -0
- data/test/fixtures/posts.yml +48 -0
- data/test/fixtures/project.rb +27 -0
- data/test/fixtures/projects.yml +7 -0
- data/test/fixtures/reader.rb +4 -0
- data/test/fixtures/readers.yml +4 -0
- data/test/fixtures/reply.rb +37 -0
- data/test/fixtures/subject.rb +4 -0
- data/test/fixtures/subscriber.rb +6 -0
- data/test/fixtures/subscribers/first +2 -0
- data/test/fixtures/subscribers/second +2 -0
- data/test/fixtures/tag.rb +7 -0
- data/test/fixtures/tagging.rb +6 -0
- data/test/fixtures/taggings.yml +18 -0
- data/test/fixtures/tags.yml +7 -0
- data/test/fixtures/task.rb +3 -0
- data/test/fixtures/tasks.yml +7 -0
- data/test/fixtures/topic.rb +25 -0
- data/test/fixtures/topics.yml +22 -0
- data/test/fixtures/vertex.rb +9 -0
- data/test/fixtures/vertices.yml +4 -0
- data/test/fixtures_test.rb +401 -0
- data/test/inheritance_test.rb +205 -0
- data/test/lifecycle_test.rb +137 -0
- data/test/locking_test.rb +190 -0
- data/test/method_scoping_test.rb +416 -0
- data/test/migration_test.rb +768 -0
- data/test/migration_test_firebird.rb +124 -0
- data/test/mixin_nested_set_test.rb +196 -0
- data/test/mixin_test.rb +550 -0
- data/test/modules_test.rb +34 -0
- data/test/multiple_db_test.rb +60 -0
- data/test/pk_test.rb +104 -0
- data/test/readonly_test.rb +107 -0
- data/test/reflection_test.rb +159 -0
- data/test/schema_authorization_test_postgresql.rb +75 -0
- data/test/schema_dumper_test.rb +96 -0
- data/test/schema_test_postgresql.rb +64 -0
- data/test/synonym_test_oracle.rb +17 -0
- data/test/table_name_test_sqlserver.rb +23 -0
- data/test/threaded_connections_test.rb +48 -0
- data/test/transactions_test.rb +230 -0
- data/test/unconnected_test.rb +32 -0
- data/test/validations_test.rb +1097 -0
- data/test/xml_serialization_test.rb +125 -0
- metadata +365 -0
@@ -0,0 +1,270 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Calculations #:nodoc:
|
3
|
+
CALCULATIONS_OPTIONS = [:conditions, :joins, :order, :select, :group, :having, :distinct, :limit, :offset, :include]
|
4
|
+
def self.included(base)
|
5
|
+
base.extend(ClassMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
# Count operates using three different approaches.
|
10
|
+
#
|
11
|
+
# * Count all: By not passing any parameters to count, it will return a count of all the rows for the model.
|
12
|
+
# * Count by conditions or joins: This API has been deprecated and will be removed in Rails 2.0
|
13
|
+
# * Count using options will find the row count matched by the options used.
|
14
|
+
#
|
15
|
+
# The last approach, count using options, accepts an option hash as the only parameter. The options are:
|
16
|
+
#
|
17
|
+
# * <tt>:conditions</tt>: An SQL fragment like "administrator = 1" or [ "user_name = ?", username ]. See conditions in the intro.
|
18
|
+
# * <tt>:joins</tt>: An SQL fragment for additional joins like "LEFT JOIN comments ON comments.post_id = id". (Rarely needed).
|
19
|
+
# The records will be returned read-only since they will have attributes that do not correspond to the table's columns.
|
20
|
+
# * <tt>:include</tt>: Named associations that should be loaded alongside using LEFT OUTER JOINs. The symbols named refer
|
21
|
+
# to already defined associations. When using named associations count returns the number DISTINCT items for the model you're counting.
|
22
|
+
# See eager loading under Associations.
|
23
|
+
# * <tt>:order</tt>: An SQL fragment like "created_at DESC, name" (really only used with GROUP BY calculations).
|
24
|
+
# * <tt>:group</tt>: An attribute name by which the result should be grouped. Uses the GROUP BY SQL-clause.
|
25
|
+
# * <tt>:select</tt>: By default, this is * as in SELECT * FROM, but can be changed if you for example want to do a join, but not
|
26
|
+
# include the joined columns.
|
27
|
+
# * <tt>:distinct</tt>: Set this to true to make this a distinct calculation, such as SELECT COUNT(DISTINCT posts.id) ...
|
28
|
+
#
|
29
|
+
# Examples for counting all:
|
30
|
+
# Person.count # returns the total count of all people
|
31
|
+
#
|
32
|
+
# Examples for count by +conditions+ and +joins+ (this has been deprecated):
|
33
|
+
# Person.count("age > 26") # returns the number of people older than 26
|
34
|
+
# Person.find("age > 26 AND job.salary > 60000", "LEFT JOIN jobs on jobs.person_id = person.id") # returns the total number of rows matching the conditions and joins fetched by SELECT COUNT(*).
|
35
|
+
#
|
36
|
+
# Examples for count with options:
|
37
|
+
# Person.count(:conditions => "age > 26")
|
38
|
+
# Person.count(:conditions => "age > 26 AND job.salary > 60000", :include => :job) # because of the named association, it finds the DISTINCT count using LEFT OUTER JOIN.
|
39
|
+
# Person.count(:conditions => "age > 26 AND job.salary > 60000", :joins => "LEFT JOIN jobs on jobs.person_id = person.id") # finds the number of rows matching the conditions and joins.
|
40
|
+
# Person.count('id', :conditions => "age > 26") # Performs a COUNT(id)
|
41
|
+
# Person.count(:all, :conditions => "age > 26") # Performs a COUNT(*) (:all is an alias for '*')
|
42
|
+
#
|
43
|
+
# Note: Person.count(:all) will not work because it will use :all as the condition. Use Person.count instead.
|
44
|
+
def count(*args)
|
45
|
+
calculate(:count, *construct_count_options_from_legacy_args(*args))
|
46
|
+
end
|
47
|
+
|
48
|
+
# Calculates average value on a given column. The value is returned as a float. See #calculate for examples with options.
|
49
|
+
#
|
50
|
+
# Person.average('age')
|
51
|
+
def average(column_name, options = {})
|
52
|
+
calculate(:avg, column_name, options)
|
53
|
+
end
|
54
|
+
|
55
|
+
# Calculates the minimum value on a given column. The value is returned with the same data type of the column.. See #calculate for examples with options.
|
56
|
+
#
|
57
|
+
# Person.minimum('age')
|
58
|
+
def minimum(column_name, options = {})
|
59
|
+
calculate(:min, column_name, options)
|
60
|
+
end
|
61
|
+
|
62
|
+
# Calculates the maximum value on a given column. The value is returned with the same data type of the column.. See #calculate for examples with options.
|
63
|
+
#
|
64
|
+
# Person.maximum('age')
|
65
|
+
def maximum(column_name, options = {})
|
66
|
+
calculate(:max, column_name, options)
|
67
|
+
end
|
68
|
+
|
69
|
+
# Calculates the sum value on a given column. The value is returned with the same data type of the column.. See #calculate for examples with options.
|
70
|
+
#
|
71
|
+
# Person.sum('age')
|
72
|
+
def sum(column_name, options = {})
|
73
|
+
calculate(:sum, column_name, options)
|
74
|
+
end
|
75
|
+
|
76
|
+
# This calculates aggregate values in the given column: Methods for count, sum, average, minimum, and maximum have been added as shortcuts.
|
77
|
+
# Options such as :conditions, :order, :group, :having, and :joins can be passed to customize the query.
|
78
|
+
#
|
79
|
+
# There are two basic forms of output:
|
80
|
+
# * Single aggregate value: The single value is type cast to Fixnum for COUNT, Float for AVG, and the given column's type for everything else.
|
81
|
+
# * Grouped values: This returns an ordered hash of the values and groups them by the :group option. It takes either a column name, or the name
|
82
|
+
# of a belongs_to association.
|
83
|
+
#
|
84
|
+
# values = Person.maximum(:age, :group => 'last_name')
|
85
|
+
# puts values["Drake"]
|
86
|
+
# => 43
|
87
|
+
#
|
88
|
+
# drake = Family.find_by_last_name('Drake')
|
89
|
+
# values = Person.maximum(:age, :group => :family) # Person belongs_to :family
|
90
|
+
# puts values[drake]
|
91
|
+
# => 43
|
92
|
+
#
|
93
|
+
# values.each do |family, max_age|
|
94
|
+
# ...
|
95
|
+
# end
|
96
|
+
#
|
97
|
+
# Options:
|
98
|
+
# * <tt>:conditions</tt>: An SQL fragment like "administrator = 1" or [ "user_name = ?", username ]. See conditions in the intro.
|
99
|
+
# * <tt>:joins</tt>: An SQL fragment for additional joins like "LEFT JOIN comments ON comments.post_id = id". (Rarely needed).
|
100
|
+
# The records will be returned read-only since they will have attributes that do not correspond to the table's columns.
|
101
|
+
# * <tt>:order</tt>: An SQL fragment like "created_at DESC, name" (really only used with GROUP BY calculations).
|
102
|
+
# * <tt>:group</tt>: An attribute name by which the result should be grouped. Uses the GROUP BY SQL-clause.
|
103
|
+
# * <tt>:select</tt>: By default, this is * as in SELECT * FROM, but can be changed if you for example want to do a join, but not
|
104
|
+
# include the joined columns.
|
105
|
+
# * <tt>:distinct</tt>: Set this to true to make this a distinct calculation, such as SELECT COUNT(DISTINCT posts.id) ...
|
106
|
+
#
|
107
|
+
# Examples:
|
108
|
+
# Person.calculate(:count, :all) # The same as Person.count
|
109
|
+
# Person.average(:age) # SELECT AVG(age) FROM people...
|
110
|
+
# Person.minimum(:age, :conditions => ['last_name != ?', 'Drake']) # Selects the minimum age for everyone with a last name other than 'Drake'
|
111
|
+
# Person.minimum(:age, :having => 'min(age) > 17', :group => :last_name) # Selects the minimum age for any family without any minors
|
112
|
+
def calculate(operation, column_name, options = {})
|
113
|
+
validate_calculation_options(operation, options)
|
114
|
+
column_name = options[:select] if options[:select]
|
115
|
+
column_name = '*' if column_name == :all
|
116
|
+
column = column_for column_name
|
117
|
+
catch :invalid_query do
|
118
|
+
if options[:group]
|
119
|
+
return execute_grouped_calculation(operation, column_name, column, options)
|
120
|
+
else
|
121
|
+
return execute_simple_calculation(operation, column_name, column, options)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
0
|
125
|
+
end
|
126
|
+
|
127
|
+
protected
|
128
|
+
def construct_count_options_from_legacy_args(*args)
|
129
|
+
options = {}
|
130
|
+
column_name = :all
|
131
|
+
|
132
|
+
# We need to handle
|
133
|
+
# count()
|
134
|
+
# count(options={})
|
135
|
+
# count(column_name=:all, options={})
|
136
|
+
# count(conditions=nil, joins=nil) # deprecated
|
137
|
+
if args.size > 2
|
138
|
+
raise ArgumentError, "Unexpected parameters passed to count(options={}): #{args.inspect}"
|
139
|
+
elsif args.size > 0
|
140
|
+
if args[0].is_a?(Hash)
|
141
|
+
options = args[0]
|
142
|
+
elsif args[1].is_a?(Hash)
|
143
|
+
column_name, options = args
|
144
|
+
else
|
145
|
+
# Deprecated count(conditions, joins=nil)
|
146
|
+
ActiveSupport::Deprecation.warn(
|
147
|
+
"You called count(#{args[0].inspect}, #{args[1].inspect}), which is a deprecated API call. " +
|
148
|
+
"Instead you should use count(column_name, options). Passing the conditions and joins as " +
|
149
|
+
"string parameters will be removed in Rails 2.0.", caller(2)
|
150
|
+
)
|
151
|
+
options.merge!(:conditions => args[0])
|
152
|
+
options.merge!(:joins => args[1]) if args[1]
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
[column_name, options]
|
157
|
+
end
|
158
|
+
|
159
|
+
def construct_calculation_sql(operation, column_name, options) #:nodoc:
|
160
|
+
operation = operation.to_s.downcase
|
161
|
+
options = options.symbolize_keys
|
162
|
+
|
163
|
+
scope = scope(:find)
|
164
|
+
merged_includes = merge_includes(scope ? scope[:include] : [], options[:include])
|
165
|
+
aggregate_alias = column_alias_for(operation, column_name)
|
166
|
+
use_workaround = !Base.connection.supports_count_distinct? && options[:distinct] && operation.to_s.downcase == 'count'
|
167
|
+
join_dependency = nil
|
168
|
+
|
169
|
+
if merged_includes.any? && operation.to_s.downcase == 'count'
|
170
|
+
options[:distinct] = true
|
171
|
+
column_name = options[:select] || [table_name, primary_key] * '.'
|
172
|
+
end
|
173
|
+
|
174
|
+
sql = "SELECT #{operation}(#{'DISTINCT ' if options[:distinct]}#{column_name}) AS #{aggregate_alias}"
|
175
|
+
|
176
|
+
# A (slower) workaround if we're using a backend, like sqlite, that doesn't support COUNT DISTINCT.
|
177
|
+
sql = "SELECT COUNT(*) AS #{aggregate_alias}" if use_workaround
|
178
|
+
|
179
|
+
sql << ", #{options[:group_field]} AS #{options[:group_alias]}" if options[:group]
|
180
|
+
sql << " FROM (SELECT DISTINCT #{column_name}" if use_workaround
|
181
|
+
sql << " FROM #{table_name} "
|
182
|
+
if merged_includes.any?
|
183
|
+
join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(self, merged_includes, options[:joins])
|
184
|
+
sql << join_dependency.join_associations.collect{|join| join.association_join }.join
|
185
|
+
end
|
186
|
+
add_joins!(sql, options, scope)
|
187
|
+
add_conditions!(sql, options[:conditions], scope)
|
188
|
+
add_limited_ids_condition!(sql, options, join_dependency) if join_dependency && !using_limitable_reflections?(join_dependency.reflections) && ((scope && scope[:limit]) || options[:limit])
|
189
|
+
|
190
|
+
if options[:group]
|
191
|
+
group_key = Base.connection.adapter_name == 'FrontBase' ? :group_alias : :group_field
|
192
|
+
sql << " GROUP BY #{options[group_key]} "
|
193
|
+
end
|
194
|
+
|
195
|
+
if options[:group] && options[:having]
|
196
|
+
# FrontBase requires identifiers in the HAVING clause and chokes on function calls
|
197
|
+
if Base.connection.adapter_name == 'FrontBase'
|
198
|
+
options[:having].downcase!
|
199
|
+
options[:having].gsub!(/#{operation}\s*\(\s*#{column_name}\s*\)/, aggregate_alias)
|
200
|
+
end
|
201
|
+
|
202
|
+
sql << " HAVING #{options[:having]} "
|
203
|
+
end
|
204
|
+
|
205
|
+
sql << " ORDER BY #{options[:order]} " if options[:order]
|
206
|
+
add_limit!(sql, options, scope)
|
207
|
+
sql << ')' if use_workaround
|
208
|
+
sql
|
209
|
+
end
|
210
|
+
|
211
|
+
def execute_simple_calculation(operation, column_name, column, options) #:nodoc:
|
212
|
+
value = connection.select_value(construct_calculation_sql(operation, column_name, options))
|
213
|
+
type_cast_calculated_value(value, column, operation)
|
214
|
+
end
|
215
|
+
|
216
|
+
def execute_grouped_calculation(operation, column_name, column, options) #:nodoc:
|
217
|
+
group_attr = options[:group].to_s
|
218
|
+
association = reflect_on_association(group_attr.to_sym)
|
219
|
+
associated = association && association.macro == :belongs_to # only count belongs_to associations
|
220
|
+
group_field = (associated ? "#{options[:group]}_id" : options[:group]).to_s
|
221
|
+
group_alias = column_alias_for(group_field)
|
222
|
+
group_column = column_for group_field
|
223
|
+
sql = construct_calculation_sql(operation, column_name, options.merge(:group_field => group_field, :group_alias => group_alias))
|
224
|
+
calculated_data = connection.select_all(sql)
|
225
|
+
aggregate_alias = column_alias_for(operation, column_name)
|
226
|
+
|
227
|
+
if association
|
228
|
+
key_ids = calculated_data.collect { |row| row[group_alias] }
|
229
|
+
key_records = association.klass.base_class.find(key_ids)
|
230
|
+
key_records = key_records.inject({}) { |hsh, r| hsh.merge(r.id => r) }
|
231
|
+
end
|
232
|
+
|
233
|
+
calculated_data.inject(ActiveSupport::OrderedHash.new) do |all, row|
|
234
|
+
key = associated ? key_records[row[group_alias].to_i] : type_cast_calculated_value(row[group_alias], group_column)
|
235
|
+
value = row[aggregate_alias]
|
236
|
+
all << [key, type_cast_calculated_value(value, column, operation)]
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
private
|
241
|
+
def validate_calculation_options(operation, options = {})
|
242
|
+
options.assert_valid_keys(CALCULATIONS_OPTIONS)
|
243
|
+
end
|
244
|
+
|
245
|
+
# converts a given key to the value that the database adapter returns as
|
246
|
+
#
|
247
|
+
# users.id #=> users_id
|
248
|
+
# sum(id) #=> sum_id
|
249
|
+
# count(distinct users.id) #=> count_distinct_users_id
|
250
|
+
# count(*) #=> count_all
|
251
|
+
def column_alias_for(*keys)
|
252
|
+
connection.table_alias_for(keys.join(' ').downcase.gsub(/\*/, 'all').gsub(/\W+/, ' ').strip.gsub(/ +/, '_'))
|
253
|
+
end
|
254
|
+
|
255
|
+
def column_for(field)
|
256
|
+
field_name = field.to_s.split('.').last
|
257
|
+
columns.detect { |c| c.name.to_s == field_name }
|
258
|
+
end
|
259
|
+
|
260
|
+
def type_cast_calculated_value(value, column, operation = nil)
|
261
|
+
operation = operation.to_s.downcase
|
262
|
+
case operation
|
263
|
+
when 'count' then value.to_i
|
264
|
+
when 'avg' then value.to_f
|
265
|
+
else column ? column.type_cast(value) : value
|
266
|
+
end
|
267
|
+
end
|
268
|
+
end
|
269
|
+
end
|
270
|
+
end
|
@@ -0,0 +1,367 @@
|
|
1
|
+
require 'observer'
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
# Callbacks are hooks into the lifecycle of an Active Record object that allows you to trigger logic
|
5
|
+
# before or after an alteration of the object state. This can be used to make sure that associated and
|
6
|
+
# dependent objects are deleted when destroy is called (by overwriting before_destroy) or to massage attributes
|
7
|
+
# before they're validated (by overwriting before_validation). As an example of the callbacks initiated, consider
|
8
|
+
# the Base#save call:
|
9
|
+
#
|
10
|
+
# * (-) save
|
11
|
+
# * (-) valid?
|
12
|
+
# * (1) before_validation
|
13
|
+
# * (2) before_validation_on_create
|
14
|
+
# * (-) validate
|
15
|
+
# * (-) validate_on_create
|
16
|
+
# * (3) after_validation
|
17
|
+
# * (4) after_validation_on_create
|
18
|
+
# * (5) before_save
|
19
|
+
# * (6) before_create
|
20
|
+
# * (-) create
|
21
|
+
# * (7) after_create
|
22
|
+
# * (8) after_save
|
23
|
+
#
|
24
|
+
# That's a total of eight callbacks, which gives you immense power to react and prepare for each state in the
|
25
|
+
# Active Record lifecycle.
|
26
|
+
#
|
27
|
+
# Examples:
|
28
|
+
# class CreditCard < ActiveRecord::Base
|
29
|
+
# # Strip everything but digits, so the user can specify "555 234 34" or
|
30
|
+
# # "5552-3434" or both will mean "55523434"
|
31
|
+
# def before_validation_on_create
|
32
|
+
# self.number = number.gsub(/[^0-9]/, "") if attribute_present?("number")
|
33
|
+
# end
|
34
|
+
# end
|
35
|
+
#
|
36
|
+
# class Subscription < ActiveRecord::Base
|
37
|
+
# before_create :record_signup
|
38
|
+
#
|
39
|
+
# private
|
40
|
+
# def record_signup
|
41
|
+
# self.signed_up_on = Date.today
|
42
|
+
# end
|
43
|
+
# end
|
44
|
+
#
|
45
|
+
# class Firm < ActiveRecord::Base
|
46
|
+
# # Destroys the associated clients and people when the firm is destroyed
|
47
|
+
# before_destroy { |record| Person.destroy_all "firm_id = #{record.id}" }
|
48
|
+
# before_destroy { |record| Client.destroy_all "client_of = #{record.id}" }
|
49
|
+
# end
|
50
|
+
#
|
51
|
+
# == Inheritable callback queues
|
52
|
+
#
|
53
|
+
# Besides the overwriteable callback methods, it's also possible to register callbacks through the use of the callback macros.
|
54
|
+
# Their main advantage is that the macros add behavior into a callback queue that is kept intact down through an inheritance
|
55
|
+
# hierarchy. Example:
|
56
|
+
#
|
57
|
+
# class Topic < ActiveRecord::Base
|
58
|
+
# before_destroy :destroy_author
|
59
|
+
# end
|
60
|
+
#
|
61
|
+
# class Reply < Topic
|
62
|
+
# before_destroy :destroy_readers
|
63
|
+
# end
|
64
|
+
#
|
65
|
+
# Now, when Topic#destroy is run only +destroy_author+ is called. When Reply#destroy is run both +destroy_author+ and
|
66
|
+
# +destroy_readers+ is called. Contrast this to the situation where we've implemented the save behavior through overwriteable
|
67
|
+
# methods:
|
68
|
+
#
|
69
|
+
# class Topic < ActiveRecord::Base
|
70
|
+
# def before_destroy() destroy_author end
|
71
|
+
# end
|
72
|
+
#
|
73
|
+
# class Reply < Topic
|
74
|
+
# def before_destroy() destroy_readers end
|
75
|
+
# end
|
76
|
+
#
|
77
|
+
# In that case, Reply#destroy would only run +destroy_readers+ and _not_ +destroy_author+. So use the callback macros when
|
78
|
+
# you want to ensure that a certain callback is called for the entire hierarchy and the regular overwriteable methods when you
|
79
|
+
# want to leave it up to each descendent to decide whether they want to call +super+ and trigger the inherited callbacks.
|
80
|
+
#
|
81
|
+
# *IMPORTANT:* In order for inheritance to work for the callback queues, you must specify the callbacks before specifying the
|
82
|
+
# associations. Otherwise, you might trigger the loading of a child before the parent has registered the callbacks and they won't
|
83
|
+
# be inherited.
|
84
|
+
#
|
85
|
+
# == Types of callbacks
|
86
|
+
#
|
87
|
+
# There are four types of callbacks accepted by the callback macros: Method references (symbol), callback objects,
|
88
|
+
# inline methods (using a proc), and inline eval methods (using a string). Method references and callback objects are the
|
89
|
+
# recommended approaches, inline methods using a proc are sometimes appropriate (such as for creating mix-ins), and inline
|
90
|
+
# eval methods are deprecated.
|
91
|
+
#
|
92
|
+
# The method reference callbacks work by specifying a protected or private method available in the object, like this:
|
93
|
+
#
|
94
|
+
# class Topic < ActiveRecord::Base
|
95
|
+
# before_destroy :delete_parents
|
96
|
+
#
|
97
|
+
# private
|
98
|
+
# def delete_parents
|
99
|
+
# self.class.delete_all "parent_id = #{id}"
|
100
|
+
# end
|
101
|
+
# end
|
102
|
+
#
|
103
|
+
# The callback objects have methods named after the callback called with the record as the only parameter, such as:
|
104
|
+
#
|
105
|
+
# class BankAccount < ActiveRecord::Base
|
106
|
+
# before_save EncryptionWrapper.new("credit_card_number")
|
107
|
+
# after_save EncryptionWrapper.new("credit_card_number")
|
108
|
+
# after_initialize EncryptionWrapper.new("credit_card_number")
|
109
|
+
# end
|
110
|
+
#
|
111
|
+
# class EncryptionWrapper
|
112
|
+
# def initialize(attribute)
|
113
|
+
# @attribute = attribute
|
114
|
+
# end
|
115
|
+
#
|
116
|
+
# def before_save(record)
|
117
|
+
# record.credit_card_number = encrypt(record.credit_card_number)
|
118
|
+
# end
|
119
|
+
#
|
120
|
+
# def after_save(record)
|
121
|
+
# record.credit_card_number = decrypt(record.credit_card_number)
|
122
|
+
# end
|
123
|
+
#
|
124
|
+
# alias_method :after_find, :after_save
|
125
|
+
#
|
126
|
+
# private
|
127
|
+
# def encrypt(value)
|
128
|
+
# # Secrecy is committed
|
129
|
+
# end
|
130
|
+
#
|
131
|
+
# def decrypt(value)
|
132
|
+
# # Secrecy is unveiled
|
133
|
+
# end
|
134
|
+
# end
|
135
|
+
#
|
136
|
+
# So you specify the object you want messaged on a given callback. When that callback is triggered, the object has
|
137
|
+
# a method by the name of the callback messaged.
|
138
|
+
#
|
139
|
+
# The callback macros usually accept a symbol for the method they're supposed to run, but you can also pass a "method string",
|
140
|
+
# which will then be evaluated within the binding of the callback. Example:
|
141
|
+
#
|
142
|
+
# class Topic < ActiveRecord::Base
|
143
|
+
# before_destroy 'self.class.delete_all "parent_id = #{id}"'
|
144
|
+
# end
|
145
|
+
#
|
146
|
+
# Notice that single plings (') are used so the #{id} part isn't evaluated until the callback is triggered. Also note that these
|
147
|
+
# inline callbacks can be stacked just like the regular ones:
|
148
|
+
#
|
149
|
+
# class Topic < ActiveRecord::Base
|
150
|
+
# before_destroy 'self.class.delete_all "parent_id = #{id}"',
|
151
|
+
# 'puts "Evaluated after parents are destroyed"'
|
152
|
+
# end
|
153
|
+
#
|
154
|
+
# == The after_find and after_initialize exceptions
|
155
|
+
#
|
156
|
+
# Because after_find and after_initialize are called for each object found and instantiated by a finder, such as Base.find(:all), we've had
|
157
|
+
# to implement a simple performance constraint (50% more speed on a simple test case). Unlike all the other callbacks, after_find and
|
158
|
+
# after_initialize will only be run if an explicit implementation is defined (<tt>def after_find</tt>). In that case, all of the
|
159
|
+
# callback types will be called.
|
160
|
+
#
|
161
|
+
# == before_validation* returning statements
|
162
|
+
#
|
163
|
+
# If the returning value of a before_validation callback can be evaluated to false, the process will be aborted and Base#save will return false.
|
164
|
+
# If Base#save! is called it will raise a RecordNotSave error.
|
165
|
+
# Nothing will be appended to the errors object.
|
166
|
+
#
|
167
|
+
# == Cancelling callbacks
|
168
|
+
#
|
169
|
+
# If a before_* callback returns false, all the later callbacks and the associated action are cancelled. If an after_* callback returns
|
170
|
+
# false, all the later callbacks are cancelled. Callbacks are generally run in the order they are defined, with the exception of callbacks
|
171
|
+
# defined as methods on the model, which are called last.
|
172
|
+
module Callbacks
|
173
|
+
CALLBACKS = %w(
|
174
|
+
after_find after_initialize before_save after_save before_create after_create before_update after_update before_validation
|
175
|
+
after_validation before_validation_on_create after_validation_on_create before_validation_on_update
|
176
|
+
after_validation_on_update before_destroy after_destroy
|
177
|
+
)
|
178
|
+
|
179
|
+
def self.included(base) #:nodoc:
|
180
|
+
base.extend(ClassMethods)
|
181
|
+
base.class_eval do
|
182
|
+
class << self
|
183
|
+
include Observable
|
184
|
+
alias_method_chain :instantiate, :callbacks
|
185
|
+
end
|
186
|
+
|
187
|
+
[:initialize, :create_or_update, :valid?, :create, :update, :destroy].each do |method|
|
188
|
+
alias_method_chain method, :callbacks
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
CALLBACKS.each do |method|
|
193
|
+
base.class_eval <<-"end_eval"
|
194
|
+
def self.#{method}(*callbacks, &block)
|
195
|
+
callbacks << block if block_given?
|
196
|
+
write_inheritable_array(#{method.to_sym.inspect}, callbacks)
|
197
|
+
end
|
198
|
+
end_eval
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
module ClassMethods #:nodoc:
|
203
|
+
def instantiate_with_callbacks(record)
|
204
|
+
object = instantiate_without_callbacks(record)
|
205
|
+
|
206
|
+
if object.respond_to_without_attributes?(:after_find)
|
207
|
+
object.send(:callback, :after_find)
|
208
|
+
end
|
209
|
+
|
210
|
+
if object.respond_to_without_attributes?(:after_initialize)
|
211
|
+
object.send(:callback, :after_initialize)
|
212
|
+
end
|
213
|
+
|
214
|
+
object
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
# Is called when the object was instantiated by one of the finders, like Base.find.
|
219
|
+
#def after_find() end
|
220
|
+
|
221
|
+
# Is called after the object has been instantiated by a call to Base.new.
|
222
|
+
#def after_initialize() end
|
223
|
+
|
224
|
+
def initialize_with_callbacks(attributes = nil) #:nodoc:
|
225
|
+
initialize_without_callbacks(attributes)
|
226
|
+
result = yield self if block_given?
|
227
|
+
callback(:after_initialize) if respond_to_without_attributes?(:after_initialize)
|
228
|
+
result
|
229
|
+
end
|
230
|
+
|
231
|
+
# Is called _before_ Base.save (regardless of whether it's a create or update save).
|
232
|
+
def before_save() end
|
233
|
+
|
234
|
+
# Is called _after_ Base.save (regardless of whether it's a create or update save).
|
235
|
+
#
|
236
|
+
# class Contact < ActiveRecord::Base
|
237
|
+
# after_save { logger.info( 'New contact saved!' ) }
|
238
|
+
# end
|
239
|
+
def after_save() end
|
240
|
+
def create_or_update_with_callbacks #:nodoc:
|
241
|
+
return false if callback(:before_save) == false
|
242
|
+
result = create_or_update_without_callbacks
|
243
|
+
callback(:after_save)
|
244
|
+
result
|
245
|
+
end
|
246
|
+
|
247
|
+
# Is called _before_ Base.save on new objects that haven't been saved yet (no record exists).
|
248
|
+
def before_create() end
|
249
|
+
|
250
|
+
# Is called _after_ Base.save on new objects that haven't been saved yet (no record exists).
|
251
|
+
def after_create() end
|
252
|
+
def create_with_callbacks #:nodoc:
|
253
|
+
return false if callback(:before_create) == false
|
254
|
+
result = create_without_callbacks
|
255
|
+
callback(:after_create)
|
256
|
+
result
|
257
|
+
end
|
258
|
+
|
259
|
+
# Is called _before_ Base.save on existing objects that have a record.
|
260
|
+
def before_update() end
|
261
|
+
|
262
|
+
# Is called _after_ Base.save on existing objects that have a record.
|
263
|
+
def after_update() end
|
264
|
+
|
265
|
+
def update_with_callbacks #:nodoc:
|
266
|
+
return false if callback(:before_update) == false
|
267
|
+
result = update_without_callbacks
|
268
|
+
callback(:after_update)
|
269
|
+
result
|
270
|
+
end
|
271
|
+
|
272
|
+
# Is called _before_ Validations.validate (which is part of the Base.save call).
|
273
|
+
def before_validation() end
|
274
|
+
|
275
|
+
# Is called _after_ Validations.validate (which is part of the Base.save call).
|
276
|
+
def after_validation() end
|
277
|
+
|
278
|
+
# Is called _before_ Validations.validate (which is part of the Base.save call) on new objects
|
279
|
+
# that haven't been saved yet (no record exists).
|
280
|
+
def before_validation_on_create() end
|
281
|
+
|
282
|
+
# Is called _after_ Validations.validate (which is part of the Base.save call) on new objects
|
283
|
+
# that haven't been saved yet (no record exists).
|
284
|
+
def after_validation_on_create() end
|
285
|
+
|
286
|
+
# Is called _before_ Validations.validate (which is part of the Base.save call) on
|
287
|
+
# existing objects that have a record.
|
288
|
+
def before_validation_on_update() end
|
289
|
+
|
290
|
+
# Is called _after_ Validations.validate (which is part of the Base.save call) on
|
291
|
+
# existing objects that have a record.
|
292
|
+
def after_validation_on_update() end
|
293
|
+
|
294
|
+
def valid_with_callbacks? #:nodoc:
|
295
|
+
return false if callback(:before_validation) == false
|
296
|
+
if new_record? then result = callback(:before_validation_on_create) else result = callback(:before_validation_on_update) end
|
297
|
+
return false if result == false
|
298
|
+
|
299
|
+
result = valid_without_callbacks?
|
300
|
+
|
301
|
+
callback(:after_validation)
|
302
|
+
if new_record? then callback(:after_validation_on_create) else callback(:after_validation_on_update) end
|
303
|
+
|
304
|
+
return result
|
305
|
+
end
|
306
|
+
|
307
|
+
# Is called _before_ Base.destroy.
|
308
|
+
#
|
309
|
+
# Note: If you need to _destroy_ or _nullify_ associated records first,
|
310
|
+
# use the _:dependent_ option on your associations.
|
311
|
+
def before_destroy() end
|
312
|
+
|
313
|
+
# Is called _after_ Base.destroy (and all the attributes have been frozen).
|
314
|
+
#
|
315
|
+
# class Contact < ActiveRecord::Base
|
316
|
+
# after_destroy { |record| logger.info( "Contact #{record.id} was destroyed." ) }
|
317
|
+
# end
|
318
|
+
def after_destroy() end
|
319
|
+
def destroy_with_callbacks #:nodoc:
|
320
|
+
return false if callback(:before_destroy) == false
|
321
|
+
result = destroy_without_callbacks
|
322
|
+
callback(:after_destroy)
|
323
|
+
result
|
324
|
+
end
|
325
|
+
|
326
|
+
private
|
327
|
+
def callback(method)
|
328
|
+
notify(method)
|
329
|
+
|
330
|
+
callbacks_for(method).each do |callback|
|
331
|
+
result = case callback
|
332
|
+
when Symbol
|
333
|
+
self.send(callback)
|
334
|
+
when String
|
335
|
+
eval(callback, binding)
|
336
|
+
when Proc, Method
|
337
|
+
callback.call(self)
|
338
|
+
else
|
339
|
+
if callback.respond_to?(method)
|
340
|
+
callback.send(method, self)
|
341
|
+
else
|
342
|
+
raise ActiveRecordError, "Callbacks must be a symbol denoting the method to call, a string to be evaluated, a block to be invoked, or an object responding to the callback method."
|
343
|
+
end
|
344
|
+
end
|
345
|
+
return false if result == false
|
346
|
+
end
|
347
|
+
|
348
|
+
result = send(method) if respond_to_without_attributes?(method)
|
349
|
+
|
350
|
+
return result
|
351
|
+
end
|
352
|
+
|
353
|
+
def callbacks_for(method)
|
354
|
+
self.class.read_inheritable_attribute(method.to_sym) or []
|
355
|
+
end
|
356
|
+
|
357
|
+
def invoke_and_notify(method)
|
358
|
+
notify(method)
|
359
|
+
send(method) if respond_to_without_attributes?(method)
|
360
|
+
end
|
361
|
+
|
362
|
+
def notify(method) #:nodoc:
|
363
|
+
self.class.changed
|
364
|
+
self.class.notify_observers(method, self)
|
365
|
+
end
|
366
|
+
end
|
367
|
+
end
|