linker 0.0.5 → 0.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7215c599b0c4568eaee73a1f5dd17ef4494d8c67
4
- data.tar.gz: 1f7122fc680bcbfa1471871e617966da3ec6c986
3
+ metadata.gz: b48ed834d2a2939cd146a4df6f0cea04cab670bf
4
+ data.tar.gz: acdc8c5c7188540ac99e79e22667262d2b8af3de
5
5
  SHA512:
6
- metadata.gz: b6307b9c840caffcaaa6afbae8205bcfd203c74e34459ae8e74fa3a6211c5178efac4703103aadfe569ec7922965029b6bcf399eb133094f78c949ad6093f944
7
- data.tar.gz: 3893664531b92f965ada075022e2413ccea81558d236c00355a3e0c368e7e6b80acb41e4878d21fe94a4ef4c6a6db384558077d498959a6005f8f384d2b5eb8b
6
+ metadata.gz: 517dd1f9aac26980d68f308ad4381e1d3ad5e3e84aec4b5c503b49c09290574d84694deea22f8970473aec0cfe561c5fbc043a821c9007a1352470098f6168d5
7
+ data.tar.gz: 43050b4f7f21a3072348ecc0132d7e84c590fcb4847f4e472a04d27a01d5843406686849f5481b65b174341fad08ed8613e7940eaafd859ecdb8cbe9b6afc0fc
@@ -19,6 +19,7 @@ module Linker
19
19
  set_fields_for_methods(map_belongs_to_associations, true)
20
20
  set_fields_for_methods(map_has_one_associations, true)
21
21
  set_non_fields_for_methods(map_has_one_associations)
22
+ set_remove_accessor(map_has_many_associations)
22
23
  end
23
24
 
24
25
  def set_reader_for_main_model
@@ -97,6 +98,13 @@ module Linker
97
98
  end
98
99
  end
99
100
 
101
+ def set_remove_accessor assoc_set
102
+ assoc_set.each do |c|
103
+ #ap "Criando attr_accessor :_remove para #{c[:klass]}"
104
+ c[:klass].constantize.class_eval{ attr_accessor :_remove }
105
+ end
106
+ end
107
+
100
108
  def map_associations assoc
101
109
  assoc.inject([]) do |t, c|
102
110
  t << {
@@ -6,11 +6,6 @@ module Linker
6
6
 
7
7
  included do
8
8
  def params= params
9
- # Delete all associated objects if there is nothing in params
10
- @mapped_hm_assoc.each do |c|
11
- _get_main_model.send(c[:name]).destroy_all if !params.key?("#{c[:name]}_attributes")
12
- end
13
-
14
9
  params.each do |param, value|
15
10
  if value.is_a?(Hash)
16
11
  table = param.gsub(%r{_attributes$}, '')
@@ -32,14 +27,19 @@ module Linker
32
27
 
33
28
  # has_many attrs
34
29
  else
35
- ids = value.map.with_index{|c,i| c.last['id'].present? ? c.last['id'] : nil }.compact
36
- _get_main_model.send(table).where(["#{_get_main_model.send(table).table.name}.id NOT IN (?)", ids]).destroy_all if ids.present?
30
+ ids_to_remove = value.map{|c| c.last['id'] if c.last['id'].present? && c.last.key?('_remove') && c.last['_remove'] == '1' }.compact
31
+
32
+ if ids_to_remove.present?
33
+ r = search_has_many(table)
34
+ r[:klass].constantize.send(:where, ["#{r[:klass].constantize.table_name}.id IN (?)", ids_to_remove]).destroy_all
35
+ value.delete_if{|i, c| ids_to_remove.include?(c['id']) }
36
+ end
37
37
 
38
38
  value.each do |c|
39
39
  if c.last['id'].present?
40
- _get_main_model.send(table).find(c.last['id']).update_attributes(c.last)
40
+ _get_main_model.send(table).find(c.last['id']).update_attributes(c.last.except('_remove'))
41
41
  else
42
- _get_main_model.send(table).send(:build, c.last)
42
+ _get_main_model.send(table).send(:build, c.last.except('_remove'))
43
43
  end
44
44
  end
45
45
  end
@@ -73,7 +73,12 @@ module Linker
73
73
  end
74
74
 
75
75
  def search_has_one name
76
- s = map_has_one_associations.detect{|c| c[:name] == name}
76
+ s = @mapped_ho_assoc.detect{|c| c[:name] == name}
77
+ s.present? && s
78
+ end
79
+
80
+ def search_has_many name
81
+ s = @mapped_hm_assoc.detect{|c| c[:name] == name}
77
82
  s.present? && s
78
83
  end
79
84
  end
@@ -1,3 +1,3 @@
1
1
  module Linker
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -26,11 +26,13 @@ describe UsersForm do
26
26
 
27
27
  subject(:dependent_users_sample) { users_form.dependent_users.sample }
28
28
  it { expect(dependent_users_sample).to be_an(DependentUser) }
29
+ it { expect(dependent_users_sample).to respond_to(:_remove) }
29
30
 
30
31
  it { expect(users_form).to respond_to(:my_tasks, :my_tasks_attributes=) }
31
32
  it { expect(users_form.my_tasks).to be_an(Array) }
32
33
  subject(:tasks_sample) { users_form.my_tasks.sample }
33
- it { expect(tasks_sample).to be_an(Task) }
34
+ it { expect(tasks_sample).to be_a(Task) }
35
+ it { expect(tasks_sample).to respond_to(:_remove) }
34
36
 
35
37
  it { expect(users_form).to respond_to(:address, :address_attributes=) }
36
38
  subject(:address) { users_form.address }
@@ -142,7 +144,7 @@ describe UsersForm do
142
144
  'profile_list' => '',
143
145
  'my_phone_list' => '',
144
146
  'little_pet_attributes' => {'id' => '1', 'name' => 'Stuart 2'},
145
- #'tasks_attributes' => {'0' => {'id' => '', 'name' => 'T1'}, '1' => {'id' => '', 'name' => 'T2'}},
147
+ 'my_tasks_attributes' => {'0' => {'id' => '1', 'name' => 'Task 1', '_remove' => '1'}, '1' => {'id' => '', 'name' => 'Task 3'}},
146
148
  'dependent_users_attributes' => {'0' => {'id' => '1', 'name' => 'John 2', 'date_birth' => Date.new(1990, 2, 2)}, '1' => {'id' => '', 'name' => '', 'date_birth' => ''}}
147
149
  }
148
150
  users_form_existing_user.save
@@ -156,15 +158,16 @@ describe UsersForm do
156
158
  subject(:users_form_existing_user_pet) { users_form_existing_user.little_pet }
157
159
  subject(:users_form_existing_user_family) { users_form_existing_user.my_family }
158
160
  subject(:users_form_existing_user_user_tasks) { users_form_existing_user.user.my_tasks }
159
- subject(:users_form_existing_user_tasks_sample) { users_form_existing_user.my_tasks.sample }
161
+ subject(:users_form_existing_user_tasks) { users_form_existing_user.my_tasks }
160
162
 
161
163
  subject(:users_form_existing_user_to_model) { users_form_existing_user.to_model }
162
164
  it { expect(users_form_existing_user_to_model.class).to eq(User) }
163
165
  it { expect(users_form_existing_user_to_model.persisted?).to eq(true) }
164
166
 
165
- it { expect(users_form_existing_user_user_tasks.empty?).to be(true) }
166
- it { expect(users_form_existing_user_tasks_sample.persisted?).to be(false) }
167
- it { expect(users_form_existing_user_tasks_sample).to be_a(Task) }
167
+ it { expect(users_form_existing_user_tasks.first).to be_a(Task) }
168
+ it { expect(users_form_existing_user_tasks.first.persisted?).to eq(true) }
169
+ it { expect(users_form_existing_user_tasks.first.name).to eq('Task 3') }
170
+ it { expect(Task.find_by_id(1)).to eq(nil) }
168
171
 
169
172
  it { expect(users_form_existing_user_dependent_users.sample.persisted?).to eq(true) }
170
173
  it { expect(users_form_existing_user_dependent_users.first.id).to eq(1) }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: linker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Glauco Custódio