Add Utils to your rails app




Github /  Twitter  

@jalkoby   





There are many programming languages

&

we still use Ruby & Rails





From time to time any rails developer looks
on an another solution ,
plays with it

&
comes back to Rails



The framework does the dirty work   

introduces a lot of things that I has never thought about





The most useful rails parts

 

architecture  &  conventions






For more details watch a presentation about L ineman  






http://www.confreaks.com/videos/3045-larubyconf2014-as-easy-as-rails




With a rails convention it's always clear where any part of code should be placed & named


but in every application there were a chucks of code which I didn't know where to put








Yes, it's not a terrible situation & you could live with it. 

But there are a few problems


  • with app growth a count of these chunks increases too
  • some of them has a duplicated logic
  • it's harder to test it

time to search answers





They are good but your code becomes too much OO

&

gets a new problems with growth of files & classes





There should be something better 
&
more simple at the same time





Welcome utils




The idea is not new for IT world



  • object in Scala
  • namespaces(modules) in pure functional languages
  • Ruby singleton without share state & complication in method accessing 

A few lines to start using





As the result

 


  • get `helpers` but for all Rails application
  • take benefits  of a functional programming using Ruby
  • avoid creating new instances of classes
  • you know where to put not directly related "MVC" code




Use Cases


Decoration




Conversion

Proxy



Proxy





Also you could use Utils for:



  • generation collection for selects in view
  • parsing common params in controllers
  • other standalone stateless functions




IMPORTANT to avoid share state in Utils


For share state use an instance of a class





QUestions?

Utils in Rails

By Sergey Pchelincev

Utils in Rails

  • 1,053
Loading comments...

More from Sergey Pchelincev