Last active
February 6, 2019 05:26
-
-
Save pocari/cb06e78149597ec1678b2c2360c94839 to your computer and use it in GitHub Desktop.
rails form object
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 CompanyRegistrationForm | |
include ActiveModel::Model | |
attr_accessor :name, :address | |
attr_reader :employees_attributes | |
attr_reader :president_attributes | |
def company | |
@company ||= Company.new | |
end | |
def employees | |
@employees_attributes ||= [Employee.new] | |
end | |
def employees_attributes=(attributes) | |
@employees_attributes = attributes.map {|attr| | |
Employee.new(attr) | |
} | |
end | |
def president | |
@president_attributes ||= president.new | |
end | |
def president_attributes=(attributes) | |
@president_attributes = President.new(attributes.first) | |
end | |
def save | |
company.assign_attributes(company_params) | |
build_asscociations | |
company.save | |
end | |
private | |
def company_params | |
{ | |
name: name, | |
address: address | |
} | |
end | |
def build_asscociations | |
company.employees << employees | |
company.president = president | |
end | |
def validate_something | |
# Do something | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment