forest_liana 1.1.29 → 1.1.30

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1b687506a6797279131b36df0bfc64abd86fade4
4
- data.tar.gz: 4d4a03bcd5f529c234cdfc160f4e8c061c97dea1
3
+ metadata.gz: a8c61cca79d81e2d1d325eed17335c37ccd04347
4
+ data.tar.gz: 53171e4c8959338e211dfa666c79cc6e7a16aaaa
5
5
  SHA512:
6
- metadata.gz: 4d17df6bfb4519de6e7425fb7a5315b3233ea58d0a7dc696d16f868f51c3c6829b4eecfaf14a2117f5e5ca1419c98cf47f8b8a8393a43df5b949ce21ff93ec67
7
- data.tar.gz: 8159e958705d5042b3d8622bb5f8994f5c45710fe50eeae16316f0d29eccedc8b84357fbd2a0c2ebc77b2fd78679198cca4d3f4859ddb4d571129432d927caad
6
+ metadata.gz: 91828deca444c0822687ddce5f77b717647c08b4835d1c7678c1f9b1928cfd42916926142e43b6bff605585336693f9154a672a6283520bf3ed535d9a469ec68
7
+ data.tar.gz: 994e9efdc1f89a99004d89e226ae84d8616db81cab8ef7e7d0041a33308f1b7568d05300d981637ce249cadbaa50b74189cd279ff21afc88bc929dda694ea103
@@ -25,8 +25,9 @@ module ForestLiana
25
25
  end
26
26
 
27
27
  def find_association
28
+ # Rails 3 wants a :sym argument.
28
29
  @association = @resource.reflect_on_association(
29
- params[:association_name])
30
+ params[:association_name].try(:to_sym))
30
31
 
31
32
  # Only accept "many" associations
32
33
  if @association.nil? ||
@@ -1,10 +1,9 @@
1
1
  module ForestLiana
2
2
  class ResourceDeserializer
3
3
 
4
- def initialize(resource, record, params)
4
+ def initialize(resource, params)
5
5
  @params = params
6
6
  @resource = resource
7
- @record = record
8
7
  end
9
8
 
10
9
  def perform
@@ -27,25 +26,16 @@ module ForestLiana
27
26
  if @params['data']['relationships']
28
27
  @params['data']['relationships'].each do |name, relationship|
29
28
  data = relationship['data']
30
- association = @resource.reflect_on_association(name)
29
+ # Rails 3 requires a :sym argument for the reflect_on_association
30
+ # call.
31
+ association = @resource.reflect_on_association(name.try(:to_sym))
31
32
 
32
- case association.try(:macro)
33
- when :has_one, :belongs_to
33
+ if [:has_one, :belongs_to].include?(association.try(:macro))
34
34
  if data.is_a?(Hash)
35
35
  @attributes[name] = association.klass.find(data[:id])
36
36
  elsif data.blank?
37
37
  @attributes[name] = nil
38
38
  end
39
- when :has_many, :has_and_belongs_to_many
40
- if data.is_a?(Array)
41
- data.each do |x|
42
- existing_records = @record.send(name)
43
- new_record = association.klass.find(x[:id])
44
- if !existing_records.include?(new_record)
45
- existing_records << new_record
46
- end
47
- end
48
- end
49
39
  end
50
40
  end
51
41
  end
@@ -50,7 +50,7 @@ module ForestLiana
50
50
  end
51
51
 
52
52
  def type
53
- object.class.table_name.demodulize.tableize.dasherize
53
+ object.class.table_name.demodulize.dasherize
54
54
  end
55
55
 
56
56
  def format_name(attribute_name)
@@ -89,18 +89,22 @@ module ForestLiana
89
89
  end
90
90
 
91
91
  if ret[:href].blank?
