linker 0.0.1 → 0.0.2

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: e2ed1e6ea0a1cbeb8e9a03be501ebd955520c170
4
- data.tar.gz: 96e64ebd76d12627a1b7cdb9ed6b6a9f9c4e0397
3
+ metadata.gz: bcc18fea535a80a29996d4cc6ae5b80336032844
4
+ data.tar.gz: 73535617eb9909c44eb49867be0038cd6f3cfb5e
5
5
  SHA512:
6
- metadata.gz: 5023b6abca89663ca60e77d67e78c8ec2d90d618ea2fff75b4f024285e8bba6eba5779f25cb28d1571c38d175aa2336ef1d9321ffbb860d27d698ce89f49991e
7
- data.tar.gz: 0550fa1b92021d00cc446ffd72dcc15ddf6b022eff5de7d569371999bdfade9552ed88140483ddde0e60174bb1980de8f077a65c0bece3b68da8d083a86e2f91
6
+ metadata.gz: 976e4b2e0cbcdd89f7f7426c60b75276dc687e3bdf9a634383eefa61f98cea2d9816355013a8806fbe510331046bbad8cbceafbfc9c2f2c5ce345586d9036e7b
7
+ data.tar.gz: 3e127b05c15cf6d8c0851aa0ea60fc39620d25c19937ed65906fb0b7e8f247af0cca3a57eba1d81c5459017a2f600a09397832e80bfcb2c94b7f72ed5fa1621c
data/README.md CHANGED
@@ -37,20 +37,20 @@ class User < ActiveRecord::Base
37
37
  end
38
38
  ```
39
39
 
40
- Create a form class, include Linker and set main model:
40
+ Create a form class through `rails g form whatever` or manually (it should include Linker and have main model set like below).
41
41
  ```ruby
42
- class UsersForm
42
+ class UserForm
43
43
  include Linker
44
44
 
45
45
  main_model User # or :user or 'User'
46
46
  end
47
47
  ```
48
48
 
49
- Now you can create a new form for existing user `UsersForm.new(User.find params[:id])` or to a new one `UsersForm.new(User.new)`:
49
+ Now you can create a new form for existing user `UserForm.new(User.find params[:id])` or to a new one `UserForm.new(User.new)`:
50
50
  ```ruby
51
51
  class UsersController < ApplicationController
52
52
  def new
53
- @user_form = UsersForm.new(User.new)
53
+ @user_form = UserForm.new(User.new)
54
54
  end
55
55
 
56
56
  def create
@@ -0,0 +1,24 @@
1
+ module Rails
2
+ module Generators
3
+ class FormGenerator < Rails::Generators::NamedBase
4
+ source_root File.expand_path('../templates', __FILE__)
5
+
6
+ desc <<DESC
7
+ Description:
8
+ Stubs out a form class in app/forms directory.
9
+
10
+ Examples:
11
+ `rails g form user`
12
+
13
+ This creates:
14
+ app/forms/user_form.rb
15
+ DESC
16
+
17
+ def create_form_file
18
+ template 'form.rb', File.join('app/forms', "#{singular_name}_form.rb")
19
+ end
20
+
21
+ hook_for :test_framework
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,5 @@
1
+ class <%= class_name %>Form
2
+ include Linker
3
+
4
+ main_model <%= class_name.singularize.camelize %>
5
+ end
@@ -0,0 +1,9 @@
1
+ module Rspec
2
+ class FormGenerator < ::Rails::Generators::NamedBase
3
+ source_root File.expand_path('../templates', __FILE__)
4
+
5
+ def create_spec_file
6
+ template 'form_spec.rb', File.join('spec/forms', "#{singular_name}_form_spec.rb")
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,4 @@
1
+ require 'spec_helper'
2
+
3
+ describe <%= singular_name.camelize %>Form do
4
+ end
@@ -0,0 +1,9 @@
1
+ module TestUnit
2
+ class FormGenerator < ::Rails::Generators::NamedBase
3
+ source_root File.expand_path('../templates', __FILE__)
4
+
5
+ def create_test_file
6
+ template 'form_test.rb', File.join('test/forms', "#{singular_name}_form_test.rb")
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,4 @@
1
+ require 'test_helper'
2
+
3
+ class <%= singular_name.camelize %>FormTest < ActiveSupport::TestCase
4
+ end
@@ -65,12 +65,10 @@ module Linker
65
65
  # Create required methods to use `fields_for`
