simple_history 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2fd6f4ec5b267ca18991ac5a6957e15b84bc021123714808afd057b7852eaae9
4
- data.tar.gz: 2eed4c5497f9dc51208390b53cfed2c6be5cadd9253e540782ef9a1a64eafe78
3
+ metadata.gz: 3d0190eb6cf9e58fb60ec05e637139d042f497fa9fb41773b757890e9fbccb6d
4
+ data.tar.gz: e65813c3a0c4e59148ab8afdbc89a3e2915bef499fe22ee8911176f778fddd66
5
5
  SHA512:
6
- metadata.gz: f19f554f14a252f34caa95526349b39d1b2a10374918b0006c89d55df72209e3c9162c1b7bc938b1ad714e7c98f780a461a98c2d61c465b82d1fd204f0cd8308
7
- data.tar.gz: 8f7bdccd181b69b27ba2226056cf34c99161b1eb1709b5fa9af828094a084e6677963aadc05e1684dd95bfc68ebe66bb82994c72081cfae5759902af68fcb279
6
+ metadata.gz: 49c16db263ce05c4abbc74853621d044c98da8b06bc467bdf840edd8e4fe5b668ce7be56d93904acae720be777c7ea5fecc7b89ebd962b87117a2d0fa11f5673
7
+ data.tar.gz: 296bc1f8107a6f8cc58c95f4b9393a46a3574dd468883392321ece1b6e19929a123bde66eb56b6a4355cca2970913a1dbb6746aa7c49bfbad7e5ff738fa1be4c
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2021 Andrea Fomera
1
+ Copyright 2023 Andrea Fomera
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/Rakefile CHANGED
@@ -6,12 +6,3 @@ load "rails/tasks/engine.rake"
6
6
  load "rails/tasks/statistics.rake"
7
7
 
8
8
  require "bundler/gem_tasks"
9
- require "rake/testtask"
10
-
11
- Rake::TestTask.new(:test) do |t|
12
- t.libs << "test"
13
- t.pattern = "test/**/*_test.rb"
14
- t.verbose = false
15
- end
16
-
17
- task default: :test
@@ -0,0 +1,18 @@
1
+ module SimpleHistory
2
+ class HistoriesController < ApplicationController
3
+ def index
4
+ if params["record_type"].present? && params["record_id"].present?
5
+ @histories = History.where(record_type: params["record_type"], record_id: params["record_id"]).order(created_at: :desc)
6
+ elsif params["record_type"].present?
7
+ @histories = History.where(record_type: params["record_type"]).order(created_at: :desc)
8
+ else
9
+ @histories = History.all.order(created_at: :desc)
10
+ end
11
+ @record_types = History.pluck(:record_type).uniq
12
+ end
13
+
14
+ def show
15
+ @history = History.find(params[:id])
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,6 @@
1
+ module SimpleHistory
2
+ class ApplicationMailer < ActionMailer::Base
3
+ default from: "from@example.com"
4
+ layout "mailer"
5
+ end
6
+ end
@@ -1,5 +1,5 @@
1
1
  module SimpleHistory
2
2
  class ApplicationRecord < ActiveRecord::Base
3
- primary_abstract_class
3
+ self.abstract_class = true
4
4
  end
5
5
  end
@@ -0,0 +1,8 @@
1
+ module SimpleHistory
2
+ class History < ApplicationRecord
3
+ belongs_to :record, polymorphic: true
4
+ belongs_to :user, polymorphic: true, optional: true
5
+
6
+ serialize :changed_data, JSON
7
+ end
8
+ end
@@ -0,0 +1,7 @@
1
+ module SimpleHistory
2
+ module SimpleHistory
3
+ def self.table_name_prefix
4
+ "simple_history_simple_history_"
5
+ end
6
+ end
7
+ end
@@ -1,15 +1,21 @@
1
1
  <!DOCTYPE html>
2
2
  <html>
3
- <head>
4
- <title>Simple history</title>
5
- <%= csrf_meta_tags %>
6
- <%= csp_meta_tag %>
3
+ <head>
4
+ <title>SimpleHistory</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <%= csrf_meta_tags %>
7
+ <%= csp_meta_tag %>
8
+ <%= stylesheet_link_tag "simple_history/application", media: "all" %>
9
+ <script src="https://cdn.tailwindcss.com?plugins=forms,typography,aspect-ratio"></script>
10
+ </head>
7
11
 
