Class: GraphQL::Models::Mutator
- Inherits:
-
Object
- Object
- GraphQL::Models::Mutator
- Defined in:
- lib/graphql/models/mutator.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
Returns the value of attribute context.
-
#field_map ⇒ Object
Returns the value of attribute field_map.
-
#inputs ⇒ Object
Returns the value of attribute inputs.
-
#root_model ⇒ Object
Returns the value of attribute root_model.
Instance Method Summary collapse
- #apply_changes ⇒ Object
- #authorize! ⇒ Object
- #changed_models ⇒ Object
-
#initialize(field_map, root_model, inputs, context) ⇒ Mutator
constructor
A new instance of Mutator.
- #save! ⇒ Object
- #validate! ⇒ Object
Constructor Details
#initialize(field_map, root_model, inputs, context) ⇒ Mutator
Returns a new instance of Mutator.
7 8 9 10 11 12 |
# File 'lib/graphql/models/mutator.rb', line 7 def initialize(field_map, root_model, inputs, context) @field_map = field_map @root_model = root_model @inputs = inputs @context = context end |
Instance Attribute Details
#context ⇒ Object
Returns the value of attribute context.
5 6 7 |
# File 'lib/graphql/models/mutator.rb', line 5 def context @context end |
#field_map ⇒ Object
Returns the value of attribute field_map.
5 6 7 |
# File 'lib/graphql/models/mutator.rb', line 5 def field_map @field_map end |
#inputs ⇒ Object
Returns the value of attribute inputs.
5 6 7 |
# File 'lib/graphql/models/mutator.rb', line 5 def inputs @inputs end |
#root_model ⇒ Object
Returns the value of attribute root_model.
5 6 7 |
# File 'lib/graphql/models/mutator.rb', line 5 def root_model @root_model end |
Instance Method Details
#apply_changes ⇒ Object
14 15 16 17 18 |
# File 'lib/graphql/models/mutator.rb', line 14 def apply_changes raise StandardError, "Called apply_changes twice for the same mutator" if @all_changes @all_changes = MutationHelpers.apply_changes(field_map, root_model, inputs, context) changed_models end |
#authorize! ⇒ Object
30 31 32 33 |
# File 'lib/graphql/models/mutator.rb', line 30 def raise StandardError, "Need to call apply_changes before #{__method__}" unless @all_changes MutationHelpers.(context, @all_changes) end |
#changed_models ⇒ Object
20 21 22 23 |
# File 'lib/graphql/models/mutator.rb', line 20 def changed_models raise StandardError, "Need to call apply_changes before #{__method__}" unless @all_changes @all_changes.map { |c| c[:model_instance] }.uniq end |
#save! ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/graphql/models/mutator.rb', line 35 def save! raise StandardError, "Need to call apply_changes before #{__method__}" unless @all_changes ActiveRecord::Base.transaction(requires_new: true) do changed_models.each do |model| next if model.destroyed? if model.marked_for_destruction? model.destroy else model.save! end end changed_models.reject(&:destroyed?) end end |
#validate! ⇒ Object
25 26 27 28 |
# File 'lib/graphql/models/mutator.rb', line 25 def validate! raise StandardError, "Need to call apply_changes before #{__method__}" unless @all_changes MutationHelpers.validate_changes(inputs, field_map, root_model, context, @all_changes) end |