linker 0.0.9 → 0.0.10
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 +37 -0
- data/lib/linker/forms/params.rb +22 -2
- data/lib/linker/version.rb +1 -1
- data/spec/cars_form_spec.rb +18 -0
- data/spec/fake_app/active_record/models.rb +9 -0
- data/spec/fake_app/forms/cars_form.rb +22 -0
- data/spec/fake_app/rails_app.rb +1 -0
- metadata +8 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 202e246d9b8bba56438ee91d84cf718d61307e8c
|
4
|
+
data.tar.gz: bb02b4dd6d5672839ad67f88efabb552cae8e475
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6e05c2b3f2805cc9364ab4c3e7ce5a692e97462a51e0e5adb1ad1f1b635284b56eed847bdbd85e997e9590753c4b96a060a5c4fda98120d3ce6e03169ebe5d9e
|
7
|
+
data.tar.gz: 757b6f285cc457725fe4d7a4554e27b545b331dbc31f5ea6f1750e42accdf40a6e8d7ba53f7bfc2c73ebdb6397645b7c3f721f23a60fe4a6a2370d0ce81f8436
|
data/README.md
CHANGED
@@ -44,6 +44,10 @@ class UserForm
|
|
44
44
|
include Linker
|
45
45
|
|
46
46
|
main_model User # or :user or 'User'
|
47
|
+
|
48
|
+
# Use relationship's name followed by "__" plus attribute's name
|
49
|
+
# to validate has_one and belongs_to associations
|
50
|
+
validates :name, :address__street, :company__name, presence: true
|
47
51
|
end
|
48
52
|
```
|
49
53
|
|
@@ -99,6 +103,39 @@ It will also create `params=` and `save` methods responsible to set new objects
|
|
99
103
|
|
100
104
|
You can check out a demo project using Linker gem [here](https://github.com/glaucocustodio/linker_demo). [Click here](http://linker-demo.herokuapp.com/) to see it live.
|
101
105
|
|
106
|
+
## Callbacks
|
107
|
+
|
108
|
+
There are some callbacks you can override to keep your controllers DRY:
|
109
|
+
|
110
|
+
* `before_set_params(params)`: run before `params=` method. Can be used to change params inside the form class, like string formatting.
|
111
|
+
* `before_save`: run before save method.
|
112
|
+
* `after_save`: run after save method. You can enqueue some background job here for ie.
|
113
|
+
|
114
|
+
Example:
|
115
|
+
|
116
|
+
```ruby
|
117
|
+
class CarsForm
|
118
|
+
include Linker
|
119
|
+
attr_accessor :before_save_checked
|
120
|
+
|
121
|
+
main_model :car
|
122
|
+
|
123
|
+
validates :name, presence: true
|
124
|
+
|
125
|
+
def before_set_params params
|
126
|
+
params['name'] = "#{params['name']} 2000"
|
127
|
+
end
|
128
|
+
|
129
|
+
def before_save
|
130
|
+
@before_save_checked = true
|
131
|
+
end
|
132
|
+
|
133
|
+
def after_save
|
134
|
+
HardWorker.perform_async('bob', 5)
|
135
|
+
end
|
136
|
+
end
|
137
|
+
```
|
138
|
+
|
102
139
|
## Contributing
|
103
140
|
|
104
141
|
1. Fork it
|
data/lib/linker/forms/params.rb
CHANGED
@@ -6,6 +6,7 @@ module Linker
|
|
6
6
|
|
7
7
|
included do
|
8
8
|
def params= params
|
9
|
+
before_set_params(params)
|
9
10
|
params.each do |param, value|
|
10
11
|
if value.is_a?(Hash)
|
11
12
|
table = param.gsub(%r{_attributes$}, '')
|
@@ -55,14 +56,33 @@ module Linker
|
|
55
56
|
end
|
56
57
|
end
|
57
58
|
|
59
|
+
# Saves main model with its associated records, with or without validation
|
60
|
+
# (defaults to `:true`, with validation)
|
61
|
+
#
|
62
|
+
# @param validate [boolean] a boolean declaring if the form class must be validated.
|
63
|
+
# @return [boolean] a boolean representing if the form class was validated (or no, if `validate` is `false`) and
|
64
|
+
# saved successfully
|
58
65
|
def save validate: true
|
59
66
|
main_model = _get_main_model
|
67
|
+
before_save
|
60
68
|
|
61
69
|
if validate
|
62
|
-
self.valid? && main_model.save
|
70
|
+
r = (self.valid? && main_model.save)
|
71
|
+
after_save
|
63
72
|
else
|
64
|
-
main_model.save
|
73
|
+
r = main_model.save
|
74
|
+
after_save
|
65
75
|
end
|
76
|
+
r
|
77
|
+
end
|
78
|
+
|
79
|
+
def before_set_params params
|
80
|
+
end
|
81
|
+
|
82
|
+
def before_save
|
83
|
+
end
|
84
|
+
|
85
|
+
def after_save
|
66
86
|
end
|
67
87
|
|
68
88
|
end
|
data/lib/linker/version.rb
CHANGED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CarsForm do
|
4
|
+
let(:cars_form) { CarsForm.new(Car.new) }
|
5
|
+
|
6
|
+
context 'before_set_params' do
|
7
|
+
before do
|
8
|
+
cars_form.params = {
|
9
|
+
'name' => "Uno"
|
10
|
+
}
|
11
|
+
cars_form.save
|
12
|
+
end
|
13
|
+
|
14
|
+
it { expect(cars_form.car.name).to eq('Uno 2000') }
|
15
|
+
it { expect(cars_form.before_save_checked).to eq(true) }
|
16
|
+
it { expect(cars_form.after_save_checked).to eq(true) }
|
17
|
+
end
|
18
|
+
end
|
@@ -41,6 +41,9 @@ end
|
|
41
41
|
class Phone < ActiveRecord::Base
|
42
42
|
end
|
43
43
|
|
44
|
+
class Car < ActiveRecord::Base
|
45
|
+
end
|
46
|
+
|
44
47
|
#migrations
|
45
48
|
class CreateAllTables < ActiveRecord::Migration
|
46
49
|
def self.up
|
@@ -111,6 +114,12 @@ class CreateAllTables < ActiveRecord::Migration
|
|
111
114
|
t.timestamps
|
112
115
|
end
|
113
116
|
|
117
|
+
create_table :cars do |t|
|
118
|
+
t.string :name
|
119
|
+
|
120
|
+
t.timestamps
|
121
|
+
end
|
122
|
+
|
114
123
|
end
|
115
124
|
end
|
116
125
|
ActiveRecord::Migration.verbose = false
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'linker'
|
2
|
+
|
3
|
+
class CarsForm
|
4
|
+
include Linker
|
5
|
+
attr_accessor :before_save_checked, :after_save_checked
|
6
|
+
|
7
|
+
main_model :car
|
8
|
+
|
9
|
+
validates :name, presence: true
|
10
|
+
|
11
|
+
def before_set_params params
|
12
|
+
params['name'] = "#{params['name']} 2000"
|
13
|
+
end
|
14
|
+
|
15
|
+
def before_save
|
16
|
+
@before_save_checked = true
|
17
|
+
end
|
18
|
+
|
19
|
+
def after_save
|
20
|
+
@after_save_checked = true
|
21
|
+
end
|
22
|
+
end
|
data/spec/fake_app/rails_app.rb
CHANGED
@@ -17,6 +17,7 @@ app.initialize!
|
|
17
17
|
require_relative 'active_record/models'
|
18
18
|
require_relative 'forms/dependent_user_form'
|
19
19
|
require_relative 'forms/users_form'
|
20
|
+
require_relative 'forms/cars_form'
|
20
21
|
# controllers
|
21
22
|
# class ApplicationController < ActionController::Base; end
|
22
23
|
|
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.10
|
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:
|
11
|
+
date: 2015-10-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -148,9 +148,11 @@ files:
|
|
148
148
|
- lib/linker/forms/params.rb
|
149
149
|
- lib/linker/version.rb
|
150
150
|
- linker.gemspec
|
151
|
+
- spec/cars_form_spec.rb
|
151
152
|
- spec/dependent_user_form_spec.rb
|
152
153
|
- spec/fake_app/active_record/models.rb
|
153
154
|
- spec/fake_app/config/database.yml
|
155
|
+
- spec/fake_app/forms/cars_form.rb
|
154
156
|
- spec/fake_app/forms/dependent_user_form.rb
|
155
157
|
- spec/fake_app/forms/users_form.rb
|
156
158
|
- spec/fake_app/rails_app.rb
|
@@ -176,16 +178,19 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
176
178
|
version: '0'
|
177
179
|
requirements: []
|
178
180
|
rubyforge_project:
|
179
|
-
rubygems_version: 2.4.
|
181
|
+
rubygems_version: 2.4.6
|
180
182
|
signing_key:
|
181
183
|
specification_version: 4
|
182
184
|
summary: A wrapper to form objects in ActiveRecord. Forget accepts_nested_attributes_for.
|
183
185
|
test_files:
|
186
|
+
- spec/cars_form_spec.rb
|
184
187
|
- spec/dependent_user_form_spec.rb
|
185
188
|
- spec/fake_app/active_record/models.rb
|
186
189
|
- spec/fake_app/config/database.yml
|
190
|
+
- spec/fake_app/forms/cars_form.rb
|
187
191
|
- spec/fake_app/forms/dependent_user_form.rb
|
188
192
|
- spec/fake_app/forms/users_form.rb
|
189
193
|
- spec/fake_app/rails_app.rb
|
190
194
|
- spec/spec_helper.rb
|
191
195
|
- spec/users_form_spec.rb
|
196
|
+
has_rdoc:
|