A Little RAILS_ROOT Tidiness
Ever write code like this in Rails?
[sourcecode language='ruby']
if File.exists?(File.join(RAILS_ROOT, 'config', 's3.yml'))
has_attached_file :s3_image,
:storage => :s3,
:s3_credentials => File.join(RAILS_ROOT, 'config', 's3.yml')
end
[/sourcecode]
That particular snippet is from an application that optionally uses Amazon S3 to store images, if there is a configuration file for S3 supplied in the deployment. Well, you can clean that up a bit. First, although the
[sourcecode language='ruby']
if File.exists?(Rails.root.join('config', 's3.yml'))
has_attached_file :s3_image,
:storage => :s3,
:s3_credentials => Rails.root.join('config', 's3.yml')
end
[/sourcecode]
A tiny improvement, but of such tiny improvements are a pleasant framework made.
[sourcecode language='ruby']
if File.exists?(File.join(RAILS_ROOT, 'config', 's3.yml'))
has_attached_file :s3_image,
:storage => :s3,
:s3_credentials => File.join(RAILS_ROOT, 'config', 's3.yml')
end
[/sourcecode]
That particular snippet is from an application that optionally uses Amazon S3 to store images, if there is a configuration file for S3 supplied in the deployment. Well, you can clean that up a bit. First, although the
RAILS_ROOT
constant is still supported, we've had the prettier-looking Rails.root
for months. Second, as of a recent commit to edge rails, Rails.root
returns a Pathname
object, so it directly supports the join
method. Hence, the code snippet above can now be:[sourcecode language='ruby']
if File.exists?(Rails.root.join('config', 's3.yml'))
has_attached_file :s3_image,
:storage => :s3,
:s3_credentials => Rails.root.join('config', 's3.yml')
end
[/sourcecode]
A tiny improvement, but of such tiny improvements are a pleasant framework made.