There’s likely data used and looped through frequently in an app, so it’s useful to have the data stored in a specific location and accessed when needed. This can be done by storing them in a YAML file, accessing certain info through a Models file, and then pulling it in the Controller.
For a list of articles, all the data can be stored in a YAML file. It can be stored in
db, or a custom
- name: New York Times
author: Mackenzie McKale
- name: Wall Street Journal
author: Will McAvoy
You can then access and organize this data as needed in a models file. This is technically organizing and creating a data structure, which is why it’d fit well in the Models folder.
There’s many different approaches here, and ways to filter and modify the data. This example simply uses a Struct to get all the basic info and return an array of hashes.
AllArticles = Struct.new(:name, :author, :url) do
DATA_PATH = Rails.root.join("data", "articles.yml").to_s.freeze
YAML.load_file(DATA_PATH)["articles"].map do |article|
new(article["name"], article["author"], article["url"])
Now this module can be called in a controller and passed to a template.
@articles = Articles::AllArticles.list