News Rating System
Relevant source files
The following files were used as context for generating this wiki page:
The News Rating System within the Actionmap project allows users to search for news articles related to specific political representatives and issues, and subsequently provide numerical ratings for those articles. This system facilitates community engagement by allowing users to curate and evaluate the relevance or quality of news items associated with public officials.
The system integrates with external news services via the news-api gem to fetch live articles and persists both the news metadata and the user-specific ratings in a relational database. It is a multi-step process involving representative selection, article discovery, and a final rating submission.
Sources: app/controllers/my_news_items_controller.rb:1-50, README.md:1-10
Data Architecture
The rating system is built upon a primary ratings table and enhancements to the news_items table. The Rating entity represents a specific user's evaluation of a piece of content, linked to their account, while the NewsItem entity stores the article metadata itself.
Database Schema
The following table describes the data structure for ratings:
| Field | Type | Description |
|---|---|---|
id | Integer | Primary key |
title | String | The title of the news article (Null: false) |
link | String | URL to the news article (Null: false) |
description | Text | Summary or snippet of the article content |
rating | Integer | Numerical score assigned by the user (Null: false) |
user_id | Integer | Foreign key referencing the user who created the rating |
created_at | DateTime | Timestamp of creation |
Sources: db/migrate/20221203115012_create_ratings.rb:5-12, db/schema.rb:56-65
Entity Relationships
The ratings table maintains a belongs_to relationship with the users table. Additionally, the news_items table was expanded to include a rating column and an all_info text field to support the storage of processed article data.
Article Selection and Rating Workflow
The workflow for rating a news item is handled by the MyNewsItemsController. It utilizes a three-step process: article search, article selection, and final persistence.
1. Searching for Articles
The system uses the news-api to search for articles based on a specific representative's name and a selected issue. The search query combines these two parameters using a boolean AND.
2. Selection Process
The search results (limited to the top five articles) are stored in the user's session. The user then selects an article and provides a rating on a "second page" interface.
3. Rating Submission
When a user submits a rating, the controller parses the combined article information (all_info) to extract the title, link, and description. It then checks if the current user has already rated this specific article; if so, it updates the existing rating; otherwise, it creates a new record.
Sources: app/controllers/my_news_items_controller.rb:16-60
Logic Flow Diagram
The following diagram illustrates the sequence of actions from search to database persistence:
Implementation Details
Article Attribute Parsing
The controller includes a private helper method article_attrs to handle the deconstruction of article metadata stored in the all_info parameter. It uses regular expressions and string splitting to separate the URL from the title and description.
def article_attrs(all_info, rating)
article_title = all_info.split(/(?=https)/)[0]
rest_of_string = all_info.split(/(?=https)/)[1]
article_link = rest_of_string.split[0]
rest_of_string.slice!(article_link)
article_description = rest_of_string
article_rating = rating.to_i
{ title: article_title, link: article_link, description: article_description, rating: article_rating }
end
Sources: app/controllers/my_news_items_controller.rb:104-112
Controller Actions
| Action | Purpose |
|---|---|
select_representative_issue | Searches NewsAPI and redirects to selection page. |
second_page | Displays the list of articles found in the session for user selection. |
rate | Logic for finding/creating a Rating record for Current.user. |
Sources: app/controllers/my_news_items_controller.rb:16-65
Conclusion
The News Rating System provides a bridge between external news data and internal user feedback. By utilizing a session-based workflow and a dedicated ratings table, the system allows for the tracking of community sentiment regarding news coverage of political representatives.
Sources: app/controllers/my_news_items_controller.rb:48-65, db/schema.rb:56-65