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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 950b22ac671b6bd1041d477455a63206f9a5ccff3f938c4ebfd32b338a218b91
4
- data.tar.gz: f1657a66341279d986d214b37ffa495723f0764a5c654ca932a723da6aa5b44e
3
+ metadata.gz: ba53c93ab8aa6e03bf60d6aae43ab7ef670e57e1ddef44cb9110ec5249daa367
4
+ data.tar.gz: 90634bd3d59070c8787b3af20e381607a63077c02c8f34b4438612aa8c581186
5
5
  SHA512:
6
- metadata.gz: 78091d999a338c25f988de11560d969f520df54f8db7adc1b6ebfa10fd84119154542142e29fcb9b3d0e9ddd91ffd5f6e06f7ee632977240b46b0fdd4abf50e4
7
- data.tar.gz: 876f8cd4655189bf4a9d91df8e2d2af10d264902c82cd4751226777c1968292b9eb5dc380fec02bc2151dab2718c4e02c8bcfa52e0bc066e2c10950c38d1150a
6
+ metadata.gz: 5a9461267bfca7bd06026366a4684c7182d87c7403c52335d3ce13a648db146bbde6c724ec6deb7d85b3551e5ce2259de80e0edce667b7bef5392fc81fcb7917
7
+ data.tar.gz: 62d179fc31026c9c8a6cf1be3b8f86bbb92b54366c889b74ceaf652640cce62d7387e4d3f330e93ca9d182be1a5dfecd5c7a0faeeeae10a68a0d0c02d4109820
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- records_rip (0.2.4)
4
+ records_rip (0.3.0)
5
5
  activesupport (>= 4.2, < 6.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # Records, R.I.P.
2
2
 
3
- This is a gem to let deleted records rest in peace. :coffin:
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.where(name: 'Smith')
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 :object, limit: TEXT_BYTES
14
+ t.text :epitaph, limit: TEXT_BYTES
15
15
 
16
16
  t.datetime :created_at
17
17
 
data/lib/records_rip.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  require "active_support"
2
+ require "active_record"
2
3
  require "records_rip/version"
3
4
  require "records_rip/model"
5
+ require "records_rip/tomb"
4
6
 
5
7
  ActiveSupport.on_load(:active_record) do
6
8
  include(RecordsRip::Model)
@@ -11,11 +11,14 @@ module RecordsRip
11
11
  model_class.send(
12
12
  "before_destroy",
13
13
  lambda do |record|
14
- ::RecordsRip::Tomb.create(item_id: record.id, item_type: record.class.name, object: '{}')
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
- scope :tomb, -> {::RecordsRip::Tomb.where(item_type: model_class.to_s)}
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
@@ -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
@@ -1,3 +1,3 @@
1
1
  module RecordsRip
2
- VERSION = "0.2.4"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -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.2.4
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-19 00:00:00.000000000 Z
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: