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.
6 7 8 9 10 11 |
# File 'lib/graphql/models/mutator.rb', line 6 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.
4 5 6 |
# File 'lib/graphql/models/mutator.rb', line 4 def context @context end |
#field_map ⇒ Object
Returns the value of attribute field_map.
4 5 6 |
# File 'lib/graphql/models/mutator.rb', line 4 def field_map @field_map end |
#inputs ⇒ Object
Returns the value of attribute inputs.
4 5 6 |
# File 'lib/graphql/models/mutator.rb', line 4 def inputs @inputs end |
#root_model ⇒ Object
Returns the value of attribute root_model.
4 5 6 |
# File 'lib/graphql/models/mutator.rb', line 4 def root_model @root_model end |
Instance Method Details
#apply_changes ⇒ Object
13 14 15 16 17 |
# File 'lib/graphql/models/mutator.rb', line 13 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
29 30 31 32 |
# File 'lib/graphql/models/mutator.rb', line 29 def raise StandardError, "Need to call apply_changes before #{__method__}" unless @all_changes MutationHelpers.(context, @all_changes) end |
#changed_models ⇒ Object
19 20 21 22 |
# File 'lib/graphql/models/mutator.rb', line 19 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
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/graphql/models/mutator.rb', line 34 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
24 25 26 27 |
# File 'lib/graphql/models/mutator.rb', line 24 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 |