Class: Hai::Update

Inherits:
Object
  • Object
show all
Defined in:
lib/hai/update.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, context) ⇒ Update

Returns a new instance of Update.



5
6
7
8
# File 'lib/hai/update.rb', line 5

def initialize(model, context)
  @model = model
  @context = context
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



3
4
5
# File 'lib/hai/update.rb', line 3

def context
  @context
end

#modelObject (readonly)

Returns the value of attribute model.



3
4
5
# File 'lib/hai/update.rb', line 3

def model
  @model
end

Instance Method Details

#execute(id:, attributes:) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/hai/update.rb', line 10

def execute(id:, attributes:)
  record = model.find(id)
  return unauthorized_error unless check_policy(record)

  if record.update(**attributes)
    { errors: [], result: record }
  else
    { errors: record.errors.map(&:full_message), result: nil }
  end
end