Skip to content

Instantly share code, notes, and snippets.

@zernie
Created October 2, 2017 17:09
Show Gist options
  • Save zernie/1fc5ed4accd324ecb6e7b86b97b1db3a to your computer and use it in GitHub Desktop.
Save zernie/1fc5ed4accd324ecb6e7b86b97b1db3a to your computer and use it in GitHub Desktop.
require 'ostruct'
class BaseForm
include ActiveModel::Model
attr_reader :attributes,
:attributes_hash,
:current_user,
:params,
:params_hash,
:model
delegate :id,
:model_name,
:new_record?,
:persisted?,
to: :model
def initialize(current_user, params, attributes = {})
@current_user = current_user
@params_hash = params
@params = OpenStruct.new(params)
@attributes_hash = attributes.to_h.symbolize_keys
@attributes = OpenStruct.new(attributes)
@model = setup_model
prepopulate
end
def call
raise NotImplementedError
end
def url
self
end
def valid?
super && model.valid?
end
private
def setup_model
raise NotImplementedError
end
def prepopulate
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment