ru.Bee 2.7.6 → 2.7.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/db/test.db +0 -0
- data/lib/rubee/extensions/hookable.rb +2 -2
- data/lib/rubee/models/sequel_object.rb +8 -0
- data/lib/rubee.rb +1 -1
- data/lib/tests/controllers/base_controller_test.rb +20 -0
- data/lib/tests/models/comment_model_test.rb +20 -0
- data/readme.md +32 -3
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6fda6033bcea8b06d6c16acb8a311890bffff185065180d34e9251dd4dfde2ad
|
|
4
|
+
data.tar.gz: ad9be6f289ca514edc1f0a773bafde9d5f46205304e8752b139c665f1a792956
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8ab7565588364fa4c1590978209e1db758d6665ef38e7d86cbb8a7c3da7a2c376a4d34e699a0545c9855a6ac665b12950c888f2f0364ee1c1225f876adac1ec4
|
|
7
|
+
data.tar.gz: 43b5bee1d5f7ba3e878f4456363604d042fc1d5607260f435688882abced06cc92bde5a9a2864d6300e9eb1a6b90b34e6127d0f450cfa7f46bf48d62cc1d24fe
|
data/lib/db/test.db
CHANGED
|
Binary file
|
|
@@ -47,11 +47,11 @@ module Rubee
|
|
|
47
47
|
if conditions_met?(options[:if], options[:unless])
|
|
48
48
|
if handler.respond_to?(:call)
|
|
49
49
|
result = nil
|
|
50
|
-
safe_call(handler, [self, args]) do
|
|
50
|
+
handler_result = safe_call(handler, [self, args]) do
|
|
51
51
|
result = super(*args, &block)
|
|
52
52
|
end
|
|
53
53
|
|
|
54
|
-
result
|
|
54
|
+
result || handler_result
|
|
55
55
|
else
|
|
56
56
|
send(handler) do
|
|
57
57
|
super(*args, &block)
|
|
@@ -208,6 +208,14 @@ module Rubee
|
|
|
208
208
|
::Rubee::AssocArray.new([], self, query_dataset.where(**args))
|
|
209
209
|
end
|
|
210
210
|
|
|
211
|
+
def find_first(args, options = {})
|
|
212
|
+
where(args, options).order(:id).limit(1).last
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
def find_last(args, options = {})
|
|
216
|
+
where(args, options).order(id: :desc).limit(1).last
|
|
217
|
+
end
|
|
218
|
+
|
|
211
219
|
def order(args, options = {})
|
|
212
220
|
query_dataset = options[:__query_dataset] || dataset
|
|
213
221
|
|
data/lib/rubee.rb
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
require_relative '../test_helper'
|
|
2
2
|
|
|
3
3
|
class TestRedirectController < Rubee::BaseController
|
|
4
|
+
around :test_me, ->(controller, &test_method) do
|
|
5
|
+
if true # We wnat to make sure that origianl method is replaced
|
|
6
|
+
controller.response_with(type: :json, object: { hijacked: :yes })
|
|
7
|
+
else
|
|
8
|
+
test_method.call
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
4
12
|
def index
|
|
5
13
|
response_with(type: :redirect, to: '/test')
|
|
6
14
|
end
|
|
@@ -8,6 +16,10 @@ class TestRedirectController < Rubee::BaseController
|
|
|
8
16
|
def test
|
|
9
17
|
response_with(type: :json, object: { ok: :ok })
|
|
10
18
|
end
|
|
19
|
+
|
|
20
|
+
def test_me
|
|
21
|
+
response_with(type: :json, object: { ok: :ok })
|
|
22
|
+
end
|
|
11
23
|
end
|
|
12
24
|
|
|
13
25
|
class BaseControllerTest < Minitest::Test
|
|
@@ -21,6 +33,7 @@ class BaseControllerTest < Minitest::Test
|
|
|
21
33
|
Rubee::Router.draw do |route|
|
|
22
34
|
route.get('/test', to: 'test_redirect#test')
|
|
23
35
|
route.get('/index', to: 'test_redirect#index')
|
|
36
|
+
route.get('/test_me', to: 'test_redirect#test_me')
|
|
24
37
|
end
|
|
25
38
|
end
|
|
26
39
|
|
|
@@ -45,4 +58,11 @@ class BaseControllerTest < Minitest::Test
|
|
|
45
58
|
assert_equal('/test', last_response.headers['Location'])
|
|
46
59
|
assert_equal('', last_response.body)
|
|
47
60
|
end
|
|
61
|
+
|
|
62
|
+
def test_hijacked_test_by_around
|
|
63
|
+
get('/test_me')
|
|
64
|
+
|
|
65
|
+
assert_equal(200, last_response.status)
|
|
66
|
+
assert_equal({ "hijacked" => "yes" }, JSON.parse(last_response.body))
|
|
67
|
+
end
|
|
48
68
|
end
|
|
@@ -119,6 +119,26 @@ describe 'Comment model' do
|
|
|
119
119
|
end
|
|
120
120
|
end
|
|
121
121
|
|
|
122
|
+
describe 'find_first' do
|
|
123
|
+
it 'finds first record' do
|
|
124
|
+
Comment.destroy_all
|
|
125
|
+
comment_1 = Comment.create(text: 'test123123')
|
|
126
|
+
comment_2 = Comment.create(text: 'test123123')
|
|
127
|
+
_(Comment.find_first(text: 'test123123').id).must_equal(comment_1.id)
|
|
128
|
+
Comment.destroy_all
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
describe 'find_last' do
|
|
133
|
+
it 'finds last record' do
|
|
134
|
+
Comment.destroy_all
|
|
135
|
+
comment_1 = Comment.create(text: 'test123123')
|
|
136
|
+
comment_2 = Comment.create(text: 'test123123')
|
|
137
|
+
_(Comment.find_last(text: 'test123123').id).must_equal(comment_2.id)
|
|
138
|
+
Comment.destroy_all
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
|
|
122
142
|
describe 'method' do
|
|
123
143
|
it 'updates existing model' do
|
|
124
144
|
comment = Comment.new(text: 'test 1')
|
data/readme.md
CHANGED
|
@@ -380,7 +380,16 @@ Get all records scoped by a field
|
|
|
380
380
|
irb(main):005> User.where(email: "ok23@ok.com")
|
|
381
381
|
=> [#<User:0x000000010cfaa5c0 @email="ok23@ok.com", @id=2, @password="123">]
|
|
382
382
|
```
|
|
383
|
-
|
|
383
|
+
Get the first record, scoped by field. It is a shortcut for `User.where(email: "ok23@ok.com").order(:id).limit(1).last`
|
|
384
|
+
```ruby
|
|
385
|
+
irb(main):006> User.find_first(email: "ok23@ok.com")
|
|
386
|
+
=> #<User:0x000000010cfaa5c0 @email="ok23@ok.com", @id=2, @password="123">
|
|
387
|
+
```
|
|
388
|
+
Get the last record, scoped by field. It is a shortcut for `User.where(email: "ok23@ok.com").order(id: :desc).limit(1).last`
|
|
389
|
+
```ruby
|
|
390
|
+
irb(main):007> User.find_last(email: "ok23@ok.com")
|
|
391
|
+
=> #<User:0x000000010cfaa5c0 @email="ok23@ok.com", @id=2, @password="123">
|
|
392
|
+
```
|
|
384
393
|
Get all records
|
|
385
394
|
```ruby
|
|
386
395
|
irb(main):001> User.all
|
|
@@ -931,11 +940,31 @@ class AnyClass
|
|
|
931
940
|
end
|
|
932
941
|
end
|
|
933
942
|
```
|
|
943
|
+
The "around" hook can be used to hijack origianl method execution:
|
|
944
|
+
```ruby
|
|
945
|
+
class AnyClass
|
|
946
|
+
include Rubee::Hookable
|
|
947
|
+
around :print_world, ->(this_instance, &target_method) do
|
|
948
|
+
if condition_that_return_false
|
|
949
|
+
target_method.call
|
|
950
|
+
else
|
|
951
|
+
this_instance.print_hello
|
|
952
|
+
end
|
|
953
|
+
end
|
|
954
|
+
|
|
955
|
+
def print_world
|
|
956
|
+
puts "world!"
|
|
957
|
+
end
|
|
958
|
+
|
|
959
|
+
def print_hello
|
|
960
|
+
puts "hello!"
|
|
961
|
+
end
|
|
962
|
+
end
|
|
963
|
+
```
|
|
934
964
|
|
|
935
|
-
|
|
965
|
+
This will replace origianl method execution with `print_hello` if `condition_that_return_false` returns false.
|
|
936
966
|
```bash
|
|
937
967
|
hello!
|
|
938
|
-
world!
|
|
939
968
|
```
|
|
940
969
|
|
|
941
970
|
[Back to content](#content)
|