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.
5 6 7 8 9 10 |
# File 'lib/graphql/models/mutator.rb', line 5 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.
3 4 5 |
# File 'lib/graphql/models/mutator.rb', line 3 def context @context end |
#field_map ⇒ Object
Returns the value of attribute field_map.
3 4 5 |
# File 'lib/graphql/models/mutator.rb', line 3 def field_map @field_map end |
#inputs ⇒ Object
Returns the value of attribute inputs.
3 4 5 |
# File 'lib/graphql/models/mutator.rb', line 3 def inputs @inputs end |
#root_model ⇒ Object
Returns the value of attribute root_model.
3 4 5 |
# File 'lib/graphql/models/mutator.rb', line 3 def root_model @root_model end |
Instance Method Details
#apply_changes ⇒ Object
12 13 14 15 16 |
# File 'lib/graphql/models/mutator.rb', line 12 def apply_changes fail StandardError.new("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
28 29 30 31 |
# File 'lib/graphql/models/mutator.rb', line 28 def fail StandardError.new("Need to call apply_changes before #{__method__}") unless @all_changes MutationHelpers.(context, @all_changes) end |
#changed_models ⇒ Object
18 19 20 21 |
# File 'lib/graphql/models/mutator.rb', line 18 def changed_models fail StandardError.new("Need to call apply_changes before #{__method__}") unless @all_changes @all_changes.map { |c| c[:model_instance] }.uniq end |
#save! ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/graphql/models/mutator.rb', line 33 def save! fail StandardError.new("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
23 24 25 26 |
# File 'lib/graphql/models/mutator.rb', line 23 def validate! fail StandardError.new("Need to call apply_changes before #{__method__}") unless @all_changes MutationHelpers.validate_changes(inputs, field_map, root_model, context, @all_changes) end |