66
66
  def set_fields_for_methods assoc_set, singular = false
67
67
  assoc_set.each do |c|
68
- method_prefix = singular ? c[:tablelized_klass].singularize : c[:tablelized_klass]
69
-
70
- #ap "criando método #{method_prefix}"
71
- self.class.send(:define_method, method_prefix) do
68
+ #ap "criando método #{c[:name]}"
69
+ self.class.send(:define_method, c[:name]) do
72
70
  assocs = instance_variable_get("@#{get_main_model.to_s.underscore}")
73
- .send(method_prefix)
71
+ .send(c[:name])
74
72
 
75
73
  neww = singular ? c[:klass].constantize.new : [c[:klass].constantize.new] * 2
76
74
 
@@ -81,8 +79,8 @@ module Linker
81
79
  end
82
80
  end
83
81
 
84
- #ap "criando método #{method_prefix}_attributes="
85
- self.class.send(:define_method, "#{method_prefix}_attributes=") do |attributes|
82
+ #ap "criando método #{c[:name]}_attributes="
83
+ self.class.send(:define_method, "#{c[:name]}_attributes=") do |attributes|
86
84
  end
87
85
  end
88
86
  end
@@ -90,8 +88,8 @@ module Linker
90
88
  def map_associations assoc
91
89
  assoc.inject([]) do |t, c|
92
90
  t << {
93
- klass: c.klass.name,
94
- tablelized_klass: c.klass.name.pluralize.underscore,
91
+ name: c.name.to_s,
92
+ klass: c.klass.name,
95
93
  # delete_if remove useless attrs
96
94
  columns: filter_columns(c.klass)
97
95
  }
@@ -8,7 +8,7 @@ module Linker
8
8
  def params= params
9
9
  # Delete all associated objects if there is nothing in params
10
10
  @mapped_hm_assoc.each do |c|
11
- _get_main_model.send(c[:tablelized_klass]).destroy_all if !params.key?("#{c[:tablelized_klass]}_attributes")
11
+ _get_main_model.send(c[:name]).destroy_all if !params.key?("#{c[:name]}_attributes")
12
12
  end
13
13
 
14
14
  params.each do |param, value|
@@ -16,7 +16,7 @@ module Linker
16
16
 
17
17
  if value.is_a?(Hash)
18
18
  # belongs_to attrs
19
- if map_belongs_to_associations.select{|c| c[:klass] == table.singularize.camelize}.present?
19
+ if map_belongs_to_associations.select{|c| c[:name] == table.singularize}.present?
20
20
  if value['id'].present?
21
21
  _get_main_model.send(table).update_attributes(value)
22
22
  else
@@ -24,23 +24,23 @@ module Linker
24
24
  end
25
25
 
26
26
  # has_one attrs
27
- elsif map_has_one_associations.select{|c| c[:klass] == table.camelize}.present?
27
+ elsif map_has_one_associations.select{|c| c[:name] == table}.present?
28
28
  if value['id'].present?
29
29
  _get_main_model.send(table).update_attributes(value)
30
30
  else
31
- _get_main_model.send("#{table}=", table.camelize.constantize.new(value))
31
+ _get_main_model.send("build_#{table}", value)
32
32
  end
33
33
 
34
34
  # has_many attrs
35
35
  else
36
36
  ids = value.map.with_index{|c,i| c.last['id'].present? ? c.last['id'] : nil }.compact
37
- _get_main_model.send(table).where(["#{table}.id NOT IN (?)", ids]).destroy_all if ids.present?
37
+ _get_main_model.send(table).where(["#{_get_main_model.send(table).table.name}.id NOT IN (?)", ids]).destroy_all if ids.present?
38
38
 
39
39
  value.each do |c|
40
40
  if c.last['id'].present?
41
41
  _get_main_model.send(table).find(c.last['id']).update_attributes(c.last)
