Class: InventoryRefresh::InventoryCollection
- Inherits:
-
Object
- Object
- InventoryRefresh::InventoryCollection
- Defined in:
- lib/inventory_refresh/inventory_collection.rb,
lib/inventory_refresh/inventory_collection/graph.rb,
lib/inventory_refresh/inventory_collection/scanner.rb,
lib/inventory_refresh/inventory_collection/reference.rb,
lib/inventory_refresh/inventory_collection/index/proxy.rb,
lib/inventory_refresh/inventory_collection/data_storage.rb,
lib/inventory_refresh/inventory_collection/serialization.rb,
lib/inventory_refresh/inventory_collection/index/type/base.rb,
lib/inventory_refresh/inventory_collection/index/type/data.rb,
lib/inventory_refresh/inventory_collection/references_storage.rb,
lib/inventory_refresh/inventory_collection/index/type/local_db.rb,
lib/inventory_refresh/inventory_collection/index/type/skeletal.rb
Overview
For more usage examples please follow spec examples in
-
spec/models/inventory_refresh/save_inventory/single_inventory_collection_spec.rb
-
spec/models/inventory_refresh/save_inventory/acyclic_graph_of_inventory_collections_spec.rb
-
spec/models/inventory_refresh/save_inventory/graph_of_inventory_collections_spec.rb
-
spec/models/inventory_refresh/save_inventory/graph_of_inventory_collections_targeted_refresh_spec.rb
-
spec/models/inventory_refresh/save_inventory/strategies_and_references_spec.rb
Defined Under Namespace
Modules: Index Classes: DataStorage, Graph, Reference, ReferencesStorage, Scanner, Serialization
Instance Attribute Summary collapse
-
#all_manager_uuids ⇒ Array?
If present, InventoryCollection switches into delete_complement mode, where it will delete every record from the DB, that is not present in this list.
-
#arel ⇒ Object
readonly
Returns the value of attribute arel.
-
#association ⇒ Object
readonly
Returns the value of attribute association.
-
#attributes_blacklist ⇒ Object
Returns the value of attribute attributes_blacklist.
-
#attributes_whitelist ⇒ Object
Returns the value of attribute attributes_whitelist.
-
#batch_extra_attributes ⇒ Object
readonly
Returns the value of attribute batch_extra_attributes.
-
#check_changed ⇒ Object
readonly
Returns the value of attribute check_changed.
-
#complete ⇒ Object
readonly
Returns the value of attribute complete.
-
#create_only ⇒ Object
readonly
Returns the value of attribute create_only.
-
#created_records ⇒ Object
readonly
Returns the value of attribute created_records.
-
#custom_reconnect_block ⇒ Object
readonly
Returns the value of attribute custom_reconnect_block.
-
#custom_save_block ⇒ Object
readonly
Returns the value of attribute custom_save_block.
-
#data_collection_finalized ⇒ Boolean
A true value marks that we collected all the data of the InventoryCollection, meaning we also collected all the references.
-
#data_storage ⇒ InventoryRefresh::InventoryCollection::DataStorage
An InventoryCollection encapsulating all data with indexes.
-
#default_values ⇒ Object
readonly
Returns the value of attribute default_values.
-
#delete_method ⇒ Object
readonly
Returns the value of attribute delete_method.
-
#deleted_records ⇒ Object
readonly
Returns the value of attribute deleted_records.
-
#dependees ⇒ Set
A set of InventoryCollection objects that depends on this InventoryCollection object.
-
#dependency_attributes ⇒ Object
readonly
Returns the value of attribute dependency_attributes.
-
#internal_attributes ⇒ Object
readonly
Returns the value of attribute internal_attributes.
-
#inventory_object_attributes ⇒ Object
readonly
Returns the value of attribute inventory_object_attributes.
-
#manager_ref ⇒ Object
readonly
Returns the value of attribute manager_ref.
-
#manager_ref_allowed_nil ⇒ Object
readonly
Returns the value of attribute manager_ref_allowed_nil.
-
#model_class ⇒ Object
readonly
Returns the value of attribute model_class.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#parent_inventory_collections ⇒ Array<Symbol>
parameters.
-
#references_storage ⇒ Object
readonly
Returns the value of attribute references_storage.
-
#saved ⇒ Boolean
True if this collection is already saved into the DB.
-
#saver_strategy ⇒ Object
readonly
Returns the value of attribute saver_strategy.
-
#strategy ⇒ Object
readonly
Returns the value of attribute strategy.
-
#targeted ⇒ Object
readonly
Returns the value of attribute targeted.
-
#targeted_arel ⇒ Object
readonly
Returns the value of attribute targeted_arel.
-
#targeted_scope ⇒ Object
readonly
Returns the value of attribute targeted_scope.
-
#transitive_dependency_attributes ⇒ Object
readonly
Returns the value of attribute transitive_dependency_attributes.
-
#update_only ⇒ Object
readonly
Returns the value of attribute update_only.
-
#updated_records ⇒ Object
readonly
Returns the value of attribute updated_records.
-
#use_ar_object ⇒ Object
readonly
Returns the value of attribute use_ar_object.
Instance Method Summary collapse
-
#association_to_base_class_mapping ⇒ Hash{Symbol => String}
Hash with association name mapped to base class of the association.
-
#association_to_foreign_key_mapping ⇒ Hash{Symbol => String}
Hash with association name mapped to foreign key column name.
-
#association_to_foreign_type_mapping ⇒ Hash{Symbol => String}
Hash with association name mapped to polymorphic foreign key type column name.
-
#base_class_name ⇒ String
Base class name of the model_class of this InventoryCollection.
- #base_columns ⇒ Object
-
#batch_size ⇒ Integer
Default batch size for talking to the DB.
-
#batch_size_pure_sql ⇒ Integer
Default batch size for talking to the DB if not using ApplicationRecord objects.
-
#belongs_to_associations ⇒ Array<ActiveRecord::Reflection::BelongsToReflection">
All belongs_to associations.
-
#blacklist_attributes!(attributes) ⇒ Array<Symbol>
Add passed attributes to blacklist.
-
#build_multi_selection_condition(hashes, keys = manager_ref) ⇒ String
Builds a multiselection conditions like (table1.a = a1 AND table2.b = b1) OR (table1.a = a2 AND table2.b = b2).
-
#check_changed? ⇒ Boolean
True means we want to call .changed? on every ActiveRecord object before saving it.
-
#clone ⇒ InventoryCollection
A shallow copy of InventoryCollection, the copy will share data_storage of the original collection, otherwise we would be copying a lot of records in memory.
-
#complete? ⇒ Boolean
True means the data is not complete, leading to only creating and updating data.
-
#create_allowed? ⇒ Boolean
True means we will delete/soft-delete data.
-
#create_only? ⇒ Boolean
True means that only create of new data is allowed.
-
#data_collection_finalized? ⇒ Boolean
True if no more data will be added to this InventoryCollection object, that usually happens after the parsing step is finished.
-
#db_collection_for_comparison ⇒ ActiveRecord::Relation
A relation that can fetch all data of this InventoryCollection from the DB.
-
#db_collection_for_comparison_for(references) ⇒ ActiveRecord::Relation
Builds an ActiveRecord::Relation that can fetch all the references from the DB.
-
#db_collection_for_comparison_for_complement_of(manager_uuids_set) ⇒ ActiveRecord::Relation
Builds an ActiveRecord::Relation that can fetch complement of all the references from the DB.
-
#delete_allowed? ⇒ Boolean
True means we will delete/soft-delete data.
-
#dependencies ⇒ Array<InventoryRefresh::InventoryCollection>
All unique non saved dependencies.
-
#dependency_attributes_for(inventory_collections) ⇒ Array<InventoryRefresh::InventoryCollection>
Returns what attributes are causing a dependencies to certain InventoryCollection objects.
-
#filtered_dependency_attributes ⇒ Hash{Symbol => Set}
List attributes causing a dependency and filters them by attributes_blacklist and attributes_whitelist.
-
#fixed_attributes ⇒ Array<Symbol>
Attributes that are needed to be able to save the record, i.e.
-
#fixed_dependencies ⇒ Object
Returns fixed dependencies, which are the ones we can’t move, because we wouldn’t be able to save the data.
-
#fixed_foreign_keys ⇒ Array<Symbol>
List of all column names that are foreign keys and cannot removed, otherwise we couldn’t save the record.
-
#foreign_key_to_association_mapping ⇒ Hash{String => Hash}
Hash with foreign_key column name mapped to association name.
-
#foreign_keys ⇒ Array<Symbol>
List of all column names that are foreign keys.
-
#foreign_type_to_association_mapping ⇒ Hash{Symbol => String}
Hash with polymorphic foreign key type column name mapped to association name.
-
#full_collection_for_comparison ⇒ ActiveRecord::Relation
Relation that can fetch all the references from the DB.
-
#initialize(model_class: nil, manager_ref: nil, association: nil, parent: nil, strategy: nil, custom_save_block: nil, delete_method: nil, dependency_attributes: nil, attributes_blacklist: nil, attributes_whitelist: nil, complete: nil, update_only: nil, check_changed: nil, arel: nil, default_values: {}, create_only: nil, inventory_object_attributes: nil, name: nil, saver_strategy: nil, parent_inventory_collections: nil, manager_uuids: [], all_manager_uuids: nil, targeted_arel: nil, targeted: nil, manager_ref_allowed_nil: nil, secondary_refs: {}, use_ar_object: nil, custom_reconnect_block: nil, batch_extra_attributes: []) ⇒ InventoryCollection
constructor
A new instance of InventoryCollection.
-
#inspect ⇒ String
A concise form of the InventoryCollection for easy logging.
- #internal_columns ⇒ Object
- #internal_timestamp_columns ⇒ Object
-
#inventory_object?(value) ⇒ Boolean
True is value is kind of InventoryRefresh::InventoryObject.
-
#inventory_object_lazy?(value) ⇒ Boolean
True is value is kind of InventoryRefresh::InventoryObjectLazy.
-
#manager_ref_to_cols ⇒ Array<String>
Convert manager_ref list of attributes to list of DB columns.
-
#manager_uuids ⇒ Array<String>
Returns a list of stringified uuids of all scoped InventoryObjects, which is used for scoping in targeted mode.
-
#new_inventory_object(hash) ⇒ InventoryRefresh::InventoryObject
Creates InventoryRefresh::InventoryObject object from passed hash data.
-
#noop? ⇒ Boolean
True if processing of this InventoryCollection object would lead to no operations.
-
#object_index_with_keys(keys, record) ⇒ String
Builds string uuid from passed Object and keys.
-
#parallel_safe? ⇒ Boolean
True if we are using a saver strategy that allows saving in parallel processes.
-
#process_saver_strategy(saver_strategy) ⇒ Symbol
Processes passed saver strategy.
-
#process_strategy(strategy_name) ⇒ Symbol
Processes passed strategy, modifies :data_collection_finalized and :saved attributes for db only strategies.
-
#saveable? ⇒ Boolean
True if all dependencies have all data persisted.
-
#saved? ⇒ Boolean
True if the whole InventoryCollection object has all data persisted.
-
#store_created_records(records) ⇒ Object
Caches what records were created, for later use, e.g.
-
#store_deleted_records(records) ⇒ Object
Caches what records were deleted/soft-deleted, for later use, e.g.
-
#store_updated_records(records) ⇒ Object
Caches what records were updated, for later use, e.g.
-
#supports_created_at? ⇒ Boolean
True if the model_class has created_at column.
-
#supports_created_on? ⇒ Boolean
True if the model_class has created_on column.
-
#supports_resource_timestamp? ⇒ Boolean
True if the model_class has resource_timestamp column.
-
#supports_resource_timestamps? ⇒ Boolean
True if the model_class has resource_timestamps column.
-
#supports_resource_timestamps_max? ⇒ Boolean
True if the model_class has resource_timestamps_max column.
-
#supports_resource_version? ⇒ Boolean
True if the model_class has resource_version column.
-
#supports_resource_versions? ⇒ Boolean
True if the model_class has resource_versions column.
-
#supports_resource_versions_max? ⇒ Boolean
True if the model_class has resource_versions_max column.
-
#supports_sti? ⇒ Boolean
True if the model_class supports STI.
-
#supports_updated_at? ⇒ Boolean
True if the model_class has updated_at column.
-
#supports_updated_on? ⇒ Boolean
True if the model_class has updated_on column.
-
#targeted? ⇒ Boolean
True is processing of this InventoryCollection will be in targeted mode.
-
#targeted_arel_default ⇒ InventoryRefresh::ApplicationRecordIterator
Builds targeted query limiting the results by the :references defined in parent_inventory_collections.
-
#targeted_iterator_for(references, query = nil) ⇒ InventoryRefresh::ApplicationRecordIterator
Returns iterator for the passed references and a query.
-
#targeted_selection_for(references) ⇒ String
Builds a multiselection conditions like (table1.a = a1 AND table2.b = b1) OR (table1.a = a2 AND table2.b = b2) for passed references.
-
#to_s ⇒ String
A concise form of the inventoryCollection for easy logging.
-
#transform_references_to_hashes(references) ⇒ Array<Hash>
Gets targeted references and transforms them into list of hashes.
-
#unique_index_columns ⇒ Array<Symbol>
All columns that are part of the best fit unique index.
-
#unique_index_for(keys) ⇒ ActiveRecord::ConnectionAdapters::IndexDefinition
Finds an index that fits the list of columns (keys) the best.
- #unique_index_keys ⇒ Object
-
#unique_indexes ⇒ Array<ActiveRecord::ConnectionAdapters::IndexDefinition>
Array of all unique indexes known to model.
-
#update_only? ⇒ Boolean
True means we want to only update data.
-
#use_ar_object? ⇒ Boolean
True means we want to use ActiveRecord object for writing attributes and we want to perform casting on all columns.
-
#whitelist_attributes!(attributes) ⇒ Array<Symbol>
Add passed attributes to whitelist.
Constructor Details
#initialize(model_class: nil, manager_ref: nil, association: nil, parent: nil, strategy: nil, custom_save_block: nil, delete_method: nil, dependency_attributes: nil, attributes_blacklist: nil, attributes_whitelist: nil, complete: nil, update_only: nil, check_changed: nil, arel: nil, default_values: {}, create_only: nil, inventory_object_attributes: nil, name: nil, saver_strategy: nil, parent_inventory_collections: nil, manager_uuids: [], all_manager_uuids: nil, targeted_arel: nil, targeted: nil, manager_ref_allowed_nil: nil, secondary_refs: {}, use_ar_object: nil, custom_reconnect_block: nil, batch_extra_attributes: []) ⇒ InventoryCollection
Returns a new instance of InventoryCollection.
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 412 def initialize(model_class: nil, manager_ref: nil, association: nil, parent: nil, strategy: nil, custom_save_block: nil, delete_method: nil, dependency_attributes: nil, attributes_blacklist: nil, attributes_whitelist: nil, complete: nil, update_only: nil, check_changed: nil, arel: nil, default_values: {}, create_only: nil, inventory_object_attributes: nil, name: nil, saver_strategy: nil, parent_inventory_collections: nil, manager_uuids: [], all_manager_uuids: nil, targeted_arel: nil, targeted: nil, manager_ref_allowed_nil: nil, secondary_refs: {}, use_ar_object: nil, custom_reconnect_block: nil, batch_extra_attributes: []) @model_class = model_class @manager_ref = manager_ref || [:ems_ref] @secondary_refs = secondary_refs @association = association @parent = parent || nil @arel = arel @dependency_attributes = dependency_attributes || {} @strategy = process_strategy(strategy) @delete_method = delete_method || :destroy @custom_save_block = custom_save_block @custom_reconnect_block = custom_reconnect_block @check_changed = check_changed.nil? ? true : check_changed @internal_attributes = i(__feedback_edge_set_parent __parent_inventory_collections) @complete = complete.nil? ? true : complete @update_only = update_only.nil? ? false : update_only @create_only = create_only.nil? ? false : create_only @default_values = default_values @name = name || association || model_class.to_s.demodulize.tableize @saver_strategy = process_saver_strategy(saver_strategy) @use_ar_object = use_ar_object || false @batch_extra_attributes = batch_extra_attributes @manager_ref_allowed_nil = manager_ref_allowed_nil || [] # Targeted mode related attributes # TODO(lsmola) Should we refactor this to use references too? @all_manager_uuids = all_manager_uuids @parent_inventory_collections = parent_inventory_collections @targeted_arel = targeted_arel @targeted = !!targeted @inventory_object_attributes = inventory_object_attributes @saved ||= false @attributes_blacklist = Set.new @attributes_whitelist = Set.new @transitive_dependency_attributes = Set.new @dependees = Set.new @data_storage = ::InventoryRefresh::InventoryCollection::DataStorage.new(self, secondary_refs) @references_storage = ::InventoryRefresh::InventoryCollection::ReferencesStorage.new(index_proxy) @targeted_scope = ::InventoryRefresh::InventoryCollection::ReferencesStorage.new(index_proxy).merge!(manager_uuids) @created_records = [] @updated_records = [] @deleted_records = [] blacklist_attributes!(attributes_blacklist) if attributes_blacklist.present? whitelist_attributes!(attributes_whitelist) if attributes_whitelist.present? end |
Instance Attribute Details
#all_manager_uuids ⇒ Array?
If present, InventoryCollection switches into delete_complement mode, where it will delete every record from the DB, that is not present in this list. This is used for the batch processing, where we don’t know which InventoryObject should be deleted, but we know all manager_uuids of all InventoryObject objects that exists in the provider.
75 76 77 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 75 def all_manager_uuids @all_manager_uuids end |
#arel ⇒ Object (readonly)
Returns the value of attribute arel.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def arel @arel end |
#association ⇒ Object (readonly)
Returns the value of attribute association.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def association @association end |
#attributes_blacklist ⇒ Object
Returns the value of attribute attributes_blacklist.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def attributes_blacklist @attributes_blacklist end |
#attributes_whitelist ⇒ Object
Returns the value of attribute attributes_whitelist.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def attributes_whitelist @attributes_whitelist end |
#batch_extra_attributes ⇒ Object (readonly)
Returns the value of attribute batch_extra_attributes.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def batch_extra_attributes @batch_extra_attributes end |
#check_changed ⇒ Object (readonly)
Returns the value of attribute check_changed.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def check_changed @check_changed end |
#complete ⇒ Object (readonly)
Returns the value of attribute complete.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def complete @complete end |
#create_only ⇒ Object (readonly)
Returns the value of attribute create_only.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def create_only @create_only end |
#created_records ⇒ Object (readonly)
Returns the value of attribute created_records.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def created_records @created_records end |
#custom_reconnect_block ⇒ Object (readonly)
Returns the value of attribute custom_reconnect_block.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def custom_reconnect_block @custom_reconnect_block end |
#custom_save_block ⇒ Object (readonly)
Returns the value of attribute custom_save_block.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def custom_save_block @custom_save_block end |
#data_collection_finalized ⇒ Boolean
Returns A true value marks that we collected all the data of the InventoryCollection, meaning we also collected all the references.
58 59 60 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 58 def data_collection_finalized @data_collection_finalized end |
#data_storage ⇒ InventoryRefresh::InventoryCollection::DataStorage
Returns An InventoryCollection encapsulating all data with indexes.
62 63 64 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 62 def data_storage @data_storage end |
#default_values ⇒ Object (readonly)
Returns the value of attribute default_values.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def default_values @default_values end |
#delete_method ⇒ Object (readonly)
Returns the value of attribute delete_method.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def delete_method @delete_method end |
#deleted_records ⇒ Object (readonly)
Returns the value of attribute deleted_records.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def deleted_records @deleted_records end |
#dependees ⇒ Set
Returns A set of InventoryCollection objects that depends on this InventoryCollection object.
78 79 80 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 78 def dependees @dependees end |
#dependency_attributes ⇒ Object (readonly)
Returns the value of attribute dependency_attributes.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def dependency_attributes @dependency_attributes end |
#internal_attributes ⇒ Object (readonly)
Returns the value of attribute internal_attributes.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def internal_attributes @internal_attributes end |
#inventory_object_attributes ⇒ Object (readonly)
Returns the value of attribute inventory_object_attributes.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def inventory_object_attributes @inventory_object_attributes end |
#manager_ref ⇒ Object (readonly)
Returns the value of attribute manager_ref.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def manager_ref @manager_ref end |
#manager_ref_allowed_nil ⇒ Object (readonly)
Returns the value of attribute manager_ref_allowed_nil.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def manager_ref_allowed_nil @manager_ref_allowed_nil end |
#model_class ⇒ Object (readonly)
Returns the value of attribute model_class.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def model_class @model_class end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def name @name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def parent @parent end |
#parent_inventory_collections ⇒ Array<Symbol>
Returns @see #parent_inventory_collections documentation of InventoryCollection.new kwargs parameters.
82 83 84 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 82 def parent_inventory_collections @parent_inventory_collections end |
#references_storage ⇒ Object (readonly)
Returns the value of attribute references_storage.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def references_storage @references_storage end |
#saved ⇒ Boolean
Returns true if this collection is already saved into the DB. E.g. InventoryCollections with DB only strategy are marked as saved. This causes InventoryCollection not being a dependency for any other InventoryCollection, since it is already persisted into the DB.
67 68 69 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 67 def saved @saved end |
#saver_strategy ⇒ Object (readonly)
Returns the value of attribute saver_strategy.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def saver_strategy @saver_strategy end |
#strategy ⇒ Object (readonly)
Returns the value of attribute strategy.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def strategy @strategy end |
#targeted ⇒ Object (readonly)
Returns the value of attribute targeted.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def targeted @targeted end |
#targeted_arel ⇒ Object (readonly)
Returns the value of attribute targeted_arel.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def targeted_arel @targeted_arel end |
#targeted_scope ⇒ Object (readonly)
Returns the value of attribute targeted_scope.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def targeted_scope @targeted_scope end |
#transitive_dependency_attributes ⇒ Object (readonly)
Returns the value of attribute transitive_dependency_attributes.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def transitive_dependency_attributes @transitive_dependency_attributes end |
#update_only ⇒ Object (readonly)
Returns the value of attribute update_only.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def update_only @update_only end |
#updated_records ⇒ Object (readonly)
Returns the value of attribute updated_records.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def updated_records @updated_records end |
#use_ar_object ⇒ Object (readonly)
Returns the value of attribute use_ar_object.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def use_ar_object @use_ar_object end |
Instance Method Details
#association_to_base_class_mapping ⇒ Hash{Symbol => String}
Returns Hash with association name mapped to base class of the association.
942 943 944 945 946 947 948 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 942 def association_to_base_class_mapping return {} unless model_class @association_to_base_class_mapping ||= model_class.reflect_on_all_associations.each_with_object({}) do |x, obj| obj[x.name] = x.klass.base_class.name unless x.polymorphic? end end |
#association_to_foreign_key_mapping ⇒ Hash{Symbol => String}
Returns Hash with association name mapped to foreign key column name.
906 907 908 909 910 911 912 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 906 def association_to_foreign_key_mapping return {} unless model_class @association_to_foreign_key_mapping ||= belongs_to_associations.each_with_object({}) do |x, obj| obj[x.name] = x.foreign_key end end |
#association_to_foreign_type_mapping ⇒ Hash{Symbol => String}
Returns Hash with association name mapped to polymorphic foreign key type column name.
924 925 926 927 928 929 930 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 924 def association_to_foreign_type_mapping return {} unless model_class @association_to_foreign_type_mapping ||= model_class.reflect_on_all_associations.each_with_object({}) do |x, obj| obj[x.name] = x.foreign_type if x.polymorphic? end end |
#base_class_name ⇒ String
Returns Base class name of the model_class of this InventoryCollection.
972 973 974 975 976 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 972 def base_class_name return "" unless model_class @base_class_name ||= model_class.base_class.name end |
#base_columns ⇒ Object
723 724 725 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 723 def base_columns @base_columns ||= unique_index_columns + internal_columns end |
#batch_size ⇒ Integer
Returns default batch size for talking to the DB.
996 997 998 999 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 996 def batch_size # TODO(lsmola) mode to the settings 1000 end |
#batch_size_pure_sql ⇒ Integer
Returns default batch size for talking to the DB if not using ApplicationRecord objects.
1002 1003 1004 1005 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 1002 def batch_size_pure_sql # TODO(lsmola) mode to the settings 10_000 end |
#belongs_to_associations ⇒ Array<ActiveRecord::Reflection::BelongsToReflection">
Returns All belongs_to associations.
901 902 903 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 901 def belongs_to_associations model_class.reflect_on_all_associations.select { |x| x.kind_of?(ActiveRecord::Reflection::BelongsToReflection) } end |
#blacklist_attributes!(attributes) ⇒ Array<Symbol>
Add passed attributes to blacklist. The manager_ref attributes cannot be blacklisted, otherwise we will not be able to identify the inventory_object. We do not automatically remove attributes causing fixed dependencies, so beware that without them, you won’t be able to create the record.
865 866 867 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 865 def blacklist_attributes!(attributes) self.attributes_blacklist += attributes - (fixed_attributes + internal_attributes) end |
#build_multi_selection_condition(hashes, keys = manager_ref) ⇒ String
Builds a multiselection conditions like (table1.a = a1 AND table2.b = b1) OR (table1.a = a2 AND table2.b = b2)
1022 1023 1024 1025 1026 1027 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 1022 def build_multi_selection_condition(hashes, keys = manager_ref) arel_table = model_class.arel_table # We do pure SQL OR, since Arel is nesting every .or into another parentheses, otherwise this would be just # inject(:or) instead of to_sql with .join(" OR ") hashes.map { |hash| "(#{keys.map { |key| arel_table[key].eq(hash[key]) }.inject(:and).to_sql})" }.join(" OR ") end |
#check_changed? ⇒ Boolean
Returns true means we want to call .changed? on every ActiveRecord object before saving it.
533 534 535 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 533 def check_changed? check_changed end |
#clone ⇒ InventoryCollection
Returns a shallow copy of InventoryCollection, the copy will share data_storage of the original collection, otherwise we would be copying a lot of records in memory.
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 881 def clone cloned = self.class.new(:model_class => model_class, :manager_ref => manager_ref, :association => association, :parent => parent, :arel => arel, :strategy => strategy, :saver_strategy => saver_strategy, :custom_save_block => custom_save_block, # We want cloned IC to be update only, since this is used for cycle resolution :update_only => true, # Dependency attributes need to be a hard copy, since those will differ for each # InventoryCollection :dependency_attributes => dependency_attributes.clone) cloned.data_storage = data_storage cloned end |
#complete? ⇒ Boolean
Returns true means the data is not complete, leading to only creating and updating data.
544 545 546 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 544 def complete? complete end |
#create_allowed? ⇒ Boolean
Returns true means we will delete/soft-delete data.
559 560 561 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 559 def create_allowed? !update_only? end |
#create_only? ⇒ Boolean
Returns true means that only create of new data is allowed.
564 565 566 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 564 def create_only? create_only end |
#data_collection_finalized? ⇒ Boolean
Returns true if no more data will be added to this InventoryCollection object, that usually happens after the parsing step is finished.
729 730 731 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 729 def data_collection_finalized? data_collection_finalized end |
#db_collection_for_comparison ⇒ ActiveRecord::Relation
Returns A relation that can fetch all data of this InventoryCollection from the DB.
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 1030 def db_collection_for_comparison if targeted? if targeted_arel.respond_to?(:call) targeted_arel.call(self) elsif parent_inventory_collections.present? targeted_arel_default else targeted_iterator_for(targeted_scope.primary_references) end else full_collection_for_comparison end end |
#db_collection_for_comparison_for(references) ⇒ ActiveRecord::Relation
Builds an ActiveRecord::Relation that can fetch all the references from the DB
1097 1098 1099 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 1097 def db_collection_for_comparison_for(references) full_collection_for_comparison.where(targeted_selection_for(references)) end |
#db_collection_for_comparison_for_complement_of(manager_uuids_set) ⇒ ActiveRecord::Relation
Builds an ActiveRecord::Relation that can fetch complement of all the references from the DB
1105 1106 1107 1108 1109 1110 1111 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 1105 def db_collection_for_comparison_for_complement_of(manager_uuids_set) # TODO(lsmola) this should have the build_multi_selection_condition, like in the method above # TODO(lsmola) this query will be highly ineffective, we will try approach with updating a timestamp of all # records, then we can get list of all records that were not update. That would be equivalent to result of this # more effective query and without need of all manager_uuids full_collection_for_comparison.where.not(manager_ref.first => manager_uuids_set) end |
#delete_allowed? ⇒ Boolean
Returns true means we will delete/soft-delete data.
554 555 556 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 554 def delete_allowed? complete? && !update_only? end |
#dependencies ⇒ Array<InventoryRefresh::InventoryCollection>
Returns all unique non saved dependencies.
842 843 844 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 842 def dependencies filtered_dependency_attributes.values.map(&:to_a).flatten.uniq.reject(&:saved?) end |
#dependency_attributes_for(inventory_collections) ⇒ Array<InventoryRefresh::InventoryCollection>
Returns what attributes are causing a dependencies to certain InventoryCollection objects.
851 852 853 854 855 856 857 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 851 def dependency_attributes_for(inventory_collections) attributes = Set.new inventory_collections.each do |inventory_collection| attributes += filtered_dependency_attributes.select { |_key, value| value.include?(inventory_collection) }.keys end attributes end |
#filtered_dependency_attributes ⇒ Hash{Symbol => Set}
List attributes causing a dependency and filters them by attributes_blacklist and attributes_whitelist
801 802 803 804 805 806 807 808 809 810 811 812 813 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 801 def filtered_dependency_attributes filtered_attributes = dependency_attributes if attributes_blacklist.present? filtered_attributes = filtered_attributes.reject { |key, _value| attributes_blacklist.include?(key) } end if attributes_whitelist.present? filtered_attributes = filtered_attributes.select { |key, _value| attributes_whitelist.include?(key) } end filtered_attributes end |
#fixed_attributes ⇒ Array<Symbol>
Attributes that are needed to be able to save the record, i.e. attributes that are part of the unique index and attributes with presence validation or NOT NULL constraint
819 820 821 822 823 824 825 826 827 828 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 819 def fixed_attributes if model_class presence_validators = model_class.validators.detect { |x| x.kind_of?(ActiveRecord::Validations::PresenceValidator) } end # Attributes that has to be always on the entity, so attributes making unique index of the record + attributes # that have presence validation fixed_attributes = manager_ref fixed_attributes += presence_validators.attributes if presence_validators.present? fixed_attributes end |
#fixed_dependencies ⇒ Object
Returns fixed dependencies, which are the ones we can’t move, because we wouldn’t be able to save the data
833 834 835 836 837 838 839 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 833 def fixed_dependencies fixed_attrs = fixed_attributes filtered_dependency_attributes.each_with_object(Set.new) do |(key, value), fixed_deps| fixed_deps.merge(value) if fixed_attrs.include?(key) end.reject(&:saved?) end |
#fixed_foreign_keys ⇒ Array<Symbol>
Returns List of all column names that are foreign keys and cannot removed, otherwise we couldn’t save the record.
959 960 961 962 963 964 965 966 967 968 969 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 959 def fixed_foreign_keys # Foreign keys that are part of a manager_ref must be present, otherwise the record would get lost. This is a # minimum check we can do to not break a referential integrity. return @fixed_foreign_keys_cache unless @fixed_foreign_keys_cache.nil? manager_ref_set = (manager_ref - manager_ref_allowed_nil) @fixed_foreign_keys_cache = manager_ref_set.map { |x| association_to_foreign_key_mapping[x] }.compact @fixed_foreign_keys_cache += foreign_keys & manager_ref @fixed_foreign_keys_cache.map!(&:to_sym) @fixed_foreign_keys_cache end |
#foreign_key_to_association_mapping ⇒ Hash{String => Hash}
Returns Hash with foreign_key column name mapped to association name.
915 916 917 918 919 920 921 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 915 def foreign_key_to_association_mapping return {} unless model_class @foreign_key_to_association_mapping ||= belongs_to_associations.each_with_object({}) do |x, obj| obj[x.foreign_key] = x.name end end |
#foreign_keys ⇒ Array<Symbol>
Returns List of all column names that are foreign keys.
951 952 953 954 955 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 951 def foreign_keys return [] unless model_class @foreign_keys_cache ||= belongs_to_associations.map(&:foreign_key).map!(&:to_sym) end |
#foreign_type_to_association_mapping ⇒ Hash{Symbol => String}
Returns Hash with polymorphic foreign key type column name mapped to association name.
933 934 935 936 937 938 939 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 933 def foreign_type_to_association_mapping return {} unless model_class @foreign_type_to_association_mapping ||= model_class.reflect_on_all_associations.each_with_object({}) do |x, obj| obj[x.foreign_type] = x.name if x.polymorphic? end end |
#full_collection_for_comparison ⇒ ActiveRecord::Relation
Returns relation that can fetch all the references from the DB.
1114 1115 1116 1117 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 1114 def full_collection_for_comparison return arel unless arel.nil? parent.send(association) end |
#inspect ⇒ String
Returns a concise form of the InventoryCollection for easy logging.
991 992 993 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 991 def inspect to_s end |
#internal_columns ⇒ Object
697 698 699 700 701 702 703 704 705 706 707 708 709 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 697 def internal_columns return @internal_columns if @internal_columns @internal_columns = [] + @internal_columns << :type if supports_sti? @internal_columns << :resource_timestamps_max if @internal_columns << :resource_timestamps if @internal_columns << :resource_timestamp if @internal_columns << :resource_versions_max if supports_resource_versions_max? @internal_columns << :resource_versions if supports_resource_versions? @internal_columns << :resource_version if supports_resource_version? @internal_columns end |
#internal_timestamp_columns ⇒ Object
711 712 713 714 715 716 717 718 719 720 721 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 711 def return if = [] << :created_on if supports_created_on? << :created_at if supports_created_at? << :updated_on if supports_updated_on? << :updated_at if supports_updated_at? end |
#inventory_object?(value) ⇒ Boolean
Returns true is value is kind of InventoryRefresh::InventoryObject.
735 736 737 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 735 def inventory_object?(value) value.kind_of?(::InventoryRefresh::InventoryObject) end |
#inventory_object_lazy?(value) ⇒ Boolean
Returns true is value is kind of InventoryRefresh::InventoryObjectLazy.
741 742 743 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 741 def inventory_object_lazy?(value) value.kind_of?(::InventoryRefresh::InventoryObjectLazy) end |
#manager_ref_to_cols ⇒ Array<String>
Convert manager_ref list of attributes to list of DB columns
790 791 792 793 794 795 796 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 790 def manager_ref_to_cols # TODO(lsmola) this should contain the polymorphic _type, otherwise the IC with polymorphic unique key will get # conflicts manager_ref.map do |ref| association_to_foreign_key_mapping[ref] || ref end end |
#manager_uuids ⇒ Array<String>
Returns a list of stringified uuids of all scoped InventoryObjects, which is used for scoping in targeted mode
1010 1011 1012 1013 1014 1015 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 1010 def manager_uuids # TODO(lsmola) LEGACY: this is still being used by :targetel_arel definitions and it expects array of strings raise "This works only for :manager_ref size 1" if manager_ref.size > 1 key = manager_ref.first transform_references_to_hashes(targeted_scope.primary_references).map { |x| x[key] } end |
#new_inventory_object(hash) ⇒ InventoryRefresh::InventoryObject
Creates InventoryRefresh::InventoryObject object from passed hash data
1123 1124 1125 1126 1127 1128 1129 1130 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 1123 def new_inventory_object(hash) manager_ref.each do |x| # TODO(lsmola) with some effort, we can do this, but it's complex raise "A lazy_find with a :key can't be a part of the manager_uuid" if inventory_object_lazy?(hash[x]) && hash[x].key end inventory_object_class.new(self, hash) end |
#noop? ⇒ Boolean
True if processing of this InventoryCollection object would lead to no operations. Then we use this marker to stop processing of the InventoryCollector object very soon, to avoid a lot of unnecessary Db queries, etc.
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 759 def noop? # If this InventoryCollection doesn't do anything. it can easily happen for targeted/batched strategies. if targeted? if parent_inventory_collections.nil? && targeted_scope.primary_references.blank? && all_manager_uuids.nil? && parent_inventory_collections.blank? && custom_save_block.nil? && skeletal_primary_index.blank? # It's a noop Parent targeted InventoryCollection true elsif !parent_inventory_collections.nil? && parent_inventory_collections.all? { |x| x.targeted_scope.primary_references.blank? } && skeletal_primary_index.blank? # It's a noop Child targeted InventoryCollection true else false end elsif data.blank? && !delete_allowed? && skeletal_primary_index.blank? # If we have no data to save and delete is not allowed, we can just skip true else false end end |
#object_index_with_keys(keys, record) ⇒ String
Builds string uuid from passed Object and keys
750 751 752 753 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 750 def object_index_with_keys(keys, record) # TODO(lsmola) remove, last usage is in k8s reconnect logic build_stringified_reference_for_record(record, keys) end |
#parallel_safe? ⇒ Boolean
Returns true if we are using a saver strategy that allows saving in parallel processes.
579 580 581 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 579 def parallel_safe? @parallel_safe_cache ||= i(concurrent_safe concurrent_safe_batch).include?(saver_strategy) end |
#process_saver_strategy(saver_strategy) ⇒ Symbol
Processes passed saver strategy
495 496 497 498 499 500 501 502 503 504 505 506 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 495 def process_saver_strategy(saver_strategy) return :default unless saver_strategy saver_strategy = saver_strategy.to_sym case saver_strategy when :default, :batch, :concurrent_safe, :concurrent_safe_batch saver_strategy else raise "Unknown InventoryCollection saver strategy: :#{saver_strategy}, allowed strategies are "\ ":default, :batch, :concurrent_safe and :concurrent_safe_batch" end end |
#process_strategy(strategy_name) ⇒ Symbol
Processes passed strategy, modifies :data_collection_finalized and :saved attributes for db only strategies
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 512 def process_strategy(strategy_name) self.data_collection_finalized = false return unless strategy_name strategy_name = strategy_name.to_sym case strategy_name when :local_db_cache_all self.data_collection_finalized = true self.saved = true when :local_db_find_references self.saved = true when :local_db_find_missing_references else raise "Unknown InventoryCollection strategy: :#{strategy_name}, allowed strategies are :local_db_cache_all, "\ ":local_db_find_references and :local_db_find_missing_references." end strategy_name end |
#saveable? ⇒ Boolean
Returns true if all dependencies have all data persisted.
574 575 576 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 574 def saveable? dependencies.all?(&:saved?) end |
#saved? ⇒ Boolean
Returns true if the whole InventoryCollection object has all data persisted.
569 570 571 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 569 def saved? saved end |
#store_created_records(records) ⇒ Object
Caches what records were created, for later use, e.g. post provision behavior
473 474 475 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 473 def store_created_records(records) @created_records.concat(records_identities(records)) end |
#store_deleted_records(records) ⇒ Object
Caches what records were deleted/soft-deleted, for later use, e.g. post provision behavior
487 488 489 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 487 def store_deleted_records(records) @deleted_records.concat(records_identities(records)) end |
#store_updated_records(records) ⇒ Object
Caches what records were updated, for later use, e.g. post provision behavior
480 481 482 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 480 def store_updated_records(records) @updated_records.concat(records_identities(records)) end |
#supports_created_at? ⇒ Boolean
Returns true if the model_class has created_at column.
606 607 608 609 610 611 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 606 def supports_created_at? if @supports_created_at_cache.nil? @supports_created_at_cache = (model_class.column_names.include?("created_at") && ActiveRecord::Base.) end @supports_created_at_cache end |
#supports_created_on? ⇒ Boolean
Returns true if the model_class has created_on column.
590 591 592 593 594 595 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 590 def supports_created_on? if @supports_created_on_cache.nil? @supports_created_on_cache = (model_class.column_names.include?("created_on") && ActiveRecord::Base.) end @supports_created_on_cache end |
#supports_resource_timestamp? ⇒ Boolean
Returns true if the model_class has resource_timestamp column.
632 633 634 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 632 def ||= model_class.column_names.include?("resource_timestamp") end |
#supports_resource_timestamps? ⇒ Boolean
Returns true if the model_class has resource_timestamps column.
627 628 629 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 627 def ||= model_class.column_names.include?("resource_timestamps") end |
#supports_resource_timestamps_max? ⇒ Boolean
Returns true if the model_class has resource_timestamps_max column.
622 623 624 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 622 def ||= model_class.column_names.include?("resource_timestamps_max") end |
#supports_resource_version? ⇒ Boolean
Returns true if the model_class has resource_version column.
647 648 649 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 647 def supports_resource_version? @supports_resource_version_cache ||= model_class.column_names.include?("resource_version") end |
#supports_resource_versions? ⇒ Boolean
Returns true if the model_class has resource_versions column.
642 643 644 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 642 def supports_resource_versions? @supports_resource_versions_cache ||= model_class.column_names.include?("resource_versions") end |
#supports_resource_versions_max? ⇒ Boolean
Returns true if the model_class has resource_versions_max column.
637 638 639 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 637 def supports_resource_versions_max? @supports_resource_versions_max_cache ||= model_class.column_names.include?("resource_versions_max") end |
#supports_sti? ⇒ Boolean
Returns true if the model_class supports STI.
584 585 586 587 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 584 def supports_sti? @supports_sti_cache = model_class.column_names.include?("type") if @supports_sti_cache.nil? @supports_sti_cache end |
#supports_updated_at? ⇒ Boolean
Returns true if the model_class has updated_at column.
614 615 616 617 618 619 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 614 def supports_updated_at? if @supports_updated_at_cache.nil? @supports_updated_at_cache = (model_class.column_names.include?("updated_at") && ActiveRecord::Base.) end @supports_updated_at_cache end |
#supports_updated_on? ⇒ Boolean
Returns true if the model_class has updated_on column.
598 599 600 601 602 603 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 598 def supports_updated_on? if @supports_updated_on_cache.nil? @supports_updated_on_cache = (model_class.column_names.include?("updated_on") && ActiveRecord::Base.) end @supports_updated_on_cache end |
#targeted? ⇒ Boolean
Returns true is processing of this InventoryCollection will be in targeted mode.
783 784 785 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 783 def targeted? targeted end |
#targeted_arel_default ⇒ InventoryRefresh::ApplicationRecordIterator
Builds targeted query limiting the results by the :references defined in parent_inventory_collections
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 1047 def targeted_arel_default if parent_inventory_collections.collect { |x| x.model_class.base_class }.uniq.count > 1 raise "Multiple :parent_inventory_collections with different base class are not supported by default. Write "\ ":targeted_arel manually, or separate [#{self}] into 2 InventoryCollection objects." end parent_collection = parent_inventory_collections.first references = parent_inventory_collections.map { |x| x.targeted_scope.primary_references }.reduce({}, :merge!) parent_collection.targeted_iterator_for(references, full_collection_for_comparison) end |
#targeted_iterator_for(references, query = nil) ⇒ InventoryRefresh::ApplicationRecordIterator
Returns iterator for the passed references and a query
1085 1086 1087 1088 1089 1090 1091 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 1085 def targeted_iterator_for(references, query = nil) InventoryRefresh::ApplicationRecordIterator.new( :inventory_collection => self, :manager_uuids_set => references, :query => query ) end |
#targeted_selection_for(references) ⇒ String
Builds a multiselection conditions like (table1.a = a1 AND table2.b = b1) OR (table1.a = a2 AND table2.b = b2) for passed references
1076 1077 1078 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 1076 def targeted_selection_for(references) build_multi_selection_condition(transform_references_to_hashes(references)) end |
#to_s ⇒ String
Returns a concise form of the inventoryCollection for easy logging.
979 980 981 982 983 984 985 986 987 988 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 979 def to_s whitelist = ", whitelist: [#{attributes_whitelist.to_a.join(", ")}]" if attributes_whitelist.present? blacklist = ", blacklist: [#{attributes_blacklist.to_a.join(", ")}]" if attributes_blacklist.present? strategy_name = ", strategy: #{strategy}" if strategy name = model_class || association "InventoryCollection:<#{name}>#{whitelist}#{blacklist}#{strategy_name}" end |
#transform_references_to_hashes(references) ⇒ Array<Hash>
Gets targeted references and transforms them into list of hashes
1062 1063 1064 1065 1066 1067 1068 1069 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 1062 def transform_references_to_hashes(references) if references.kind_of?(Array) # Sliced InventoryRefresh::Inventorycollection::TargetedScope references.map { |x| x.second.full_reference } else references.values.map(&:full_reference) end end |
#unique_index_columns ⇒ Array<Symbol>
Returns all columns that are part of the best fit unique index.
652 653 654 655 656 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 652 def unique_index_columns return @unique_index_columns if @unique_index_columns @unique_index_columns = unique_index_for(unique_index_keys).columns.map(&:to_sym) end |
#unique_index_for(keys) ⇒ ActiveRecord::ConnectionAdapters::IndexDefinition
Finds an index that fits the list of columns (keys) the best
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 681 def unique_index_for(keys) @unique_index_for_keys_cache ||= {} @unique_index_for_keys_cache[keys] if @unique_index_for_keys_cache[keys] # Find all uniq indexes that that are covering our keys uniq_key_candidates = unique_indexes.each_with_object([]) { |i, obj| obj << i if (keys - i.columns.map(&:to_sym)).empty? } if @unique_indexes_cache.blank? raise "#{self} and its table #{model_class.table_name} must have a unique index defined "\ "covering columns #{keys} to be able to use saver_strategy :concurrent_safe or :concurrent_safe_batch." end # Take the uniq key having the least number of columns @unique_index_for_keys_cache[keys] = uniq_key_candidates.min_by { |x| x.columns.count } end |
#unique_index_keys ⇒ Object
658 659 660 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 658 def unique_index_keys @unique_index_keys ||= manager_ref_to_cols.map(&:to_sym) end |
#unique_indexes ⇒ Array<ActiveRecord::ConnectionAdapters::IndexDefinition>
Returns array of all unique indexes known to model.
663 664 665 666 667 668 669 670 671 672 673 674 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 663 def unique_indexes @unique_indexes_cache if @unique_indexes_cache @unique_indexes_cache = model_class.connection.indexes(model_class.table_name).select(&:unique) if @unique_indexes_cache.blank? raise "#{self} and its table #{model_class.table_name} must have a unique index defined, to"\ " be able to use saver_strategy :concurrent_safe or :concurrent_safe_batch." end @unique_indexes_cache end |
#update_only? ⇒ Boolean
Returns true means we want to only update data.
549 550 551 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 549 def update_only? update_only end |
#use_ar_object? ⇒ Boolean
Returns true means we want to use ActiveRecord object for writing attributes and we want to perform casting on all columns.
539 540 541 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 539 def use_ar_object? use_ar_object end |
#whitelist_attributes!(attributes) ⇒ Array<Symbol>
Add passed attributes to whitelist. The manager_ref attributes always needs to be in the white list, otherwise we will not be able to identify theinventory_object. We do not automatically add attributes causing fixed dependencies, so beware that without them, you won’t be able to create the record.
875 876 877 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 875 def whitelist_attributes!(attributes) self.attributes_whitelist += attributes + (fixed_attributes + internal_attributes) end |