News Item Management
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
News Item Management is a core feature of the Actionmap application that allows users to aggregate, view, and rate news articles associated with specific political representatives. The system provides functionality for public viewing of news items and authenticated management tools for creating, editing, and deleting articles linked to a representative and a specific political issue.
The system integrates with external news services to fetch relevant articles and includes a rating mechanism where users can evaluate the content of the news items. This module is closely integrated with the representative tracking system and the user session management.
Sources: README.md:1-12, app/controllers/my_news_items_controller.rb:5-10
Data Architecture
The data model for news items is centered around the NewsItem entity, which maintains a mandatory relationship with a Representative.
Database Schema
The news_items table stores metadata about articles, including links, descriptions, and user-provided ratings. It also includes an issue field to categorize the news content and an all_info field for storing combined article data.
| Field | Type | Description |
|---|---|---|
id | integer | Primary key |
title | string | Title of the news article (Required) |
link | string | URL of the news article (Required) |
description | text | Brief summary or content of the article |
representative_id | integer | Foreign key referencing the representative (Required) |
issue | string | The political category or topic of the news |
rating | integer | User-assigned score for the article |
all_info | text | Serialized or combined information about the article |
Sources: db/schema.rb:39-48, db/migrate/20200616205245_create_news_items.rb:4-10
Entity Relationships
The following ER diagram illustrates how news items connect to representatives and how users can provide ratings.
The news item architecture ensures that every article is pinned to a specific representative via the representative_id index.
Sources: db/schema.rb:39-65
News Article Retrieval and Selection
The system uses the news-api library to fetch relevant articles based on a combination of the representative's name and a selected issue. This process is managed by the MyNewsItemsController.
Article Search Logic
- Input: The user selects a representative and a political issue.
- Search Query: The system constructs a keyword string:
"{representative_name} AND {issue}". - API Call: The
News.new(api_key).get_everythingmethod fetches the top 5 most popular English articles. - Session Storage: The results are temporarily stored in
session[:top_five_articles]for selection on a subsequent page.
Sources: app/controllers/my_news_items_controller.rb:14-26
Selection Flow
The sequence diagram below describes the multi-step process for identifying and rating an article.
Controller Logic and API Endpoints
Public Access
The NewsItemsController handles read-only access to news items for all users. It filters articles based on the associated representative.
GET /representatives/:representative_id/news_items: Lists all news items for a specific representative.GET /representatives/:representative_id/news_items/:id: Displays a specific news item.
Sources: app/controllers/news_items_controller.rb:3-12, config/routes.rb:41-42
Management Access (Authenticated)
The MyNewsItemsController handles CRUD operations and the rating workflow. It inherits from SessionController to ensure user authentication.
| Method | Path | Action |
|---|---|---|
POST | /representatives/:id/my_news_item/first | Executes search via News API |
GET | /representatives/:id/my_news_item/second | Displays search results for selection |
GET | /representatives/:id/my_news_item/rate | Processes the final rating and saves to DB |
POST | /representatives/:id/my_news_item/new | Creates a new NewsItem record |
PATCH/PUT | /representatives/:id/my_news_item/:id | Updates an existing NewsItem |
DELETE | /representatives/:id/my_news_item/:id | Removes a NewsItem |
Sources: config/routes.rb:43-57, app/controllers/my_news_items_controller.rb:5-75
Attribute Parsing
The controller includes a private helper article_attrs to parse combined article information (title, link, and description) from the all_info string using regex and string splitting. This is used during the rating process to reconcile article data with the Rating model.
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:102-110
Conclusion
The News Item Management system provides a structured way to connect current events to political representatives. By integrating the News API for discovery and a custom rating system for user engagement, it enables a collaborative environment for tracking representative activities. The dual-controller architecture separates public viewing from administrative management, ensuring a secure and organized workflow.