Class: EacRailsUtils::Models::TablelessAssociations::HasManyForActiveModelAssociation
- Inherits:
-
ActiveRecord::Associations::HasManyAssociation
- Object
- ActiveRecord::Associations::HasManyAssociation
- EacRailsUtils::Models::TablelessAssociations::HasManyForActiveModelAssociation
- Defined in:
- lib/eac_rails_utils/models/tableless_associations/has_many_for_active_model_association.rb
Instance Method Summary collapse
-
#concat(*records) ⇒ Object
no need transaction.
-
#empty? ⇒ Boolean
not support counter_cache.
-
#find_target? ⇒ Boolean
remove conditions: owner.new_record?, foreign_key_present?.
-
#null_scope? ⇒ Boolean
no dependent action.
-
#replace(other_array) ⇒ Object
full replace simplely.
Instance Method Details
#concat(*records) ⇒ Object
no need transaction
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/eac_rails_utils/models/tableless_associations/has_many_for_active_model_association.rb', line 48 def concat(*records) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength load_target flatten_records = records.flatten flatten_records.each { |val| raise_on_type_mismatch!(val) } target_ids = reflection.[:target_ids] owner[target_ids] ||= [] owner[target_ids].concat(flatten_records.map(&:id)) flatten_records.each do |record| if (index = @target.index(record)) @target[index] = record else @target << record end end target end |
#empty? ⇒ Boolean
not support counter_cache
18 19 20 21 22 23 24 |
# File 'lib/eac_rails_utils/models/tableless_associations/has_many_for_active_model_association.rb', line 18 def empty? if loaded? size.zero? else @target.blank? && !scope.exists? end end |
#find_target? ⇒ Boolean
remove conditions: owner.new_record?, foreign_key_present?
8 9 10 |
# File 'lib/eac_rails_utils/models/tableless_associations/has_many_for_active_model_association.rb', line 8 def find_target? !loaded? && klass end |
#null_scope? ⇒ Boolean
no dependent action
13 14 15 |
# File 'lib/eac_rails_utils/models/tableless_associations/has_many_for_active_model_association.rb', line 13 def null_scope? false end |
#replace(other_array) ⇒ Object
full replace simplely
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/eac_rails_utils/models/tableless_associations/has_many_for_active_model_association.rb', line 27 def replace(other_array) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength original_target = load_target.dup other_array.each { |val| raise_on_type_mismatch!(val) } target_ids = reflection.[:target_ids] owner[target_ids] = other_array.map(&:id) old_records = original_target - other_array old_records.each do |record| @target.delete(record) end other_array.each do |record| if (index = @target.index(record)) @target[index] = record else @target << record end end end |