Module: ActiveRecord::Core::ClassMethods
- Defined in:
- lib/active_record/core.rb
Instance Method Summary collapse
-
#===(object) ⇒ Object
Overwrite the default class equality method to provide support for decorated models.
-
#_internal? ⇒ Boolean
:nodoc:.
-
#arel_attribute(name, table = arel_table) ⇒ Object
:nodoc:.
-
#arel_table ⇒ Object
Returns an instance of
Arel::Table
loaded with the current table name. -
#cached_find_by_statement(key, &block) ⇒ Object
:nodoc:.
-
#filter_attributes ⇒ Object
Returns columns which shouldn't be exposed while calling
#inspect
. -
#filter_attributes=(filter_attributes) ⇒ Object
Specifies columns which shouldn't be exposed while calling
#inspect
. -
#find(*ids) ⇒ Object
:nodoc:.
-
#find_by(*args) ⇒ Object
:nodoc:.
-
#find_by!(*args) ⇒ Object
:nodoc:.
-
#generated_association_methods ⇒ Object
:nodoc:.
-
#inherited(child_class) ⇒ Object
:nodoc:.
-
#initialize_find_by_cache ⇒ Object
:nodoc:.
-
#initialize_generated_modules ⇒ Object
:nodoc:.
-
#inspect ⇒ Object
Returns a string like 'Post(id:integer, title:string, body:text)'.
-
#inspection_filter ⇒ Object
:nodoc:.
-
#predicate_builder ⇒ Object
:nodoc:.
-
#type_caster ⇒ Object
:nodoc:.
Instance Method Details
#===(object) ⇒ Object
Overwrite the default class equality method to provide support for decorated models.
452 453 454 |
# File 'lib/active_record/core.rb', line 452 def ===(object) # :nodoc: object.is_a?(self) end |
#_internal? ⇒ Boolean
:nodoc:
478 479 480 |
# File 'lib/active_record/core.rb', line 478 def _internal? # :nodoc: false end |
#arel_attribute(name, table = arel_table) ⇒ Object
:nodoc:
465 466 467 |
# File 'lib/active_record/core.rb', line 465 def arel_attribute(name, table = arel_table) # :nodoc: table[name] end |
#arel_table ⇒ Object
Returns an instance of Arel::Table
loaded with the current table name.
class Post < ActiveRecord::Base
scope :published_and_commented, -> { published.and(arel_table[:comments_count].gt(0)) }
end
461 462 463 |
# File 'lib/active_record/core.rb', line 461 def arel_table # :nodoc: @arel_table ||= Arel::Table.new(table_name, klass: self) end |
#cached_find_by_statement(key, &block) ⇒ Object
:nodoc:
482 483 484 485 |
# File 'lib/active_record/core.rb', line 482 def cached_find_by_statement(key, &block) # :nodoc: cache = @find_by_statement_cache[connection.prepared_statements] cache.compute_if_absent(key) { StatementCache.create(connection, &block) } end |
#filter_attributes ⇒ Object
Returns columns which shouldn't be exposed while calling #inspect
.
410 411 412 413 414 415 416 |
# File 'lib/active_record/core.rb', line 410 def filter_attributes if defined?(@filter_attributes) @filter_attributes else superclass.filter_attributes end end |
#filter_attributes=(filter_attributes) ⇒ Object
Specifies columns which shouldn't be exposed while calling #inspect
.
419 420 421 422 |
# File 'lib/active_record/core.rb', line 419 def filter_attributes=(filter_attributes) @inspection_filter = nil @filter_attributes = filter_attributes end |
#find(*ids) ⇒ Object
:nodoc:
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/active_record/core.rb', line 330 def find(*ids) # :nodoc: # We don't have cache keys for this stuff yet return super unless ids.length == 1 return super if block_given? || primary_key.nil? || scope_attributes? id = ids.first return super if StatementCache.unsupported_value?(id) key = primary_key statement = cached_find_by_statement(key) { |params| where(key => params.bind).limit(1) } statement.execute([id], connection).first || raise(RecordNotFound.new("Couldn't find #{name} with '#{key}'=#{id}", name, key, id)) end |
#find_by(*args) ⇒ Object
:nodoc:
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
# File 'lib/active_record/core.rb', line 349 def find_by(*args) # :nodoc: return super if scope_attributes? hash = args.first return super unless Hash === hash hash = hash.each_with_object({}) do |(key, value), h| key = key.to_s key = attribute_aliases[key] || key return super if reflect_on_aggregation(key) reflection = _reflect_on_association(key) if !reflection value = value.id if value.respond_to?(:id) elsif reflection.belongs_to? && !reflection.polymorphic? key = reflection.join_foreign_key pkey = reflection.join_primary_key value = value.public_send(pkey) if value.respond_to?(pkey) end if !columns_hash.key?(key) || StatementCache.unsupported_value?(value) return super end h[key] = value end keys = hash.keys statement = cached_find_by_statement(keys) { |params| wheres = keys.index_with { params.bind } where(wheres).limit(1) } begin statement.execute(hash.values, connection).first rescue TypeError raise ActiveRecord::StatementInvalid end end |
#find_by!(*args) ⇒ Object
:nodoc:
391 392 393 |
# File 'lib/active_record/core.rb', line 391 def find_by!(*args) # :nodoc: find_by(*args) || raise(RecordNotFound.new("Couldn't find #{name}", name)) end |
#generated_association_methods ⇒ Object
:nodoc:
399 400 401 402 403 404 405 406 407 |
# File 'lib/active_record/core.rb', line 399 def generated_association_methods # :nodoc: @generated_association_methods ||= begin mod = const_set(:GeneratedAssociationMethods, Module.new) private_constant :GeneratedAssociationMethods include mod mod end end |
#inherited(child_class) ⇒ Object
:nodoc:
317 318 319 320 321 322 323 324 325 326 327 328 |
# File 'lib/active_record/core.rb', line 317 def inherited(child_class) # :nodoc: # initialize cache at class definition for thread safety child_class.initialize_find_by_cache unless child_class.base_class? klass = self until klass.base_class? klass.initialize_find_by_cache klass = klass.superclass end end super end |
#initialize_find_by_cache ⇒ Object
:nodoc:
313 314 315 |
# File 'lib/active_record/core.rb', line 313 def initialize_find_by_cache # :nodoc: @find_by_statement_cache = { true => Concurrent::Map.new, false => Concurrent::Map.new } end |
#initialize_generated_modules ⇒ Object
:nodoc:
395 396 397 |
# File 'lib/active_record/core.rb', line 395 def initialize_generated_modules # :nodoc: generated_association_methods end |
#inspect ⇒ Object
Returns a string like 'Post(id:integer, title:string, body:text)'
436 437 438 439 440 441 442 443 444 445 446 447 448 449 |
# File 'lib/active_record/core.rb', line 436 def inspect # :nodoc: if self == Base super elsif abstract_class? "#{super}(abstract)" elsif !connected? "#{super} (call '#{super}.connection' to establish a connection)" elsif table_exists? attr_list = attribute_types.map { |name, type| "#{name}: #{type.type}" } * ", " "#{super}(#{attr_list})" else "#{super}(Table doesn't exist)" end end |
#inspection_filter ⇒ Object
:nodoc:
424 425 426 427 428 429 430 431 432 433 |
# File 'lib/active_record/core.rb', line 424 def inspection_filter # :nodoc: if defined?(@filter_attributes) @inspection_filter ||= begin mask = InspectionMask.new(ActiveSupport::ParameterFilter::FILTERED) ActiveSupport::ParameterFilter.new(@filter_attributes, mask: mask) end else superclass.inspection_filter end end |
#predicate_builder ⇒ Object
:nodoc:
470 471 472 |
# File 'lib/active_record/core.rb', line 470 def predicate_builder # :nodoc: @predicate_builder ||= PredicateBuilder.new() end |
#type_caster ⇒ Object
:nodoc:
474 475 476 |
# File 'lib/active_record/core.rb', line 474 def type_caster # :nodoc: TypeCaster::Map.new(self) end |