Skip to content

Instantly share code, notes, and snippets.

@elskwid
Created May 8, 2013 04:46
Show Gist options
  • Save elskwid/5538254 to your computer and use it in GitHub Desktop.
Save elskwid/5538254 to your computer and use it in GitHub Desktop.
Protomodel
# Example of prototyping models with a mix of ActiveRecord and Virtus
#
# Useful with large models where some fields are in flux during spiking/dev
# and you don't want or need to run migrations to try ideas out.
class A < ActiveRecord::Base
# ...
# belongs_to
# has_many
# ...
class Proto
include Virtus
attribute :amount, Float
attribute :name, String
attribute :foo, String
end
# helper
def proto
@proto ||= Proto.new
end
# delegate messages to the proto instance
def method_missing(method, *args, &block)
proto.respond_to?(method) ? proto.send(method, *args, &block) : super
end
# ensure the model quacks
def respond_to_missing?(method, include_private = false)
proto.respond_to?(method, include_private) || super
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment