Project

General

Profile

Patch #44274 » example-journalized-news.patch

Florian Walchshofer, 2026-10-18 21:29

View differences:

app/controllers/news_controller.rb
helper :watchers
helper :attachments
helper :issues
helper :custom_fields
def index
case params[:format]
......
end
def show
@journals = @news.journals.visible.preload(:user, :details).reorder(:id).to_a
@comments = @news.comments.preload(:commented).to_a
@comments.reverse! if User.current.wants_comments_in_reverse_order?
......
end
def update
@news.init_journal(User.current)
@news.safe_attributes = params[:news]
@news.save_attachments(params[:attachments] || (params[:news] && params[:news][:uploads]))
if @news.save
app/models/news.rb
validates_length_of :summary, :maximum => 255
acts_as_attachable :edit_permission => :manage_news,
:delete_permission => :manage_news
:delete_permission => :manage_news,
:after_add => :attachment_added,
:after_remove => :attachment_removed
acts_as_searchable :columns => ['title', 'summary', "#{table_name}.description"],
:preload => :project
acts_as_event :url => Proc.new {|o| {:controller => 'news', :action => 'show', :id => o.id}}
......
:author_key => :author_id
acts_as_watchable
acts_as_webhookable
acts_as_journalized :activity_type => 'news',
:attributes => %w(title summary description),
:permissions => {
:edit_notes => :manage_news,
:add_watchers => :manage_news
}
include News::Webhookable
after_create :add_author_as_watcher
after_create_commit :send_notification
after_save :create_journal
scope :visible, (lambda do |*args|
joins(:project).
......
!user.nil? && user.allowed_to?(:view_news, project)
end
def init_journal(user, notes = '')
@current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes)
end
attr_reader :current_journal
# Callback on file attachment
def attachment_added(attachment)
if current_journal && !attachment.new_record?
current_journal.journalize_attachment(attachment, :added)
end
end
# Callback on attachment deletion
def attachment_removed(attachment)
if current_journal && !attachment.new_record?
current_journal.journalize_attachment(attachment, :removed)
current_journal.save
end
end
def journal_event_title(journal)
"#{l(:label_news)}: #{title}"
end
def journal_event_type(journal)
'news'
end
# Returns true if the news can be commented by user
def commentable?(user=User.current)
user.allowed_to?(:comment_news, project)
......
private
def create_journal
current_journal&.save
@current_journal = nil
end
def add_author_as_watcher
Watcher.create(:watchable => self, :user => author)
end
app/views/news/show.html.erb
</div>
<br />
<% if @journals.any? %>
<div id="history" class="journals">
<h3><%= l(:label_history) %></h3>
<% @journals.each do |journal| %>
<div id="change-<%= journal.id %>" class="<%= journal.css_classes %>">
<h4 class="journal-header">
<span class="journal-info"><%= avatar(journal.user) %> <%= authoring journal.created_on, journal.user, :label => :label_updated_time_by %></span>
</h4>
<ul class="details">
<% journal.visible_details.each do |detail| %>
<% if detail.property == 'attr' && detail.prop_key == 'description' %>
<%# The diff of JournalsController is specific to issues, show it inline %>
<li>
<%= l(:text_journal_changed_no_detail, :label => content_tag(:strong, l(:field_description))).html_safe %>
<details>
<summary><%= l(:label_diff) %></summary>
<pre class="text-diff"><%= Redmine::Helpers::Diff.new(detail.value, detail.old_value).to_html %></pre>
</details>
</li>
<% else %>
<% details_to_strings([detail]).each do |string| %>
<li><%= string %></li>
<% end %>
<% end %>
<% end %>
</ul>
<%= textilizable(journal, :notes) if journal.notes.present? %>
</div>
<% end %>
</div>
<% end %>
<div id="comments" class="journals">
<h3 class="comments"><%= l(:label_comment_plural) %></h3>
<% if @news.commentable? && @comments.size >= 3 %>
lib/redmine/preparation.rb
# Journalized types, registered before their classes are loaded
Redmine::Acts::Journalized.register 'Issue'
Redmine::Acts::Journalized.register 'News'
Scm::Base.add "Subversion"
Scm::Base.add "Mercurial"
......
Activity.map do |activity|
activity.register :issues, :class_name => %w(Issue Journal)
activity.register :changesets
activity.register :news
activity.register :news, :class_name => %w(News Journal)
activity.register :documents, :class_name => %w(Document Attachment)
activity.register :files, :class_name => 'Attachment'
activity.register :wiki_edits, :class_name => 'WikiContentVersion', :default => false
(3-3/4)