forest_liana 1.1.16 → 1.1.17
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/app/controllers/forest_liana/associations_controller.rb +53 -0
- data/app/deserializers/forest_liana/resource_deserializer.rb +16 -14
- data/app/serializers/forest_liana/serializer_factory.rb +2 -0
- data/app/services/forest_liana/has_many_getter.rb +0 -2
- data/config/routes.rb +3 -0
- data/lib/forest_liana/version.rb +1 -1
- metadata +6 -5
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9975c9e7f8829d9319f87884f378a6ce6fe14003
|
|
4
|
+
data.tar.gz: a3df564fc0d904e8b06ce425829e5e48294dce91
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a43660cf62cee56e03cc22a8600cde3d87c86ae2027fc314918d1a7000705576aa90b5c682db55761cb024c8947fdfee11e357a45dd5e37f368b71654bcc2d2c
|
|
7
|
+
data.tar.gz: 7a8d51ac14099dc957ac5a55df661da890867a33c946e93b136e0e97efb37f7b073297ef488712d280e0bcf0017bfc7416f65b951ea2e19089236652d108ab0b
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
module ForestLiana
|
|
2
|
+
class AssociationsController < ForestLiana::ApplicationController
|
|
3
|
+
|
|
4
|
+
before_filter :find_resource
|
|
5
|
+
before_filter :find_association
|
|
6
|
+
|
|
7
|
+
def index
|
|
8
|
+
getter = HasManyGetter.new(@resource, @association, params)
|
|
9
|
+
getter.perform
|
|
10
|
+
|
|
11
|
+
render json: serialize_models(getter.records,
|
|
12
|
+
include: includes,
|
|
13
|
+
count: getter.count,
|
|
14
|
+
params: params)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
private
|
|
18
|
+
|
|
19
|
+
def find_resource
|
|
20
|
+
@resource = SchemaUtils.find_model_from_table_name(params[:collection])
|
|
21
|
+
|
|
22
|
+
if @resource.nil? || !@resource.ancestors.include?(ActiveRecord::Base)
|
|
23
|
+
render json: {status: 404}, status: :not_found
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def find_association
|
|
28
|
+
@association = @resource.reflect_on_association(
|
|
29
|
+
params[:association_name])
|
|
30
|
+
|
|
31
|
+
# Only accept "many" associations
|
|
32
|
+
if @association.nil? ||
|
|
33
|
+
[:belongs_to, :has_one].include?(@association.macro)
|
|
34
|
+
render json: {status: 404}, status: :not_found
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def resource_params
|
|
39
|
+
ResourceDeserializer.new(@resource, params[:resource]).perform
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def includes
|
|
43
|
+
@association.klass
|
|
44
|
+
.reflect_on_all_associations
|
|
45
|
+
.select do |a|
|
|
46
|
+
[:belongs_to, :has_and_belongs_to_many].include?(a.macro) &&
|
|
47
|
+
!a.options[:polymorphic]
|
|
48
|
+
end
|
|
49
|
+
.map {|a| a.name.to_s }
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
end
|
|
53
|
+
end
|
|
@@ -15,23 +15,31 @@ module ForestLiana
|
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
def extract_attributes
|
|
18
|
-
@params[
|
|
18
|
+
if @params[:data][:attributes]
|
|
19
|
+
@params['data']['attributes'].select {|attr| column?(attr)}
|
|
20
|
+
else
|
|
21
|
+
ActionController::Parameters.new()
|
|
22
|
+
end
|
|
19
23
|
end
|
|
20
24
|
|
|
21
25
|
def extract_relationships
|
|
22
26
|
if @params['data']['relationships']
|
|
23
27
|
@params['data']['relationships'].each do |name, relationship|
|
|
24
28
|
data = relationship['data']
|
|
29
|
+
association = @resource.reflect_on_association(name)
|
|
25
30
|
|
|
26
|
-
|
|
31
|
+
case association.try(:macro)
|
|
32
|
+
when :has_one, :belongs_to
|
|
27
33
|
if data.is_a?(Hash)
|
|
28
|
-
@attributes[name
|
|
29
|
-
elsif
|
|
30
|
-
@attributes[name
|
|
34
|
+
@attributes[name] = association.klass.find(data[:id])
|
|
35
|
+
elsif data.blank?
|
|
36
|
+
@attributes[name] = nil
|
|
31
37
|
end
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
38
|
+
when :has_many, :has_and_belongs_to_many
|
|
39
|
+
if data.is_a?(Hash)
|
|
40
|
+
@attributes[name] = data.map do |x|
|
|
41
|
+
association.klass.find(x[:id])
|
|
42
|
+
end
|
|
35
43
|
end
|
|
36
44
|
end
|
|
37
45
|
end
|
|
@@ -62,11 +70,5 @@ module ForestLiana
|
|
|
62
70
|
def column?(attribute)
|
|
63
71
|
@resource.columns.find {|x| x.name == attribute}.present?
|
|
64
72
|
end
|
|
65
|
-
|
|
66
|
-
def associated_record(type, id)
|
|
67
|
-
resource = SchemaUtils.find_model_from_table_name(type.underscore)
|
|
68
|
-
resource.find(id)
|
|
69
|
-
end
|
|
70
|
-
|
|
71
73
|
end
|
|
72
74
|
end
|
|
@@ -66,8 +66,10 @@ module ForestLiana
|
|
|
66
66
|
if relationship_records.respond_to?(:each)
|
|
67
67
|
|
|
68
68
|
if Rails::VERSION::MAJOR == 4
|
|
69
|
+
ret[:href] = "/forest/#{object.class.table_name}/#{object.id}/#{attribute_name}"
|
|
69
70
|
ret[:meta] = { count: relationship_records.distinct.count }
|
|
70
71
|
else
|
|
72
|
+
ret[:href] = "/forest/#{object.class.table_name}/#{object.id}/#{attribute_name}"
|
|
71
73
|
ret[:meta] = {
|
|
72
74
|
count: relationship_records.count(:id, distinct: true)
|
|
73
75
|
}
|
data/config/routes.rb
CHANGED
|
@@ -15,4 +15,7 @@ ForestLiana::Engine.routes.draw do
|
|
|
15
15
|
post ':collection' => 'resources#create'
|
|
16
16
|
put ':collection/:id' => 'resources#update'
|
|
17
17
|
delete ':collection/:id' => 'resources#destroy'
|
|
18
|
+
|
|
19
|
+
# Associations
|
|
20
|
+
get ':collection/:id/:association_name' => 'associations#index'
|
|
18
21
|
end
|
data/lib/forest_liana/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: forest_liana
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.1.
|
|
4
|
+
version: 1.1.17
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Sandro Munda
|
|
@@ -28,16 +28,16 @@ dependencies:
|
|
|
28
28
|
name: jsonapi-serializers
|
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
|
30
30
|
requirements:
|
|
31
|
-
- -
|
|
31
|
+
- - '='
|
|
32
32
|
- !ruby/object:Gem::Version
|
|
33
|
-
version:
|
|
33
|
+
version: 0.3.0
|
|
34
34
|
type: :runtime
|
|
35
35
|
prerelease: false
|
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
|
37
37
|
requirements:
|
|
38
|
-
- -
|
|
38
|
+
- - '='
|
|
39
39
|
- !ruby/object:Gem::Version
|
|
40
|
-
version:
|
|
40
|
+
version: 0.3.0
|
|
41
41
|
- !ruby/object:Gem::Dependency
|
|
42
42
|
name: jwt
|
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -123,6 +123,7 @@ files:
|
|
|
123
123
|
- app/assets/stylesheets/scaffold.css
|
|
124
124
|
- app/controllers/forest_liana/apimaps_controller.rb
|
|
125
125
|
- app/controllers/forest_liana/application_controller.rb
|
|
126
|
+
- app/controllers/forest_liana/associations_controller.rb
|
|
126
127
|
- app/controllers/forest_liana/resources_controller.rb
|
|
127
128
|
- app/controllers/forest_liana/stats_controller.rb
|
|
128
129
|
- app/controllers/forest_liana/stripe_controller.rb
|