Slide 9
Slide 9 text
1 class SignUpForm!
2 include ActiveModel::Model!
3 !
4 attr_reader :user!
5 attr_reader :items!
6 !
7 def initialize(params = {})!
8 @user = User.new(user_params(params))!
9 @items = build_items(items_params(params))!
10 end!
11 !
12 def build_items(items_attributes)!
13 items_attributes.map do |attributes|!
14 Item.new(attributes)!
15 end!
16 end!
17 !
18 def user_params(params)!
19 params.fetch(:user, {}).slice(:email).to_hash!
20 end!
21 !
22 def items_params(params)!
23 params.fetch(:user, {}).fetch(:items, {}).values.map do |item_hash|!
24 item_hash.slice(:title).to_hash!
25 end!
26 end!
27 !
28 end