Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #10583 (closed enhancement: wontfix)

Opened 7 months ago

Last modified 6 months ago

[PATCH] Add query methods to RAILS_ENV

Reported by: jaycfields Assigned to: core
Priority: normal Milestone: 2.x
Component: Railties Version: edge
Severity: normal Keywords:
Cc: henrik+ror@nyh.se

Description

It would be nice if RAILS_ENV supported .development?, .test?, and .production?

For example, I could do caches_pages :index if RAILS_ENV.development? instead of caches_pages :index if RAILS_ENV == "development"

This can be very helpful in helping remove bugs such as the following typo:

caches_pages :index if RAILS_ENV == "developnent"

Attachments

add_query_methods_to_RAILS_ENV.diff (2.4 kB) - added by jaycfields on 12/28/07 03:53:22.

Change History

12/21/07 04:37:03 changed by chuyeow

I love this!

+1

12/21/07 04:47:10 changed by djanowski

I love it too.

Shouldn't this check for the available files under config/environments? Some people use more than the default ones and it'd be nice for them to have RAILS_ENV.staging?

12/21/07 04:49:23 changed by jaycfields

I considered looking for files, but I thought this might be good enough for 95% of the time. I'm open to changing it though if other people think checking files is a good idea.

12/21/07 04:56:42 changed by ryankinderman

I'll plus it if it scans the environment files. I usually have at least an additional qa.rb in there :)

12/21/07 05:10:49 changed by stevenbristol

+1 from me. But I'd love it more if it scans the env dir. A lot of my projects are ending up with a staging env.

12/21/07 05:12:11 changed by jaycfields

fair enough, version using files under config/environments done.

12/21/07 05:12:58 changed by jaycfields

  • keywords set to Verified.

12/21/07 05:18:47 changed by ryankinderman

+1

12/21/07 05:55:37 changed by dkubb

+1

12/21/07 09:47:36 changed by turnip

Is there meant to be a Kernel#p call on line 10 of test_help.rb? It would be good to eliminate the duplication too, if possible.

12/21/07 09:55:16 changed by chuyeow

Yup agree with turnip.

-1 on the new patch (despite my +1 on the original patch).

If you can move out the duplicated code in railties/lib/test_help.rb that'd be great. I haven't tested it but I don't think you'd have to redefine the methods all over again in the test helper.

12/21/07 14:28:04 changed by codebrulee

Agreed that the duplicated code should be removed, if possible.

Otherwise, +1. I like it!

12/21/07 15:10:56 changed by jaycfields

Okay, new version.. No dup code, I followed the pattern introduced by ruby_version_check and just put the dup code in a file.

Also, it still scans the file system to get the environments.

12/21/07 15:13:32 changed by djanowski

Just in case, +1 ;)

12/21/07 15:17:26 changed by turnip

cool, +1 :)

12/21/07 18:13:34 changed by bloodroot

+1

12/21/07 22:04:30 changed by topfunky

+1

Definitely a great idea and I'm glad someone finally submitted a patch for this.

12/21/07 22:46:35 changed by matt

+1 good idea. I'm just wondering if using RAILS_ENV.development? is better than using Environment.development?

Any thoughts?

12/22/07 02:52:29 changed by jemminger

+1

12/22/07 16:19:45 changed by dcmanges

I definitely like RAILS_ENV.development? better than RAILS_ENV == "development". However, I think using either one in code is generally a bad idea. Instead of checking to see if RAILS_ENV is development, I'd prefer some sort of meaningful constant set in development.rb.

Based on the example in the ticket description, I prefer: caches_page :index if CACHE_INDEX_PAGE

This makes it easier to create other environments. Let's say I want an environment called 'dans_development' that is like development, but a little bit different. I could create dans_development.rb, and do: load File.dirname(__FILE__) + "/development.rb", and then make some custom tweaks. With a constant set in development.rb, behavior would remain the same, where with checks for RAILS_ENV.development?, things would change.

12/22/07 18:08:42 changed by dcmanges

  • keywords changed from Verified to verified.

12/28/07 03:32:17 changed by rmm5t

  • keywords deleted.

Jay, add_query_methods_to_RAILS_ENV.rb is missing from the latest patch. For that reason, I'm removing the verified keyword for now. Please add "verified" back after a new patch is submitted.

Regardless, I like the idea behind this patch.

12/28/07 03:53:22 changed by jaycfields

  • attachment add_query_methods_to_RAILS_ENV.diff added.

12/28/07 03:54:21 changed by jaycfields

  • keywords set to verified.

Updated, sorry I missed that.

Cheers, Jay

12/28/07 04:10:35 changed by rmm5t

+1 Thanks.

12/31/07 03:11:11 changed by fxn

+1 usage looks clean

01/03/08 00:23:48 changed by david

  • keywords deleted.
  • status changed from new to closed.
  • resolution set to wontfix.

Like the general idea, but not as a hack on a string like RAILS_ENV. Instead, we should take this as an opportunity to come up with a unified class for this:

  Rails.log # => RAILS_LOG
  Rails.environment.development?
  Rails.cache # => RAILS_CACHE

...and so on. We should probably start that as a new ticket. Jay, want to have a go at it?

01/03/08 11:33:55 changed by Henrik N

  • cc set to henrik+ror@nyh.se.