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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d8f236c05a4fda3ca63553fbe3bdc740c16a7199
4
- data.tar.gz: ea83ef6b9644a7598356e7a1c0a5948c4eb6332a
3
+ metadata.gz: 9975c9e7f8829d9319f87884f378a6ce6fe14003
4
+ data.tar.gz: a3df564fc0d904e8b06ce425829e5e48294dce91
5
5
  SHA512:
6
- metadata.gz: 0f974c93a1fe87bc596ba6684f179dbe7bcec36e4b171d29215cf2c16b93ba1d254730ae1b7463245a5c9dba0655dc37f100eb7f0f3ddab599c6c32650012531
7
- data.tar.gz: 0894696de4d8986d7bcc9936cb3087e28e5197bb7d8a4db91867bdc1a3ae5f6796e504ad9ac8b72347a1407361528fb7b9d9019654ee8f6e640ea194cf9e0e89
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['data']['attributes'].select {|attr| column?(attr)}
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
- if column?(name.foreign_key) # belongs_to
31
+ case association.try(:macro)
32
+ when :has_one, :belongs_to
27
33
  if data.is_a?(Hash)
28
- @attributes[name.foreign_key] = data[:id]
29
- elsif !data
30
- @attributes[name.foreign_key] = nil
34
+ @attributes[name] = association.klass.find(data[:id])
35
+ elsif data.blank?
36
+ @attributes[name] = nil
31
37
  end
32
- elsif data # has_many
33
- @attributes[name] = data.map do |x|
34
- associated_record(x[:type], x[:id])
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
  }
@@ -8,8 +8,6 @@ module ForestLiana
8
8
 
9
9
  def perform
10
10
  @records = @resource.find(@params[:id]).send(@params[:association_name])
11
-
12
- @records
13
11
  end
14
12
 
15
13
  def records
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
@@ -1,3 +1,3 @@
1
1
  module ForestLiana
2
- VERSION = "1.1.16"
2
+ VERSION = "1.1.17"
3
3
  end
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.16
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: '0'
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: '0'
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