Class: ActsAsTable::RecordModelClassMethods::SetValue
- Inherits:
-
FindOrInitializeBy
- Object
- FindOrInitializeBy
- ActsAsTable::RecordModelClassMethods::SetValue
- Defined in:
- app/models/concerns/acts_as_table/record_model_class_methods.rb
Overview
Set the new value for the given record using the given options.
Instance Attribute Summary collapse
-
#new_value_by_record_model_and_value_provider ⇒ Hash<ActsAsTable::RecordModel, Hash<ActsAsTable::ValueProvider::InstanceMethods, Object>>
readonly
The new values by ActsAsTable record model and value provider for this ActsAsTable "destructive" traversal.
-
#options ⇒ Hash<Symbol, Object>
readonly
The options for this ActsAsTable "destructive" traversal.
Instance Method Summary collapse
-
#call(record_model, base = nil) ⇒ ActsAsTable::ValueProvider::WrappedValue
Invokes the traversal.
-
#initialize(new_value_by_record_model_and_value_provider = {}, **options) ⇒ ActsAsTable::RecordModelClassMethods::SetValue
constructor
Returns a new ActsAsTable "destructive" traversal.
Methods inherited from FindOrInitializeBy
Constructor Details
#initialize(new_value_by_record_model_and_value_provider = {}, **options) ⇒ ActsAsTable::RecordModelClassMethods::SetValue
Returns a new ActsAsTable "destructive" traversal.
448 449 450 451 452 453 454 |
# File 'app/models/concerns/acts_as_table/record_model_class_methods.rb', line 448 def initialize(new_value_by_record_model_and_value_provider = {}, **) super() .assert_valid_keys(:default) @new_value_by_record_model_and_value_provider, @options = new_value_by_record_model_and_value_provider, .dup end |
Instance Attribute Details
#new_value_by_record_model_and_value_provider ⇒ Hash<ActsAsTable::RecordModel, Hash<ActsAsTable::ValueProvider::InstanceMethods, Object>> (readonly)
The new values by ActsAsTable record model and value provider for this ActsAsTable "destructive" traversal.
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 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 |
# File 'app/models/concerns/acts_as_table/record_model_class_methods.rb', line 439 class SetValue < FindOrInitializeBy attr_reader :new_value_by_record_model_and_value_provider, :options # Returns a new ActsAsTable "destructive" traversal. # # @param [Hash<ActsAsTable::RecordModel, Hash<ActsAsTable::ValueProvider::InstanceMethods, Object>>] new_value_by_record_model_and_value_provider # @param [Hash<Symbol, Object>] options # @option options [Boolean] :default # @return [ActsAsTable::RecordModelClassMethods::SetValue] def initialize(new_value_by_record_model_and_value_provider = {}, **) super() .assert_valid_keys(:default) @new_value_by_record_model_and_value_provider, @options = new_value_by_record_model_and_value_provider, .dup end # Invokes the traversal. # # @param [ActsAsTable::RecordModel] record_model # @param [ActiveRecord::Base, nil] base # @return [ActsAsTable::ValueProvider::WrappedValue] # @raise [ArgumentError] If the name of a class for a given record does not match the class name for the corresponding ActsAsTable record model. def call(record_model, base = nil) ActsAsTable.adapter.wrap_value_for(record_model, base, nil, self.inject({}, record_model, base)) end protected # @param [Hash<ActsAsTable::RecordModel, Array<Object>>] acc # @param [ActsAsTable::Path] path # @yieldparam [Hash<ActsAsTable::RecordModel, Array<Object>>] acc # @yieldparam [ActsAsTable::Path] path # @yieldreturn [Hash<ActsAsTable::RecordModel, Array<Object>>] # @return [Hash<ActsAsTable::RecordModel, Array<Object>>] # @raise [ArgumentError] def _around_inject(acc, path, &block) # @return [ActsAsTable::RecordModel] source_record_model = path..dig(:data, :source_record_model) # @return [ActsAsTable::RecordModel] target_record_model = path..dig(:data, :target_record_model) if path.collect(&:options).any? { || .dig(:data, :source_record_model) == target_record_model } acc else # @return [Hash<ActsAsTable::RecordModel, Array<Object>>] new_acc = block.call(acc) unless source_record_model.nil? if new_acc[target_record_model][1].changed? new_acc[source_record_model][1].changed! end end new_acc end end # @param [Hash<ActsAsTable::RecordModel, Array<Object>>] acc # @param [ActsAsTable::RecordModel] record_model # @return [ActiveRecord::Base, nil] def _at(acc, record_model) acc.key?(record_model) ? acc[record_model][0] : nil end # @param [Hash<ActsAsTable::RecordModel, Array<Object>>] acc # @param [ActsAsTable::RecordModel] record_model # @param [ActiveRecord::Base, nil] base # @param [Hash<Symbol, Object>] options # @option options [#call] :find_or_initialize_by # @option options [#call] :new # @return [Array<Object>] # @raise [ArgumentError] def _find_or_initialize(acc, record_model, base = nil, **) acc[record_model] ||= begin # @return [Hash<ActsAsTable::ValueProvider::InstanceMethods, Object>, nil] orig_value_by_value_provider = @new_value_by_record_model_and_value_provider.try(:[], record_model) unless (primary_key = record_model.primary_keys.first).nil? || (base_id = orig_value_by_value_provider.try(:[], primary_key)).nil? # @return [ActiveRecord::Base] # @raise [ActiveRecord::RecordNotFound] base = [:find_or_initialize_by].call({ primary_key.method_name => base_id, }) # @return [Hash<ActsAsTable::ValueProvider::InstanceMethods, Object>, nil] new_value_by_value_provider = orig_value_by_value_provider.try(:each_pair).try(:all?) { |pair| (pair[0] == primary_key) || pair[0].column_model.nil? || pair[1].nil? } ? orig_value_by_value_provider.try(:delete_if) { |value_provider, value| value_provider != primary_key } : orig_value_by_value_provider.try(:delete_if) { |value_provider, value| value_provider.column_model.nil? } [ base, ActsAsTable.adapter.set_value_for(record_model, base, new_value_by_value_provider, **@options), ] else if base.nil? base = [:new].call end [ base, ActsAsTable.adapter.set_value_for(record_model, base, orig_value_by_value_provider, **@options), ] end end [acc, base] end end |
#options ⇒ Hash<Symbol, Object> (readonly)
The options for this ActsAsTable "destructive" traversal.
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 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 |
# File 'app/models/concerns/acts_as_table/record_model_class_methods.rb', line 439 class SetValue < FindOrInitializeBy attr_reader :new_value_by_record_model_and_value_provider, :options # Returns a new ActsAsTable "destructive" traversal. # # @param [Hash<ActsAsTable::RecordModel, Hash<ActsAsTable::ValueProvider::InstanceMethods, Object>>] new_value_by_record_model_and_value_provider # @param [Hash<Symbol, Object>] options # @option options [Boolean] :default # @return [ActsAsTable::RecordModelClassMethods::SetValue] def initialize(new_value_by_record_model_and_value_provider = {}, **) super() .assert_valid_keys(:default) @new_value_by_record_model_and_value_provider, @options = new_value_by_record_model_and_value_provider, .dup end # Invokes the traversal. # # @param [ActsAsTable::RecordModel] record_model # @param [ActiveRecord::Base, nil] base # @return [ActsAsTable::ValueProvider::WrappedValue] # @raise [ArgumentError] If the name of a class for a given record does not match the class name for the corresponding ActsAsTable record model. def call(record_model, base = nil) ActsAsTable.adapter.wrap_value_for(record_model, base, nil, self.inject({}, record_model, base)) end protected # @param [Hash<ActsAsTable::RecordModel, Array<Object>>] acc # @param [ActsAsTable::Path] path # @yieldparam [Hash<ActsAsTable::RecordModel, Array<Object>>] acc # @yieldparam [ActsAsTable::Path] path # @yieldreturn [Hash<ActsAsTable::RecordModel, Array<Object>>] # @return [Hash<ActsAsTable::RecordModel, Array<Object>>] # @raise [ArgumentError] def _around_inject(acc, path, &block) # @return [ActsAsTable::RecordModel] source_record_model = path..dig(:data, :source_record_model) # @return [ActsAsTable::RecordModel] target_record_model = path..dig(:data, :target_record_model) if path.collect(&:options).any? { || .dig(:data, :source_record_model) == target_record_model } acc else # @return [Hash<ActsAsTable::RecordModel, Array<Object>>] new_acc = block.call(acc) unless source_record_model.nil? if new_acc[target_record_model][1].changed? new_acc[source_record_model][1].changed! end end new_acc end end # @param [Hash<ActsAsTable::RecordModel, Array<Object>>] acc # @param [ActsAsTable::RecordModel] record_model # @return [ActiveRecord::Base, nil] def _at(acc, record_model) acc.key?(record_model) ? acc[record_model][0] : nil end # @param [Hash<ActsAsTable::RecordModel, Array<Object>>] acc # @param [ActsAsTable::RecordModel] record_model # @param [ActiveRecord::Base, nil] base # @param [Hash<Symbol, Object>] options # @option options [#call] :find_or_initialize_by # @option options [#call] :new # @return [Array<Object>] # @raise [ArgumentError] def _find_or_initialize(acc, record_model, base = nil, **) acc[record_model] ||= begin # @return [Hash<ActsAsTable::ValueProvider::InstanceMethods, Object>, nil] orig_value_by_value_provider = @new_value_by_record_model_and_value_provider.try(:[], record_model) unless (primary_key = record_model.primary_keys.first).nil? || (base_id = orig_value_by_value_provider.try(:[], primary_key)).nil? # @return [ActiveRecord::Base] # @raise [ActiveRecord::RecordNotFound] base = [:find_or_initialize_by].call({ primary_key.method_name => base_id, }) # @return [Hash<ActsAsTable::ValueProvider::InstanceMethods, Object>, nil] new_value_by_value_provider = orig_value_by_value_provider.try(:each_pair).try(:all?) { |pair| (pair[0] == primary_key) || pair[0].column_model.nil? || pair[1].nil? } ? orig_value_by_value_provider.try(:delete_if) { |value_provider, value| value_provider != primary_key } : orig_value_by_value_provider.try(:delete_if) { |value_provider, value| value_provider.column_model.nil? } [ base, ActsAsTable.adapter.set_value_for(record_model, base, new_value_by_value_provider, **@options), ] else if base.nil? base = [:new].call end [ base, ActsAsTable.adapter.set_value_for(record_model, base, orig_value_by_value_provider, **@options), ] end end [acc, base] end end |
Instance Method Details
#call(record_model, base = nil) ⇒ ActsAsTable::ValueProvider::WrappedValue
Invokes the traversal.
462 463 464 |
# File 'app/models/concerns/acts_as_table/record_model_class_methods.rb', line 462 def call(record_model, base = nil) ActsAsTable.adapter.wrap_value_for(record_model, base, nil, self.inject({}, record_model, base)) end |