1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
ActiveRecord::Schema.define(:version => 4) do

  create_table "comments", :force => true do |t|
    t.integer  "domain_id"
    t.string   "username"
    t.text     "comment"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "domains", :force => true do |t|
    t.integer  "task_id"
    t.string   "domain_name"
    t.string   "domain_provider"
    t.string   "domain_tld"
    t.string   "term_length"
    t.boolean  "ssl_required"
    t.text     "comments"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "requesters", :force => true do |t|
    t.string   "username"
    t.string   "email"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "tasks", :force => true do |t|
    t.integer  "requester_id"
    t.string   "username"
    t.string   "email"
    t.string   "client"
    t.string   "job_number"
    t.string   "email_list"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

end

  def new
    @requester = Requester.new
	@task = @requester.tasks.build
	@task.domains.build
	
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @requester }
      
    end
  end

<h1>New requester</h1>

<%= error_messages_for :requester %>

<% form_for(@requester) do |f| %>
 <fieldset>
  <legend>Information</legend>
   <ol>
    <li>
     <label for="user">user <em>*</em></label>
     <%= f.text_field :username %>
    </li>
    <li>
     <label for="job-number">Email <em>*</em></label>
     <%= f.text_field :email %>
     </li>
    </ol>
  </fieldset>  
  <div id="tasks">
  <%= render :partial => 'task', :collection => @requester.tasks %>
  </div>
  <%= add_domain_link "Add a domain" %>
  <div id="domains">
  <%= render :partial => 'domain', :collection => @task.domains %>
  </div>

  <p>
    <%= f.submit "Create" %>
  </p>
<% end %>

<%= link_to 'Back', requesters_path %>


-- _task.html.erb
<% new_or_existing = task.new_record? ? 'new' : 'existing' %>
<% prefix = "requester[#{new_or_existing}_task_attributes][]" %>
<% fields_for prefix, task do |task_form| %>
<fieldset>
<legend>Information</legend>
    <ol>
        <li>
            <label for="user">Client <em>*</em></label>
            <%= task_form.text_field :client %>
        </li>
        <li>
            <label for="job-number">Job # <em>*</em></label>
            <%= task_form.text_field :job_number %>
        </li>
        <li>
            <label for="email_list">emails <em>*</em></label>
            <%= task_form.text_area :email_list, :rows => 10, :columns => 10 %>
        </li>
	</ol>
</fieldset>
<% end %>

-- _domain.html.erb
<div class="domain">
	<% new_or_existing = domain.new_record? ? 'new' : 'existing' %>
	<% prefix = "requester[#{new_or_existing}_domain_attributes][]" %>
	<% fields_for prefix, domain do |domain_form| %>
	<fieldset>
            <legend>Domain(s)</legend>
            
        	
            <%= link_to_function "remove" , "$(this).parents('.domain').remove()" %>
            <ol>
                <li>
                    <label for="name">Domain Name <em>*</em></label>
                    <%= domain_form.text_field :domain_name, :size => 20 %>
                </li>
                <li>
                    <label for="name">Domain Provider <em>*</em></label>
                    <%= domain_form.select :domain_provider, %w{Godaddy Register} %>
                </li>
                <li>
                    <label for="name">Domain TLD <em>*</em></label>
                    <%= domain_form.select :domain_tld, %w{.com .net .org .biz} %>
                </li>
                <li>
                    <label for="term">Term Length: <em>*</em></label>
                    <%= domain_form.select :term_length, %w{1 6 12 24} %>
                </li>
                <li>
                <fieldset>
                    <legend>SSL Cert? <em>*</em></legend>
                    <%= domain_form.check_box :ssl_required %>
                </fieldset>
                </li>
            </ol>
    </fieldset>   
    <% end %>
</div>

class Requester < ActiveRecord::Base
	has_many :tasks
	#has_many :domains, :through => :tasks
	
	validates_presence_of :username
	after_update :save_tasks
	
	def existing_domain_attributes=(domain_attributes)
		domains.reject(&:new_record?).each do |domain|
			attributes = domain_attributes[domain.id.to_s]
			if attributes
				domain.attributes = attributes
			else
				domains.delete(domain)
			end
		end
	end
	
	def new_task_attributes=(task_attributes)
		task_attributes.each do |attributes|
			tasks.build(attributes)
		end
	end
	
	def new_domain_attributes=(domain_attributes)
		domain_attributes.each do |attributes|
			domains.build(attributes)
		end
	end

	def save_tasks
		tasks.each do |task|
			task.save(false)
		end
	end
end

class Task < ActiveRecord::Base
	belongs_to :requester
	has_many :domains
end

class Domain < ActiveRecord::Base
	belongs_to :task
	has_many :comments
end

class Comment < ActiveRecord::Base
	belongs_to :domain
end