Skip to content

Instantly share code, notes, and snippets.

@mxrguspxrt
Created September 13, 2013 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mxrguspxrt/6548606 to your computer and use it in GitHub Desktop.
Save mxrguspxrt/6548606 to your computer and use it in GitHub Desktop.
class FormModel
extend ActiveModel::Naming
include ActiveModel::Conversion
include ActiveModel::Validations
def self.form_fields
@form_fields ||= []
end
def self.form_fields=(form_fields)
@form_fields = form_fields
end
def self.add_form_field(*form_fields)
form_fields = form_fields.flatten.compact
self.form_fields = form_fields
form_fields.each do |field|
delegate :"#{field}", to: :protected_model
delegate :"#{field}=", to: :protected_model
end
end
def self.add_readonly_form_fields(*form_fields)
form_fields = form_fields.flatten.compact
form_fields.each do |field|
delegate :"#{field}", to: :protected_model
end
end
attr_accessor :protected_model
def errors
protected_model.errors
end
def persisted?
protected_model.persisted?
end
def save
protected_model.update_attributes(attributes)
if valid? && protected_model.valid?
protected_model.save
else
false
end
end
def id
protected_model.id
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment