Topic: how to avoid duplication/share variables between views and model?

I need to use a variable "api_key" in both my views and a model.  The api_key depends on a few factors and I want to avoid duplication and be able to change it in just one place.  Is it possible to share a variable (or method) between views and a model?

i'm thinking of making a module in the lib folder and including it in application_helper and the model.  is there a more direct way of doing this?

Last edited by sthapit (2007-06-16 12:05:02)

Re: how to avoid duplication/share variables between views and model?

hm.  application_helper is itself a module so i don't think i can include another module into it.    not sure how to go about it now.

Re: how to avoid duplication/share variables between views and model?

How about creating a new model, ApiKey, and including the key variable in it? You could even include methods to account for the "few factors" upon which the api key "depends." Then have a to_s method to just dump the string. Actually, I think that if you have a to_string method (which could just wrap the to_s method), you would be able to use the class just as you would a string, it will call to_string automatically when necessary:

my_api_key = ApiKey.new(...)
puts "Your API Key is set to #{my_api_key}"

Just to clarify, ApiKey would not be an ActiveRecord model.

Re: how to avoid duplication/share variables between views and model?

thanks!  that made perfect sense and i created a new model like this:

class ApiKey
  def self.get_googlemaps_key
    case request.env['SERVER_NAME']
      when 'niftysite.com', 'www.niftysite.com'
        @key = 'ABQIAAAAkJQtmHK_xQTzpg6sjZjewRSrP55PmAbsiPSxDkgwT-NY6usoChQxsF'
      when 'niftysite.net', 'www.niftysite.net'
        @key = 'ABQIAAAAkJQtmHK_xQTzpg6sjZjewRSxyWJz5tXhWT0tbtkepf1ReCS3-xQylk'
      when 'niftysite.org', 'www.niftysite.org'
        @key = 'ABQIAAAAkJQtmHK_xQTzpg6sjZjewRSoIMLr7cDGlUyNN9sJHZf7VepoJRQ0vB'
      else #localhost
        @key = 'ABQIAAAAkJQtmHK_xQTzpg6sjZjewRTJQa0g3IQ9GZqIMmInSLzwtGDKaBQXar'
    end
  end 
end

unfortunately get_googlemaps_key depends on request.env['SERVER_NAME'] and the model doesn't seem to know what to do with it and gives me this

undefined local variable or method `request' for ApiKey:Class

Last edited by sthapit (2007-06-16 13:07:07)

Re: how to avoid duplication/share variables between views and model?

You should just be able to pass the server name in the function:

class ApiKey
  def self.get_googlemaps_key(server_name)
    case server_name

Then in your view
ApiKey.get_googlemaps_key(request.env['SERVER_NAME'])

Alex

Re: how to avoid duplication/share variables between views and model?

hi alexpt.  that works for controllers/views, but i have another model that needs the api_key...

Re: how to avoid duplication/share variables between views and model?

Pass the api key object to the model from the controller

# controller:
@model.do_something_with_google_maps(my_api_key_object)

# model
class MyModel < ActiveRecord::Base
  def do_something_with_google_maps(api_key)
    # do it up!
  end
end

Re: how to avoid duplication/share variables between views and model?

Actually looking around the rails source I think you can just do ENV["SERVER_NAME"]. Worth a try anyway wink

Alex

Re: how to avoid duplication/share variables between views and model?

thanks!  that works beautifully.

(changed request.env['SERVER_NAME'] to ENV["SERVER_NAME"] )

Re: how to avoid duplication/share variables between views and model?

darn.  that only worked on my local machine because localhost was at the end of my case statements.  ENV["SERVER_NAME"] doesn't seem to be giving the correct server name from within the model.

Re: how to avoid duplication/share variables between views and model?

Thats odd... I don't know why that would be happening.

However, as for getting the URL dynamically from within the model, I'm afraid it seems somewhat hopeless. While you can access url_for from outside of the Model, you need to manually supply the host name. I think your best bet is to take fabio's advice.

Alex