horza 0.3.5 → 0.3.6
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/README.md +14 -0
- data/lib/horza.rb +8 -0
- data/lib/horza/adapters/abstract_adapter.rb +4 -0
- data/lib/horza/adapters/active_record.rb +7 -0
- data/lib/horza/adapters/instance_methods.rb +4 -0
- data/spec/active_record_spec.rb +57 -0
- 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: 3e0c5a3f309830bcdf12d45b896c194edd65564a
|
4
|
+
data.tar.gz: 0abdd19ad225b57350ef96861620ef52b5977d7b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 96e4b16fb9944e3b4e5ea6bbdf9d0cf90902a59316f35facda683b3831d6e37d9f189121293c922e6a68800910dc7f7df9b00ffe7633ee8faaf9cb6f237580c3
|
7
|
+
data.tar.gz: 21a843a404376023ed172d82c7d6bc4f0638f599a113b6219e853464656fa3b131a04f33b01ab5455697f7aa632940040093fd2147600e1467b0ae5c5b437122
|
data/README.md
CHANGED
@@ -21,17 +21,31 @@ user = Horza.adapt(User)
|
|
21
21
|
# Examples
|
22
22
|
user.get(id) # Find by id - Return nil on fail
|
23
23
|
user.get!(id) # Find by id - Error on fail
|
24
|
+
|
24
25
|
user.find_first(options) # Find 1 user - Orders by id desc by default - Return nil on fail
|
25
26
|
user.find_first!(options) # Find 1 user - Orders by id desc by default - Error nil on fail
|
27
|
+
|
26
28
|
user.find_all(options) # Find all users that match parameters
|
29
|
+
|
27
30
|
user.create(options) # Create record - return nil on fail
|
28
31
|
user.create!(options) # Create record - raise error on fail
|
32
|
+
|
29
33
|
user.update(options) # Update record - return nil on fail
|
30
34
|
user.update!(options) # Update record - raise error on fail
|
35
|
+
|
31
36
|
user.delete(options) # Delete record - return nil on fail
|
32
37
|
user.delete!(options) # Delete record - raise error on fail
|
38
|
+
|
33
39
|
user.association(target: :employer, via: []) # Traverse association
|
34
40
|
|
41
|
+
# Special case: create a child record when mass-assignment is disabled for parent instance_methods
|
42
|
+
# klass is a symbol version of your model name, ie Employer is :employer, SportsCar is :sports_car
|
43
|
+
parent = { id: parent_id, klass: :employer }
|
44
|
+
|
45
|
+
user.create_as_child(parent, options) # Create record - return nil on fail
|
46
|
+
user.create_as_child!(parent, options) # Create record - raise error on fail
|
47
|
+
|
48
|
+
# With args
|
35
49
|
conditions = { last_name: 'Turner' }
|
36
50
|
|
37
51
|
# Ordering
|
data/lib/horza.rb
CHANGED
@@ -40,6 +40,13 @@ module Horza
|
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
43
|
+
def create_as_child!(parent_args, options = {})
|
44
|
+
run_and_convert_exceptions do
|
45
|
+
parent = Horza.adapter.context_for_entity(parent_args[:klass]).find(parent_args[:id])
|
46
|
+
create!(options.merge(parent_args[:klass] => parent))
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
43
50
|
def update!(id, options = {})
|
44
51
|
run_and_convert_exceptions do
|
45
52
|
record = @context.find(id)
|
data/spec/active_record_spec.rb
CHANGED
@@ -274,6 +274,63 @@ describe Horza do
|
|
274
274
|
end
|
275
275
|
end
|
276
276
|
|
277
|
+
describe '#create_as_child' do
|
278
|
+
let(:employer) { HorzaSpec::Employer.create }
|
279
|
+
let(:parent) do
|
280
|
+
{
|
281
|
+
id: employer.id,
|
282
|
+
klass: :employer
|
283
|
+
}
|
284
|
+
end
|
285
|
+
let(:action) { user_adapter.create_as_child(parent, last_name: last_name) }
|
286
|
+
|
287
|
+
context 'when parameters are valid' do
|
288
|
+
it 'creates the record' do
|
289
|
+
expect { action }.to change(HorzaSpec::User, :count).by(1)
|
290
|
+
end
|
291
|
+
|
292
|
+
it 'returns the entity' do
|
293
|
+
expect(action.last_name).to eq last_name
|
294
|
+
expect(action.employer_id).to eq employer.id
|
295
|
+
end
|
296
|
+
end
|
297
|
+
|
298
|
+
context 'when parameters are invalid' do
|
299
|
+
let(:parent) do
|
300
|
+
{
|
301
|
+
id: 999,
|
302
|
+
klass: :employer
|
303
|
+
}
|
304
|
+
end
|
305
|
+
|
306
|
+
it 'does not create the record' do
|
307
|
+
expect { action }.to change(HorzaSpec::User, :count).by(0)
|
308
|
+
end
|
309
|
+
|
310
|
+
it 'returns nil' do
|
311
|
+
expect(action).to be nil
|
312
|
+
end
|
313
|
+
end
|
314
|
+
end
|
315
|
+
|
316
|
+
describe '#create_as_child!' do
|
317
|
+
let(:employer) { HorzaSpec::Employer.create }
|
318
|
+
let(:action) { user_adapter.create_as_child!(parent, last_name: last_name) }
|
319
|
+
|
320
|
+
context 'when parameters are invalid' do
|
321
|
+
let(:parent) do
|
322
|
+
{
|
323
|
+
id: 999,
|
324
|
+
klass: :employer
|
325
|
+
}
|
326
|
+
end
|
327
|
+
|
328
|
+
it 'throws error' do
|
329
|
+
expect { action }.to raise_error Horza::Errors::RecordNotFound
|
330
|
+
end
|
331
|
+
end
|
332
|
+
end
|
333
|
+
|
277
334
|
describe '#update' do
|
278
335
|
let(:customer) { HorzaSpec::Customer.create(last_name: last_name) }
|
279
336
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: horza
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Blake Turner
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-06-
|
11
|
+
date: 2015-06-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: hashie
|