Skip to content

Commit 2a49339

Browse files
committed
Samples and tests for Plain Old Ruby Objects with feedback demo
1 parent dc57ced commit 2a49339

File tree

3 files changed

+102
-4
lines changed

3 files changed

+102
-4
lines changed

README.md

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ Consider an example where you want to create/update a conference that can have m
2020
```ruby
2121
class ConferenceForm < ActiveForm::Base
2222
self.main_model = :conference
23-
23+
2424
attributes :name, :city
25-
25+
2626
validates :name, :city, presence: true
2727
end
2828
```
@@ -223,11 +223,11 @@ Use `fields_for` in a Rails environment to correctly setup the structure of para
223223
<%= form_for @conference_form |f| %>
224224
<%= f.text_field :name %>
225225
<%= f.text_field :city %>
226-
226+
227227
<%= f.fields_for :speakers do |s| %>
228228
<%= s.text_field :name %>
229229
<%= s.text_field :occupation %>
230-
230+
231231
<%= s.fields_for :presentation do |p| %>
232232
<%= p.text_field :topic %>
233233
<%= p.text_field :duration %>
@@ -323,6 +323,49 @@ And `app/views/conferences/_presentation_fields.html.erb` would be:
323323
</div>
324324
```
325325

326+
## Plain Old Ruby Object Forms
327+
328+
ActiveForm also can accept `ActiveModel::Model` instances as a model.
329+
330+
Let's define the Feeback class class with `ActiveModel::Model` that will be used for customer's feedback:
331+
332+
```ruby
333+
class Feedback
334+
include ActiveModel::Model
335+
336+
attr_accessible :name, :body, :email
337+
338+
def save
339+
FeedbackMailer.send_email(email, name, body)
340+
end
341+
end
342+
```
343+
344+
The form should look like this.
345+
346+
```ruby
347+
class FeedbackForm < ActiveForm::Base
348+
attributes :name, :body, :email, required: true
349+
end
350+
```
351+
352+
And then in controller:
353+
354+
```ruby
355+
class FeedbacksController
356+
def create
357+
feedback = Feedback.new
358+
@feedback_form = FeedbackForm.new(feedback)
359+
@feedback_form.submit(feedback_params)
360+
361+
if @feedback_form.save
362+
head :ok
363+
else
364+
render json: @feedback_form.errors
365+
end
366+
end
367+
```
368+
326369
## Demos
327370

328371
You can find a list of applications using this gem in this repository: https://github.com/m-Peter/nested-form-examples .

test/forms/poro_form_fixture.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Poro
2+
include ActiveModel::Model
3+
4+
attr_accessor :name, :city
5+
6+
def save
7+
true
8+
end
9+
end
10+
11+
class PoroFormFixture < ActiveForm::Base
12+
self.main_model = :conference
13+
attributes :name, :city, required: true
14+
end

test/forms/poro_form_test.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
require 'test_helper'
2+
require_relative 'poro_form_fixture'
3+
4+
class PoroFormTest < ActiveSupport::TestCase
5+
include ActiveModel::Lint::Tests
6+
7+
setup do
8+
@poro = Poro.new
9+
@form = PoroFormFixture.new(@poro)
10+
@model = @form
11+
end
12+
13+
14+
test "main form validates itself" do
15+
params = {
16+
name: "Euruco",
17+
city: "Athens"
18+
}
19+
20+
@form.submit(params)
21+
22+
assert @form.valid?
23+
24+
@form.submit({ name: nil, city: nil })
25+
26+
assert_not @form.valid?
27+
assert_includes @form.errors[:name], "can't be blank"
28+
assert_includes @form.errors[:city], "can't be blank"
29+
end
30+
31+
test "save works" do
32+
params = {
33+
name: "Euruco",
34+
city: "Athens"
35+
}
36+
37+
@form.submit(params)
38+
39+
assert @form.save
40+
end
41+
end

0 commit comments

Comments
 (0)