Where do I put my global enviroment variables?

Hi

I’m not sure where in my script I would put my global enviroment
variables and when to load them.
I would like to create a database table that stores some applications
settings.
For example the maximum allowed size of an image that the user may
upload.
Where in the rails app would I load that data and where would I store
it so it is accessible in both the model and the controller?

thanks for your help

Marc

MarcS wrote:

Hi

I’m not sure where in my script I would put my global enviroment
variables and when to load them.
I would like to create a database table that stores some applications
settings.
For example the maximum allowed size of an image that the user may
upload.
Where in the rails app would I load that data and where would I store
it so it is accessible in both the model and the controller?

thanks for your help

Marc

This can be a choice.

In your application.rb

def imageSize
@imageSize = 1000 #1000kb = 1mb
end

and in your controller

before_filter :imageSize, :only => [:your_controller]

or…

if you only need them in your views, you can put them in your helpers :slight_smile:

def imageSize

end

and in your view

<%= imageSize %>

This is not a global environment variable. This is business logic that
belongs in the model that handles your image uploads. attachment_fu
already provides a way to specify max upload size, for instance.

Otherwise:

class Image < ActiveRecord::Base

MAX_IMAGE_SIZE = 10.megabytes
def self.max_image_size; MAX_IMAGE_SIZE; end

end

It certainly doesn’t belong in the controller or the view. Global
environment variables are a bad code smell in ruby/rails, mainly a
holdover from people with a PHP background, I think.

Rein

Jamal: If it is somehow related only to the presentation of
information then it may make sense to put it in a helper. It’s always
best to encapsulate such things properly to separate concerns and
avoid polluting the namespace. Ruby/Rails makes this easy.

On Sep 3, 1:05 pm, Jamal S. [email protected]

Rein H. wrote:

This is not a global environment variable. This is business logic that
belongs in the model that handles your image uploads. attachment_fu
already provides a way to specify max upload size, for instance.

Otherwise:

class Image < ActiveRecord::Base

MAX_IMAGE_SIZE = 10.megabytes
def self.max_image_size; MAX_IMAGE_SIZE; end

end

It certainly doesn’t belong in the controller or the view. Global
environment variables are a bad code smell in ruby/rails, mainly a
holdover from people with a PHP background, I think.

Rein

You are right :slight_smile:

and I come from PHP background :smiley:

but if he needs global variable to access it, he can place it in the
helper if it doesn’t have anything to do with his tabels?

thanks for your answers.
You are right … I come from a php background and back in the days I
always had my config.php file that had all the settings in it.

But what I’d like to do right now is load my settings from a table in
the database and then work with them.
Would I need to create a seperate settings model for that?

I wanna use the size property for example in the following model where
I define the max size of an image that the user can upload

class ArticleImage < ActiveRecord::Base
belongs_to :article

has_attachment :storage => :file_system,
:content_type => :image,
:size => 0.bytes…2.megabytes,
:thumbnails => { :thumb => ‘120x120>’ },
:processor => :MiniMagick

If I create a seperate model for the settings. How would I access that
in here?
Sorry for the newbie questions :slight_smile:

You can have a settings table (because you want the client or
operations people to be able to change it at run-time).

class Setting < ActiveRecord::Base
end

The attributes would be in the migration for name, value, etc.

In the other model/controller/view simply do something like
Setting.find_by_name(“max_image_size”).value to get at the setting
value.

Michael

thanks for your answers

On 9/4/07, Scott A s [email protected] wrote:

> > you may also want to use a .yml file and load this using your config > file, you cant change the settings at runtime but it will be a lot > better from the performance side of things

You can change settings at runtime by using YAML.dump to save your new
settings to the config file.

The bug tracking system Retrospectiva http://www.retrospectiva.org
uses a YAML based general settings and configuration file which is
managed by a configuration manager class. Read the following code for
more information:
http://retrospectiva.org/browse/trunk/lib/retrospectiva/configuration_manager/lib/configuration_manager.rb

Adam

Michael L. wrote:

You can have a settings table (because you want the client or
operations people to be able to change it at run-time).

class Setting < ActiveRecord::Base
end

The attributes would be in the migration for name, value, etc.

In the other model/controller/view simply do something like
Setting.find_by_name(“max_image_size”).value to get at the setting
value.

Michael

you may also want to use a .yml file and load this using your config
file, you cant change the settings at runtime but it will be a lot
better from the performance side of things

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs