Sha256: 9f3b64d9491c974eaba4fb148478f722f166a0a85a8cf62347ee2cbcd5103756

Contents?: true

Size: 960 Bytes

Versions: 1

Compression:

Stored size: 960 Bytes

Contents

# frozen_string_literal: true

$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
require "voltage"
require "active_record"

ActiveRecord::Base.establish_connection(
  adapter: "sqlite3",
  database: ":memory:"
)

ActiveRecord::Schema.define(version: 0) do
  create_table :things do |t|
    t.string :name
    t.timestamps null: false
  end
end

class Thing < ActiveRecord::Base
  include Voltage::ActiveRecord

  validates_presence_of :name
end

thing = Thing.new(name: "Stuff")
thing.on(:create) {|model| puts model.updated_at, model.name }
thing.on(:update) {|model| puts model.updated_at, model.name }
thing.on(:remove) {|model| puts model.destroyed? }
thing.on(:validation) {|model| p model.errors.full_messages }

thing.save!
#=> 2013-01-26 10:32:39 -0200
#=> Stuff

thing.update_attributes(name: "Updated stuff")
#=> 2013-01-26 10:33:11 -0200
#=> Updated stuff

thing.update_attributes(name: nil)
#=> ["Name can"t be blank"]

thing.destroy
#=> true

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
voltage-0.1.0 examples/activerecord_model.rb