92
- relationship_records = object.send(attribute_name)
93
-
94
- if relationship_records.respond_to?(:each)
95
- if Rails::VERSION::MAJOR == 4
96
- ret[:href] = "/forest/#{object.class.table_name}/#{object.id}/#{attribute_name}"
97
- ret[:meta] = { count: relationship_records.distinct.count }
98
- else
99
- ret[:href] = "/forest/#{object.class.table_name}/#{object.id}/#{attribute_name}"
100
- ret[:meta] = {
101
- count: relationship_records.count(:id, distinct: true)
102
- }
92
+ begin
93
+ relationship_records = object.send(attribute_name)
94
+
95
+ if relationship_records.respond_to?(:each)
96
+ if Rails::VERSION::MAJOR == 4
97
+ ret[:href] = "/forest/#{object.class.table_name}/#{object.id}/#{attribute_name}"
98
+ ret[:meta] = { count: relationship_records.distinct.count }
99
+ else
100
+ ret[:href] = "/forest/#{object.class.table_name}/#{object.id}/#{attribute_name}"
101
+ ret[:meta] = {
102
+ count: relationship_records.count(:id, distinct: true)
103
+ }
104
+ end
103
105
  end
106
+ rescue TypeError
107
+ puts "Cannot load the association #{attribute_name} on #{object.class.name} #{object.id}."
104
108
  end
105
109
  end
106
110
 
@@ -13,11 +13,33 @@ module ForestLiana
13
13
  else
14
14
  @record = @resource.create!(resource_params, without_protection: true)
15
15
  end
16
+
17
+ set_has_many_relationships
16
18
  end
17
19
 
18
20
  def resource_params
19
21
  ResourceDeserializer.new(@resource, @params[:resource]).perform
20
22
  end
21
23
 
24
+ def set_has_many_relationships
25
+ if @params['data']['relationships']
26
+ @params['data']['relationships'].each do |name, relationship|
27
+ data = relationship['data']
28
+ association = @resource.reflect_on_association(name)
29
+ if [:has_many, :has_and_belongs_to_many].include?(
30
+ association.try(:macro))
31
+ if data.is_a?(Array)
32
+ data.each do |x|
33
+ existing_records = @record.send(name)
34
+ new_record = association.klass.find(x[:id])
35
+ if !existing_records.include?(new_record)
36
+ existing_records << new_record
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
22
44
  end
23
45
  end
@@ -15,11 +15,33 @@ module ForestLiana
15
15
  else
16
16
  @record.update_attributes!(resource_params, without_protection: true)
17
17
  end
18
+
19
+ set_has_many_relationships
18
20
  end
19
21
 
20
22
  def resource_params
21
- ResourceDeserializer.new(@resource, @record, @params[:resource]).perform
23
+ ResourceDeserializer.new(@resource, @params[:resource]).perform
22
24
  end
23
25
 
26
+ def set_has_many_relationships
27
+ if @params['data']['relationships']
28
+ @params['data']['relationships'].each do |name, relationship|
29
+ data = relationship['data']
30
+ association = @resource.reflect_on_association(name)
31
+ if [:has_many, :has_and_belongs_to_many].include?(
32
+ association.try(:macro))
33
+ if data.is_a?(Array)
34
+ data.each do |x|
35
+ existing_records = @record.send(name)
36
+ new_record = association.klass.find(x[:id])
37
+ if !existing_records.include?(new_record)
38
+ existing_records << new_record
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
24
46
  end
25
47
  end
@@ -6,7 +6,7 @@ module ForestLiana
6
6
 
7
7
  def perform
8
8
  @collection = ForestLiana::Model::Collection.new({
9
- name: @model.name.tableize,
9
+ name: @model.table_name,
10
10
  fields: []
11
11
  })
12
12
 
@@ -1,3 +1,3 @@
1
1
  module ForestLiana
2
- VERSION = "1.1.29"
2
+ VERSION = "1.1.30"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: forest_liana
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.29
4
+ version: 1.1.30
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sandro Munda
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-02 00:00:00.000000000 Z
11
+ date: 2016-02-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails