I am using Rails 3. I was coding a controller very verbose. So i am trying refactoring the controller.
I coded a class called ProductMaker which make a product and modify session (product task for wizard form as current_step, if the request is a refresh, etc)
This class has method that receiving session as parameter, modify and then return this new session.
Controller action:
def new
#INITIALIZE CODE
session[:refresh] ||= SortedArray.new [1]
#...MORE CODE
end
def create
#...MUCH CODE
unless Utilities.is_refresh(session[:refresh])
#...more code
session = ProductMaker.some_method_which_return_session(session) #KEY PROBLEM LINE
#...more code
end
#... MORE CODE
end
My ProductMaker class in lib folder:
class ProductMaker
def self.some_method_which_return_session(session)
session[:any_key] = "some value"
return session
end
end
However when I write the KEY PROBLEM LINE the session is a nil value. If i comment this line the session is a ActionDispatch::Session::AbstractStore::SessionHash.
Which could be the problem?
How could i refactoring controller logic, that modify many session keys and ‘fill’ a model depending the session values, to model/class ?
UPDATE:
I am reading about binding in ruby.
How could modify the session using bindings and eval method?
If you have other ideas, please post your answer.
Thanks in advance
Finally I use bindings.
For example:
I had to modify and manage many session values so before I has a very verbose controller.
Now I could refactoring this logic to model.