Class: GraphqlDevise::Model::WithEmailUpdater

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql_devise/model/with_email_updater.rb

Instance Method Summary collapse

Constructor Details

#initialize(resource, attributes) ⇒ WithEmailUpdater

Returns a new instance of WithEmailUpdater.



6
7
8
9
# File 'lib/graphql_devise/model/with_email_updater.rb', line 6

def initialize(resource, attributes)
  @attributes = attributes.with_indifferent_access
  @resource   = resource
end

Instance Method Details

#callObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/graphql_devise/model/with_email_updater.rb', line 11

def call
  resource_attributes = @attributes.except(:confirmation_url)
  return @resource.update(resource_attributes) unless requires_reconfirmation?(resource_attributes)

  @resource.assign_attributes(resource_attributes)

  if @resource.email == email_in_database
    @resource.save
  elsif required_reconfirm_attributes?
    return false unless @resource.valid?

    store_unconfirmed_email
    saved = @resource.save
    send_confirmation_instructions(saved)

    saved
  else
    raise(
      ::GraphqlDevise::Error,
      'Method `update_with_email` requires attribute `confirmation_url` for email reconfirmation to work'
    )
  end
end