Can I put all my Ruby constants in environment.rb?

Hello, I got a lot of Ruby constants spread over a couple of models.
Is it possible to throw them all into environment.rb instead? I'd
really enjoy having things in one place. Thanks.

Save The Vinyls wrote:

Hello, I got a lot of Ruby constants spread over a couple of models.
Is it possible to throw them all into environment.rb instead? I'd
really enjoy having things in one place. Thanks.

I would guess you have more than constants "spread over a couple of models"! If your design is indeed not clear and DRY, you ought to work to put each operation into the model where it belongs.

The same with constants; each must have a model that needs it most. Put it inside that model, and access it with Model:: notation.

Save The Vinyls wrote:

Hello, I got a lot of Ruby constants spread over a couple of models.
Is it possible to throw them all into environment.rb instead? I'd
really enjoy having things in one place. Thanks.

Think it depends on what those constants are...

if they are environment specific stuff like

SITE_LOCKOUT_TIME = 15.minutes

which you may want to change to 30.seconds in development,
then I'd probably put the default in environment.rb
and do a

SITE_LOCKOUT_TIME = 15.minutes unless defined?(SITE_LOCKOUT_TIME)

then, you can override it for the development environment in in
environments/development.rb

however,
if it's something that isn't global, and isnt environment specific,
then you should keep it in the relevant model.

eg.

class User
  LOCKOUT_TIME = 15.minutes

  def is_locked_out?
    self.locked_at && self.locked_at > self.class::LOCKOUT_TIME.ago
  end
end

say for people.rb:

  MAX_EMAIL = MAX_PASSWORD = SMALL_STRING_LENGTH

  MAX_NAME = SMALL_STRING_LENGTH

  MAX_NICKNAME = 15

  EMAIL_REGEX = /\A[A-Z0-9\._%-]+@([A-Z0-9-]+\.)+[A-Z]{2,4}\z/i

  DESCRIPTION_LENGTH = 2000

  TRASH_TIME_AGO = 1.month.ago

  SEARCH_LIMIT = 20

  SEARCH_PER_PAGE = 8

  MESSAGES_PER_PAGE = 5

  NUM_RECENT_MESSAGES = 4

  NUM_WALL_COMMENTS = 10

  NUM_RECENT = 8

  FEED_SIZE = 10

  TIME_AGO_FOR_MOSTLY_ACTIVE = 1.month.ago

say for people.rb:

  MAX_EMAIL = MAX_PASSWORD = SMALL_STRING_LENGTH

  MAX_NAME = SMALL_STRING_LENGTH

  MAX_NICKNAME = 15

  EMAIL_REGEX = /\A[A-Z0-9\._%-]+@([A-Z0-9-]+\.)+[A-Z]{2,4}\z/i

  DESCRIPTION_LENGTH = 2000

  TRASH_TIME_AGO = 1.month.ago

  SEARCH_LIMIT = 20

  SEARCH_PER_PAGE = 8

  MESSAGES_PER_PAGE = 5

  NUM_RECENT_MESSAGES = 4

  NUM_WALL_COMMENTS = 10

  NUM_RECENT = 8

  FEED_SIZE = 10

  TIME_AGO_FOR_MOSTLY_ACTIVE = 1.month.ago

Save The Vinyls wrote:

say for people.rb:

  MAX_EMAIL = MAX_PASSWORD = SMALL_STRING_LENGTH
  EMAIL_REGEX = /\A[A-Z0-9\._%-]+@([A-Z0-9-]+\.)+[A-Z]{2,4}\z/i
  DESCRIPTION_LENGTH = 2000
  TRASH_TIME_AGO = 1.month.ago
  SEARCH_LIMIT = 20
  SEARCH_PER_PAGE = 8
  MESSAGES_PER_PAGE = 5
  NUM_RECENT_MESSAGES = 4
  NUM_WALL_COMMENTS = 10
  NUM_RECENT = 8
  FEED_SIZE = 10
  TIME_AGO_FOR_MOSTLY_ACTIVE = 1.month.ago

On Jul 6, 11:20�pm, Matthew Rudy Jacobs <rails-mailing-l...@andreas-

depends if you need to reuse this stuff or not.
think it's probably unecessary to freeze some of these as constants.

would probably rewrite it

class People
  validates_length_of :email, :password, :name, :maximum =>
SMALL_STRING_LENGTH
  validates_length_of :nickname, :maximum => 15
  validates_format_of :email,
/\A[A-Z0-9\._%-]+@([A-Z0-9-]+\.)+[A-Z]{2,4}\z/i
  validates_length_of :description, :maximum => 2000

  # note: TRASH_TIME_AGO = 1.month.ago will break
  # as the constant is set at class load time
  def self.to_trash(time=1.month)
    self.find(:all, ["created_at < ?", time.ago])
  end

  named_scope :mostly_active, lambda{:conditions => ["updated_at > ?",
1.month.ago]}
end

the stuff about number of messages to display and such
are probably view variables,
and you may want override with params...

hope that helps.

http://www.workingwithrails.com/person/12394-matthew-rudy-jacobs

thanks a lot for the response guys. i no longer feel this is
necessary, i'll keep the constants where they are.