I would like to set some site-wide (global) values across my application
layout.
All these global values are stored in my Settings table.
So in my Application controller I have something like this:
#Application Controller:
before_filter :grab_settings
def grab_settings
@set = Setting.first
end
#Application Layout view
<div>
<p><%= @set.company_name %></p>
</div>
All of this works fine, though. But if the "company_name" field is
empty, than you wouldn't see anything.
So in that case I would like to default to a other value.
I fixed that by doing the following:
<div>
<% if @set.company_name.empty? %>
<p>My company</p>
<% else %>
<p><%= @set.company_name %></p>
<% end %>
</div>
However isn't there a better way to do this? Initially I thought I could
do something more elegant like this:
<%= @set.company_name || "My company" %> but ofcourse, that won't work.
So basically I'm looking for a more polished/clean/better approach.
Well, you could always read the Settings, then impose your own defaults
inside the grab_settings method. Any code outside that method has no
idea whether the value was read, or set by you. It just knows that the
value is filled.
If you wanted different vals per environment (ie test, development,
production), then you could define APP_VALS in each of those
environment config files.
You can put your defaults in a file default.yml and then "over-ride"
with them with a separate production.yml etc etc.
If you wanted different vals per environment (ie test, development,
production), then you could define APP_VALS in each of those
environment config files.