Background
I’m writing a photo gallery, the gallery can create both previews and thumbnails.
I want the user to customize the creation of these (resolution etc)
What I have so far
config/gallery.yml– Stores the actual settings, a block for each envconfig/initializers/gallery.rb– Loads the YAML file into Gallery::Applicationapp/models/image.rb– The model that reads the configuration and creates the images
My question
Where is the best place to merge the settings with the default values?
I could either do it in gallery.rb or image.rb where it’s actually used.
Otherwise, is this a sane approach or have I gone about this all wrong?
Any feedback is greatly appreciated.
Files
config/initializers/gallery.yml
production:
create_previews: true
create_thumbnails: true
development:
create_thumbnails: true
create_previews: true
test:
create_thumbnails: false
create_previews: false
config/initializers/gallery.rb
Gallery::Application.config.image_directory = File.join(Rails.root, 'images')
Gallery::Application.config.thumbnail_directory = File.join(Rails.root, 'thumbnails')
Gallery::Application.config.preview_directory = File.join(Rails.root, 'previews')
# DO NOT MODIFY THE LINES BELOW
EXTERNAL_CONFIG_FILE_PATH = "#{Rails.root}/config/gallery.yml"
if FileTest.exists? EXTERNAL_CONFIG_FILE_PATH
vals = YAML.load_file(EXTERNAL_CONFIG_FILE_PATH)[::Rails.env]
else
vals = {}
end
Gallery::Application.config.create_previews = vals['create_previews']
Gallery::Application.config.preview_settings = vals['preview_settings']
Gallery::Application.config.create_thumbnails = vals['create_thumbnails']
Gallery::Application.config.thumbnail_settings = vals['thumbnail_settings']
I also agree this is good place in config/initializers.
but other place and way is read/write database table field in GalleryConfig ActiveRecord model using MySQL.
because easy for testcase or rails console.