42
42
  else
43
- _get_main_model.send(table) << table.singularize.camelize.constantize.new(c.last)
43
+ _get_main_model.send(table).send(:build, c.last)
44
44
  end
45
45
  end
46
46
  end
@@ -1,3 +1,3 @@
1
1
  module Linker
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -1,11 +1,12 @@
1
1
  class User < ActiveRecord::Base
2
2
  belongs_to :company
3
- belongs_to :family
3
+ belongs_to :my_family, class_name: 'Family'
4
4
 
5
5
  has_one :address, dependent: :destroy
6
+ has_one :little_pet, class_name: 'Pet'
6
7
 
7
8
  has_many :dependent_users, dependent: :destroy
8
- has_many :tasks, dependent: :destroy
9
+ has_many :my_tasks, dependent: :destroy, class_name: 'Task'
9
10
 
10
11
  #has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
11
12
  #validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
@@ -14,6 +15,9 @@ end
14
15
  class Address < ActiveRecord::Base
15
16
  end
16
17
 
18
+ class Pet < ActiveRecord::Base
19
+ end
20
+
17
21
  class Company < ActiveRecord::Base
18
22
  has_many :users
19
23
  end
@@ -35,7 +39,7 @@ class CreateAllTables < ActiveRecord::Migration
35
39
  create_table :users do |t|
36
40
  t.string :name
37
41
  t.references :company, index: true
38
- t.references :family, index: true
42
+ t.references :my_family, index: true
39
43
 
40
44
  t.timestamps
41
45
  end
@@ -76,6 +80,14 @@ class CreateAllTables < ActiveRecord::Migration
76
80
 
77
81
  t.timestamps
78
82
  end
83
+
84
+ create_table :pets do |t|
85
+ t.string :name
86
+
87
+ t.references :user, index: true
88
+
89
+ t.timestamps
90
+ end
79
91
  end
80
92
  end
81
93
  ActiveRecord::Migration.verbose = false
@@ -83,5 +95,6 @@ CreateAllTables.up
83
95
 
84
96
  # seed
85
97
  bar = User.create(name: 'Bar')
86
- bar.tasks << Task.create(name: 'Task 1')
98
+ bar.my_tasks << Task.create(name: 'Task 1')
99
+ bar.little_pet = Pet.create(name: 'Stuart')
87
100
  bar.dependent_users << DependentUser.create(name: 'John', date_birth: Date.new(1990, 2, 1))
@@ -27,21 +27,25 @@ describe UsersForm do
27
27
  subject(:dependent_users_sample) { users_form.dependent_users.sample }
28
28
  it { expect(dependent_users_sample).to be_an(DependentUser) }
29
29
 
30
- it { expect(users_form).to respond_to(:tasks, :tasks_attributes=) }
31
- it { expect(users_form.tasks).to be_an(Array) }
32
- subject(:tasks_sample) { users_form.tasks.sample }
30
+ it { expect(users_form).to respond_to(:my_tasks, :my_tasks_attributes=) }
31
+ it { expect(users_form.my_tasks).to be_an(Array) }
32
+ subject(:tasks_sample) { users_form.my_tasks.sample }
33
33
  it { expect(tasks_sample).to be_an(Task) }
34
34
 
35
35
  it { expect(users_form).to respond_to(:address, :address_attributes=) }
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(:little_pet, :little_pet_attributes=) }
40
+ subject(:pet) { users_form.little_pet }
41
+ it { expect(pet).to be_an(Pet) }
42
+
39
43
  it { expect(users_form).to respond_to(:company, :company_attributes=) }
40
44
  subject(:company) { users_form.company }
41
45
  it { expect(company).to be_an(Company) }
42
46
 
43
- it { expect(users_form).to respond_to(:family, :family_attributes=) }
44
- subject(:family) { users_form.family }
47
+ it { expect(users_form).to respond_to(:my_family, :my_family_attributes=) }
48
+ subject(:family) { users_form.my_family }
45
49
  it { expect(family).to be_an(Family) }
46
50
 
47
51
  context 'new' do
@@ -60,6 +64,8 @@ describe UsersForm do
60
64
 
