# DRYing/shortening form processing

**URL:** <https://www.ruby-forum.com/t/drying-shortening-form-processing/151708>\
**Category:** Rails\
**Created:** [October 24, 2008, 5:53pm UTC](https://www.ruby-forum.com/t/drying-shortening-form-processing/151708 "2008-10-24T17:53:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@nick](https://www.ruby-forum.com/u/nick)\
**Post date:** [October 24, 2008, 5:53pm UTC](https://www.ruby-forum.com/t/drying-shortening-form-processing/151708/1 "2008-10-24T17:53:14Z")

</div>

So I have this form with \>10 fields on it. When the form is submitted,  
the corresponding controller action calls the class method  
#filtered\_properties to process the data.

I’ve created separate class methods for processing each parameter,  
which #filtered\_properties  
calls. However, #filtered\_properties is still 120 lines long. This is  
because each “section” that processes a param is 10 lines long. Each  
of these “sections” is different in one or two ways, but they’re all  
extremely similar.

Might you have any suggestions for how to make #filtered\_properties  
more DRY and/or shorter?:  
[http://pastie.org/299720](http://pastie.org/299720)

Thanks, guys!  
Nick

---

<div class="post-metadata">

**Author:** ![nick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@nick](https://www.ruby-forum.com/u/nick)\
**Post date:** [October 24, 2008, 7:03pm UTC](https://www.ruby-forum.com/t/drying-shortening-form-processing/151708/2 "2008-10-24T19:03:21Z")

</div>

Hi Nick,

I can’t imagine a reason why these methods are class and not instance  
methods, could you please explain a litle bit more about your idea,  
your model and your controller and what do you really want with these  
filter\_property methods?

On Fri, Oct 24, 2008 at 12:52 PM, Nick [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> extremely similar.
> 
> Might you have any suggestions for how to make #filtered\_properties  
> more DRY and/or shorter?:  
> [http://pastie.org/299720](http://pastie.org/299720)
> 
> Thanks, guys!  
> Nick
> 
> >

–  
Maurício Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)  
João Pessoa, PB, +55 83 8867-7208

---

<div class="post-metadata">

**Author:** ![nick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@nick](https://www.ruby-forum.com/u/nick)\
**Post date:** [October 24, 2008, 9:37pm UTC](https://www.ruby-forum.com/t/drying-shortening-form-processing/151708/3 "2008-10-24T21:37:38Z")

</div>

Well, instead of doing it using class methods , you could do it using  
this →   
[http://github.com/staugaard/active\_record\_base\_without\_table/tree/master](http://github.com/staugaard/active_record_base_without_table/tree/master)

You get all active record validations, active record error handling  
and instead of doing it in an ugly class method that looks like a  
“function” you can have your Property instance to generate the SQL  
code you’re generating now in the class methods.

On Fri, Oct 24, 2008 at 4:18 PM, Nick [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> -number of bedrooms;
> 
> ```
> 
> messages, or the properties that match the search criteria).
> 
> I hope that explains things a bit more clearly. Cheers,  
> Nick
> 
> >

–  
Maurício Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)  
João Pessoa, PB, +55 83 8867-7208

---

<div class="post-metadata">

**Author:** ![nick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@nick](https://www.ruby-forum.com/u/nick)\
**Post date:** [October 24, 2008, 9:54pm UTC](https://www.ruby-forum.com/t/drying-shortening-form-processing/151708/4 "2008-10-24T21:54:03Z")

</div>

Here’s an idea of what i’m talking about =\> [http://pastie.org/299940](http://pastie.org/299940)

On Fri, Oct 24, 2008 at 4:18 PM, Nick [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> -number of bedrooms;
> 
> ```
> 
> messages, or the properties that match the search criteria).
> 
> I hope that explains things a bit more clearly. Cheers,  
> Nick
> 
> >

–  
Maurício Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)  
João Pessoa, PB, +55 83 8867-7208

---

<div class="post-metadata">

**Author:** ![nick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@nick](https://www.ruby-forum.com/u/nick)\
**Post date:** [October 24, 2008, 9:18pm UTC](https://www.ruby-forum.com/t/drying-shortening-form-processing/151708/5 "2008-10-24T21:18:52Z")

</div>

On Oct 24, 1:02 pm, “Maurício Linhares” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi Nick,
> 
> I can’t imagine a reason why these methods are class and not instance  
> methods, could you please explain a litle bit more about your idea,  
> your model and your controller and what do you really want with these  
> filter\_property methods?

Hi Maurício. They’re class methods because they don’t rely upon an  
instance of the Property model. They’re for finding properties that  
match the search criteria submitted by the user.

Here’s how it works:

1. A user browses to /neighbourhoods/1/map/ . A Google Map is  
displayed, and properties within that neighbourhood are plotted on the  
map.
2. There’s a form that allows the user to filter which properties are  
plotted on the map. For example, they can filter based on the:  
-number of bedrooms;  
-number of bathrooms;  
-min or max price;  
-etc.
3. When the user submits the form, the “map\_filter” neighbourhood  
action is called, which uses Property#filtered\_properties to determine  
which properties match the user’s search criteria.
4. Property#filtered\_properties calls the #filtered\_X class method  
that corresponds to each form field value. When #filtered\_properties  
finishes processing each form field value, it checks to see if an  
error occured, and returns an appropriate value (IE: either the error  
messages, or the properties that match the search criteria).

I hope that explains things a bit more clearly. Cheers,  
Nick

---

<div class="post-metadata">

**Author:** ![nick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@nick](https://www.ruby-forum.com/u/nick)\
**Post date:** [October 24, 2008, 9:54pm UTC](https://www.ruby-forum.com/t/drying-shortening-form-processing/151708/6 "2008-10-24T21:54:03Z")

</div>

On Oct 24, 3:37 pm, “Maurício Linhares” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Well, instead of doing it using class methods , you could do it using  
> this → [http://github.com/staugaard/active\_record\_base\_without\_table/tree/master](http://github.com/staugaard/active_record_base_without_table/tree/master)
> 
> You get all active record validations, active record error handling  
> and instead of doing it in an ugly class method that looks like a  
> “function” you can have your Property instance to generate the SQL  
> code you’re generating now in the class methods.

Wow, that’s a really useful plugin! Thanks for the link, mate. I’m  
going to take a serious look at it.  
-Nick
