dynamic-fields-for 1.0.4 → 1.1.0

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.
data/README.md CHANGED
@@ -14,6 +14,7 @@ The main features are:
14
14
  * Simple and predictable interface and behavior;
15
15
  * Not requires any special HTML entities inside templates;
16
16
  * Supports [Simple Form](https://github.com/plataformatec/simple_form).
17
+ * Supports not ActiveRecord models
17
18
 
18
19
  ## Alternatives
19
20
  * [coccon](https://github.com/nathanvda/cocoon)
@@ -106,6 +107,46 @@ DynamicFieldsFor supports SimpleForm:
106
107
  = f.submit
107
108
  ```
108
109
 
110
+ ## Not ActiveRecord models
111
+ To use DynamicFieldsFor with not ActiveRecord, need to define two methods in your model, `{association}_soft_build` and `{association}_attributes=`:
112
+
113
+ ```ruby
114
+ class EmailForm
115
+ include ActiveAttr::Model
116
+
117
+ attribute :recipients, type: Object, default: []
118
+
119
+ def recipients_attributes=(attributes)
120
+ self.recipients = attributes.values.map{ |attrs| recipients_soft_build(attrs) }
121
+ end
122
+
123
+ def recipients_soft_build(attrs = {})
124
+ Recipient.new(attrs)
125
+ end
126
+
127
+ end
128
+
129
+ class Recipient
130
+ include ActiveAttr::Model
131
+
132
+ attribute :email
133
+
134
+ validates :email, presence: true
135
+ end
136
+ ```
137
+
138
+ Template will stay to be as usual:
139
+ ```haml
140
+ = form_for resource do |f|
141
+ = f.fields_for :recipients, dynamic: true do |rf|
142
+ = rf.text_field :email
143
+ = rf.remove_fields_link 'Remove recipient'
144
+
145
+ = f.add_fields_link :recipients, 'Add recipient'
146
+
147
+ = f.submit
148
+ ```
149
+
109
150
  ## JavaScript events
110
151
  There are the events which will be triggered on `add_fields_link` click, in actual order:
111
152
  * `dynamic-fields:before-add-into` touched to dynamic fields parent node;
@@ -16,7 +16,10 @@ RubyFeatures.define 'dynamic_fields_for' do
16
16
 
17
17
  return fields_for_without_dynamic_fields(association, record_object, options, &block) unless options.delete(:dynamic)
18
18
 
19
- new_object = @object.association(association).soft_build
19
+ soft_build_method_name = :"#{association}_soft_build"
20
+ new_object = @object.respond_to?(soft_build_method_name) ?
21
+ @object.send(soft_build_method_name) :
22
+ @object.association(association).soft_build
20
23
 
21
24
  options[:child_index] = 'dynamic_fields_index'
22
25
  remove_template = fields_for_without_dynamic_fields(association, new_object, options) do |f|
@@ -1,3 +1,3 @@
1
1
  module DynamicFieldsFor
2
- VERSION = '1.0.4'.freeze
2
+ VERSION = '1.1.0'.freeze
3
3
  end
@@ -9,16 +9,24 @@ describe DynamicFieldsFor do
9
9
  all('[name$="[role_name]"]')
10
10
  end
11
11
 
12
- def remove_links
12
+ def remove_role_links
13
13
  all('a', text: 'Remove role')
14
14
  end
15
15
 
16
+ def email_inputs
17
+ all('[name$="[email]"]')
18
+ end
19
+
20
+ def remove_email_links
21
+ all('a', text: 'Remove recipient')
22
+ end
23
+
16
24
  def deal_with_dynamic_roles
17
- expect{remove_links.last.click}.to change{remove_links.size}.from(3).to(2)
18
- expect{2.times{ click_link 'Add role'}}.to change{remove_links.size}.to(4)
19
- expect{remove_links.last.click}.to change{remove_links.size}.to(3)
20
- expect{click_link 'Add role'}.to change{remove_links.size}.to(4)
21
- expect{remove_links[1].click}.to change{remove_links.size}.to(3)
25
+ expect{remove_role_links.last.click}.to change{remove_role_links.size}.from(3).to(2)
26
+ expect{2.times{ click_link 'Add role'}}.to change{remove_role_links.size}.to(4)
27
+ expect{remove_role_links.last.click}.to change{remove_role_links.size}.to(3)
28
+ expect{click_link 'Add role'}.to change{remove_role_links.size}.to(4)
29
+ expect{remove_role_links[1].click}.to change{remove_role_links.size}.to(3)
22
30
 
23
31
  role_inputs.last(2).each_with_index do |element, index|
24
32
  element.set("new role #{index}")
@@ -56,9 +64,24 @@ describe DynamicFieldsFor do
56
64
 
57
65
  expect_result(user)
58
66
  end
67
+
59
68
  end
60
69
  end
61
70
 
71
+ it 'should work with ActiveAttr' do
72
+ visit '/email_forms/new'
73
+ 3.times{ click_link 'Add recipient' }
74
+ remove_email_links.last.click
75
+
76
+ email_inputs.last(2).each_with_index do |element, index|
77
+ element.set("email#{index}@qwerty.com")
78
+ end
79
+
80
+ click_button 'Create Email form'
81
+
82
+ expect(page).to have_content('Emails was sent to email0@qwerty.com, email1@qwerty.com')
83
+ end
84
+
62
85
  it 'should not fail when remove link clicked but dynamic fields are not exists' do
63
86
  visit "/users/#{user.id}/edit_without_fields"
64
87
 
@@ -78,7 +101,7 @@ describe DynamicFieldsFor do
78
101
  it 'should remove fields with clean text' do
79
102
  visit "/users/#{user.id}/edit_with_clean_text"
80
103
  within('.clean-text-with-remove-link') do
81
- remove_links.last.click
104
+ remove_role_links.last.click
82
105
  expect(page).to have_content(Array.new(2, 'clean text Remove role').push('Add role').join(' '))
83
106
  end
84
107
  end
@@ -88,7 +111,7 @@ describe DynamicFieldsFor do
88
111
  click_link 'Edit'
89
112
 
90
113
  within('#form_for') do
91
- expect{click_link 'Add role'}.to change{remove_links.size}.from(3).to(4)
114
+ expect{click_link 'Add role'}.to change{remove_role_links.size}.from(3).to(4)
92
115
  end
93
116
 
94
117
  end
@@ -0,0 +1,9 @@
1
+ class EmailFormsController < ApplicationController
2
+ before_filter :build_resource, only: [:new, :create]
3
+
4
+ private
5
+
6
+ def build_resource
7
+ @resource = EmailForm.new(params[:email_form] || {})
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ class EmailForm
2
+ include ActiveAttr::Model
3
+
4
+ attribute :recipients, type: Object, default: []
5
+
6
+ def recipients_attributes=(attributes)
7
+ self.recipients = attributes.values.map{ |attrs| recipients_soft_build(attrs) }
8
+ end
9
+
10
+ def recipients_soft_build(attrs = {})
11
+ Recipient.new(attrs)
12
+ end
13
+
14
+ end
@@ -0,0 +1,7 @@
1
+ class Recipient
2
+ include ActiveAttr::Model
3
+
4
+ attribute :email
5
+
6
+ validates :email, presence: true
7
+ end
@@ -0,0 +1,8 @@
1
+ = form_for resource do |f|
2
+ = f.fields_for :recipients, dynamic: true do |rf|
3
+ = rf.text_field :email
4
+ = rf.remove_fields_link 'Remove recipient'
5
+
6
+ = f.add_fields_link :recipients, 'Add recipient'
7
+
8
+ = f.submit
@@ -0,0 +1,2 @@
1
+ Emails was sent to
2
+ = @resource.recipients.map(&:email).join(', ')
@@ -0,0 +1 @@
1
+ = render 'form', resource: @resource
@@ -1,4 +1,6 @@
1
1
  Rails.application.routes.draw do
2
+ resources :email_forms
3
+
2
4
  resources :users do
3
5
  member do
4
6
  get 'edit_without_fields'
@@ -3174,3 +3174,105 @@ Started GET "/assets/turbolinks.self.js?body=1" for 127.0.0.1 at 2015-05-26 21:3
3174
3174
 
3175
3175
 
3176
3176
  Started GET "/assets/dynamic-fields-for.self.js?body=1" for 127.0.0.1 at 2015-05-26 21:34:16 +0300
3177
+
3178
+
3179
+ Started GET "/" for 127.0.0.1 at 2015-06-18 15:06:53 +0300
3180
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
3181
+ Processing by Rails::WelcomeController#index as HTML
3182
+ Rendered /Users/st/.rbenv/versions/1.9.3-p547/lib/ruby/gems/1.9.1/gems/railties-4.1.10/lib/rails/templates/rails/welcome/index.html.erb (1.8ms)
3183
+ Completed 200 OK in 25ms (Views: 7.8ms | ActiveRecord: 0.0ms)
3184
+
3185
+
3186
+ Started GET "/email_forms/new" for 127.0.0.1 at 2015-06-18 15:07:01 +0300
3187
+ Processing by EmailFormsController#new as HTML
3188
+ Rendered email_forms/_form.html.haml (9.4ms)
3189
+ Rendered email_forms/new.html.haml within layouts/application (12.3ms)
3190
+ Completed 200 OK in 239ms (Views: 168.7ms | ActiveRecord: 0.0ms)
3191
+
3192
+
3193
+ Started GET "/assets/jquery.self.js?body=1" for 127.0.0.1 at 2015-06-18 15:07:01 +0300
3194
+
3195
+
3196
+ Started GET "/assets/dynamic-fields-for.self.js?body=1" for 127.0.0.1 at 2015-06-18 15:07:01 +0300
3197
+
3198
+
3199
+ Started GET "/assets/turbolinks.self.js?body=1" for 127.0.0.1 at 2015-06-18 15:07:01 +0300
3200
+
3201
+
3202
+ Started GET "/assets/application.self.js?body=1" for 127.0.0.1 at 2015-06-18 15:07:01 +0300
3203
+
3204
+
3205
+ Started GET "/email_forms/new" for 127.0.0.1 at 2015-06-18 15:08:53 +0300
3206
+ Processing by EmailFormsController#new as HTML
3207
+ Rendered email_forms/_form.html.haml (1.8ms)
3208
+ Rendered email_forms/new.html.haml within layouts/application (2.3ms)
3209
+ Completed 200 OK in 14ms (Views: 11.4ms | ActiveRecord: 0.0ms)
3210
+
3211
+
3212
+ Started GET "/assets/jquery.self.js?body=1" for 127.0.0.1 at 2015-06-18 15:08:53 +0300
3213
+
3214
+
3215
+ Started GET "/assets/application.self.js?body=1" for 127.0.0.1 at 2015-06-18 15:08:53 +0300
3216
+
3217
+
3218
+ Started GET "/assets/dynamic-fields-for.self.js?body=1" for 127.0.0.1 at 2015-06-18 15:08:53 +0300
3219
+
3220
+
3221
+ Started GET "/assets/turbolinks.self.js?body=1" for 127.0.0.1 at 2015-06-18 15:08:53 +0300
3222
+
3223
+
3224
+ Started GET "/email_forms/new" for 127.0.0.1 at 2015-06-18 15:09:01 +0300
3225
+ Processing by EmailFormsController#new as HTML
3226
+ Rendered email_forms/_form.html.haml (1.8ms)
3227
+ Rendered email_forms/new.html.haml within layouts/application (2.4ms)
3228
+ Completed 200 OK in 12ms (Views: 11.3ms | ActiveRecord: 0.0ms)
3229
+
3230
+
3231
+ Started GET "/assets/jquery.self.js?body=1" for 127.0.0.1 at 2015-06-18 15:09:01 +0300
3232
+
3233
+
3234
+ Started GET "/assets/turbolinks.self.js?body=1" for 127.0.0.1 at 2015-06-18 15:09:01 +0300
3235
+
3236
+
3237
+ Started GET "/assets/dynamic-fields-for.self.js?body=1" for 127.0.0.1 at 2015-06-18 15:09:01 +0300
3238
+
3239
+
3240
+ Started GET "/assets/application.self.js?body=1" for 127.0.0.1 at 2015-06-18 15:09:01 +0300
3241
+
3242
+
3243
+ Started GET "/email_forms/new" for 127.0.0.1 at 2015-06-18 15:09:05 +0300
3244
+ Processing by EmailFormsController#new as HTML
3245
+ Rendered email_forms/_form.html.haml (2.1ms)
3246
+ Rendered email_forms/new.html.haml within layouts/application (2.7ms)
3247
+ Completed 200 OK in 14ms (Views: 13.2ms | ActiveRecord: 0.0ms)
3248
+
3249
+
3250
+ Started GET "/assets/jquery.self.js?body=1" for 127.0.0.1 at 2015-06-18 15:09:05 +0300
3251
+
3252
+
3253
+ Started GET "/assets/dynamic-fields-for.self.js?body=1" for 127.0.0.1 at 2015-06-18 15:09:05 +0300
3254
+
3255
+
3256
+ Started GET "/assets/turbolinks.self.js?body=1" for 127.0.0.1 at 2015-06-18 15:09:05 +0300
3257
+
3258
+
3259
+ Started GET "/assets/application.self.js?body=1" for 127.0.0.1 at 2015-06-18 15:09:05 +0300
3260
+
3261
+
3262
+ Started GET "/email_forms/new" for 127.0.0.1 at 2015-06-18 15:09:15 +0300
3263
+ Processing by EmailFormsController#new as HTML
3264
+ Rendered email_forms/_form.html.haml (2.7ms)
3265
+ Rendered email_forms/new.html.haml within layouts/application (3.2ms)
3266
+ Completed 200 OK in 13ms (Views: 12.6ms | ActiveRecord: 0.0ms)
3267
+
3268
+
3269
+ Started GET "/assets/jquery.self.js?body=1" for 127.0.0.1 at 2015-06-18 15:09:15 +0300
3270
+
3271
+
3272
+ Started GET "/assets/turbolinks.self.js?body=1" for 127.0.0.1 at 2015-06-18 15:09:15 +0300
3273
+
3274
+
3275
+ Started GET "/assets/dynamic-fields-for.self.js?body=1" for 127.0.0.1 at 2015-06-18 15:09:15 +0300
3276
+
3277
+
3278
+ Started GET "/assets/application.self.js?body=1" for 127.0.0.1 at 2015-06-18 15:09:15 +0300