Created
October 2, 2017 17:09
-
-
Save zernie/1fc5ed4accd324ecb6e7b86b97b1db3a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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