diff --git a/app/controllers/versions_controller.rb b/app/controllers/versions_controller.rb new file mode 100644 index 00000000..75a4f64c --- /dev/null +++ b/app/controllers/versions_controller.rb @@ -0,0 +1,15 @@ +# Supervise all the actions done +class VersionsController < ApplicationController + def index + respond_to do |format| + format.html do + @search = PaperTrail::Version.search params[:q] + @search.sorts = 'created_at desc' if @search.sorts.empty? + @versions = @search.result.page params[:page] + end + format.rss do + @versions = PaperTrail::Version.order('created_at desc').limit(20) + end + end + end +end diff --git a/app/models/event.rb b/app/models/event.rb index d25cbe1b..585d87cc 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -4,7 +4,7 @@ class Event < ActiveRecord::Base extend SimpleCalendar strip_attributes - has_paper_trail + has_paper_trail ignore: [:last_updated, :secret, :submitter] belongs_to :region has_many :notes, dependent: :destroy diff --git a/app/models/orga.rb b/app/models/orga.rb index e832f64b..13dbc5a9 100644 --- a/app/models/orga.rb +++ b/app/models/orga.rb @@ -1,6 +1,7 @@ -# Groups related to this agenda +# Organisations related to this agenda class Orga < ActiveRecord::Base - has_paper_trail + strip_attributes + has_paper_trail ignore: [:last_updated, :secret, :submitter] belongs_to :region belongs_to :kind @@ -54,4 +55,8 @@ class Orga < ActiveRecord::Base def name_as_tag name.gsub(/\AL'/, '').gsub(/[\s\*']/, '-').delete ':' end + + def to_s + "[#{kind.name}] #{name}" + end end diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index c18eb93e..d477df47 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -62,6 +62,9 @@ = link_to orgas_path do %em.fa.fa-users = Orga.model_name.human.pluralize + = link_to versions_path do + %em.fa.fa-exchange + = t 'versions.index.title' = link_to application_infos_path do %em.fa.fa-info = t '.infos' diff --git a/app/views/versions/index.haml b/app/views/versions/index.haml new file mode 100644 index 00000000..3e0daeee --- /dev/null +++ b/app/views/versions/index.haml @@ -0,0 +1,49 @@ +%h2 + %em.fa.fa-exchange + = title t '.title' + +%table.list.autopagerize_page_element + %thead + %th.event= sort_link @search, :event + %th.whodunnit= sort_link @search, :whodunnit + %th.object= sort_link @search, :object_id + %th.created_at= sort_link @search, :created_at + %th.view/ + + %tbody + - @versions.each do |version| + :ruby + if version.event == 'create' + object = version.item_type.constantize.find version.item_id + else + object = version.reify + end + next unless object.moderated? + + changes = '' + if version.event == 'update' + version.changeset.each do |key, val| + changes += version.item_type.constantize.human_attribute_name(key) + changes += ': ' + if key == 'description' + changes += Differ.diff(val[1], val[0]).to_s + else + changes += "#{val[0]} -> #{val[1]}" + end + changes += ' + ' + end + changes = strip_tags(changes).gsub(/^\s*/, '') + end + + %tr + %td{ title: changes }= t ".#{version.event}_html" + %td= User.find version.whodunnit if version.whodunnit + %td= object + %td= l version.created_at + %td + %a{ href: polymorphic_path(version.item_type.tableize.singularize, + id: version.item_id) } + %em.fa.fa-eye + += paginate @versions diff --git a/app/views/versions/index.rss.builder b/app/views/versions/index.rss.builder new file mode 100644 index 00000000..d7738297 --- /dev/null +++ b/app/views/versions/index.rss.builder @@ -0,0 +1,78 @@ +require 'differ/format/patch' +Differ.format = Differ::Format::Patch + +xml.instruct! + +xml.rdf :RDF, + 'xmlns:rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', + 'xmlns' => 'http://purl.org/rss/1.0/', + 'xmlns:dc' => 'http://purl.org/dc/elements/1.1/', + 'xmlns:sy' => 'http://purl.org/rss/1.0/modules/syndication/', + 'xmlns:admin' => 'http://webns.net/mvcb/', + 'xmlns:cc' => 'http://web.resource.org/cc/', + 'xmlns:content' => 'http://purl.org/rss/1.0/modules/content/', + 'xmlns:georss' => 'http://www.georss.org/georss' do + xml.channel 'rdf:about' => root_url do + title = t 'layouts.application.title' + if params[:region].present? && params[:region] != 'all' + region = Region.find(params[:region]).name + title += " [#{region}]" + end + xml.title title + xml.description t 'layouts.application.subtitle' + xml.link root_url + xml.dc :language, 'fr' + xml.dc :creator, root_url + + xml.items do + xml.rdf :Seq do + @versions.each do |version| + xml.rdf :li, 'rdf:resource' => + polymorphic_url(version.item_type.tableize.singularize, + id: version.item_id) + end + end + end + end + + @versions.each do |version| + if version.event == 'create' + object = version.item_type.constantize.find version.item_id + else + object = version.reify + end + next unless object.moderated? + url = polymorphic_url(version.item_type.tableize.singularize, + id: version.item_id) + + xml.item 'rdf:about' => url do + xml.title "#{version.event} - #{object}" + xml.link url + domain = root_url.gsub(/www/, '').gsub(/http.?:../, '').gsub(/:.*/, '') + xml.dc :identifier, "version_#{version.id}@#{domain}" + xml.dc :date, version.created_at.iso8601 + + if object.try(:description) + if version.event == 'create' + xml.description strip_tags object.description + xml.content(:encoded) { xml.cdata! object.description } + elsif version.event == 'update' + changes = '' + version.changeset.each do |key, val| + changes += version.item_type.constantize.human_attribute_name(key) + changes += ': ' + if key == 'description' + changes += Differ.diff(val[1], val[0]).to_s + else + changes += "#{val[0]} -> #{val[1]}" + end + changes += ' + ' + end + xml.description changes + xml.content(:encoded) { xml.cdata! changes } + end + end + end + end +end diff --git a/config/locales/views/en.yml b/config/locales/views/en.yml index 16606854..b9f7c31e 100644 --- a/config/locales/views/en.yml +++ b/config/locales/views/en.yml @@ -380,15 +380,7 @@ reason: signature: Have a good day! accept: subject: "Organisation '%{subject}' moderated" - body: "Organisation has been modérée by %{moderator}. -\nYou can acces it here:" - edit_link: "Vous pouvez modifier cette organisation ultérieurement pour y ajouter des -\nprécisions en vous rendant à l'adresse:" - delete_link: "You can also delete it using the address:" - signature: Thank you for your contribution and see you soon! - accept: - subject: "Organisation '%{subject}' moderated" - body: The orgnisation has been moderated by %{author}. + body: The organisation has been moderated by %{author}. access: "You can consult it here:" signature: Thanks for your contribution! destroy: @@ -399,3 +391,10 @@ reason: moderation team. reminder: "Reminder, here is your organisation's content:" signature: With all our thanks for your contribution + + versions: + index: + title: Versions + create_html: + update_html: + destroy_html: diff --git a/config/locales/views/fr.yml b/config/locales/views/fr.yml index 8ee7a7c5..58a0d756 100644 --- a/config/locales/views/fr.yml +++ b/config/locales/views/fr.yml @@ -391,3 +391,10 @@ l'adresse:" de modérateurs. reminder: "Pour rappel, voici le contenu de l'organisation:" signature: Avec tous nos remerciements pour votre contribution + + versions: + index: + title: Versions + create_html: + update_html: + destroy_html: diff --git a/config/routes.rb b/config/routes.rb index 2347f406..80f21d67 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,6 +3,7 @@ Rails.application.routes.draw do get 'application/contact' get 'application/rules' get 'stats', to: 'stats#index' + get 'versions', to: 'versions#index' resources :users resources :events do diff --git a/db/migrate/20160109203136_add_object_changes_to_versions.rb b/db/migrate/20160109203136_add_object_changes_to_versions.rb new file mode 100644 index 00000000..e3bc70f0 --- /dev/null +++ b/db/migrate/20160109203136_add_object_changes_to_versions.rb @@ -0,0 +1,10 @@ +# Can trace the exact changes to paper trailed objects +class AddObjectChangesToVersions < ActiveRecord::Migration + # The largest text column available in all supported RDBMS. + # See `create_versions.rb` for details. + TEXT_BYTES = 1_073_741_823 + + def change + add_column :versions, :object_changes, :text, limit: TEXT_BYTES + end +end diff --git a/db/schema.rb b/db/schema.rb index 65bf495b..bacefd2b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160107203117) do +ActiveRecord::Schema.define(version: 20160109203136) do create_table "active_admin_comments", force: :cascade do |t| t.string "namespace", limit: 255 @@ -121,6 +121,7 @@ ActiveRecord::Schema.define(version: 20160107203117) do t.text "tag" t.text "tags", default: "" t.text "diaspora" + t.text "object_changes" end add_index "orgas", ["kind_id"], name: "index_orgas_on_kind_id" @@ -166,12 +167,13 @@ ActiveRecord::Schema.define(version: 20160107203117) do end create_table "versions", force: :cascade do |t| - t.string "item_type", null: false - t.integer "item_id", null: false - t.string "event", null: false + t.string "item_type", null: false + t.integer "item_id", null: false + t.string "event", null: false t.string "whodunnit" t.text "object" t.datetime "created_at" + t.text "object_changes", limit: 1073741823 end add_index "versions", ["item_type", "item_id"], name: "index_versions_on_item_type_and_item_id"