activerecord_authorails 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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,211 @@
|
|
|
1
|
+
module ActiveRecord
|
|
2
|
+
module Acts #:nodoc:
|
|
3
|
+
module NestedSet #:nodoc:
|
|
4
|
+
def self.included(base)
|
|
5
|
+
base.extend(ClassMethods)
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
# This acts provides Nested Set functionality. Nested Set is similiar to Tree, but with
|
|
9
|
+
# the added feature that you can select the children and all of their descendents with
|
|
10
|
+
# a single query. A good use case for this is a threaded post system, where you want
|
|
11
|
+
# to display every reply to a comment without multiple selects.
|
|
12
|
+
#
|
|
13
|
+
# A google search for "Nested Set" should point you in the direction to explain the
|
|
14
|
+
# database theory. I figured out a bunch of this from
|
|
15
|
+
# http://threebit.net/tutorials/nestedset/tutorial1.html
|
|
16
|
+
#
|
|
17
|
+
# Instead of picturing a leaf node structure with children pointing back to their parent,
|
|
18
|
+
# the best way to imagine how this works is to think of the parent entity surrounding all
|
|
19
|
+
# of its children, and its parent surrounding it, etc. Assuming that they are lined up
|
|
20
|
+
# horizontally, we store the left and right boundries in the database.
|
|
21
|
+
#
|
|
22
|
+
# Imagine:
|
|
23
|
+
# root
|
|
24
|
+
# |_ Child 1
|
|
25
|
+
# |_ Child 1.1
|
|
26
|
+
# |_ Child 1.2
|
|
27
|
+
# |_ Child 2
|
|
28
|
+
# |_ Child 2.1
|
|
29
|
+
# |_ Child 2.2
|
|
30
|
+
#
|
|
31
|
+
# If my cirlces in circles description didn't make sense, check out this sweet
|
|
32
|
+
# ASCII art:
|
|
33
|
+
#
|
|
34
|
+
# ___________________________________________________________________
|
|
35
|
+
# | Root |
|
|
36
|
+
# | ____________________________ ____________________________ |
|
|
37
|
+
# | | Child 1 | | Child 2 | |
|
|
38
|
+
# | | __________ _________ | | __________ _________ | |
|
|
39
|
+
# | | | C 1.1 | | C 1.2 | | | | C 2.1 | | C 2.2 | | |
|
|
40
|
+
# 1 2 3_________4 5________6 7 8 9_________10 11_______12 13 14
|
|
41
|
+
# | |___________________________| |___________________________| |
|
|
42
|
+
# |___________________________________________________________________|
|
|
43
|
+
#
|
|
44
|
+
# The numbers represent the left and right boundries. The table then might
|
|
45
|
+
# look like this:
|
|
46
|
+
# ID | PARENT | LEFT | RIGHT | DATA
|
|
47
|
+
# 1 | 0 | 1 | 14 | root
|
|
48
|
+
# 2 | 1 | 2 | 7 | Child 1
|
|
49
|
+
# 3 | 2 | 3 | 4 | Child 1.1
|
|
50
|
+
# 4 | 2 | 5 | 6 | Child 1.2
|
|
51
|
+
# 5 | 1 | 8 | 13 | Child 2
|
|
52
|
+
# 6 | 5 | 9 | 10 | Child 2.1
|
|
53
|
+
# 7 | 5 | 11 | 12 | Child 2.2
|
|
54
|
+
#
|
|
55
|
+
# So, to get all children of an entry, you
|
|
56
|
+
# SELECT * WHERE CHILD.LEFT IS BETWEEN PARENT.LEFT AND PARENT.RIGHT
|
|
57
|
+
#
|
|
58
|
+
# To get the count, it's (LEFT - RIGHT + 1)/2, etc.
|
|
59
|
+
#
|
|
60
|
+
# To get the direct parent, it falls back to using the PARENT_ID field.
|
|
61
|
+
#
|
|
62
|
+
# There are instance methods for all of these.
|
|
63
|
+
#
|
|
64
|
+
# The structure is good if you need to group things together; the downside is that
|
|
65
|
+
# keeping data integrity is a pain, and both adding and removing an entry
|
|
66
|
+
# require a full table write.
|
|
67
|
+
#
|
|
68
|
+
# This sets up a before_destroy trigger to prune the tree correctly if one of its
|
|
69
|
+
# elements gets deleted.
|
|
70
|
+
#
|
|
71
|
+
module ClassMethods
|
|
72
|
+
# Configuration options are:
|
|
73
|
+
#
|
|
74
|
+
# * +parent_column+ - specifies the column name to use for keeping the position integer (default: parent_id)
|
|
75
|
+
# * +left_column+ - column name for left boundry data, default "lft"
|
|
76
|
+
# * +right_column+ - column name for right boundry data, default "rgt"
|
|
77
|
+
# * +scope+ - restricts what is to be considered a list. Given a symbol, it'll attach "_id"
|
|
78
|
+
# (if that hasn't been already) and use that as the foreign key restriction. It's also possible
|
|
79
|
+
# to give it an entire string that is interpolated if you need a tighter scope than just a foreign key.
|
|
80
|
+
# Example: <tt>acts_as_list :scope => 'todo_list_id = #{todo_list_id} AND completed = 0'</tt>
|
|
81
|
+
def acts_as_nested_set(options = {})
|
|
82
|
+
configuration = { :parent_column => "parent_id", :left_column => "lft", :right_column => "rgt", :scope => "1 = 1" }
|
|
83
|
+
|
|
84
|
+
configuration.update(options) if options.is_a?(Hash)
|
|
85
|
+
|
|
86
|
+
configuration[:scope] = "#{configuration[:scope]}_id".intern if configuration[:scope].is_a?(Symbol) && configuration[:scope].to_s !~ /_id$/
|
|
87
|
+
|
|
88
|
+
if configuration[:scope].is_a?(Symbol)
|
|
89
|
+
scope_condition_method = %(
|
|
90
|
+
def scope_condition
|
|
91
|
+
if #{configuration[:scope].to_s}.nil?
|
|
92
|
+
"#{configuration[:scope].to_s} IS NULL"
|
|
93
|
+
else
|
|
94
|
+
"#{configuration[:scope].to_s} = \#{#{configuration[:scope].to_s}}"
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
)
|
|
98
|
+
else
|
|
99
|
+
scope_condition_method = "def scope_condition() \"#{configuration[:scope]}\" end"
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
class_eval <<-EOV
|
|
103
|
+
include ActiveRecord::Acts::NestedSet::InstanceMethods
|
|
104
|
+
|
|
105
|
+
#{scope_condition_method}
|
|
106
|
+
|
|
107
|
+
def left_col_name() "#{configuration[:left_column]}" end
|
|
108
|
+
|
|
109
|
+
def right_col_name() "#{configuration[:right_column]}" end
|
|
110
|
+
|
|
111
|
+
def parent_column() "#{configuration[:parent_column]}" end
|
|
112
|
+
|
|
113
|
+
EOV
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
module InstanceMethods
|
|
118
|
+
# Returns true is this is a root node.
|
|
119
|
+
def root?
|
|
120
|
+
parent_id = self[parent_column]
|
|
121
|
+
(parent_id == 0 || parent_id.nil?) && (self[left_col_name] == 1) && (self[right_col_name] > self[left_col_name])
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# Returns true is this is a child node
|
|
125
|
+
def child?
|
|
126
|
+
parent_id = self[parent_column]
|
|
127
|
+
!(parent_id == 0 || parent_id.nil?) && (self[left_col_name] > 1) && (self[right_col_name] > self[left_col_name])
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# Returns true if we have no idea what this is
|
|
131
|
+
def unknown?
|
|
132
|
+
!root? && !child?
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
# Adds a child to this object in the tree. If this object hasn't been initialized,
|
|
137
|
+
# it gets set up as a root node. Otherwise, this method will update all of the
|
|
138
|
+
# other elements in the tree and shift them to the right, keeping everything
|
|
139
|
+
# balanced.
|
|
140
|
+
def add_child( child )
|
|
141
|
+
self.reload
|
|
142
|
+
child.reload
|
|
143
|
+
|
|
144
|
+
if child.root?
|
|
145
|
+
raise "Adding sub-tree isn\'t currently supported"
|
|
146
|
+
else
|
|
147
|
+
if ( (self[left_col_name] == nil) || (self[right_col_name] == nil) )
|
|
148
|
+
# Looks like we're now the root node! Woo
|
|
149
|
+
self[left_col_name] = 1
|
|
150
|
+
self[right_col_name] = 4
|
|
151
|
+
|
|
152
|
+
# What do to do about validation?
|
|
153
|
+
return nil unless self.save
|
|
154
|
+
|
|
155
|
+
child[parent_column] = self.id
|
|
156
|
+
child[left_col_name] = 2
|
|
157
|
+
child[right_col_name]= 3
|
|
158
|
+
return child.save
|
|
159
|
+
else
|
|
160
|
+
# OK, we need to add and shift everything else to the right
|
|
161
|
+
child[parent_column] = self.id
|
|
162
|
+
right_bound = self[right_col_name]
|
|
163
|
+
child[left_col_name] = right_bound
|
|
164
|
+
child[right_col_name] = right_bound + 1
|
|
165
|
+
self[right_col_name] += 2
|
|
166
|
+
self.class.base_class.transaction {
|
|
167
|
+
self.class.base_class.update_all( "#{left_col_name} = (#{left_col_name} + 2)", "#{scope_condition} AND #{left_col_name} >= #{right_bound}" )
|
|
168
|
+
self.class.base_class.update_all( "#{right_col_name} = (#{right_col_name} + 2)", "#{scope_condition} AND #{right_col_name} >= #{right_bound}" )
|
|
169
|
+
self.save
|
|
170
|
+
child.save
|
|
171
|
+
}
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
# Returns the number of nested children of this object.
|
|
177
|
+
def children_count
|
|
178
|
+
return (self[right_col_name] - self[left_col_name] - 1)/2
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
# Returns a set of itself and all of its nested children
|
|
182
|
+
def full_set
|
|
183
|
+
self.class.base_class.find(:all, :conditions => "#{scope_condition} AND (#{left_col_name} BETWEEN #{self[left_col_name]} and #{self[right_col_name]})" )
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
# Returns a set of all of its children and nested children
|
|
187
|
+
def all_children
|
|
188
|
+
self.class.base_class.find(:all, :conditions => "#{scope_condition} AND (#{left_col_name} > #{self[left_col_name]}) and (#{right_col_name} < #{self[right_col_name]})" )
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
# Returns a set of only this entry's immediate children
|
|
192
|
+
def direct_children
|
|
193
|
+
self.class.base_class.find(:all, :conditions => "#{scope_condition} and #{parent_column} = #{self.id}")
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
# Prunes a branch off of the tree, shifting all of the elements on the right
|
|
197
|
+
# back to the left so the counts still work.
|
|
198
|
+
def before_destroy
|
|
199
|
+
return if self[right_col_name].nil? || self[left_col_name].nil?
|
|
200
|
+
dif = self[right_col_name] - self[left_col_name] + 1
|
|
201
|
+
|
|
202
|
+
self.class.base_class.transaction {
|
|
203
|
+
self.class.base_class.delete_all( "#{scope_condition} and #{left_col_name} > #{self[left_col_name]} and #{right_col_name} < #{self[right_col_name]}" )
|
|
204
|
+
self.class.base_class.update_all( "#{left_col_name} = (#{left_col_name} - #{dif})", "#{scope_condition} AND #{left_col_name} >= #{self[right_col_name]}" )
|
|
205
|
+
self.class.base_class.update_all( "#{right_col_name} = (#{right_col_name} - #{dif} )", "#{scope_condition} AND #{right_col_name} >= #{self[right_col_name]}" )
|
|
206
|
+
}
|
|
207
|
+
end
|
|
208
|
+
end
|
|
209
|
+
end
|
|
210
|
+
end
|
|
211
|
+
end
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
module ActiveRecord
|
|
2
|
+
module Acts #:nodoc:
|
|
3
|
+
module Tree #:nodoc:
|
|
4
|
+
def self.included(base)
|
|
5
|
+
base.extend(ClassMethods)
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
# Specify this act if you want to model a tree structure by providing a parent association and a children
|
|
9
|
+
# association. This act requires that you have a foreign key column, which by default is called parent_id.
|
|
10
|
+
#
|
|
11
|
+
# class Category < ActiveRecord::Base
|
|
12
|
+
# acts_as_tree :order => "name"
|
|
13
|
+
# end
|
|
14
|
+
#
|
|
15
|
+
# Example:
|
|
16
|
+
# root
|
|
17
|
+
# \_ child1
|
|
18
|
+
# \_ subchild1
|
|
19
|
+
# \_ subchild2
|
|
20
|
+
#
|
|
21
|
+
# root = Category.create("name" => "root")
|
|
22
|
+
# child1 = root.children.create("name" => "child1")
|
|
23
|
+
# subchild1 = child1.children.create("name" => "subchild1")
|
|
24
|
+
#
|
|
25
|
+
# root.parent # => nil
|
|
26
|
+
# child1.parent # => root
|
|
27
|
+
# root.children # => [child1]
|
|
28
|
+
# root.children.first.children.first # => subchild1
|
|
29
|
+
#
|
|
30
|
+
# In addition to the parent and children associations, the following instance methods are added to the class
|
|
31
|
+
# after specifying the act:
|
|
32
|
+
# * siblings : Returns all the children of the parent, excluding the current node ([ subchild2 ] when called from subchild1)
|
|
33
|
+
# * self_and_siblings : Returns all the children of the parent, including the current node ([ subchild1, subchild2 ] when called from subchild1)
|
|
34
|
+
# * ancestors : Returns all the ancestors of the current node ([child1, root] when called from subchild2)
|
|
35
|
+
# * root : Returns the root of the current node (root when called from subchild2)
|
|
36
|
+
module ClassMethods
|
|
37
|
+
# Configuration options are:
|
|
38
|
+
#
|
|
39
|
+
# * <tt>foreign_key</tt> - specifies the column name to use for tracking of the tree (default: parent_id)
|
|
40
|
+
# * <tt>order</tt> - makes it possible to sort the children according to this SQL snippet.
|
|
41
|
+
# * <tt>counter_cache</tt> - keeps a count in a children_count column if set to true (default: false).
|
|
42
|
+
def acts_as_tree(options = {})
|
|
43
|
+
configuration = { :foreign_key => "parent_id", :order => nil, :counter_cache => nil }
|
|
44
|
+
configuration.update(options) if options.is_a?(Hash)
|
|
45
|
+
|
|
46
|
+
belongs_to :parent, :class_name => name, :foreign_key => configuration[:foreign_key], :counter_cache => configuration[:counter_cache]
|
|
47
|
+
has_many :children, :class_name => name, :foreign_key => configuration[:foreign_key], :order => configuration[:order], :dependent => :destroy
|
|
48
|
+
|
|
49
|
+
class_eval <<-EOV
|
|
50
|
+
include ActiveRecord::Acts::Tree::InstanceMethods
|
|
51
|
+
|
|
52
|
+
def self.roots
|
|
53
|
+
find(:all, :conditions => "#{configuration[:foreign_key]} IS NULL", :order => #{configuration[:order].nil? ? "nil" : %Q{"#{configuration[:order]}"}})
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def self.root
|
|
57
|
+
find(:first, :conditions => "#{configuration[:foreign_key]} IS NULL", :order => #{configuration[:order].nil? ? "nil" : %Q{"#{configuration[:order]}"}})
|
|
58
|
+
end
|
|
59
|
+
EOV
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
module InstanceMethods
|
|
64
|
+
# Returns list of ancestors, starting from parent until root.
|
|
65
|
+
#
|
|
66
|
+
# subchild1.ancestors # => [child1, root]
|
|
67
|
+
def ancestors
|
|
68
|
+
node, nodes = self, []
|
|
69
|
+
nodes << node = node.parent while node.parent
|
|
70
|
+
nodes
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def root
|
|
74
|
+
node = self
|
|
75
|
+
node = node.parent while node.parent
|
|
76
|
+
node
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def siblings
|
|
80
|
+
self_and_siblings - [self]
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def self_and_siblings
|
|
84
|
+
parent ? parent.children : self.class.roots
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
module ActiveRecord
|
|
2
|
+
module Aggregations # :nodoc:
|
|
3
|
+
def self.included(base)
|
|
4
|
+
base.extend(ClassMethods)
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def clear_aggregation_cache #:nodoc:
|
|
8
|
+
self.class.reflect_on_all_aggregations.to_a.each do |assoc|
|
|
9
|
+
instance_variable_set "@#{assoc.name}", nil
|
|
10
|
+
end unless self.new_record?
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# Active Record implements aggregation through a macro-like class method called +composed_of+ for representing attributes
|
|
14
|
+
# as value objects. It expresses relationships like "Account [is] composed of Money [among other things]" or "Person [is]
|
|
15
|
+
# composed of [an] address". Each call to the macro adds a description of how the value objects are created from the
|
|
16
|
+
# attributes of the entity object (when the entity is initialized either as a new object or from finding an existing object)
|
|
17
|
+
# and how it can be turned back into attributes (when the entity is saved to the database). Example:
|
|
18
|
+
#
|
|
19
|
+
# class Customer < ActiveRecord::Base
|
|
20
|
+
# composed_of :balance, :class_name => "Money", :mapping => %w(balance amount)
|
|
21
|
+
# composed_of :address, :mapping => [ %w(address_street street), %w(address_city city) ]
|
|
22
|
+
# end
|
|
23
|
+
#
|
|
24
|
+
# The customer class now has the following methods to manipulate the value objects:
|
|
25
|
+
# * <tt>Customer#balance, Customer#balance=(money)</tt>
|
|
26
|
+
# * <tt>Customer#address, Customer#address=(address)</tt>
|
|
27
|
+
#
|
|
28
|
+
# These methods will operate with value objects like the ones described below:
|
|
29
|
+
#
|
|
30
|
+
# class Money
|
|
31
|
+
# include Comparable
|
|
32
|
+
# attr_reader :amount, :currency
|
|
33
|
+
# EXCHANGE_RATES = { "USD_TO_DKK" => 6 }
|
|
34
|
+
#
|
|
35
|
+
# def initialize(amount, currency = "USD")
|
|
36
|
+
# @amount, @currency = amount, currency
|
|
37
|
+
# end
|
|
38
|
+
#
|
|
39
|
+
# def exchange_to(other_currency)
|
|
40
|
+
# exchanged_amount = (amount * EXCHANGE_RATES["#{currency}_TO_#{other_currency}"]).floor
|
|
41
|
+
# Money.new(exchanged_amount, other_currency)
|
|
42
|
+
# end
|
|
43
|
+
#
|
|
44
|
+
# def ==(other_money)
|
|
45
|
+
# amount == other_money.amount && currency == other_money.currency
|
|
46
|
+
# end
|
|
47
|
+
#
|
|
48
|
+
# def <=>(other_money)
|
|
49
|
+
# if currency == other_money.currency
|
|
50
|
+
# amount <=> amount
|
|
51
|
+
# else
|
|
52
|
+
# amount <=> other_money.exchange_to(currency).amount
|
|
53
|
+
# end
|
|
54
|
+
# end
|
|
55
|
+
# end
|
|
56
|
+
#
|
|
57
|
+
# class Address
|
|
58
|
+
# attr_reader :street, :city
|
|
59
|
+
# def initialize(street, city)
|
|
60
|
+
# @street, @city = street, city
|
|
61
|
+
# end
|
|
62
|
+
#
|
|
63
|
+
# def close_to?(other_address)
|
|
64
|
+
# city == other_address.city
|
|
65
|
+
# end
|
|
66
|
+
#
|
|
67
|
+
# def ==(other_address)
|
|
68
|
+
# city == other_address.city && street == other_address.street
|
|
69
|
+
# end
|
|
70
|
+
# end
|
|
71
|
+
#
|
|
72
|
+
# Now it's possible to access attributes from the database through the value objects instead. If you choose to name the
|
|
73
|
+
# composition the same as the attributes name, it will be the only way to access that attribute. That's the case with our
|
|
74
|
+
# +balance+ attribute. You interact with the value objects just like you would any other attribute, though:
|
|
75
|
+
#
|
|
76
|
+
# customer.balance = Money.new(20) # sets the Money value object and the attribute
|
|
77
|
+
# customer.balance # => Money value object
|
|
78
|
+
# customer.balance.exchanged_to("DKK") # => Money.new(120, "DKK")
|
|
79
|
+
# customer.balance > Money.new(10) # => true
|
|
80
|
+
# customer.balance == Money.new(20) # => true
|
|
81
|
+
# customer.balance < Money.new(5) # => false
|
|
82
|
+
#
|
|
83
|
+
# Value objects can also be composed of multiple attributes, such as the case of Address. The order of the mappings will
|
|
84
|
+
# determine the order of the parameters. Example:
|
|
85
|
+
#
|
|
86
|
+
# customer.address_street = "Hyancintvej"
|
|
87
|
+
# customer.address_city = "Copenhagen"
|
|
88
|
+
# customer.address # => Address.new("Hyancintvej", "Copenhagen")
|
|
89
|
+
# customer.address = Address.new("May Street", "Chicago")
|
|
90
|
+
# customer.address_street # => "May Street"
|
|
91
|
+
# customer.address_city # => "Chicago"
|
|
92
|
+
#
|
|
93
|
+
# == Writing value objects
|
|
94
|
+
#
|
|
95
|
+
# Value objects are immutable and interchangeable objects that represent a given value, such as a Money object representing
|
|
96
|
+
# $5. Two Money objects both representing $5 should be equal (through methods such as == and <=> from Comparable if ranking
|
|
97
|
+
# makes sense). This is unlike entity objects where equality is determined by identity. An entity class such as Customer can
|
|
98
|
+
# easily have two different objects that both have an address on Hyancintvej. Entity identity is determined by object or
|
|
99
|
+
# relational unique identifiers (such as primary keys). Normal ActiveRecord::Base classes are entity objects.
|
|
100
|
+
#
|
|
101
|
+
# It's also important to treat the value objects as immutable. Don't allow the Money object to have its amount changed after
|
|
102
|
+
# creation. Create a new money object with the new value instead. This is exemplified by the Money#exchanged_to method that
|
|
103
|
+
# returns a new value object instead of changing its own values. Active Record won't persist value objects that have been
|
|
104
|
+
# changed through other means than the writer method.
|
|
105
|
+
#
|
|
106
|
+
# The immutable requirement is enforced by Active Record by freezing any object assigned as a value object. Attempting to
|
|
107
|
+
# change it afterwards will result in a TypeError.
|
|
108
|
+
#
|
|
109
|
+
# Read more about value objects on http://c2.com/cgi/wiki?ValueObject and on the dangers of not keeping value objects
|
|
110
|
+
# immutable on http://c2.com/cgi/wiki?ValueObjectsShouldBeImmutable
|
|
111
|
+
module ClassMethods
|
|
112
|
+
# Adds reader and writer methods for manipulating a value object:
|
|
113
|
+
# <tt>composed_of :address</tt> adds <tt>address</tt> and <tt>address=(new_address)</tt> methods.
|
|
114
|
+
#
|
|
115
|
+
# Options are:
|
|
116
|
+
# * <tt>:class_name</tt> - specify the class name of the association. Use it only if that name can't be inferred
|
|
117
|
+
# from the part id. So <tt>composed_of :address</tt> will by default be linked to the +Address+ class, but
|
|
118
|
+
# if the real class name is +CompanyAddress+, you'll have to specify it with this option.
|
|
119
|
+
# * <tt>:mapping</tt> - specifies a number of mapping arrays (attribute, parameter) that bind an attribute name
|
|
120
|
+
# to a constructor parameter on the value class.
|
|
121
|
+
# * <tt>:allow_nil</tt> - specifies that the aggregate object will not be instantiated when all mapped
|
|
122
|
+
# attributes are nil. Setting the aggregate class to nil has the effect of writing nil to all mapped attributes.
|
|
123
|
+
# This defaults to false.
|
|
124
|
+
#
|
|
125
|
+
# Option examples:
|
|
126
|
+
# composed_of :temperature, :mapping => %w(reading celsius)
|
|
127
|
+
# composed_of :balance, :class_name => "Money", :mapping => %w(balance amount)
|
|
128
|
+
# composed_of :address, :mapping => [ %w(address_street street), %w(address_city city) ]
|
|
129
|
+
# composed_of :gps_location
|
|
130
|
+
# composed_of :gps_location, :allow_nil => true
|
|
131
|
+
#
|
|
132
|
+
def composed_of(part_id, options = {})
|
|
133
|
+
options.assert_valid_keys(:class_name, :mapping, :allow_nil)
|
|
134
|
+
|
|
135
|
+
name = part_id.id2name
|
|
136
|
+
class_name = options[:class_name] || name.camelize
|
|
137
|
+
mapping = options[:mapping] || [ name, name ]
|
|
138
|
+
allow_nil = options[:allow_nil] || false
|
|
139
|
+
|
|
140
|
+
reader_method(name, class_name, mapping, allow_nil)
|
|
141
|
+
writer_method(name, class_name, mapping, allow_nil)
|
|
142
|
+
|
|
143
|
+
create_reflection(:composed_of, part_id, options, self)
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
private
|
|
147
|
+
def reader_method(name, class_name, mapping, allow_nil)
|
|
148
|
+
mapping = (Array === mapping.first ? mapping : [ mapping ])
|
|
149
|
+
|
|
150
|
+
allow_nil_condition = if allow_nil
|
|
151
|
+
mapping.collect { |pair| "!read_attribute(\"#{pair.first}\").nil?"}.join(" && ")
|
|
152
|
+
else
|
|
153
|
+
"true"
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
module_eval <<-end_eval
|
|
157
|
+
def #{name}(force_reload = false)
|
|
158
|
+
if (@#{name}.nil? || force_reload) && #{allow_nil_condition}
|
|
159
|
+
@#{name} = #{class_name}.new(#{mapping.collect { |pair| "read_attribute(\"#{pair.first}\")"}.join(", ")})
|
|
160
|
+
end
|
|
161
|
+
return @#{name}
|
|
162
|
+
end
|
|
163
|
+
end_eval
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def writer_method(name, class_name, mapping, allow_nil)
|
|
167
|
+
mapping = (Array === mapping.first ? mapping : [ mapping ])
|
|
168
|
+
|
|
169
|
+
if allow_nil
|
|
170
|
+
module_eval <<-end_eval
|
|
171
|
+
def #{name}=(part)
|
|
172
|
+
if part.nil?
|
|
173
|
+
#{mapping.collect { |pair| "@attributes[\"#{pair.first}\"] = nil" }.join("\n")}
|
|
174
|
+
else
|
|
175
|
+
@#{name} = part.freeze
|
|
176
|
+
#{mapping.collect { |pair| "@attributes[\"#{pair.first}\"] = part.#{pair.last}" }.join("\n")}
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
end_eval
|
|
180
|
+
else
|
|
181
|
+
module_eval <<-end_eval
|
|
182
|
+
def #{name}=(part)
|
|
183
|
+
@#{name} = part.freeze
|
|
184
|
+
#{mapping.collect{ |pair| "@attributes[\"#{pair.first}\"] = part.#{pair.last}" }.join("\n")}
|
|
185
|
+
end
|
|
186
|
+
end_eval
|
|
187
|
+
end
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
end
|