Skip to content

Instantly share code, notes, and snippets.

@flash-gordon
Last active October 5, 2016 21:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flash-gordon/6fb8dfcb235149f377a51181555294cb to your computer and use it in GitHub Desktop.
Save flash-gordon/6fb8dfcb235149f377a51181555294cb to your computer and use it in GitHub Desktop.
module CommandPlugins
module ActiveRecordTimestamps
class HashWithTimestamps
def initialize(input)
@input = input
end
def [](*args)
tuple = @input[*args]
now = Time.zone.now
if tuple[:created_at]
tuple[:updated_at] = now
else
tuple[:created_at] = now
tuple[:updated_at] = now
end
tuple
end
end
def self.included(klass)
super
klass.input HashWithTimestamps.new(klass.input)
end
end
end
ROM.plugins do
adapter :sql do
register :timestamps, CommandPlugins::ActiveRecordTimestamps, type: :command
end
end
class CreateAccount < ROM::Commands::Create[:sql]
register_as :create
relation :accounts
result :one
use :timestamps
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment