traim 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -1
- data/lib/traim.rb +14 -1
- data/test/resource.rb +23 -0
- data/traim.gemspec +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1093282c876de373d85f4fdb421003afced47daf
|
4
|
+
data.tar.gz: f26ab0488f24a0985e1cf8e5b4bd4c8dbec1bf00
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1291114f6985f46faa1b350f2186463887e462fd644f3b2a2b87a3d876cf750e3add302019a2decc339216f31f86715b9810f1ba9571eaa7e3d97955f57c1800
|
7
|
+
data.tar.gz: b938ba3802c656bee133aec89bdb5deecc9548f8f150df00e6c96de994bdd57d1f37ac0d2f43282459048f33b2dc6dad84d90a43e405d53229cda57774739343
|
data/README.md
CHANGED
@@ -220,13 +220,15 @@ Traim.application do
|
|
220
220
|
helpers do
|
221
221
|
def auth(user_id)
|
222
222
|
raise BadRequestError.new(message: "unauthenticated request") unless model.exists?(id: user_id)
|
223
|
+
|
224
|
+
# attribute can be added in helpers as well
|
225
|
+
attribute :name
|
223
226
|
end
|
224
227
|
end
|
225
228
|
|
226
229
|
model User
|
227
230
|
|
228
231
|
attribute :id
|
229
|
-
attribute :name
|
230
232
|
|
231
233
|
action :show do
|
232
234
|
auth(params["id"])
|
data/lib/traim.rb
CHANGED
@@ -341,6 +341,7 @@ class Traim
|
|
341
341
|
@request = request
|
342
342
|
@headers = {}
|
343
343
|
@actions = actions
|
344
|
+
@fields = []
|
344
345
|
|
345
346
|
ok
|
346
347
|
|
@@ -348,6 +349,7 @@ class Traim
|
|
348
349
|
@helper = helper
|
349
350
|
@helper.request = @request
|
350
351
|
@helper.status = @status
|
352
|
+
@helper.fields = @fields
|
351
353
|
@helper.model = @model
|
352
354
|
end
|
353
355
|
end
|
@@ -358,6 +360,7 @@ class Traim
|
|
358
360
|
attr_accessor :request
|
359
361
|
attr_accessor :helper
|
360
362
|
attr_accessor :status
|
363
|
+
attr_accessor :fields
|
361
364
|
|
362
365
|
def logger; Traim.logger end
|
363
366
|
|
@@ -373,7 +376,6 @@ class Traim
|
|
373
376
|
|
374
377
|
def actions(name); @actions[name] end
|
375
378
|
|
376
|
-
def fields; @fields ||= [] end
|
377
379
|
def attribute(name, &block)
|
378
380
|
fields << {name: name, type: 'attribute', block: block}
|
379
381
|
end
|
@@ -450,6 +452,17 @@ class Traim
|
|
450
452
|
attr_accessor :request
|
451
453
|
attr_accessor :status
|
452
454
|
attr_accessor :model
|
455
|
+
attr_accessor :fields
|
453
456
|
|
457
|
+
def fields; @fields ||= [] end
|
458
|
+
def attribute(name, &block)
|
459
|
+
fields << {name: name, type: 'attribute', block: block}
|
460
|
+
end
|
461
|
+
def has_many(name, options={}, &block)
|
462
|
+
fields << {name: name, type: 'association', resource: options[:resource], block: block}
|
463
|
+
end
|
464
|
+
def has_one(name, options={}, &block)
|
465
|
+
fields << {name: name, type: 'connection', resource: options[:resource], block: block}
|
466
|
+
end
|
454
467
|
end
|
455
468
|
end
|
data/test/resource.rb
CHANGED
@@ -452,6 +452,29 @@ test "custom resource for association" do |user|
|
|
452
452
|
assert result["second_book"]["isbn"] == "isbn_2"
|
453
453
|
end
|
454
454
|
|
455
|
+
test "add attributes in helper" do |user|
|
456
|
+
app = Traim.application do
|
457
|
+
helpers do
|
458
|
+
def add_user_name
|
459
|
+
attribute :name
|
460
|
+
end
|
461
|
+
end
|
462
|
+
resources :users do
|
463
|
+
model User
|
464
|
+
|
465
|
+
attribute :id
|
466
|
+
|
467
|
+
action :show do
|
468
|
+
helper.add_user_name
|
469
|
+
record
|
470
|
+
end
|
471
|
+
end
|
472
|
+
end
|
473
|
+
|
474
|
+
_, headers, response = mock_request(app, "/users/#{user.id}", "GET")
|
475
|
+
result = JSON.parse(response.first)
|
476
|
+
assert result["name"] == "kolo"
|
477
|
+
end
|
455
478
|
|
456
479
|
Book.delete_all
|
457
480
|
User.delete_all
|
data/traim.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: traim
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Travis Liu
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-07-
|
11
|
+
date: 2018-07-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|