records_rip 0.2.4 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +6 -2
- data/lib/generators/records_rip/install/templates/create_tombs.rb.erb +1 -1
- data/lib/records_rip.rb +2 -0
- data/lib/records_rip/model.rb +5 -2
- data/lib/records_rip/tomb.rb +8 -0
- data/lib/records_rip/version.rb +1 -1
- data/lib/records_rip/where_epitaph.rb +52 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ba53c93ab8aa6e03bf60d6aae43ab7ef670e57e1ddef44cb9110ec5249daa367
|
4
|
+
data.tar.gz: 90634bd3d59070c8787b3af20e381607a63077c02c8f34b4438612aa8c581186
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5a9461267bfca7bd06026366a4684c7182d87c7403c52335d3ce13a648db146bbde6c724ec6deb7d85b3551e5ce2259de80e0edce667b7bef5392fc81fcb7917
|
7
|
+
data.tar.gz: 62d179fc31026c9c8a6cf1be3b8f86bbb92b54366c889b74ceaf652640cce62d7387e4d3f330e93ca9d182be1a5dfecd5c7a0faeeeae10a68a0d0c02d4109820
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# Records, R.I.P.
|
2
2
|
|
3
|
-
This is a
|
3
|
+
This gem is a place to let deleted records rest in peace. :coffin:
|
4
|
+
|
5
|
+
Soft deletes are evil. _Blest be y man y spares thes stones. And curst be he y moves my bones._
|
4
6
|
|
5
7
|
## Getting Started
|
6
8
|
|
@@ -20,8 +22,10 @@ end
|
|
20
22
|
|
21
23
|
#### Scopes
|
22
24
|
|
25
|
+
You can perform WHERE queries for `tombs#epitaph` based on attributes:
|
26
|
+
|
23
27
|
```ruby
|
24
|
-
User.tomb
|
28
|
+
User.tomb(name: 'Smith')
|
25
29
|
```
|
26
30
|
|
27
31
|
## Contributing
|
@@ -11,7 +11,7 @@ class CreateTombs < ActiveRecord::Migration<%= migration_version %>
|
|
11
11
|
create_table :tombs<%= tombs_table_options %> do |t|
|
12
12
|
t.string :item_type<%= item_type_options %>
|
13
13
|
t.integer :item_id, null: false
|
14
|
-
t.text :
|
14
|
+
t.text :epitaph, limit: TEXT_BYTES
|
15
15
|
|
16
16
|
t.datetime :created_at
|
17
17
|
|
data/lib/records_rip.rb
CHANGED
data/lib/records_rip/model.rb
CHANGED
@@ -11,11 +11,14 @@ module RecordsRip
|
|
11
11
|
model_class.send(
|
12
12
|
"before_destroy",
|
13
13
|
lambda do |record|
|
14
|
-
|
14
|
+
epitaph = Hash[record.attributes.map {|k, v| [k, v]}]
|
15
|
+
::RecordsRip::Tomb.create(item_id: record.id, item_type: record.class.name, epitaph: epitaph)
|
15
16
|
end
|
16
17
|
)
|
17
18
|
|
18
|
-
|
19
|
+
define_singleton_method 'tomb' do |args = {}|
|
20
|
+
::RecordsRip::Tomb.where(item_type: model_class.to_s).where_epitaph(args)
|
21
|
+
end
|
19
22
|
end
|
20
23
|
end
|
21
24
|
end
|
data/lib/records_rip/tomb.rb
CHANGED
@@ -1,4 +1,12 @@
|
|
1
|
+
require "records_rip/where_epitaph"
|
2
|
+
|
1
3
|
module RecordsRip
|
2
4
|
class Tomb < ::ActiveRecord::Base
|
5
|
+
class << self
|
6
|
+
def where_epitaph(args = {})
|
7
|
+
raise ArgumentError, "expected to receive a Hash" unless args.is_a?(Hash)
|
8
|
+
WhereEpitaph.new(self, args).execute
|
9
|
+
end
|
10
|
+
end
|
3
11
|
end
|
4
12
|
end
|
data/lib/records_rip/version.rb
CHANGED
@@ -0,0 +1,52 @@
|
|
1
|
+
module RecordsRip
|
2
|
+
class WhereEpitaph
|
3
|
+
def initialize(tomb_model_class, attributes)
|
4
|
+
@tomb_model_class = tomb_model_class
|
5
|
+
@attributes = attributes
|
6
|
+
end
|
7
|
+
|
8
|
+
def execute
|
9
|
+
column = @tomb_model_class.columns_hash["epitaph"]
|
10
|
+
raise "where_epitaph can't be called without an epitaph column" unless column
|
11
|
+
|
12
|
+
case column.type
|
13
|
+
when :jsonb
|
14
|
+
jsonb
|
15
|
+
when :json
|
16
|
+
json
|
17
|
+
else
|
18
|
+
text
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def json
|
25
|
+
predicates = []
|
26
|
+
values = []
|
27
|
+
@attributes.each do |field, value|
|
28
|
+
predicates.push "epitaph->>? = ?"
|
29
|
+
values.concat([field, value.to_s])
|
30
|
+
end
|
31
|
+
sql = predicates.join(" and ")
|
32
|
+
@tomb_model_class.where(sql, *values)
|
33
|
+
end
|
34
|
+
|
35
|
+
def jsonb
|
36
|
+
@tomb_model_class.where("epitaph @> ?", @attributes.to_json)
|
37
|
+
end
|
38
|
+
|
39
|
+
def text
|
40
|
+
arel_field = @tomb_model_class.arel_table[:epitaph]
|
41
|
+
where_conditions = @attributes.map { |field, value|
|
42
|
+
where_epitaph_condition(arel_field, field, value)
|
43
|
+
}
|
44
|
+
where_conditions = where_conditions.reduce { |a, e| a.and(e) }
|
45
|
+
@tomb_model_class.where(where_conditions)
|
46
|
+
end
|
47
|
+
|
48
|
+
def where_epitaph_condition(arel_field, field, value)
|
49
|
+
arel_field.matches(%Q(%"#{field}"=>"#{value}"%)).or(arel_field.matches(%Q(%"#{field}"=>#{value}%)))
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: records_rip
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ts-3156
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-12-
|
11
|
+
date: 2018-12-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -121,6 +121,7 @@ files:
|
|
121
121
|
- lib/records_rip/model.rb
|
122
122
|
- lib/records_rip/tomb.rb
|
123
123
|
- lib/records_rip/version.rb
|
124
|
+
- lib/records_rip/where_epitaph.rb
|
124
125
|
- records_rip.gemspec
|
125
126
|
homepage: https://github.com/ts-3156/records_rip
|
126
127
|
licenses:
|