From d94eb22fc6014e64c4c595de49fa8bc924bb31e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andres=20Keskk=C3=BCla?= Date: Fri, 26 Sep 2014 15:01:02 +0300 Subject: [PATCH] Logging contacts and addresses,view undeleted ones --- Gemfile | 3 + Gemfile.lock | 4 ++ .../admin/contact_versions_controller.rb | 17 +++++ app/models/address.rb | 3 +- app/models/address_version.rb | 4 ++ app/models/contact.rb | 4 ++ app/models/contact_version.rb | 4 ++ app/views/admin/contact_versions/index.haml | 40 +++++++++++ app/views/admin/contact_versions/show.haml | 65 ++++++++++++++++++ app/views/layouts/application.haml | 13 ++++ config/routes.rb | 5 +- db/migrate/20140926081324_create_versions.rb | 13 ++++ ...926082627_contact_and_version_archiving.rb | 25 +++++++ db/schema.rb | 66 +++++++++++++++++++ 14 files changed, 263 insertions(+), 3 deletions(-) create mode 100644 app/controllers/admin/contact_versions_controller.rb create mode 100644 app/models/address_version.rb create mode 100644 app/models/contact_version.rb create mode 100644 app/views/admin/contact_versions/index.haml create mode 100644 app/views/admin/contact_versions/show.haml create mode 100644 db/migrate/20140926081324_create_versions.rb create mode 100644 db/migrate/20140926082627_contact_and_version_archiving.rb diff --git a/Gemfile b/Gemfile index 20b60e91d..7554cf5c0 100644 --- a/Gemfile +++ b/Gemfile @@ -58,6 +58,9 @@ gem 'cancan', '~> 1.6.10' # for login gem 'devise', '~> 3.3.0' +# for archiving +gem 'paper_trail', '~> 3.0.5' + group :assets do # See https://github.com/sstephenson/execjs#readme for more supported runtimes gem 'therubyracer', platforms: :ruby diff --git a/Gemfile.lock b/Gemfile.lock index 8d23eacd5..36d4a25cb 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -125,6 +125,9 @@ GEM mini_portile (= 0.6.0) nprogress-rails (0.1.3.1) orm_adapter (0.5.0) + paper_trail (3.0.5) + activerecord (>= 3.0, < 5.0) + activesupport (>= 3.0, < 5.0) parser (2.2.0.pre.4) ast (>= 1.1, < 3.0) slop (~> 3.4, >= 3.4.5) @@ -282,6 +285,7 @@ DEPENDENCIES kaminari (~> 0.16.1) nokogiri (~> 1.6.2.1) nprogress-rails (~> 0.1.3.1) + paper_trail (~> 3.0.5) pg phantomjs (~> 1.9.7.1) phantomjs-binaries (~> 1.9.2.4) diff --git a/app/controllers/admin/contact_versions_controller.rb b/app/controllers/admin/contact_versions_controller.rb new file mode 100644 index 000000000..4103e6abc --- /dev/null +++ b/app/controllers/admin/contact_versions_controller.rb @@ -0,0 +1,17 @@ +class Admin::ContactVersionsController < AdminController + before_action :set_contact + + def index + @q = Contact.search(params[:q]) + @contacts = @q.result.page(params[:page]) + end + + def show + @versions = @contact.versions + end + + private + def set_contact + @contact = Contact.find(params[:id]) + end +end diff --git a/app/models/address.rb b/app/models/address.rb index af33d3646..a957b86f1 100644 --- a/app/models/address.rb +++ b/app/models/address.rb @@ -10,8 +10,7 @@ class Address < ActiveRecord::Base belongs_to :contact belongs_to :country - # TODO: validate inclusion of :type in LONG_TYPES or smth similar - # validates_inclusion_of :type, in: TYPES + has_paper_trail class_name: 'AddressVersion' class << self # def validate_postal_info_types(parsed_frame) diff --git a/app/models/address_version.rb b/app/models/address_version.rb new file mode 100644 index 000000000..6f7fb7b7c --- /dev/null +++ b/app/models/address_version.rb @@ -0,0 +1,4 @@ +class AddressVersion < PaperTrail::Version + self.table_name = :address_versions + self.sequence_name = :address_version_id_seq +end diff --git a/app/models/contact.rb b/app/models/contact.rb index 8ecb78134..cec424eb6 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -1,6 +1,7 @@ class Contact < ActiveRecord::Base # TODO: Foreign contact will get email with activation link/username/temp password # TODO: Phone number validation, in first phase very minimam in order to support current registries + # TODO: Validate presence of name include EppErrors @@ -32,6 +33,9 @@ class Contact < ActiveRecord::Base delegate :street, to: :address#, prefix: true delegate :zip, to: :address#, prefix: true + # archiving + has_paper_trail class_name: 'AddressVersion' + IDENT_TYPE_ICO = 'ico' IDENT_TYPES = [ IDENT_TYPE_ICO, # Company registry code (or similar) diff --git a/app/models/contact_version.rb b/app/models/contact_version.rb new file mode 100644 index 000000000..6d55f6568 --- /dev/null +++ b/app/models/contact_version.rb @@ -0,0 +1,4 @@ +class ContactVersion < PaperTrail::Version + self.table_name = :contact_versions + self.sequence_name = :contact_version_id_seq +end diff --git a/app/views/admin/contact_versions/index.haml b/app/views/admin/contact_versions/index.haml new file mode 100644 index 000000000..77cc70a8f --- /dev/null +++ b/app/views/admin/contact_versions/index.haml @@ -0,0 +1,40 @@ +.row + .col-sm-6 + %h2.text-center-xs= t('shared.contacts_history') + = render 'admin/contacts/partials/search' + .col-sm-6 + %h2.text-right.text-center-xs + = link_to(t('shared.add'), new_admin_contact_path, class: 'btn btn-primary') +%hr +.row + .col-md-12 + .table-responsive + %table.table.table-hover.table-bordered.table-condensed + %thead + %tr + %th{class: 'col-xs-2'} + = sort_link(@q, 'name', t('shared.name')) + %th{class: 'col-xs-1'} + = sort_link(@q, 'code', t('shared.code')) + %th{class: 'col-xs-1'} + whodunnit + = #sort_link(@q, 'ident', t('shared.identity_code')) + %th{class: 'col-xs-1'} + event + = #sort_link(@q, 'email', t('shared.email')) + %th{class: 'col-xs-2'} + created_at + %th{class: 'col-xs-1'} + = t('shared.action') + %tbody + - @contacts.each do |x| + %tr + %td= x.name #link_to(x, admin_contact_path(x)) + %td= x.code #link_to(x, admin_contact_path(x)) + %td= x.versions.last.whodunnit if x.versions.last + %td= x.versions.last.event if x.versions.last + %td= x.versions.last.created_at if x.versions.last + %td= link_to(t('shared.history'), admin_contact_version_path(x), class: 'btn btn-primary btn-xs') +.row + .col-md-12 + = paginate @contacts diff --git a/app/views/admin/contact_versions/show.haml b/app/views/admin/contact_versions/show.haml new file mode 100644 index 000000000..7cbd5e271 --- /dev/null +++ b/app/views/admin/contact_versions/show.haml @@ -0,0 +1,65 @@ +.row + .col-sm-6 + %h2.text-center-xs + = "#{t('shared.contact_details')}" + .col-sm-6 + %h2.text-right.text-center-xs + +%hr +.row + .col-md-12 + .table-responsive + %table.table.table-hover.table-bordered.table-condensed + %thead + %tr + %th + whodunnit + %th + event + %th + created_at + + + + + %tbody + - @versions.each_with_index do |version, index| + - contact = version.reify(has_one: true) + - if contact + %tr.clickable{"data-target" => "#accordion-#{index}", "data-toggle" => "collapse"} + %td=version.whodunnit #contact.name if contact + %td=version.event #contact.ident if contact + %td=version.created_at #contact.email if contact + + %tr + %td{colspan: "3"} + .collapse{ id: "accordion-#{index}" } + .row + .col-md-6 + %dl.dl-horizontal + %dt= t('shared.name') + %dd= contact.name + %dt= t('shared.ident') + %dd= contact.ident + %dt= t('shared.phone') + %dd= contact.phone + %dt= t('shared.email') + %dd= contact.email + %dt= t('shared.fax') + %dd= contact.fax + %dt= t('shared.org_name') + %dd= contact.org_name + .col-md-6 + %dl.dl-horizontal + - if contact.address + %dt= t('shared.city') + %dd= contact.address.city + %dt= t('shared.street') + %dd= contact.address.street + + - else + %tr + %td=version.whodunnit #contact.name if contact + %td=version.event #contact.ident if contact + %td=version.created_at #contact.email if contact + diff --git a/app/views/layouts/application.haml b/app/views/layouts/application.haml index 035e4bfc3..4ff437c55 100644 --- a/app/views/layouts/application.haml +++ b/app/views/layouts/application.haml @@ -43,6 +43,19 @@ = link_to t('shared.epp_users'), admin_epp_users_path %li = link_to t('shared.users'), admin_users_path + + %li.dropdown + %a.dropdown-toggle{"data-toggle" => "dropdown", href: "#"} + = t('shared.archives') + %span.caret + %ul.dropdown-menu{role: "menu"} + %li.dropdown-header= t('shared.domain') + %li + = link_to t('shared.domains'), '#' + %li.dropdown-header= t('shared.contact') + %li + = link_to t('shared.contacts'), admin_contact_versions_path + %ul.nav.navbar-nav.navbar-right %li= link_to t('shared.log_out', user: current_user), '/logout' / /.nav-collapse diff --git a/config/routes.rb b/config/routes.rb index 74db2fd3f..7ed278e19 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -23,9 +23,12 @@ Rails.application.routes.draw do resources :users resources :epp_users + resources :contact_versions + resources :address_versions root 'domains#index' - end + + end ## CLIENT ROUTES namespace(:client) do diff --git a/db/migrate/20140926081324_create_versions.rb b/db/migrate/20140926081324_create_versions.rb new file mode 100644 index 000000000..23be970c6 --- /dev/null +++ b/db/migrate/20140926081324_create_versions.rb @@ -0,0 +1,13 @@ +class CreateVersions < ActiveRecord::Migration + def change + create_table :versions do |t| + 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 + end + add_index :versions, [:item_type, :item_id] + end +end diff --git a/db/migrate/20140926082627_contact_and_version_archiving.rb b/db/migrate/20140926082627_contact_and_version_archiving.rb new file mode 100644 index 000000000..bc521159d --- /dev/null +++ b/db/migrate/20140926082627_contact_and_version_archiving.rb @@ -0,0 +1,25 @@ +class ContactAndVersionArchiving < ActiveRecord::Migration + def change + create_table :contact_versions do |t| + 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 + end + + create_table :address_versions do |t| + 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 + end + + add_index :contact_versions, [:item_type, :item_id] + add_index :address_versions, [:item_type, :item_id] + + end +end diff --git a/db/schema.rb b/db/schema.rb index 111f4a1d4..65c0fba80 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -16,6 +16,17 @@ ActiveRecord::Schema.define(version: 20141001085322) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" + create_table "address_versions", force: true do |t| + 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" + end + + add_index "address_versions", ["item_type", "item_id"], name: "index_address_versions_on_item_type_and_item_id", using: :btree + create_table "addresses", force: true do |t| t.integer "contact_id" t.integer "country_id" @@ -43,6 +54,17 @@ ActiveRecord::Schema.define(version: 20141001085322) do t.datetime "updated_at" end + create_table "contact_versions", force: true do |t| + 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" + end + + add_index "contact_versions", ["item_type", "item_id"], name: "index_contact_versions_on_item_type_and_item_id", using: :btree + create_table "contacts", force: true do |t| t.string "code" t.string "type" @@ -84,6 +106,17 @@ ActiveRecord::Schema.define(version: 20141001085322) do t.datetime "updated_at" end + create_table "domain_status_versions", force: true do |t| + 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" + end + + add_index "domain_status_versions", ["item_type", "item_id"], name: "index_domain_status_versions_on_item_type_and_item_id", using: :btree + create_table "domain_statuses", force: true do |t| t.integer "domain_id" t.string "description" @@ -102,6 +135,17 @@ ActiveRecord::Schema.define(version: 20141001085322) do t.datetime "wait_until" end + create_table "domain_versions", force: true do |t| + 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" + end + + add_index "domain_versions", ["item_type", "item_id"], name: "index_domain_versions_on_item_type_and_item_id", using: :btree + create_table "domains", force: true do |t| t.string "name" t.integer "registrar_id" @@ -140,6 +184,17 @@ ActiveRecord::Schema.define(version: 20141001085322) do t.datetime "updated_at" end + create_table "nameserver_versions", force: true do |t| + 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" + end + + add_index "nameserver_versions", ["item_type", "item_id"], name: "index_nameserver_versions_on_item_type_and_item_id", using: :btree + create_table "nameservers", force: true do |t| t.string "hostname" t.string "ipv4" @@ -211,4 +266,15 @@ ActiveRecord::Schema.define(version: 20141001085322) do t.integer "country_id" end + create_table "versions", force: true do |t| + 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" + end + + add_index "versions", ["item_type", "item_id"], name: "index_versions_on_item_type_and_item_id", using: :btree + end