Created
July 21, 2016 23:42
-
-
Save ivanyv/ecee931b94066d147ba7dd1d66ad1aec to your computer and use it in GitHub Desktop.
Easy form/service objects and decorators
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
# 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 |
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
# app/models/project/form.rb | |
class Project::Form < Project | |
include ApplicationForm | |
validates :title, presence: true | |
end |
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
# app/models/project.rb | |
class Project < ActiveRecord::Base | |
end |
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
# 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