Add content changelogs #migration

This commit is contained in:
Andrew Brown 2017-07-12 20:03:38 +01:00
parent 8f37f197a7
commit 63d4c442cc
33 changed files with 471 additions and 1 deletions

View File

@ -85,4 +85,13 @@ p.long-form {
p {
margin-bottom: 18px !important;
}
}
.changelog {
td {
padding-top: 10px;
padding-bottom: 10px;
}
margin-bottom: 30px;
}

View File

@ -0,0 +1,41 @@
require 'active_support/concern'
module HasChangelog
extend ActiveSupport::Concern
included do
def change_events
ContentChangeEvent.where(content_id: id, content_type: self.class.name).order(:id)
end
after_create do
ContentChangeEvent.create(
user: user,
changed_fields: changes,
content_id: id,
content_type: self.class.name,
action: :created
)
end
after_update do
ContentChangeEvent.create(
user: user,
changed_fields: changes,
content_id: id,
content_type: self.class.name,
action: :updated
) if changes.any?
end
before_destroy do
ContentChangeEvent.create(
user: user,
changed_fields: changes,
content_id: id,
content_type: self.class.name,
action: :deleted
)
end
end
end

View File

@ -0,0 +1,5 @@
class ContentChangeEvent < ActiveRecord::Base
belongs_to :user
serialize :changed_fields, Hash
end

View File

@ -18,6 +18,7 @@ class Character < ActiveRecord::Base
include HasPrivacy
include HasContentGroupers
include HasImageUploads
include HasChangelog
include Serendipitous::Concern

View File

@ -18,6 +18,8 @@ class Creature < ActiveRecord::Base
include HasPrivacy
include HasContentGroupers
include HasImageUploads
include HasChangelog
include Serendipitous::Concern
include Authority::Abilities

View File

@ -10,6 +10,8 @@ class Group < ActiveRecord::Base
include HasPrivacy
include HasContentGroupers
include HasImageUploads
include HasChangelog
include Serendipitous::Concern
include Authority::Abilities

View File

@ -16,6 +16,8 @@ class Item < ActiveRecord::Base
include HasPrivacy
include HasContentGroupers
include HasImageUploads
include HasChangelog
include Serendipitous::Concern
include Authority::Abilities

View File

@ -10,6 +10,8 @@ class Language < ActiveRecord::Base
include HasPrivacy
include HasContentGroupers
include HasImageUploads
include HasChangelog
include Serendipitous::Concern
include Authority::Abilities

View File

@ -19,6 +19,8 @@ class Location < ActiveRecord::Base
include HasPrivacy
include HasContentGroupers
include HasImageUploads
include HasChangelog
include Serendipitous::Concern
include Authority::Abilities

View File

@ -10,6 +10,8 @@ class Magic < ActiveRecord::Base
include HasPrivacy
include HasContentGroupers
include HasImageUploads
include HasChangelog
include Serendipitous::Concern
include Authority::Abilities

View File

@ -16,6 +16,8 @@ class Race < ActiveRecord::Base
include HasPrivacy
include HasContentGroupers
include HasImageUploads
include HasChangelog
include Serendipitous::Concern
include Authority::Abilities

View File

@ -10,6 +10,8 @@ class Religion < ActiveRecord::Base
include HasPrivacy
include HasContentGroupers
include HasImageUploads
include HasChangelog
include Serendipitous::Concern
include Authority::Abilities

View File

@ -10,6 +10,8 @@ class Scene < ActiveRecord::Base
include HasPrivacy
include HasContentGroupers
include HasImageUploads
include HasChangelog
include Serendipitous::Concern
include Authority::Abilities

View File

@ -10,6 +10,8 @@ class Universe < ActiveRecord::Base
include HasAttributes
include HasPrivacy
include HasImageUploads
include HasChangelog
include Serendipitous::Concern
include Authority::Abilities

View File

@ -28,6 +28,8 @@ class User < ActiveRecord::Base
has_many :votes
has_many :raffle_entries
has_many :content_change_events
# TODO: Swap this out with a has_many when we transition from a scratchpad to users having multiple documents
has_one :document

View File

@ -0,0 +1,49 @@
<div class="flow-text">Changelog for <%= content.name %></div>
<ul class="collection">
<% content.change_events.reverse.each do |change_event| %>
<li class="collection-item">
<%= (change_event.user == current_user) ? 'You' : link_to(change_event.user.name, change_event.user) %>
<%= change_event.action %> this <%= change_event.content_type.downcase %>
<span class="grey-text"><%= time_ago_in_words change_event.created_at %> ago</span>.
<table class="highlight centered changelog">
<thead>
<tr>
<th>Changed field</th>
<th>Old value</th>
<th>New value</th>
</tr>
</thead>
<tbody>
<% change_event.changed_fields.each do |key, change| %>
<% next if change.first.blank? && change.second.blank? %>
<% next if %w(id created_at updated_at user user_id).include?(key) %>
<%
old_value = change.first.blank? ? '(blank)' : change.first.to_s
new_value = change.second.blank? ? '(blank)' : change.second.to_s
# Just for clarity / ease of mind to users -- we treat blank as private, but that's not obvious to them.
if key == 'privacy'
old_value = 'private' if old_value == '(blank)'
new_value = 'private' if new_value == '(blank)'
end
%>
<tr>
<td><%= key.titleize %></td>
<td><%= old_value.truncate(380) %></td>
<td><%= new_value.truncate(380) %></td>
</tr>
<% end %>
</tbody>
</table>
</li>
<% end %>
<li class="collection-item">
<%= content.class.name %> <%= content.name %> was created by <%= link_to content.user.name, content.user %>
<span class='grey-text'><%= time_ago_in_words content.created_at %> ago</span>.
</li>
</ul>

View File

@ -19,8 +19,14 @@
<% @content.class.attribute_categories(current_user).each do |category| %>
<% fields = category.attribute_fields %>
<%
#TODO: refactor ALL OF THIS
# Don't show changelog tab on create/edit form
next if (creating || editing) && category.name == 'changelog'
if creating || editing
# Show everything on create/edit forms
elsif category.name == 'changelog'
# Show changelog on show page
elsif category.name == 'gallery'
# Only show gallery tab if the content has any images
next if @content.image_uploads.empty?

View File

@ -28,6 +28,11 @@
<%= render partial: 'content/form/images/gallery', locals: { content: content } %>
</div>
<% end %>
<% if category.name == 'changelog' %>
<div>
<%= render partial: 'content/display/changelog', locals: { content: content } %>
</div>
<% end %>
<% category.attribute_fields.each do |attribute| %>
<% next if attribute.name.start_with?("private") && (current_user.nil? || @content.user != current_user) %>

View File

@ -113,6 +113,9 @@
:gallery:
:label: Gallery
:icon: photo_library
:changelog:
:label: Changelog
:icon: history
:notes:
:label: Notes
:icon: edit

View File

@ -73,6 +73,9 @@
:gallery:
:label: Gallery
:icon: photo_library
:changelog:
:label: Changelog
:icon: history
:notes:
:label: Notes
:icon: edit

View File

@ -75,6 +75,9 @@
:gallery:
:label: Gallery
:icon: photo_library
:changelog:
:label: Changelog
:icon: history
:notes:
:label: Notes
:icon: edit

View File

@ -41,6 +41,9 @@
:gallery:
:label: Gallery
:icon: photo_library
:changelog:
:label: Changelog
:icon: history
:notes:
:label: Notes
:icon: edit

View File

@ -43,6 +43,9 @@
:gallery:
:label: Gallery
:icon: photo_library
:changelog:
:label: Changelog
:icon: history
:notes:
:label: Notes
:icon: edit

View File

@ -65,6 +65,9 @@
:gallery:
:label: Gallery
:icon: photo_library
:changelog:
:label: Changelog
:icon: history
:notes:
:label: Notes
:icon: edit

View File

@ -51,6 +51,9 @@
:gallery:
:label: Gallery
:icon: photo_library
:changelog:
:label: Changelog
:icon: history
:notes:
:label: Notes
:icon: edit

View File

@ -65,6 +65,9 @@
:gallery:
:label: Gallery
:icon: photo_library
:changelog:
:label: Changelog
:icon: history
:notes:
:label: Notes
:icon: edit

