Module: ActiveRecord::Core::ClassMethods
- Defined in:
- lib/active_record/core.rb
Instance Method Summary collapse
-
#arel_table ⇒ Object
Returns an instance of
Arel::Table
loaded with the current table name. -
#cached_find_by_statement(connection, 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:.
-
#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
#arel_table ⇒ Object
Returns an instance of Arel::Table
loaded with the current table name.
367 368 369 |
# File 'lib/active_record/core.rb', line 367 def arel_table # :nodoc: @arel_table ||= Arel::Table.new(table_name, klass: self) end |
#cached_find_by_statement(connection, key, &block) ⇒ Object
:nodoc:
379 380 381 382 |
# File 'lib/active_record/core.rb', line 379 def cached_find_by_statement(connection, 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
.
325 326 327 328 329 330 331 |
# File 'lib/active_record/core.rb', line 325 def filter_attributes if @filter_attributes.nil? superclass.filter_attributes else @filter_attributes end end |
#filter_attributes=(filter_attributes) ⇒ Object
Specifies columns which shouldn’t be exposed while calling #inspect
.
334 335 336 337 |
# File 'lib/active_record/core.rb', line 334 def filter_attributes=(filter_attributes) @inspection_filter = nil @filter_attributes = filter_attributes end |
#find(*ids) ⇒ Object
:nodoc:
246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/active_record/core.rb', line 246 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) cached_find_by([primary_key], [id]) || raise(RecordNotFound.new("Couldn't find #{name} with '#{primary_key}'=#{id}", name, primary_key, id)) end |
#find_by(*args) ⇒ Object
:nodoc:
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/active_record/core.rb', line 259 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 if pkey.is_a?(Array) if pkey.all? { |attribute| value.respond_to?(attribute) } value = pkey.map do |attribute| if attribute == "id" value.id_value else value.public_send(attribute) end end composite_primary_key = true end else value = value.public_send(pkey) if value.respond_to?(pkey) end end if !composite_primary_key && (!columns_hash.key?(key) || StatementCache.unsupported_value?(value)) return super end h[key] = value end cached_find_by(hash.keys, hash.values) end |
#find_by!(*args) ⇒ Object
:nodoc:
306 307 308 |
# File 'lib/active_record/core.rb', line 306 def find_by!(*args) # :nodoc: find_by(*args) || where(*args).raise_record_not_found_exception! end |
#generated_association_methods ⇒ Object
:nodoc:
314 315 316 317 318 319 320 321 322 |
# File 'lib/active_record/core.rb', line 314 def generated_association_methods # :nodoc: @generated_association_methods ||= begin mod = const_set(:GeneratedAssociationMethods, Module.new) private_constant :GeneratedAssociationMethods include mod mod end end |
#initialize_find_by_cache ⇒ Object
:nodoc:
242 243 244 |
# File 'lib/active_record/core.rb', line 242 def initialize_find_by_cache # :nodoc: @find_by_statement_cache = { true => Concurrent::Map.new, false => Concurrent::Map.new } end |
#initialize_generated_modules ⇒ Object
:nodoc:
310 311 312 |
# File 'lib/active_record/core.rb', line 310 def initialize_generated_modules # :nodoc: generated_association_methods end |
#inspect ⇒ Object
Returns a string like ‘Post(id:integer, title:string, body:text)’
351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/active_record/core.rb', line 351 def inspect # :nodoc: if self == Base super elsif abstract_class? "#{super}(abstract)" elsif !schema_loaded? && !connected? "#{super} (call '#{super}.load_schema' to load schema informations)" 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:
339 340 341 342 343 344 345 346 347 348 |
# File 'lib/active_record/core.rb', line 339 def inspection_filter # :nodoc: if @filter_attributes.nil? superclass.inspection_filter else @inspection_filter ||= begin mask = InspectionMask.new(ActiveSupport::ParameterFilter::FILTERED) ActiveSupport::ParameterFilter.new(@filter_attributes, mask: mask) end end end |
#predicate_builder ⇒ Object
:nodoc:
371 372 373 |
# File 'lib/active_record/core.rb', line 371 def predicate_builder # :nodoc: @predicate_builder ||= PredicateBuilder.new() end |
#type_caster ⇒ Object
:nodoc:
375 376 377 |
# File 'lib/active_record/core.rb', line 375 def type_caster # :nodoc: TypeCaster::Map.new(self) end |