61
65
  it { expect(address.persisted?).to eq(false) }
62
66
 
67
+ it { expect(pet.persisted?).to eq(false) }
68
+
63
69
  it { expect(company.persisted?).to eq(false) }
64
70
 
65
71
  it { expect(family.persisted?).to eq(false) }
@@ -70,9 +76,10 @@ describe UsersForm do
70
76
  users_form.params = {
71
77
  'name' => "Foo",
72
78
  'company_attributes' => {'id' => '', 'name' => 'My Company', 'website' => 'mycompany.com'},
73
- 'family_attributes' => {'id' => '', 'last_name' => 'Milan'},
79
+ 'my_family_attributes' => {'id' => '', 'last_name' => 'Milan'},
74
80
  'address_attributes' => {'id' => '', 'street' => '', 'district' => ''},
75
- 'tasks_attributes' => {'0' => {'id' => '', 'name' => 'T1'}, '1' => {'id' => '', 'name' => 'T2'}},
81
+ 'little_pet_attributes' => {'id' => '', 'name' => 'Stuart'},
82
+ 'my_tasks_attributes' => {'0' => {'id' => '', 'name' => 'T1'}, '1' => {'id' => '', 'name' => 'T2'}},
76
83
  'dependent_users_attributes' => {'0' => {'id' => '', 'name' => '', 'date_birth' => ''}, '1' => {'id' => '', 'name' => '', 'date_birth' => ''}}
77
84
  }
78
85
  users_form.save
@@ -82,23 +89,29 @@ describe UsersForm do
82
89
  it { expect(user.persisted?).to eq(true) }
83
90
  it { expect(user.name).to eq('Foo') }
84
91
 
92
+ subject(:user_tasks) { user.my_tasks }
85
93
  it { expect(tasks_sample.persisted?).to eq(true) }
86
- it { expect(users_form.tasks.first.id).to eq(user.tasks.first.id) }
87
- it { expect(user.tasks.first.name).to eq('T1') }
88
- it { expect(user.tasks.last.name).to eq('T2') }
94
+ it { expect(users_form.my_tasks.first.id).to eq(user.my_tasks.first.id) }
95
+ it { expect(user_tasks.first.name).to eq('T1') }
96
+ it { expect(user_tasks.last.name).to eq('T2') }
89
97
 
90
98
  it { expect(dependent_users_sample.persisted?).to eq(true) }
91
99
  it { expect(dependent_users_sample.name).to eq('') }
92
100
 
101
+ it { expect(pet).to be_a(Pet)}
102
+ it { expect(pet.persisted?).to be(true)}
103
+ it { expect(pet.updated_at).to eq(pet.created_at)}
104
+ it { expect(pet.name).to eq('Stuart')}
105
+
93
106
  subject(:user_company) { user.company }
94
107
  it { expect(user_company.persisted?).to eq(true) }
95
108
  it { expect(users_form.company.id).to eq(user_company.id) }
96
109
  it { expect(user_company.name).to eq('My Company') }
97
110
  it { expect(user_company.website).to eq('mycompany.com') }
98
111
 
99
- subject(:user_family) { user.family }
112
+ subject(:user_family) { user.my_family }
100
113
  it { expect(user_family.persisted?).to eq(true) }
101
- it { expect(users_form.family.id).to eq(user_family.id) }
114
+ it { expect(user_family.id).to eq(user_family.id) }
102
115
  it { expect(user_family.last_name).to eq('Milan') }
103
116
  end
104
117
 
@@ -107,8 +120,9 @@ describe UsersForm do
107
120
  users_form_existing_user.params = {
108
121
  'name' => "Bar",
109
122
  'company_attributes' => {'id' => '', 'name' => 'My Company', 'website' => 'mycompany.com'},
110
- 'family_attributes' => {'id' => '', 'last_name' => 'Milan'},
123
+ 'my_family_attributes' => {'id' => '', 'last_name' => 'Milan'},
111
124
  'address_attributes' => {'id' => '', 'street' => '', 'district' => ''},
125
+ 'little_pet_attributes' => {'id' => '1', 'name' => 'Stuart 2'},
112
126
  #'tasks_attributes' => {'0' => {'id' => '', 'name' => 'T1'}, '1' => {'id' => '', 'name' => 'T2'}},
113
127
  'dependent_users_attributes' => {'0' => {'id' => '1', 'name' => 'John 2', 'date_birth' => Date.new(1990, 2, 2)}, '1' => {'id' => '', 'name' => '', 'date_birth' => ''}}
114
128
  }
@@ -120,9 +134,10 @@ describe UsersForm do
120
134
  subject(:users_form_existing_user_user_dependent_users) { users_form_existing_user.user.dependent_users }
121
135
  subject(:users_form_existing_user_company) { users_form_existing_user.company }
122
136
  subject(:users_form_existing_user_address) { users_form_existing_user.address }
123
- subject(:users_form_existing_user_family) { users_form_existing_user.family }
124
- subject(:users_form_existing_user_user_tasks) { users_form_existing_user.user.tasks }
125
- subject(:users_form_existing_user_tasks_sample) { users_form_existing_user.tasks.sample }
137
+ subject(:users_form_existing_user_pet) { users_form_existing_user.little_pet }
138
+ subject(:users_form_existing_user_family) { users_form_existing_user.my_family }
139
+ subject(:users_form_existing_user_user_tasks) { users_form_existing_user.user.my_tasks }
140
+ subject(:users_form_existing_user_tasks_sample) { users_form_existing_user.my_tasks.sample }
126
141
 
127
142
  subject(:users_form_existing_user_to_model) { users_form_existing_user.to_model }
128
143
  it { expect(users_form_existing_user_to_model.class).to eq(User) }
@@ -134,6 +149,8 @@ describe UsersForm do
134
149
 
135
150
  it { expect(users_form_existing_user_dependent_users.sample.persisted?).to eq(true) }
136
151
  it { expect(users_form_existing_user_dependent_users.first.id).to eq(1) }
152
+ it { expect(users_form_existing_user_dependent_users.first.updated_at).not_to eq(users_form_existing_user_dependent_users.first.created_at) }
153
+ it { expect(users_form_existing_user_dependent_users.last.updated_at).to eq(users_form_existing_user_dependent_users.last.created_at) }
137
154
  it { expect(users_form_existing_user_dependent_users.first.name).to eq('John 2') }
138
155
 
139
156
  it { expect(users_form_existing_user_user_dependent_users.sample.persisted?).to eq(true)}
@@ -149,8 +166,14 @@ describe UsersForm do
149
166
 
150
167
  it { expect(users_form_existing_user_family.persisted?).to be(true) }
151
168
  it { expect(users_form_existing_user_family.last_name).to eq('Milan') }
169
+
170
+ it { expect(users_form_existing_user_pet).to be_a(Pet) }
171
+ it { expect(users_form_existing_user_pet.persisted?).to be(true) }
172
+ it { expect(users_form_existing_user_pet.updated_at).not_to eq(users_form_existing_user_pet.created_at)}
173
+ it { expect(users_form_existing_user_pet.name).to eq('Stuart 2') }
152
174
 
153
175
  it { expect(users_form_existing_user_address.persisted?).to be(true) }
154
176
  it { expect(users_form_existing_user_address.street).to eq('') }
177
+ it { expect(users_form_existing_user_address.district).to eq('') }
155
178
  end
156
179
  end
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.1
4
+ version: 0.0.2
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-05 00:00:00.000000000 Z
11
+ date: 2014-09-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -136,6 +136,12 @@ files:
136
136
  - LICENSE
137
137
  - README.md
138
138
  - Rakefile
139
+ - lib/generators/rails/form_generator.rb
140
+ - lib/generators/rails/templates/form.rb
141
+ - lib/generators/rspec/form_generator.rb
142
+ - lib/generators/rspec/templates/form_spec.rb
143
+ - lib/generators/test_unit/form_generator.rb
144
+ - lib/generators/test_unit/templates/form_test.rb
139
145
  - lib/linker.rb
140
146
  - lib/linker/forms/attributes.rb
141
147
  - lib/linker/forms/configuration_methods.rb