View File

@ -59,6 +59,9 @@
:gallery:
:label: Gallery
:icon: photo_library
:changelog:
:label: Changelog
:icon: history
:notes:
:label: Notes
:icon: edit

View File

@ -31,6 +31,9 @@
:gallery:
:label: Gallery
:icon: photo_library
:changelog:
:label: Changelog
:icon: history
:notes:
:label: Notes
:icon: edit

View File

@ -35,6 +35,9 @@
:gallery:
:label: Gallery
:icon: photo_library
:changelog:
:label: Changelog
:icon: history
:settings:
:label: Settings
:icon: build

View File

@ -0,0 +1,13 @@
class CreateContentChangeEvents < ActiveRecord::Migration
def change
create_table :content_change_events do |t|
t.references :user, index: true, foreign_key: true
t.text :changed_fields
t.integer :content_id
t.string :content_type
t.string :action
t.timestamps null: false
end
end
end

View File

@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20170517164648) do
ActiveRecord::Schema.define(version: 20170712190101) do
create_table "archenemyships", force: :cascade do |t|
t.integer "user_id"
@ -157,6 +157,18 @@ ActiveRecord::Schema.define(version: 20170517164648) do
t.integer "child_id"
end
create_table "content_change_events", force: :cascade do |t|
t.integer "user_id"
t.text "changed_fields"
t.integer "content_id"
t.string "content_type"
t.string "action"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "content_change_events", ["user_id"], name: "index_content_change_events_on_user_id"
create_table "creature_relationships", force: :cascade do |t|
t.integer "user_id"
t.integer "creature_id"
@ -234,6 +246,19 @@ ActiveRecord::Schema.define(version: 20170517164648) do
t.datetime "updated_at", null: false
end
create_table "friendly_id_slugs", force: :cascade do |t|
t.string "slug", null: false
t.integer "sluggable_id", null: false
t.string "sluggable_type", limit: 50
t.string "scope"
t.datetime "created_at"
end
add_index "friendly_id_slugs", ["slug", "sluggable_type", "scope"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type_and_scope", unique: true
add_index "friendly_id_slugs", ["slug", "sluggable_type"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type"
add_index "friendly_id_slugs", ["sluggable_id"], name: "index_friendly_id_slugs_on_sluggable_id"
add_index "friendly_id_slugs", ["sluggable_type"], name: "index_friendly_id_slugs_on_sluggable_type"
create_table "group_allyships", force: :cascade do |t|
t.integer "user_id"
t.integer "group_id"
@ -699,6 +724,245 @@ ActiveRecord::Schema.define(version: 20170517164648) do
t.integer "supergroup_id"
end
create_table "thredded_categories", force: :cascade do |t|
t.integer "messageboard_id", null: false
t.string "name", limit: 191, null: false
t.string "description", limit: 255
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "slug", limit: 191, null: false
end
add_index "thredded_categories", ["messageboard_id", "slug"], name: "index_thredded_categories_on_messageboard_id_and_slug", unique: true
add_index "thredded_categories", ["messageboard_id"], name: "index_thredded_categories_on_messageboard_id"
add_index "thredded_categories", ["name"], name: "thredded_categories_name_ci"
create_table "thredded_messageboard_groups", force: :cascade do |t|
t.string "name"
t.integer "position", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "thredded_messageboard_notifications_for_followed_topics", force: :cascade do |t|
t.integer "user_id", null: false
t.integer "messageboard_id", null: false
t.string "notifier_key", limit: 90, null: false
t.boolean "enabled", default: true, null: false
end
add_index "thredded_messageboard_notifications_for_followed_topics", ["user_id", "messageboard_id", "notifier_key"], name: "thredded_messageboard_notifications_for_followed_topics_unique", unique: true
create_table "thredded_messageboard_users", force: :cascade do |t|
t.integer "thredded_user_detail_id", null: false
t.integer "thredded_messageboard_id", null: false
t.datetime "last_seen_at", null: false
end
add_index "thredded_messageboard_users", ["thredded_messageboard_id", "last_seen_at"], name: "index_thredded_messageboard_users_for_recently_active"
add_index "thredded_messageboard_users", ["thredded_messageboard_id", "thredded_user_detail_id"], name: "index_thredded_messageboard_users_primary"
create_table "thredded_messageboards", force: :cascade do |t|
t.string "name", limit: 191, null: false
t.string "slug", limit: 191
t.text "description"
t.integer "topics_count", default: 0
t.integer "posts_count", default: 0
t.integer "position", null: false
t.integer "last_topic_id"
t.integer "messageboard_group_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "thredded_messageboards", ["messageboard_group_id"], name: "index_thredded_messageboards_on_messageboard_group_id"
add_index "thredded_messageboards", ["slug"], name: "index_thredded_messageboards_on_slug"
create_table "thredded_notifications_for_followed_topics", force: :cascade do |t|
t.integer "user_id", null: false
t.string "notifier_key", limit: 90, null: false
t.boolean "enabled", default: true, null: false
end
add_index "thredded_notifications_for_followed_topics", ["user_id", "notifier_key"], name: "thredded_notifications_for_followed_topics_unique", unique: true
create_table "thredded_notifications_for_private_topics", force: :cascade do |t|
t.integer "user_id", null: false
t.string "notifier_key", limit: 90, null: false
t.boolean "enabled", default: true, null: false
end
add_index "thredded_notifications_for_private_topics", ["user_id", "notifier_key"], name: "thredded_notifications_for_private_topics_unique", unique: true
create_table "thredded_post_moderation_records", force: :cascade do |t|
t.integer "post_id"
t.integer "messageboard_id"
t.text "post_content", limit: 65535
t.integer "post_user_id"
t.text "post_user_name"
t.integer "moderator_id"
t.integer "moderation_state", null: false
t.integer "previous_moderation_state", null: false
t.datetime "created_at", null: false
end
add_index "thredded_post_moderation_records", ["messageboard_id", "created_at"], name: "index_thredded_moderation_records_for_display"
create_table "thredded_posts", force: :cascade do |t|
t.integer "user_id"
t.text "content", limit: 65535
t.string "ip", limit: 255
t.string "source", limit: 255, default: "web"
t.integer "postable_id", null: false
t.integer "messageboard_id", null: false
t.integer "moderation_state", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "thredded_posts", ["messageboard_id"], name: "index_thredded_posts_on_messageboard_id"
add_index "thredded_posts", ["moderation_state", "updated_at"], name: "index_thredded_posts_for_display"
add_index "thredded_posts", ["postable_id"], name: "index_thredded_posts_on_postable_id_and_postable_type"
add_index "thredded_posts", ["user_id"], name: "index_thredded_posts_on_user_id"
create_table "thredded_private_posts", force: :cascade do |t|
t.integer "user_id"
t.text "content", limit: 65535
t.integer "postable_id", null: false
t.string "ip", limit: 255
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "thredded_private_topics", force: :cascade do |t|
t.integer "user_id"
t.integer "last_user_id"
t.string "title", limit: 255, null: false
t.string "slug", limit: 191, null: false
t.integer "posts_count", default: 0
t.string "hash_id", limit: 191, null: false
t.datetime "last_post_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "thredded_private_topics", ["hash_id"], name: "index_thredded_private_topics_on_hash_id"
add_index "thredded_private_topics", ["slug"], name: "index_thredded_private_topics_on_slug"
create_table "thredded_private_users", force: :cascade do |t|
t.integer "private_topic_id"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "thredded_private_users", ["private_topic_id"], name: "index_thredded_private_users_on_private_topic_id"
add_index "thredded_private_users", ["user_id"], name: "index_thredded_private_users_on_user_id"
create_table "thredded_topic_categories", force: :cascade do |t|
t.integer "topic_id", null: false
t.integer "category_id", null: false
end
add_index "thredded_topic_categories", ["category_id"], name: "index_thredded_topic_categories_on_category_id"
add_index "thredded_topic_categories", ["topic_id"], name: "index_thredded_topic_categories_on_topic_id"
create_table "thredded_topics", force: :cascade do |t|
t.integer "user_id"
t.integer "last_user_id"
t.string "title", limit: 255, null: false
t.string "slug", limit: 191, null: false
t.integer "messageboard_id", null: false
t.integer "posts_count", default: 0, null: false
t.boolean "sticky", default: false, null: false
t.boolean "locked", default: false, null: false
t.string "hash_id", limit: 191, null: false
t.string "type", limit: 191
t.integer "moderation_state", null: false
t.datetime "last_post_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "thredded_topics", ["hash_id"], name: "index_thredded_topics_on_hash_id"
add_index "thredded_topics", ["messageboard_id"], name: "index_thredded_topics_on_messageboard_id"
add_index "thredded_topics", ["moderation_state", "sticky", "updated_at"], name: "index_thredded_topics_for_display"
add_index "thredded_topics", ["slug"], name: "index_thredded_topics_on_slug", unique: true
add_index "thredded_topics", ["user_id"], name: "index_thredded_topics_on_user_id"
create_table "thredded_user_details", force: :cascade do |t|
t.integer "user_id", null: false
t.datetime "latest_activity_at"
t.integer "posts_count", default: 0
t.integer "topics_count", default: 0
t.datetime "last_seen_at"
t.integer "moderation_state", default: 0, null: false
t.datetime "moderation_state_changed_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "thredded_user_details", ["latest_activity_at"], name: "index_thredded_user_details_on_latest_activity_at"
add_index "thredded_user_details", ["moderation_state", "moderation_state_changed_at"], name: "index_thredded_user_details_for_moderations"
add_index "thredded_user_details", ["user_id"], name: "index_thredded_user_details_on_user_id"
create_table "thredded_user_messageboard_preferences", force: :cascade do |t|
t.integer "user_id", null: false
t.integer "messageboard_id", null: false
t.boolean "follow_topics_on_mention", default: true, null: false
t.boolean "auto_follow_topics", default: false, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "thredded_user_messageboard_preferences", ["user_id", "messageboard_id"], name: "thredded_user_messageboard_preferences_user_id_messageboard_id", unique: true
create_table "thredded_user_post_notifications", force: :cascade do |t|
t.integer "user_id", null: false
t.integer "post_id", null: false
t.datetime "notified_at", null: false
end
add_index "thredded_user_post_notifications", ["post_id"], name: "index_thredded_user_post_notifications_on_post_id"
add_index "thredded_user_post_notifications", ["user_id", "post_id"], name: "index_thredded_user_post_notifications_on_user_id_and_post_id", unique: true
create_table "thredded_user_preferences", force: :cascade do |t|
t.integer "user_id", null: false
t.boolean "follow_topics_on_mention", default: true, null: false
t.boolean "auto_follow_topics", default: false, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "thredded_user_preferences", ["user_id"], name: "index_thredded_user_preferences_on_user_id"
create_table "thredded_user_private_topic_read_states", force: :cascade do |t|
t.integer "user_id", null: false
t.integer "postable_id", null: false
t.integer "page", default: 1, null: false
t.datetime "read_at", null: false
end
add_index "thredded_user_private_topic_read_states", ["user_id", "postable_id"], name: "thredded_user_private_topic_read_states_user_postable", unique: true
create_table "thredded_user_topic_follows", force: :cascade do |t|
t.integer "user_id", null: false
t.integer "topic_id", null: false
t.datetime "created_at", null: false
t.integer "reason", limit: 1
end
add_index "thredded_user_topic_follows", ["user_id", "topic_id"], name: "thredded_user_topic_follows_user_topic", unique: true
create_table "thredded_user_topic_read_states", force: :cascade do |t|
t.integer "user_id", null: false
t.integer "postable_id", null: false
t.integer "page", default: 1, null: false
t.datetime "read_at", null: false
end
add_index "thredded_user_topic_read_states", ["user_id", "postable_id"], name: "thredded_user_topic_read_states_user_postable", unique: true
create_table "universes", force: :cascade do |t|
t.string "name", null: false
t.text "description"

15
test/fixtures/content_change_events.yml vendored Normal file
View File

@ -0,0 +1,15 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
user_id:
changed_fields: MyText
content_id: 1
content_type: MyString
action: MyString
two:
user_id:
changed_fields: MyText
content_id: 1
content_type: MyString
action: MyString

View File

@ -0,0 +1,7 @@
require 'test_helper'
class ContentChangeEventTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end