Skip to content

Instantly share code, notes, and snippets.

@varyonic
Created February 3, 2021 19:37
Show Gist options
  • Save varyonic/71a03229e8888f5a4214d10578849a61 to your computer and use it in GitHub Desktop.
Save varyonic/71a03229e8888f5a4214d10578849a61 to your computer and use it in GitHub Desktop.
object that plays well with formtastic
# See https://dondoh-blog.tumblr.com/post/4142258573/formtastic-without-activerecord
# -------------------------------------------------------------------------------
# Faux model object that plays well with formtastic; this is used for example in
# a contact form which generates a request to salesforce
# -------------------------------------------------------------------------------
class FormtasticFauxModel
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
# Subclass may provide a types hash. Any attributes not listed will
# default to string.
# self.types = {
# :description => :text,
# :state => {:type => :string, :limit => 2}
# :country => :string,
# :newsletter_opt_in => :boolean,
# }
class << self
attr_accessor :types
end
self.types = {}
# So the controller can say "@contact = Contact.new(params[:contact])"
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
# So form_for works correctly -- we only do "new" forms
def persisted? ; false ; end
# To provide the type information
def column_for_attribute(attr)
FauxColumnInfo.new(self.class.types[attr])
end
class FauxColumnInfo
attr_accessor :type, :limit
def initialize(type_info)
type_info ||= :string
case
when type_info.instance_of?(Hash), type_info.instance_of?(OpenStruct)
self.type = type_info[:type].to_sym
self.limit = type_info[:limit]
else
self.type = type_info.to_sym
self.limit = nil
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment