activerecord 3.0.0 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activerecord might be problematic. Click here for more details.

Files changed (181) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +2102 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +35 -44
  5. data/examples/performance.rb +110 -100
  6. data/lib/active_record/aggregations.rb +59 -75
  7. data/lib/active_record/associations/alias_tracker.rb +76 -0
  8. data/lib/active_record/associations/association.rb +248 -0
  9. data/lib/active_record/associations/association_scope.rb +135 -0
  10. data/lib/active_record/associations/belongs_to_association.rb +60 -59
  11. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +16 -59
  12. data/lib/active_record/associations/builder/association.rb +108 -0
  13. data/lib/active_record/associations/builder/belongs_to.rb +98 -0
  14. data/lib/active_record/associations/builder/collection_association.rb +89 -0
  15. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +39 -0
  16. data/lib/active_record/associations/builder/has_many.rb +15 -0
  17. data/lib/active_record/associations/builder/has_one.rb +25 -0
  18. data/lib/active_record/associations/builder/singular_association.rb +32 -0
  19. data/lib/active_record/associations/collection_association.rb +608 -0
  20. data/lib/active_record/associations/collection_proxy.rb +986 -0
  21. data/lib/active_record/associations/has_and_belongs_to_many_association.rb +40 -112
  22. data/lib/active_record/associations/has_many_association.rb +83 -76
  23. data/lib/active_record/associations/has_many_through_association.rb +147 -66
  24. data/lib/active_record/associations/has_one_association.rb +67 -108
  25. data/lib/active_record/associations/has_one_through_association.rb +21 -25
  26. data/lib/active_record/associations/join_dependency/join_association.rb +174 -0
  27. data/lib/active_record/associations/join_dependency/join_base.rb +24 -0
  28. data/lib/active_record/associations/join_dependency/join_part.rb +78 -0
  29. data/lib/active_record/associations/join_dependency.rb +235 -0
  30. data/lib/active_record/associations/join_helper.rb +45 -0
  31. data/lib/active_record/associations/preloader/association.rb +121 -0
  32. data/lib/active_record/associations/preloader/belongs_to.rb +17 -0
  33. data/lib/active_record/associations/preloader/collection_association.rb +24 -0
  34. data/lib/active_record/associations/preloader/has_and_belongs_to_many.rb +60 -0
  35. data/lib/active_record/associations/preloader/has_many.rb +17 -0
  36. data/lib/active_record/associations/preloader/has_many_through.rb +19 -0
  37. data/lib/active_record/associations/preloader/has_one.rb +23 -0
  38. data/lib/active_record/associations/preloader/has_one_through.rb +9 -0
  39. data/lib/active_record/associations/preloader/singular_association.rb +21 -0
  40. data/lib/active_record/associations/preloader/through_association.rb +63 -0
  41. data/lib/active_record/associations/preloader.rb +178 -0
  42. data/lib/active_record/associations/singular_association.rb +64 -0
  43. data/lib/active_record/associations/through_association.rb +87 -0
  44. data/lib/active_record/associations.rb +512 -1224
  45. data/lib/active_record/attribute_assignment.rb +201 -0
  46. data/lib/active_record/attribute_methods/before_type_cast.rb +49 -12
  47. data/lib/active_record/attribute_methods/dirty.rb +51 -28
  48. data/lib/active_record/attribute_methods/primary_key.rb +94 -22
  49. data/lib/active_record/attribute_methods/query.rb +5 -4
  50. data/lib/active_record/attribute_methods/read.rb +63 -72
  51. data/lib/active_record/attribute_methods/serialization.rb +162 -0
  52. data/lib/active_record/attribute_methods/time_zone_conversion.rb +39 -41
  53. data/lib/active_record/attribute_methods/write.rb +39 -13
  54. data/lib/active_record/attribute_methods.rb +362 -29
  55. data/lib/active_record/autosave_association.rb +132 -75
  56. data/lib/active_record/base.rb +83 -1627
  57. data/lib/active_record/callbacks.rb +69 -47
  58. data/lib/active_record/coders/yaml_column.rb +38 -0
  59. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +411 -138
  60. data/lib/active_record/connection_adapters/abstract/database_limits.rb +21 -11
  61. data/lib/active_record/connection_adapters/abstract/database_statements.rb +234 -173
  62. data/lib/active_record/connection_adapters/abstract/query_cache.rb +36 -22
  63. data/lib/active_record/connection_adapters/abstract/quoting.rb +82 -25
  64. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +176 -414
  65. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +70 -0
  66. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +562 -232
  67. data/lib/active_record/connection_adapters/abstract/transaction.rb +203 -0
  68. data/lib/active_record/connection_adapters/abstract_adapter.rb +281 -53
  69. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +782 -0
  70. data/lib/active_record/connection_adapters/column.rb +318 -0
  71. data/lib/active_record/connection_adapters/connection_specification.rb +96 -0
  72. data/lib/active_record/connection_adapters/mysql2_adapter.rb +273 -0
  73. data/lib/active_record/connection_adapters/mysql_adapter.rb +365 -450
  74. data/lib/active_record/connection_adapters/postgresql/array_parser.rb +97 -0
  75. data/lib/active_record/connection_adapters/postgresql/cast.rb +152 -0
  76. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +242 -0
  77. data/lib/active_record/connection_adapters/postgresql/oid.rb +366 -0
  78. data/lib/active_record/connection_adapters/postgresql/quoting.rb +171 -0
  79. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +30 -0
  80. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +489 -0
  81. data/lib/active_record/connection_adapters/postgresql_adapter.rb +672 -752
  82. data/lib/active_record/connection_adapters/schema_cache.rb +129 -0
  83. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +588 -17
  84. data/lib/active_record/connection_adapters/statement_pool.rb +40 -0
  85. data/lib/active_record/connection_handling.rb +98 -0
  86. data/lib/active_record/core.rb +463 -0
  87. data/lib/active_record/counter_cache.rb +108 -101
  88. data/lib/active_record/dynamic_matchers.rb +131 -0
  89. data/lib/active_record/errors.rb +54 -13
  90. data/lib/active_record/explain.rb +38 -0
  91. data/lib/active_record/explain_registry.rb +30 -0
  92. data/lib/active_record/explain_subscriber.rb +29 -0
  93. data/lib/active_record/fixture_set/file.rb +55 -0
  94. data/lib/active_record/fixtures.rb +703 -785
  95. data/lib/active_record/inheritance.rb +200 -0
  96. data/lib/active_record/integration.rb +60 -0
  97. data/lib/active_record/locale/en.yml +8 -1
  98. data/lib/active_record/locking/optimistic.rb +69 -60
  99. data/lib/active_record/locking/pessimistic.rb +34 -12
  100. data/lib/active_record/log_subscriber.rb +40 -6
  101. data/lib/active_record/migration/command_recorder.rb +164 -0
  102. data/lib/active_record/migration/join_table.rb +15 -0
  103. data/lib/active_record/migration.rb +614 -216
  104. data/lib/active_record/model_schema.rb +345 -0
  105. data/lib/active_record/nested_attributes.rb +248 -119
  106. data/lib/active_record/null_relation.rb +65 -0
  107. data/lib/active_record/persistence.rb +275 -57
  108. data/lib/active_record/query_cache.rb +29 -9
  109. data/lib/active_record/querying.rb +62 -0
  110. data/lib/active_record/railtie.rb +135 -21
  111. data/lib/active_record/railties/console_sandbox.rb +5 -0
  112. data/lib/active_record/railties/controller_runtime.rb +17 -5
  113. data/lib/active_record/railties/databases.rake +249 -359
  114. data/lib/active_record/railties/jdbcmysql_error.rb +16 -0
  115. data/lib/active_record/readonly_attributes.rb +30 -0
  116. data/lib/active_record/reflection.rb +283 -103
  117. data/lib/active_record/relation/batches.rb +38 -34
  118. data/lib/active_record/relation/calculations.rb +252 -139
  119. data/lib/active_record/relation/delegation.rb +125 -0
  120. data/lib/active_record/relation/finder_methods.rb +182 -188
  121. data/lib/active_record/relation/merger.rb +161 -0
  122. data/lib/active_record/relation/predicate_builder.rb +86 -21
  123. data/lib/active_record/relation/query_methods.rb +917 -134
  124. data/lib/active_record/relation/spawn_methods.rb +53 -92
  125. data/lib/active_record/relation.rb +405 -143
  126. data/lib/active_record/result.rb +67 -0
  127. data/lib/active_record/runtime_registry.rb +17 -0
  128. data/lib/active_record/sanitization.rb +168 -0
  129. data/lib/active_record/schema.rb +20 -14
  130. data/lib/active_record/schema_dumper.rb +55 -46
  131. data/lib/active_record/schema_migration.rb +39 -0
  132. data/lib/active_record/scoping/default.rb +146 -0
  133. data/lib/active_record/scoping/named.rb +175 -0
  134. data/lib/active_record/scoping.rb +82 -0
  135. data/lib/active_record/serialization.rb +8 -46
  136. data/lib/active_record/serializers/xml_serializer.rb +21 -68
  137. data/lib/active_record/statement_cache.rb +26 -0
  138. data/lib/active_record/store.rb +156 -0
  139. data/lib/active_record/tasks/database_tasks.rb +203 -0
  140. data/lib/active_record/tasks/firebird_database_tasks.rb +56 -0
  141. data/lib/active_record/tasks/mysql_database_tasks.rb +143 -0
  142. data/lib/active_record/tasks/oracle_database_tasks.rb +45 -0
  143. data/lib/active_record/tasks/postgresql_database_tasks.rb +90 -0
  144. data/lib/active_record/tasks/sqlite_database_tasks.rb +51 -0
  145. data/lib/active_record/tasks/sqlserver_database_tasks.rb +48 -0
  146. data/lib/active_record/test_case.rb +57 -28
  147. data/lib/active_record/timestamp.rb +49 -18
  148. data/lib/active_record/transactions.rb +106 -63
  149. data/lib/active_record/translation.rb +22 -0
  150. data/lib/active_record/validations/associated.rb +25 -24
  151. data/lib/active_record/validations/presence.rb +65 -0
  152. data/lib/active_record/validations/uniqueness.rb +123 -83
  153. data/lib/active_record/validations.rb +29 -29
  154. data/lib/active_record/version.rb +7 -5
  155. data/lib/active_record.rb +83 -34
  156. data/lib/rails/generators/active_record/migration/migration_generator.rb +46 -9
  157. data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb +19 -0
  158. data/lib/rails/generators/active_record/migration/templates/migration.rb +30 -8
  159. data/lib/rails/generators/active_record/model/model_generator.rb +15 -5
  160. data/lib/rails/generators/active_record/model/templates/model.rb +7 -2
  161. data/lib/rails/generators/active_record/model/templates/module.rb +3 -1
  162. data/lib/rails/generators/active_record.rb +4 -8
  163. metadata +163 -121
  164. data/CHANGELOG +0 -6023
  165. data/examples/associations.png +0 -0
  166. data/lib/active_record/association_preload.rb +0 -403
  167. data/lib/active_record/associations/association_collection.rb +0 -562
  168. data/lib/active_record/associations/association_proxy.rb +0 -295
  169. data/lib/active_record/associations/through_association_scope.rb +0 -154
  170. data/lib/active_record/connection_adapters/abstract/connection_specification.rb +0 -113
  171. data/lib/active_record/connection_adapters/sqlite_adapter.rb +0 -401
  172. data/lib/active_record/dynamic_finder_match.rb +0 -53
  173. data/lib/active_record/dynamic_scope_match.rb +0 -32
  174. data/lib/active_record/named_scope.rb +0 -138
  175. data/lib/active_record/observer.rb +0 -140
  176. data/lib/active_record/session_store.rb +0 -340
  177. data/lib/rails/generators/active_record/model/templates/migration.rb +0 -16
  178. data/lib/rails/generators/active_record/observer/observer_generator.rb +0 -15
  179. data/lib/rails/generators/active_record/observer/templates/observer.rb +0 -2
  180. data/lib/rails/generators/active_record/session_migration/session_migration_generator.rb +0 -24
  181. data/lib/rails/generators/active_record/session_migration/templates/migration.rb +0 -16
@@ -1,140 +0,0 @@
1
- require 'active_support/core_ext/class/attribute'
2
-
3
- module ActiveRecord
4
- # = Active Record Observer
5
- #
6
- # Observer classes respond to life cycle callbacks to implement trigger-like
7
- # behavior outside the original class. This is a great way to reduce the
8
- # clutter that normally comes when the model class is burdened with
9
- # functionality that doesn't pertain to the core responsibility of the
10
- # class. Example:
11
- #
12
- # class CommentObserver < ActiveRecord::Observer
13
- # def after_save(comment)
14
- # Notifications.deliver_comment("admin@do.com", "New comment was posted", comment)
15
- # end
16
- # end
17
- #
18
- # This Observer sends an email when a Comment#save is finished.
19
- #
20
- # class ContactObserver < ActiveRecord::Observer
21
- # def after_create(contact)
22
- # contact.logger.info('New contact added!')
23
- # end
24
- #
25
- # def after_destroy(contact)
26
- # contact.logger.warn("Contact with an id of #{contact.id} was destroyed!")
27
- # end
28
- # end
29
- #
30
- # This Observer uses logger to log when specific callbacks are triggered.
31
- #
32
- # == Observing a class that can't be inferred
33
- #
34
- # Observers will by default be mapped to the class with which they share a name. So CommentObserver will
35
- # be tied to observing Comment, ProductManagerObserver to ProductManager, and so on. If you want to name your observer
36
- # differently than the class you're interested in observing, you can use the Observer.observe class method which takes
37
- # either the concrete class (Product) or a symbol for that class (:product):
38
- #
39
- # class AuditObserver < ActiveRecord::Observer
40
- # observe :account
41
- #
42
- # def after_update(account)
43
- # AuditTrail.new(account, "UPDATED")
44
- # end
45
- # end
46
- #
47
- # If the audit observer needs to watch more than one kind of object, this can be specified with multiple arguments:
48
- #
49
- # class AuditObserver < ActiveRecord::Observer
50
- # observe :account, :balance
51
- #
52
- # def after_update(record)
53
- # AuditTrail.new(record, "UPDATED")
54
- # end
55
- # end
56
- #
57
- # The AuditObserver will now act on both updates to Account and Balance by treating them both as records.
58
- #
59
- # == Available callback methods
60
- #
61
- # The observer can implement callback methods for each of the methods described in the Callbacks module.
62
- #
63
- # == Storing Observers in Rails
64
- #
65
- # If you're using Active Record within Rails, observer classes are usually stored in app/models with the
66
- # naming convention of app/models/audit_observer.rb.
67
- #
68
- # == Configuration
69
- #
70
- # In order to activate an observer, list it in the <tt>config.active_record.observers</tt> configuration
71
- # setting in your <tt>config/application.rb</tt> file.
72
- #
73
- # config.active_record.observers = :comment_observer, :signup_observer
74
- #
75
- # Observers will not be invoked unless you define these in your application configuration.
76
- #
77
- # == Loading
78
- #
79
- # Observers register themselves in the model class they observe, since it is the class that
80
- # notifies them of events when they occur. As a side-effect, when an observer is loaded its
81
- # corresponding model class is loaded.
82
- #
83
- # Up to (and including) Rails 2.0.2 observers were instantiated between plugins and
84
- # application initializers. Now observers are loaded after application initializers,
85
- # so observed models can make use of extensions.
86
- #
87
- # If by any chance you are using observed models in the initialization you can still
88
- # load their observers by calling <tt>ModelObserver.instance</tt> before. Observers are
89
- # singletons and that call instantiates and registers them.
90
- #
91
- class Observer < ActiveModel::Observer
92
- class_attribute :observed_methods
93
- self.observed_methods = [].freeze
94
-
95
- def initialize
96
- super
97
- observed_descendants.each { |klass| add_observer!(klass) }
98
- end
99
-
100
- def self.method_added(method)
101
- method = method.to_sym
102
-
103
- if ActiveRecord::Callbacks::CALLBACKS.include?(method)
104
- self.observed_methods += [method]
105
- self.observed_methods.freeze
106
- end
107
- end
108
-
109
- protected
110
-
111
- def observed_descendants
112
- observed_classes.sum([]) { |klass| klass.descendants }
113
- end
114
-
115
- def observe_callbacks?
116
- self.class.observed_methods.any?
117
- end
118
-
119
- def add_observer!(klass)
120
- super
121
- define_callbacks klass if observe_callbacks?
122
- end
123
-
124
- def define_callbacks(klass)
125
- existing_methods = klass.instance_methods.map { |m| m.to_sym }
126
- observer = self
127
- observer_name = observer.class.name.underscore.gsub('/', '__')
128
-
129
- self.class.observed_methods.each do |method|
130
- callback = :"_notify_#{observer_name}_for_#{method}"
131
- unless existing_methods.include? callback
132
- klass.send(:define_method, callback) do # def _notify_user_observer_for_before_save
133
- observer.update(method, self) # observer.update(:before_save, self)
134
- end # end
135
- klass.send(method, callback) # before_save :_notify_user_observer_for_before_save
136
- end
137
- end
138
- end
139
- end
140
- end
@@ -1,340 +0,0 @@
1
- module ActiveRecord
2
- # = Active Record Session Store
3
- #
4
- # A session store backed by an Active Record class. A default class is
5
- # provided, but any object duck-typing to an Active Record Session class
6
- # with text +session_id+ and +data+ attributes is sufficient.
7
- #
8
- # The default assumes a +sessions+ tables with columns:
9
- # +id+ (numeric primary key),
10
- # +session_id+ (text, or longtext if your session data exceeds 65K), and
11
- # +data+ (text or longtext; careful if your session data exceeds 65KB).
12
- #
13
- # The +session_id+ column should always be indexed for speedy lookups.
14
- # Session data is marshaled to the +data+ column in Base64 format.
15
- # If the data you write is larger than the column's size limit,
16
- # ActionController::SessionOverflowError will be raised.
17
- #
18
- # You may configure the table name, primary key, and data column.
19
- # For example, at the end of <tt>config/application.rb</tt>:
20
- #
21
- # ActiveRecord::SessionStore::Session.table_name = 'legacy_session_table'
22
- # ActiveRecord::SessionStore::Session.primary_key = 'session_id'
23
- # ActiveRecord::SessionStore::Session.data_column_name = 'legacy_session_data'
24
- #
25
- # Note that setting the primary key to the +session_id+ frees you from
26
- # having a separate +id+ column if you don't want it. However, you must
27
- # set <tt>session.model.id = session.session_id</tt> by hand! A before filter
28
- # on ApplicationController is a good place.
29
- #
30
- # Since the default class is a simple Active Record, you get timestamps
31
- # for free if you add +created_at+ and +updated_at+ datetime columns to
32
- # the +sessions+ table, making periodic session expiration a snap.
33
- #
34
- # You may provide your own session class implementation, whether a
35
- # feature-packed Active Record or a bare-metal high-performance SQL
36
- # store, by setting
37
- #
38
- # ActiveRecord::SessionStore.session_class = MySessionClass
39
- #
40
- # You must implement these methods:
41
- #
42
- # self.find_by_session_id(session_id)
43
- # initialize(hash_of_session_id_and_data)
44
- # attr_reader :session_id
45
- # attr_accessor :data
46
- # save
47
- # destroy
48
- #
49
- # The example SqlBypass class is a generic SQL session store. You may
50
- # use it as a basis for high-performance database-specific stores.
51
- class SessionStore < ActionDispatch::Session::AbstractStore
52
- module ClassMethods # :nodoc:
53
- def marshal(data)
54
- ActiveSupport::Base64.encode64(Marshal.dump(data)) if data
55
- end
56
-
57
- def unmarshal(data)
58
- Marshal.load(ActiveSupport::Base64.decode64(data)) if data
59
- end
60
-
61
- def drop_table!
62
- connection.drop_table table_name
63
- end
64
-
65
- def create_table!
66
- connection.create_table(table_name) do |t|
67
- t.string session_id_column, :limit => 255
68
- t.text data_column_name
69
- end
70
- connection.add_index table_name, session_id_column, :unique => true
71
- end
72
- end
73
-
74
- # The default Active Record class.
75
- class Session < ActiveRecord::Base
76
- extend ClassMethods
77
-
78
- ##
79
- # :singleton-method:
80
- # Customizable data column name. Defaults to 'data'.
81
- cattr_accessor :data_column_name
82
- self.data_column_name = 'data'
83
-
84
- before_save :marshal_data!
85
- before_save :raise_on_session_data_overflow!
86
-
87
- class << self
88
- def data_column_size_limit
89
- @data_column_size_limit ||= columns_hash[data_column_name].limit
90
- end
91
-
92
- # Hook to set up sessid compatibility.
93
- def find_by_session_id(session_id)
94
- setup_sessid_compatibility!
95
- find_by_session_id(session_id)
96
- end
97
-
98
- private
99
- def session_id_column
100
- 'session_id'
101
- end
102
-
103
- # Compatibility with tables using sessid instead of session_id.
104
- def setup_sessid_compatibility!
105
- # Reset column info since it may be stale.
106
- reset_column_information
107
- if columns_hash['sessid']
108
- def self.find_by_session_id(*args)
109
- find_by_sessid(*args)
110
- end
111
-
112
- define_method(:session_id) { sessid }
113
- define_method(:session_id=) { |session_id| self.sessid = session_id }
114
- else
115
- class << self; remove_method :find_by_session_id; end
116
-
117
- def self.find_by_session_id(session_id)
118
- find :first, :conditions => {:session_id=>session_id}
119
- end
120
- end
121
- end
122
- end
123
-
124
- def initialize(attributes = nil)
125
- @data = nil
126
- super
127
- end
128
-
129
- # Lazy-unmarshal session state.
130
- def data
131
- @data ||= self.class.unmarshal(read_attribute(@@data_column_name)) || {}
132
- end
133
-
134
- attr_writer :data
135
-
136
- # Has the session been loaded yet?
137
- def loaded?
138
- @data
139
- end
140
-
141
- private
142
- def marshal_data!
143
- return false unless loaded?
144
- write_attribute(@@data_column_name, self.class.marshal(data))
145
- end
146
-
147
- # Ensures that the data about to be stored in the database is not
148
- # larger than the data storage column. Raises
149
- # ActionController::SessionOverflowError.
150
- def raise_on_session_data_overflow!
151
- return false unless loaded?
152
- limit = self.class.data_column_size_limit
153
- if limit and read_attribute(@@data_column_name).size > limit
154
- raise ActionController::SessionOverflowError
155
- end
156
- end
157
- end
158
-
159
- # A barebones session store which duck-types with the default session
160
- # store but bypasses Active Record and issues SQL directly. This is
161
- # an example session model class meant as a basis for your own classes.
162
- #
163
- # The database connection, table name, and session id and data columns
164
- # are configurable class attributes. Marshaling and unmarshaling
165
- # are implemented as class methods that you may override. By default,
166
- # marshaling data is
167
- #
168
- # ActiveSupport::Base64.encode64(Marshal.dump(data))
169
- #
170
- # and unmarshaling data is
171
- #
172
- # Marshal.load(ActiveSupport::Base64.decode64(data))
173
- #
174
- # This marshaling behavior is intended to store the widest range of
175
- # binary session data in a +text+ column. For higher performance,
176
- # store in a +blob+ column instead and forgo the Base64 encoding.
177
- class SqlBypass
178
- extend ClassMethods
179
-
180
- ##
181
- # :singleton-method:
182
- # Use the ActiveRecord::Base.connection by default.
183
- cattr_accessor :connection
184
-
185
- ##
186
- # :singleton-method:
187
- # The table name defaults to 'sessions'.
188
- cattr_accessor :table_name
189
- @@table_name = 'sessions'
190
-
191
- ##
192
- # :singleton-method:
193
- # The session id field defaults to 'session_id'.
194
- cattr_accessor :session_id_column
195
- @@session_id_column = 'session_id'
196
-
197
- ##
198
- # :singleton-method:
199
- # The data field defaults to 'data'.
200
- cattr_accessor :data_column
201
- @@data_column = 'data'
202
-
203
- class << self
204
- alias :data_column_name :data_column
205
-
206
- remove_method :connection
207
- def connection
208
- @@connection ||= ActiveRecord::Base.connection
209
- end
210
-
211
- # Look up a session by id and unmarshal its data if found.
212
- def find_by_session_id(session_id)
213
- if record = connection.select_one("SELECT * FROM #{@@table_name} WHERE #{@@session_id_column}=#{connection.quote(session_id)}")
214
- new(:session_id => session_id, :marshaled_data => record['data'])
215
- end
216
- end
217
- end
218
-
219
- attr_reader :session_id, :new_record
220
- alias :new_record? :new_record
221
-
222
- attr_writer :data
223
-
224
- # Look for normal and marshaled data, self.find_by_session_id's way of
225
- # telling us to postpone unmarshaling until the data is requested.
226
- # We need to handle a normal data attribute in case of a new record.
227
- def initialize(attributes)
228
- @session_id = attributes[:session_id]
229
- @data = attributes[:data]
230
- @marshaled_data = attributes[:marshaled_data]
231
- @new_record = @marshaled_data.nil?
232
- end
233
-
234
- # Lazy-unmarshal session state.
235
- def data
236
- unless @data
237
- if @marshaled_data
238
- @data, @marshaled_data = self.class.unmarshal(@marshaled_data) || {}, nil
239
- else
240
- @data = {}
241
- end
242
- end
243
- @data
244
- end
245
-
246
- def loaded?
247
- @data
248
- end
249
-
250
- def save
251
- return false unless loaded?
252
- marshaled_data = self.class.marshal(data)
253
- connect = connection
254
-
255
- if @new_record
256
- @new_record = false
257
- connect.update <<-end_sql, 'Create session'
258
- INSERT INTO #{table_name} (
259
- #{connect.quote_column_name(session_id_column)},
260
- #{connect.quote_column_name(data_column)} )
261
- VALUES (
262
- #{connect.quote(session_id)},
263
- #{connect.quote(marshaled_data)} )
264
- end_sql
265
- else
266
- connect.update <<-end_sql, 'Update session'
267
- UPDATE #{table_name}
268
- SET #{connect.quote_column_name(data_column)}=#{connect.quote(marshaled_data)}
269
- WHERE #{connect.quote_column_name(session_id_column)}=#{connect.quote(session_id)}
270
- end_sql
271
- end
272
- end
273
-
274
- def destroy
275
- return if @new_record
276
-
277
- connect = connection
278
- connect.delete <<-end_sql, 'Destroy session'
279
- DELETE FROM #{table_name}
280
- WHERE #{connect.quote_column_name(session_id_column)}=#{connect.quote(session_id)}
281
- end_sql
282
- end
283
- end
284
-
285
- # The class used for session storage. Defaults to
286
- # ActiveRecord::SessionStore::Session
287
- cattr_accessor :session_class
288
- self.session_class = Session
289
-
290
- SESSION_RECORD_KEY = 'rack.session.record'
291
-
292
- private
293
- def get_session(env, sid)
294
- Base.silence do
295
- sid ||= generate_sid
296
- session = find_session(sid)
297
- env[SESSION_RECORD_KEY] = session
298
- [sid, session.data]
299
- end
300
- end
301
-
302
- def set_session(env, sid, session_data)
303
- Base.silence do
304
- record = get_session_model(env, sid)
305
- record.data = session_data
306
- return false unless record.save
307
-
308
- session_data = record.data
309
- if session_data && session_data.respond_to?(:each_value)
310
- session_data.each_value do |obj|
311
- obj.clear_association_cache if obj.respond_to?(:clear_association_cache)
312
- end
313
- end
314
- end
315
-
316
- sid
317
- end
318
-
319
- def destroy(env)
320
- if sid = current_session_id(env)
321
- Base.silence do
322
- get_session_model(env, sid).destroy
323
- end
324
- end
325
- end
326
-
327
- def get_session_model(env, sid)
328
- if env[ENV_SESSION_OPTIONS_KEY][:id].nil?
329
- env[SESSION_RECORD_KEY] = find_session(sid)
330
- else
331
- env[SESSION_RECORD_KEY] ||= find_session(sid)
332
- end
333
- end
334
-
335
- def find_session(id)
336
- @@session_class.find_by_session_id(id) ||
337
- @@session_class.new(:session_id => id, :data => {})
338
- end
339
- end
340
- end
@@ -1,16 +0,0 @@
1
- class <%= migration_class_name %> < ActiveRecord::Migration
2
- def self.up
3
- create_table :<%= table_name %> do |t|
4
- <% for attribute in attributes -%>
5
- t.<%= attribute.type %> :<%= attribute.name %>
6
- <% end -%>
7
- <% if options[:timestamps] %>
8
- t.timestamps
9
- <% end -%>
10
- end
11
- end
12
-
13
- def self.down
14
- drop_table :<%= table_name %>
15
- end
16
- end
@@ -1,15 +0,0 @@
1
- require 'rails/generators/active_record'
2
-
3
- module ActiveRecord
4
- module Generators
5
- class ObserverGenerator < Base
6
- check_class_collision :suffix => "Observer"
7
-
8
- def create_observer_file
9
- template 'observer.rb', File.join('app/models', class_path, "#{file_name}_observer.rb")
10
- end
11
-
12
- hook_for :test_framework
13
- end
14
- end
15
- end
@@ -1,2 +0,0 @@
1
- class <%= class_name %>Observer < ActiveRecord::Observer
2
- end
@@ -1,24 +0,0 @@
1
- require 'rails/generators/active_record'
2
-
3
- module ActiveRecord
4
- module Generators
5
- class SessionMigrationGenerator < Base
6
- argument :name, :type => :string, :default => "add_sessions_table"
7
-
8
- def create_migration_file
9
- migration_template "migration.rb", "db/migrate/#{file_name}.rb"
10
- end
11
-
12
- protected
13
-
14
- def session_table_name
15
- current_table_name = ActiveRecord::SessionStore::Session.table_name
16
- if ["sessions", "session"].include?(current_table_name)
17
- current_table_name = (ActiveRecord::Base.pluralize_table_names ? 'session'.pluralize : 'session')
18
- end
19
- current_table_name
20
- end
21
-
22
- end
23
- end
24
- end
@@ -1,16 +0,0 @@
1
- class <%= migration_class_name %> < ActiveRecord::Migration
2
- def self.up
3
- create_table :<%= session_table_name %> do |t|
4
- t.string :session_id, :null => false
5
- t.text :data
6
- t.timestamps
7
- end
8
-
9
- add_index :<%= session_table_name %>, :session_id
10
- add_index :<%= session_table_name %>, :updated_at
11
- end
12
-
13
- def self.down
14
- drop_table :<%= session_table_name %>
15
- end
16
- end