8
- <%= stylesheet_link_tag "simple_history/application", media: "all" %>
9
- </head>
10
- <body>
11
-
12
- <%= yield %>
13
-
14
- </body>
12
+ <body>
13
+ <div class="flex gap-4">
14
+ <%= render "simple_history/shared/nav" %>
15
+ <div class="ml-72 w-full p-4 sm:p-8 prose max-w-none prose-a:no-underline">
16
+ <%= render "simple_history/shared/flash" %>
17
+ <%= yield %>
18
+ </div>
19
+ </div>
20
+ </body>
15
21
  </html>
@@ -0,0 +1,18 @@
1
+ <div class="flex justify-between items-center">
2
+ <h1 class="mb-2">History</h1>
3
+ <div>
4
+ <%= form_tag(histories_path, method: :get) do %>
5
+ <%= select_tag :record_type, options_for_select(@record_types, params[:record_type]), class: "mt-2 block w-full rounded-md border-0 py-1.5 pl-3 pr-5 text-gray-900 ring-1 ring-inset ring-gray-300 focus:ring-2 focus:ring-slate-600 sm:text-sm sm:leading-6" %>
6
+ <%= text_field_tag :record_id, params[:record_id], placeholder: "ID", class: "border border-neutral-200 rounded p-2" %>
7
+ <%= submit_tag "Search", class: "bg-slate-900 text-white rounded p-2" %>
8
+ <% end %>
9
+ </div>
10
+ </div>
11
+
12
+ <% @histories.each do |history| %>
13
+ <div>
14
+ <%= link_to history do %>
15
+ <%= history.record_type %> #<%= history.record_id %> - <%= history.action %> - <%= history.created_at %>
16
+ <% end %>
17
+ </div>
18
+ <% end %>
@@ -0,0 +1,38 @@
1
+ <h1 class="mb-2"><%= @history.record_type %> <%= @history.record_id %></h1>
2
+ <p class="text-sm"><%= @history.action.humanize %> at: <%= @history.created_at %></p>
3
+
4
+ <% if @history.changed_data.present? && JSON.parse(@history.changed_data).any? %>
5
+ <div class="table w-full">
6
+ <div class="table-header-group">
7
+ <div class="table-row font-bold">
8
+ <div class="table-cell border-b border-neutral-200 p-2">Attribute</div>
9
+ <div class="table-cell border-b border-neutral-200 p-2">Previous value</div>
10
+ <div class="table-cell border-b border-neutral-200 p-2">New value</div>
11
+ </div>
12
+ </div>
13
+ <div class="table-row-group">
14
+ <% if @history.action == "destroyed" %>
15
+ <% JSON.parse(@history.changed_data).each do |change| %>
16
+ <div class="table-row">
17
+ <%= tag.div change[0], class: "table-cell border-b border-neutral-200 p-2" %>
18
+ <%= tag.div change[1], class: "table-cell border-b border-neutral-200 bg-red-50 p-2" %>
19
+ <%= tag.div "", class: "table-cell border-b border-neutral-200 bg-green-50 p-2" %>
20
+ </div>
21
+ <% end %>
22
+ <% else %>
23
+ <% JSON.parse(@history.changed_data).each do |change| %>
24
+ <div class="table-row">
25
+ <%= tag.div change[0], class: "table-cell border-b border-neutral-200 p-2" %>
26
+ <%= tag.div change[1][0], class: "table-cell border-b border-neutral-200 bg-red-50 p-2" %>
27
+ <%= tag.div change[1][1], class: "table-cell border-b border-neutral-200 bg-green-50 p-2" %>
28
+ </div>
29
+ <% end %>
30
+ <% end %>
31
+ </div>
32
+ </div>
33
+ <% end %>
34
+
35
+ <% if @history.record.present? %>
36
+ <h2 class="mt-4">Latest Attributes</h2>
37
+ <pre><code><%= JSON.pretty_generate(@history.record.attributes) %></code></pre>
38
+ <% end %>
@@ -0,0 +1,7 @@
1
+ <% if notice %>
2
+ <p><%= notice %></p>
3
+ <% end %>
4
+
5
+ <% if alert %>
6
+ <p><%= alert %></p>
7
+ <% end %>
@@ -0,0 +1,11 @@
1
+ <nav class="p-4 sm:p-8 fixed inset-y-0 w-72 bg-slate-100 text-dark">
2
+ <h1 class="flex items-center gap-1 font-extrabold">
3
+ <%= link_to "SimpleHistory", root_path %>
4
+ </h1>
5
+
6
+ <br />
7
+ <%= link_to "Back to app", main_app.root_path, class: "block text-sm" if main_app.root_path %>
8
+
9
+ <br />
10
+ <%= link_to "History", histories_path, class: "block" %>
11
+ </nav>
data/config/routes.rb CHANGED
@@ -1,2 +1,5 @@
1
1
  SimpleHistory::Engine.routes.draw do
2
+ root to: "histories#index"
3
+
4
+ resources :histories, only: [:index, :show]
2
5
  end
@@ -0,0 +1,12 @@
1
+ class CreateSimpleHistoryHistories < ActiveRecord::Migration[7.0]
2
+ def change
3
+ create_table :simple_history_histories do |t|
4
+ t.references :record, polymorphic: true, null: false
5
+ t.references :user, polymorphic: true, null: true
6
+ t.string :action
7
+ t.text :changed_data
8
+
9
+ t.timestamps
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,57 @@
1
+
2
+
3
+ module SimpleHistory
4
+ module Attribute
5
+ extend ActiveSupport::Concern
6
+
7
+ # thread_mattr_accessor :current_user
8
+
9
+ included do
10
+ after_create :record_simple_history
11
+ after_update_commit :record_simple_history
12
+ after_destroy :record_simple_history
13
+ end
14
+
15
+ private
16
+
17
+ def record_simple_history
18
+ # SimpleHistory::RecordHistoryJob.perform_later(
19
+ # self, self.changes.to_json, SimpleHistory::HasSimpleHistory.current_user, history_action
20
+ # )
21
+ # SimpleHistory::Attribute.current_user = nil
22
+
23
+ if history_action.eql?('destroyed')
24
+ History.create(
25
+ record: self,
26
+ changed_data: self.attributes.to_json,
27
+ user: nil,
28
+ action: history_action
29
+ )
30
+ else
31
+
32
+ return if self.previous_changes.empty?
33
+ History.create(
34
+ record: self,
35
+ changed_data: self.previous_changes.to_json,
36
+ user: nil,
37
+ action: history_action
38
+ )
39
+ end
40
+ end
41
+
42
+ def history_action
43
+ return 'destroyed' if destroyed?
44
+ return 'created' if created_at.eql?(updated_at)
45
+ return 'updated' if updated_at > created_at
46
+ end
47
+ end
48
+ end
49
+
50
+ # Load the has_simple_history methods
51
+ ActiveSupport.on_load(:active_record) do
52
+ class ActiveRecord::Base
53
+ def self.has_simple_history(options = {})
54
+ include SimpleHistory::Attribute
55
+ end
56
+ end
57
+ end
@@ -1,3 +1,3 @@
1
1
  module SimpleHistory
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -1,6 +1,8 @@
1
1
  require "simple_history/version"
2
2
  require "simple_history/engine"
3
3
 
4
+ require "simple_history/attribute"
5
+
4
6
  module SimpleHistory
5
7
  # Your code goes here...
6
8
  end
metadata CHANGED
@@ -1,30 +1,30 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_history
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrea Fomera
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-10-17 00:00:00.000000000 Z
11
+ date: 2023-07-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: activerecord
14
+ name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '5.2'
19
+ version: 7.0.6
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '5.2'
27
- description:
26
+ version: 7.0.6
27
+ description: Simple historical tracking of changes
28
28
  email:
29
29
  - afomera@hey.com
30
30
  executables: []
@@ -37,12 +37,22 @@ files:
37
37
  - app/assets/config/simple_history_manifest.js
38
38
  - app/assets/stylesheets/simple_history/application.css
39
39
  - app/controllers/simple_history/application_controller.rb
40
+ - app/controllers/simple_history/histories_controller.rb
40
41
  - app/helpers/simple_history/application_helper.rb
41
42
  - app/jobs/simple_history/application_job.rb
43
+ - app/mailers/simple_history/application_mailer.rb
42
44
  - app/models/simple_history/application_record.rb
45
+ - app/models/simple_history/history.rb
46
+ - app/models/simple_history/simple_history.rb
43
47
  - app/views/layouts/simple_history/application.html.erb
48
+ - app/views/simple_history/histories/index.html.erb
49
+ - app/views/simple_history/histories/show.html.erb
50
+ - app/views/simple_history/shared/_flash.html.erb
51
+ - app/views/simple_history/shared/_nav.html.erb
44
52
  - config/routes.rb
53
+ - db/migrate/20230730180547_create_simple_history_histories.rb
45
54
  - lib/simple_history.rb
55
+ - lib/simple_history/attribute.rb
46
56
  - lib/simple_history/engine.rb
47
57
  - lib/simple_history/version.rb
48
58
  - lib/tasks/simple_history_tasks.rake
@@ -67,8 +77,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
77
  - !ruby/object:Gem::Version
68
78
  version: '0'
69
79
  requirements: []
70
- rubygems_version: 3.2.3
80
+ rubygems_version: 3.4.10
71
81
  signing_key:
72
82
  specification_version: 4
73
- summary: An rails engine to provide basic historical tracking.
83
+ summary: Simple historical tracking of changes.
74
84
  test_files: []