Last active
December 14, 2015 05:40
-
-
Save bbozo/5036937 to your computer and use it in GitHub Desktop.
Form class sketch
This file contains 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
class UserController < ApplicationController | |
def create | |
@user = SomeUserForm.new(params[:user]) do |f| | |
f.user = User.new | |
f.email = Email.new | |
end | |
return unless post? | |
if @user.save | |
render "YAY" | |
end | |
end | |
def lowly_user_update | |
@user = SomeUserForm.new(params[:user]) do |f| | |
f.user = current_user | |
f.email = f.user.current_email | |
end | |
return unless put? | |
if @user.save | |
render "YAY" | |
end | |
end | |
def admin_user_update | |
@user = SomeUserForm.new(params[:user]) do |f| | |
f.user = User.find(params[:id]) | |
f.email = f.user.current_email | |
end | |
return unless put? | |
if @user.save | |
render "YAY" | |
end | |
end | |
end |
This file contains 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
class SomeUserForm < AbstractForm | |
# creates attribute accessor user | |
# delegates parent_object_id to user | |
with_option :owner => :user do |o| | |
o.field :parent_object_id | |
... | |
end | |
# attribute accessor user already exists so nothing happens | |
# delegates first_name, first_name= to user | |
with_option :owner => :user, :write => true do |o| | |
o.field :first_name | |
... | |
end | |
# creates attribute accessor email | |
# delegates :address, :address= to email | |
with_option :owner => :email do |o| | |
o.field :address, :write => true | |
end | |
# creates attr_reader / attr_writer for password, pass. confirmation and terms of service check box | |
with_options :owner => :self, :write => true do |o| | |
o.field :password | |
o.field :password_confirmation | |
o.field :terms_of_service | |
end | |
# performs validations on attribute accessors | |
validates :terms_of_service, :acceptance => true | |
validate :password_fields | |
# appends this validation to the existing model validators on User#first_name | |
validate :first_name_must_begin_with_Z | |
validate_owner :user # same thing as validate association in active record | |
def initialize(params, *args, &block) | |
# invokes block, assigns attribute hashes, raises strong parameter exception if ACL is wrong | |
super(params, *args, &block) | |
# set protected attributes on user and email | |
parent_object_id = 42 # same thing as user.parent_object_id = 42 | |
user.calculate_password_thingies! password | |
email.user_id = user.id | |
end | |
# also: | |
# | |
# attributes, attributes= works as expected | |
# | |
# valid? is valid iff the form attributes are valid, validator inheritance works | |
# on attributes shared by the form and model (first_name in this example has | |
# inherited validators from user model + validators from form class) | |
# | |
# save iterates over owners (user and email in this case) and saves them in a | |
# transaction in an "all or nothing" way | |
end |
This file contains 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
class UserController < ApplicationController | |
def new | |
@user = SomeUserForm.new do |f| | |
f.user = User.new | |
f.email = Email.new | |
end | |
end | |
def create | |
@user = SomeUserForm.new(params) do |f| | |
f.user = User.new | |
f.email = Email.new | |
end | |
# @user.valid? will be false | |
if @user.save | |
render "YAY" | |
end | |
end | |
def current_user_edit | |
@user = SomeUserForm.new do |f| | |
f.user = current_user | |
f.email = f.user.current_email | |
end | |
end | |
# + create | |
def admin_user_edit | |
@user = SomeUserForm.new do |f| | |
f.user = User.find(params[:id]) | |
f.email = f.user.current_email | |
end | |
end | |
# + create | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment