Christoffer
Christoffer

Reputation: 2411

Access session variable or controller variable from module

Using: Rails 3.0.3

I have this feature of my website (count calculate . com) which consists of plenty different calculations. The user can set his own variable for decimals (significant numbers) through a form. First I set this variable as a configatron variable which turned out to be a bad idea (a change that some user did was passed on to all users).

So, I decided to make it a session variable, which makes sense since a user should select his own decimal value.

Now, in the controller I call a module function that in turn calls 100 different module functions, depending on id.

My problem:

I want to access this session variable in that module which turns out to be impossible. Passing it already from the controller (through the model) is a bit of a nightmare since it needs to be passed for each and every function.

So:

  1. How do I access session variables from a module in the lib-catalog?
  2. How could I access a controller method from a module? If so I could call that method and fetch the session variable.

Upvotes: 3

Views: 7995

Answers (3)

cgr
cgr

Reputation: 1121

it sounds like your Model is including the module code. You really really do not want to use or access the session directly from there. It breaks the purpose for MVC, Rails is doing its best to prevent you from trying something like that.

when you say " I call a module function that in turn calls 100 different module functions, depending on id." I would like to see that chunk of code.

Your original idea is the correct one...the value needs to be passed over to the model...it just sounds like the code needs to be DRY'd up a little so that it isn't such a headache.

It is hard to give recommendations without seeing the code. I was stuck in a similar situation and used dynamic methods (overriding method_missing) to get around the problem.

Upvotes: 1

d11wtq
d11wtq

Reputation: 35298

Do you mean a ruby module? As in a mixin? If you're including it in your controller, it already has access to everything the controller has access to. Just call session, request, params etc and all will just work.

If you really need to get the session from some arbitrary part of the system, you can obtain the request with:

ActionDispatch::Request.new(ENV)

Then you can get the session from the request. (Note that request is actually a singleton in disguise... it won't build a new request if one is already built).

Upvotes: 4

house9
house9

Reputation: 20614

Here is one way to do it, mix your calculation module into the controller

# application_controller contains a method that wraps your session value
class ApplicationController < ActionController::Base
    protect_from_forgery

    def user_value
        return 5 # really a session value
    end
end

The module that contains your calculation methods needs access to the session, since you are mixing it into the controller you can call the user_value method with self

module CalculationModule  
    def super_cool_calculation
        return self.user_value * 420
    end

    # more calculation methods  
end

note the self.user_value call, that method is not defined in the module but you will need to define it in the class you mixin to

class FooController < ApplicationController
    include CalculationModule

    def show
        @calculation = super_cool_calculation # call calculation module method
    end

# etc...

if any of your model objects also define a user_value method you could then mixin the calculation module to those if needed, they would probably not be getting the value from session

Something else to consider, just store the user defined value in the database instead of the session, you will have easy access to the value from the model or controller

Upvotes: 1

Related Questions