embark-journey 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/active_resource/active_resource.rb +1 -1
- data/lib/active_resource/associations.rb +8 -2
- data/lib/active_resource/associations/builder/belongs_to.rb +19 -0
- data/lib/journey.rb +1 -0
- data/lib/journey/resource.rb +13 -1
- data/lib/journey/resource/attribute_loading.rb +1 -1
- data/lib/journey/resource/embed.rb +12 -0
- data/lib/journey/version.rb +1 -1
- data/spec/models/journey/resource_spec.rb +28 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d585fc43842f526ac92b7baa33dfcc5b291bb7c5
|
4
|
+
data.tar.gz: b2d12857cb32c3f87b3c60fbf8bbe93b27a2fa60
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 758328e28fe67df58ad60e9dba0f4a5ad8741795eab435c5c0dac31495f59f1861898f1a25836176f6522ace603ebb8a0da25b0c01bc39c15befa18f4f00f333
|
7
|
+
data.tar.gz: 197f52f761d88ec095145a1dd337a1ece4397cb8fc413fed1b33ec1692d6813861605d6bba43a636c80991cc4d2222ac43b84211a7dd7ff4036f9ef11237c706
|
@@ -1,2 +1,2 @@
|
|
1
1
|
require 'active_resource'
|
2
|
-
|
2
|
+
require_relative 'associations'
|
@@ -4,11 +4,11 @@ module ActiveResource::Associations
|
|
4
4
|
autoload :Association, 'active_resource/associations/builder/association'
|
5
5
|
autoload :HasMany, 'active_resource/associations/builder/has_many'
|
6
6
|
autoload :HasOne, 'active_resource/associations/builder/has_one'
|
7
|
-
autoload :BelongsTo, 'active_resource/associations/builder/belongs_to'
|
7
|
+
# autoload :BelongsTo, 'active_resource/associations/builder/belongs_to'
|
8
|
+
require_relative 'associations/builder/belongs_to'
|
8
9
|
end
|
9
10
|
|
10
11
|
|
11
|
-
|
12
12
|
# Specifies a one-to-many association.
|
13
13
|
#
|
14
14
|
# === Options
|
@@ -142,6 +142,12 @@ module ActiveResource::Associations
|
|
142
142
|
end
|
143
143
|
end
|
144
144
|
|
145
|
+
attr_accessor :embeds
|
146
|
+
def defines_belongs_to_embed(method_name, association_model)
|
147
|
+
self.embeds ||= []
|
148
|
+
self.embeds << method_name
|
149
|
+
end
|
150
|
+
|
145
151
|
def defines_has_many_finder_method(method_name, association_model)
|
146
152
|
ivar_name = :"@#{method_name}"
|
147
153
|
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module ActiveResource::Associations::Builder
|
2
|
+
class BelongsTo < Association
|
3
|
+
self.valid_options += [:foreign_key, :embed]
|
4
|
+
|
5
|
+
self.macro = :belongs_to
|
6
|
+
|
7
|
+
def build
|
8
|
+
validate_options
|
9
|
+
embed = options.delete(:embed)
|
10
|
+
|
11
|
+
reflection = model.create_reflection(self.class.macro, name, options)
|
12
|
+
model.defines_belongs_to_finder_method(reflection.name, reflection.klass, reflection.foreign_key)
|
13
|
+
|
14
|
+
model.defines_belongs_to_embed(reflection.name, reflection.klass) if embed
|
15
|
+
|
16
|
+
return reflection
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/journey.rb
CHANGED
data/lib/journey/resource.rb
CHANGED
@@ -1,6 +1,16 @@
|
|
1
|
-
require 'active_resource/active_resource'
|
2
1
|
module Journey
|
3
2
|
class Resource < ActiveResource::Base
|
3
|
+
|
4
|
+
def self.find(*arguments)
|
5
|
+
scope = arguments.slice!(0)
|
6
|
+
|
7
|
+
self.embeds ||= []
|
8
|
+
options = arguments.slice!(0) || {}
|
9
|
+
options.deep_merge!(params: { embed: self.embeds })
|
10
|
+
|
11
|
+
super *([scope, options] + arguments)
|
12
|
+
end
|
13
|
+
|
4
14
|
end
|
5
15
|
end
|
6
16
|
|
@@ -9,6 +19,7 @@ require 'journey/resource/attribute_loading'
|
|
9
19
|
require 'journey/resource/enums'
|
10
20
|
require 'journey/resource/queries'
|
11
21
|
require 'journey/resource/search'
|
22
|
+
require 'journey/resource/embed'
|
12
23
|
|
13
24
|
class Journey::Resource
|
14
25
|
include API
|
@@ -16,6 +27,7 @@ class Journey::Resource
|
|
16
27
|
include Enums
|
17
28
|
include AttributeLoading
|
18
29
|
include Search
|
30
|
+
include Embed
|
19
31
|
end
|
20
32
|
|
21
33
|
|
@@ -8,7 +8,7 @@ module Journey::Resource::AttributeLoading
|
|
8
8
|
# for the purpose of enums
|
9
9
|
def load(attributes, remove_root = false, persisted = false)
|
10
10
|
super(attributes, remove_root, persisted).tap do
|
11
|
-
attributes.each do |key, value|
|
11
|
+
self.attributes.each do |key, value|
|
12
12
|
send("#{key}=", value) if respond_to?("#{key}=")
|
13
13
|
end
|
14
14
|
end
|
data/lib/journey/version.rb
CHANGED
@@ -99,4 +99,32 @@ describe Journey::Resource do
|
|
99
99
|
expect(results).to_not be_any
|
100
100
|
end
|
101
101
|
end
|
102
|
+
|
103
|
+
describe '::Embed' do
|
104
|
+
class Client < Journey::Resource
|
105
|
+
end
|
106
|
+
|
107
|
+
class Technician < Journey::Resource
|
108
|
+
end
|
109
|
+
|
110
|
+
class Asset < Journey::Resource
|
111
|
+
belongs_to :client, embed: true
|
112
|
+
belongs_to :technician, embed: true
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'loads an embedded belongs_to association automatically' do
|
116
|
+
client = Client.create name: 'branch'
|
117
|
+
technician = Technician.create name: 'technician'
|
118
|
+
|
119
|
+
asset = Asset.create name: 'asset', client_id: client.id, technician_id: technician.id
|
120
|
+
id = asset.id
|
121
|
+
asset = Asset.find(id)
|
122
|
+
|
123
|
+
expect(asset.attributes['client']).to eq client
|
124
|
+
expect(asset.client).to eq client
|
125
|
+
|
126
|
+
expect(asset.attributes['technician']).to eq technician
|
127
|
+
expect(asset.technician).to eq technician
|
128
|
+
end
|
129
|
+
end
|
102
130
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: embark-journey
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dan Davey
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-03-
|
11
|
+
date: 2014-03-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: active_attr
|
@@ -142,12 +142,14 @@ files:
|
|
142
142
|
- journey.gemspec
|
143
143
|
- lib/active_resource/active_resource.rb
|
144
144
|
- lib/active_resource/associations.rb
|
145
|
+
- lib/active_resource/associations/builder/belongs_to.rb
|
145
146
|
- lib/journey.rb
|
146
147
|
- lib/journey/configurable.rb
|
147
148
|
- lib/journey/configuration.rb
|
148
149
|
- lib/journey/resource.rb
|
149
150
|
- lib/journey/resource/api.rb
|
150
151
|
- lib/journey/resource/attribute_loading.rb
|
152
|
+
- lib/journey/resource/embed.rb
|
151
153
|
- lib/journey/resource/enums.rb
|
152
154
|
- lib/journey/resource/queries.rb
|
153
155
|
- lib/journey/resource/search.rb
|