Override `created_at` value with `planted_at` value when available
parent
87c22d4a96
commit
696350343b
|
@ -1,6 +1,24 @@
|
||||||
class BasePointSerializer < ApplicationSerializer
|
class BasePointSerializer < ApplicationSerializer
|
||||||
attributes :device_id, :name, :pointer_type, :meta, :x, :y, :z
|
attributes :device_id, :name, :pointer_type, :meta, :x, :y, :z
|
||||||
|
|
||||||
|
# PROBLEM:
|
||||||
|
# * Users need a mutable way to mark a plant's creation time => `planted_at`
|
||||||
|
# * DB Admin needs to know the _real_ created_at time.
|
||||||
|
# * We can't change field names (or destroy data) that is in use by legacy devices
|
||||||
|
#
|
||||||
|
# SOLUTION:
|
||||||
|
# * Don't allow users to modify `created_at`
|
||||||
|
# * Provide `planted_at` if possible.
|
||||||
|
# * Always provide `planted_at` if it is available
|
||||||
|
# * Provide a read-only view of `created_at` if `planted_at` is `nil`
|
||||||
|
def planted_at
|
||||||
|
object.planted_at || object.created_at
|
||||||
|
end
|
||||||
|
|
||||||
|
def created_at
|
||||||
|
planted_at
|
||||||
|
end
|
||||||
|
|
||||||
def meta
|
def meta
|
||||||
object.meta || {}
|
object.meta || {}
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,10 +1,6 @@
|
||||||
class PlantSerializer < BasePointSerializer
|
class PlantSerializer < BasePointSerializer
|
||||||
attributes :openfarm_slug, :plant_stage, :planted_at, :radius, :meta
|
attributes :openfarm_slug, :plant_stage, :planted_at, :radius, :meta
|
||||||
|
|
||||||
def planted_at
|
|
||||||
object.planted_at || object.created_at
|
|
||||||
end
|
|
||||||
|
|
||||||
def x
|
def x
|
||||||
object.x.round
|
object.x.round
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue