linker 0.0.3 → 0.0.4
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/README.md +6 -2
- data/lib/linker/forms/attributes.rb +12 -0
- data/lib/linker/forms/params.rb +12 -3
- data/lib/linker/version.rb +1 -1
- data/spec/fake_app/active_record/models.rb +17 -0
- data/spec/users_form_spec.rb +12 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ac03dfe23e9fee4462e37ff348f534df0a1b5675
|
4
|
+
data.tar.gz: a2cd229386f46837212830c1b7ad4e3be5688268
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d930171e67aeec914442f5430b96eb420d3a2926fb5a0c2ee4874774823a3d569222f9bd40caab56d91db7afcf4120826b20d2c5308465c53fec29b4391ba1bf
|
7
|
+
data.tar.gz: 68ef88987ef64dc5046e6723e3cf7f51f55ddf0b33e09686957c521264611b8f1767a14ae30ea35b0362afb980dd2e5276e5a6351adc842318f29cad19ec9cf6
|
data/README.md
CHANGED
@@ -31,6 +31,7 @@ class User < ActiveRecord::Base
|
|
31
31
|
belongs_to :family
|
32
32
|
|
33
33
|
has_one :address, dependent: :destroy
|
34
|
+
has_one :profile
|
34
35
|
|
35
36
|
has_many :dependent_users, dependent: :destroy
|
36
37
|
has_many :tasks, dependent: :destroy
|
@@ -68,12 +69,12 @@ end
|
|
68
69
|
|
69
70
|
By default, `save` method will perform validations, you can disable them by passing `validate: false` as parameter.
|
70
71
|
|
71
|
-
Finally, you can use `fields_for` in order to
|
72
|
+
Finally, you can use `fields_for` in order to create/edit associated fields and the suffix `_list` (like `profile_list` below) to choose an existing associated record.
|
72
73
|
|
73
74
|
```erb
|
74
75
|
<%= form_for @user_form do |f| %>
|
75
76
|
<%= f.text_field :name %>
|
76
|
-
|
77
|
+
|
77
78
|
<%= f.fields_for :tasks do |ta| %>
|
78
79
|
<%= ta.hidden_field :id %>
|
79
80
|
<%= ta.text_field :name %>
|
@@ -82,6 +83,9 @@ Finally, you can use `fields_for` in order to display associated fields.
|
|
82
83
|
<%= co.hidden_field :id %>
|
83
84
|
<%= co.text_field :name %>
|
84
85
|
<%= co.text_field :website %>
|
86
|
+
|
87
|
+
<%= f.select :profile_list, Profile.all.map{|c| [c.profile_type, c.id]}, include_blank: true %>
|
88
|
+
|
85
89
|
<% end %>
|
86
90
|
```
|
87
91
|
|
@@ -18,6 +18,7 @@ module Linker
|
|
18
18
|
set_fields_for_methods(map_has_many_associations)
|
19
19
|
set_fields_for_methods(map_belongs_to_associations, true)
|
20
20
|
set_fields_for_methods(map_has_one_associations, true)
|
21
|
+
set_non_fields_for_methods(map_has_one_associations)
|
21
22
|
end
|
22
23
|
|
23
24
|
def set_reader_for_main_model
|
@@ -85,6 +86,17 @@ module Linker
|
|
85
86
|
end
|
86
87
|
end
|
87
88
|
|
89
|
+
def set_non_fields_for_methods assoc_set
|
90
|
+
assoc_set.each do |c|
|
91
|
+
#ap "criando método #{c[:name]}_list"
|
92
|
+
self.class.send(:define_method, "#{c[:name]}_list") do
|
93
|
+
assoc = instance_variable_get("@#{get_main_model.to_s.underscore}")
|
94
|
+
.send(c[:name])
|
95
|
+
assoc.present? && assoc.id || nil
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
88
100
|
def map_associations assoc
|
89
101
|
assoc.inject([]) do |t, c|
|
90
102
|
t << {
|
data/lib/linker/forms/params.rb
CHANGED
@@ -12,9 +12,8 @@ module Linker
|
|
12
12
|
end
|
13
13
|
|
14
14
|
params.each do |param, value|
|
15
|
-
table = param.gsub(%r{_attributes$}, '')
|
16
|
-
|
17
15
|
if value.is_a?(Hash)
|
16
|
+
table = param.gsub(%r{_attributes$}, '')
|
18
17
|
# belongs_to attrs
|
19
18
|
if map_belongs_to_associations.select{|c| c[:name] == table}.present?
|
20
19
|
if value['id'].present?
|
@@ -24,7 +23,7 @@ module Linker
|
|
24
23
|
end
|
25
24
|
|
26
25
|
# has_one attrs
|
27
|
-
elsif
|
26
|
+
elsif search_has_one(table)
|
28
27
|
if value['id'].present?
|
29
28
|
_get_main_model.send(table).update_attributes(value)
|
30
29
|
else
|
@@ -44,6 +43,12 @@ module Linker
|
|
44
43
|
end
|
45
44
|
end
|
46
45
|
end
|
46
|
+
elsif param.match(/_list$/)
|
47
|
+
assoc = param.gsub(/_list$/, '')
|
48
|
+
if search_has_one(assoc)
|
49
|
+
final = value.present? ? assoc.camelize.constantize.send(:find, value) : nil
|
50
|
+
_get_main_model.send("#{assoc}=", final)
|
51
|
+
end
|
47
52
|
else
|
48
53
|
self.send("#{param}=", value)
|
49
54
|
end
|
@@ -66,5 +71,9 @@ module Linker
|
|
66
71
|
def _get_main_model
|
67
72
|
main_model ||= self.send(self.class._main_model.underscore)
|
68
73
|
end
|
74
|
+
|
75
|
+
def search_has_one name
|
76
|
+
map_has_one_associations.select{|c| c[:name] == name}.present?
|
77
|
+
end
|
69
78
|
end
|
70
79
|
end
|
data/lib/linker/version.rb
CHANGED
@@ -3,6 +3,7 @@ class User < ActiveRecord::Base
|
|
3
3
|
belongs_to :my_family, class_name: 'Family'
|
4
4
|
|
5
5
|
has_one :address, dependent: :destroy
|
6
|
+
has_one :profile
|
6
7
|
has_one :little_pet, class_name: 'Pet'
|
7
8
|
|
8
9
|
has_many :dependent_users, dependent: :destroy
|
@@ -33,6 +34,9 @@ class Task < ActiveRecord::Base
|
|
33
34
|
attr_accessor :error_message
|
34
35
|
end
|
35
36
|
|
37
|
+
class Profile < ActiveRecord::Base
|
38
|
+
end
|
39
|
+
|
36
40
|
#migrations
|
37
41
|
class CreateAllTables < ActiveRecord::Migration
|
38
42
|
def self.up
|
@@ -88,13 +92,26 @@ class CreateAllTables < ActiveRecord::Migration
|
|
88
92
|
|
89
93
|
t.timestamps
|
90
94
|
end
|
95
|
+
|
96
|
+
create_table :profiles do |t|
|
97
|
+
t.string :profile_type
|
98
|
+
t.references :user, index: true
|
99
|
+
|
100
|
+
t.timestamps
|
101
|
+
end
|
102
|
+
|
91
103
|
end
|
92
104
|
end
|
93
105
|
ActiveRecord::Migration.verbose = false
|
94
106
|
CreateAllTables.up
|
95
107
|
|
108
|
+
Profile.create(profile_type: 'A')
|
109
|
+
Profile.create(profile_type: 'B')
|
110
|
+
Profile.create(profile_type: 'C')
|
111
|
+
|
96
112
|
# seed
|
97
113
|
bar = User.create(name: 'Bar')
|
98
114
|
bar.my_tasks << Task.create(name: 'Task 1')
|
99
115
|
bar.little_pet = Pet.create(name: 'Stuart')
|
116
|
+
bar.profile = Profile.first
|
100
117
|
bar.dependent_users << DependentUser.create(name: 'John', date_birth: Date.new(1990, 2, 1))
|
data/spec/users_form_spec.rb
CHANGED
@@ -36,6 +36,10 @@ describe UsersForm do
|
|
36
36
|
subject(:address) { users_form.address }
|
37
37
|
it { expect(address).to be_an(Address) }
|
38
38
|
|
39
|
+
it { expect(users_form).to respond_to(:profile_list) }
|
40
|
+
subject(:profile_list) { users_form.profile_list }
|
41
|
+
it { expect(profile_list).to eq(nil) }
|
42
|
+
|
39
43
|
it { expect(users_form).to respond_to(:little_pet, :little_pet_attributes=) }
|
40
44
|
subject(:pet) { users_form.little_pet }
|
41
45
|
it { expect(pet).to be_an(Pet) }
|
@@ -64,6 +68,8 @@ describe UsersForm do
|
|
64
68
|
|
65
69
|
it { expect(address.persisted?).to eq(false) }
|
66
70
|
|
71
|
+
it { expect(profile_list).to eq(nil) }
|
72
|
+
|
67
73
|
it { expect(pet.persisted?).to eq(false) }
|
68
74
|
|
69
75
|
it { expect(company.persisted?).to eq(false) }
|
@@ -78,6 +84,7 @@ describe UsersForm do
|
|
78
84
|
'company_attributes' => {'id' => '', 'name' => 'My Company', 'website' => 'mycompany.com'},
|
79
85
|
'my_family_attributes' => {'id' => '', 'last_name' => 'Milan'},
|
80
86
|
'address_attributes' => {'id' => '', 'street' => '', 'district' => ''},
|
87
|
+
'profile_list' => '1',
|
81
88
|
'little_pet_attributes' => {'id' => '', 'name' => 'Stuart'},
|
82
89
|
'my_tasks_attributes' => {'0' => {'id' => '', 'name' => 'T1'}, '1' => {'id' => '', 'name' => 'T2'}},
|
83
90
|
'dependent_users_attributes' => {'0' => {'id' => '', 'name' => '', 'date_birth' => ''}, '1' => {'id' => '', 'name' => '', 'date_birth' => ''}}
|
@@ -98,6 +105,8 @@ describe UsersForm do
|
|
98
105
|
it { expect(dependent_users_sample.persisted?).to eq(true) }
|
99
106
|
it { expect(dependent_users_sample.name).to eq('') }
|
100
107
|
|
108
|
+
it { expect(profile_list).to eq(1) }
|
109
|
+
|
101
110
|
it { expect(pet).to be_a(Pet)}
|
102
111
|
it { expect(pet.persisted?).to be(true)}
|
103
112
|
it { expect(pet.updated_at).to eq(pet.created_at)}
|
@@ -122,6 +131,7 @@ describe UsersForm do
|
|
122
131
|
'company_attributes' => {'id' => '', 'name' => 'My Company', 'website' => 'mycompany.com'},
|
123
132
|
'my_family_attributes' => {'id' => '', 'last_name' => 'Milan'},
|
124
133
|
'address_attributes' => {'id' => '', 'street' => '', 'district' => ''},
|
134
|
+
'profile_list' => '',
|
125
135
|
'little_pet_attributes' => {'id' => '1', 'name' => 'Stuart 2'},
|
126
136
|
#'tasks_attributes' => {'0' => {'id' => '', 'name' => 'T1'}, '1' => {'id' => '', 'name' => 'T2'}},
|
127
137
|
'dependent_users_attributes' => {'0' => {'id' => '1', 'name' => 'John 2', 'date_birth' => Date.new(1990, 2, 2)}, '1' => {'id' => '', 'name' => '', 'date_birth' => ''}}
|
@@ -167,6 +177,8 @@ describe UsersForm do
|
|
167
177
|
it { expect(users_form_existing_user_family.persisted?).to be(true) }
|
168
178
|
it { expect(users_form_existing_user_family.last_name).to eq('Milan') }
|
169
179
|
|
180
|
+
it { expect(users_form_existing_user.profile_list).to be(nil) }
|
181
|
+
|
170
182
|
it { expect(users_form_existing_user_pet).to be_a(Pet) }
|
171
183
|
it { expect(users_form_existing_user_pet.persisted?).to be(true) }
|
172
184
|
it { expect(users_form_existing_user_pet.updated_at).not_to eq(users_form_existing_user_pet.created_at)}
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: linker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Glauco Custódio
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-09-
|
11
|
+
date: 2014-09-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|