simple_history 0.0.2 → 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: d62fdbb3ae91c81ddf37782dacaf5fb3be9b36eb0f31e338bfb8754bcd178b83
4
- data.tar.gz: 16ad2744c7d359dc75064f9a3e54624744a0c7702cf020162525e3625c7d7baf
3
+ metadata.gz: 3d0190eb6cf9e58fb60ec05e637139d042f497fa9fb41773b757890e9fbccb6d
4
+ data.tar.gz: e65813c3a0c4e59148ab8afdbc89a3e2915bef499fe22ee8911176f778fddd66
5
5
  SHA512:
6
- metadata.gz: ba4cc0becf2499acb531b1613d2c8c32c134c52b9d0a0dfd3d925dc7f547c2ed5c42e25926eed078e7cff01755132f5f76b4eae5f0cb01ed3aab192557b65871
7
- data.tar.gz: 593494fe280618cc613d4878c134aff83ec9d37ec4099f91f3a011c493de02afc80c29da1a12dc4fdef11cf94a6eae06119f34ccaf227a752da320fee547f174
6
+ metadata.gz: 49c16db263ce05c4abbc74853621d044c98da8b06bc467bdf840edd8e4fe5b668ce7be56d93904acae720be777c7ea5fecc7b89ebd962b87117a2d0fa11f5673
7
+ data.tar.gz: 296bc1f8107a6f8cc58c95f4b9393a46a3574dd468883392321ece1b6e19929a123bde66eb56b6a4355cca2970913a1dbb6746aa7c49bfbad7e5ff738fa1be4c
@@ -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,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.2"
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,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_history
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrea Fomera
@@ -37,13 +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
42
43
  - app/mailers/simple_history/application_mailer.rb
43
44
  - app/models/simple_history/application_record.rb
45
+ - app/models/simple_history/history.rb
46
+ - app/models/simple_history/simple_history.rb
44
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
45
52
  - config/routes.rb
53
+ - db/migrate/20230730180547_create_simple_history_histories.rb
46
54
  - lib/simple_history.rb
55
+ - lib/simple_history/attribute.rb
47
56
  - lib/simple_history/engine.rb
48
57
  - lib/simple_history/version.rb
49
58
  - lib/tasks/simple_history_tasks.rake