Skip to content

Instantly share code, notes, and snippets.

@ivanyv
Created July 21, 2016 23:42
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 ivanyv/ecee931b94066d147ba7dd1d66ad1aec to your computer and use it in GitHub Desktop.
Save ivanyv/ecee931b94066d147ba7dd1d66ad1aec to your computer and use it in GitHub Desktop.
Easy form/service objects and decorators
# app/models/application_form.rb
module ApplicationForm
extend ActiveSupport::Concern
class_methods do
def model_name
# Project::Form.model_name => Project.model_name
name.deconstantize.constantize.model_name
end
end
end
# app/models/project/form.rb
class Project::Form < Project
include ApplicationForm
validates :title, presence: true
end
# app/models/project.rb
class Project < ActiveRecord::Base
end
# app/controllers/projects_controller.rb
class ProjectsController < ApplicationController
def new
@project = Project::Form.new
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment