best_in_place 3.0.0 → 3.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bedb7e2c588cc03dae88a001cdfe44d379d5be92
4
- data.tar.gz: c6e65e4b6a3a96b0a1e42477375ec87cdba24b91
3
+ metadata.gz: a7128e2317c366f2b4b3c07523f9a7007c9d441f
4
+ data.tar.gz: cd010b383a647c68f71c91ec0bc6351da8777be3
5
5
  SHA512:
6
- metadata.gz: c1850801ec202d82b903d6c6e61765bae359e1dc5de6b88374a84fe1900ae01dabf4b9a8abc9ef5875ee9f961b3691642bbc6c15e0adc9662f1bc5374879c038
7
- data.tar.gz: 2132cfc690867a04fa9b3065e4ccd2bf3796aa1b0fef1f5569518bcb0f64872ccbdf010c81e967aee182b2dda12cf9d2fb5b0462ff062e7cf3eec4f2b3a33583
6
+ metadata.gz: acb6ef8c4bf474ac43408fb12c0c37cee42c4fbb4dde9419ab3abb415ba32f29d45de261129036712fc46fcd09d48b0957a7986c90ea5e80e5edfcc273c85521
7
+ data.tar.gz: f64607487b57f5531b5d789df4cc168d07344e0282c0660492f16e477344d3a3efe191c6083ba17060c855973ecdfca492363a0fd4d71e0ea6322579781dfbe2
data/.travis.yml CHANGED
@@ -11,10 +11,7 @@ gemfile:
11
11
  - gemfiles/rails_4.1.gemfile
12
12
  - gemfiles/rails_edge.gemfile
13
13
 
14
- before_script:
15
- - "sh -e /etc/init.d/xvfb start"
16
-
17
14
  matrix:
18
15
  allow_failures:
19
16
  - gemfile: gemfiles/rails_edge.gemfile
20
- - rvm: rbx-2
17
+ - rvm: rbx-2
data/Gemfile CHANGED
@@ -1,9 +1,10 @@
1
- source "http://rubygems.org"
1
+ source 'https://rubygems.org'
2
2
  source 'https://rails-assets.org'
3
3
 
4
4
  # Specify your gem's dependencies in best_in_place.gemspec
5
5
  gemspec
6
6
 
7
+ gem 'activerecord'
7
8
  gem 'rails-assets-jquery', '1.11.1'
8
9
  gem 'rails-assets-jquery-ui', '1.10.4'
9
10
  gem 'rdiscount'
@@ -12,9 +13,10 @@ gem 'nokogiri'
12
13
  gem 'combustion'
13
14
  gem 'sprockets-rails'
14
15
  gem 'capybara'
15
- gem 'selenium-webdriver'
16
+ gem 'poltergeist'
16
17
  gem 'sqlite3'
17
18
  gem 'appraisal'
19
+ gem 'launchy'
18
20
 
19
21
  platforms :mri_21 do
20
22
  gem 'byebug'
data/README.md CHANGED
@@ -15,6 +15,37 @@ The editor works by PUTting the updated value to the server and GETting the upda
15
15
 
16
16
  ---
17
17
 
18
+ ##Installation
19
+
20
+ ###Rails
21
+
22
+ Installing *best_in_place* is very easy and straight-forward.
23
+ Just begin including the gem in your Gemfile:
24
+
25
+ gem 'best_in_place', '~> 3.0.1'
26
+
27
+ After that, specify the use of the jquery and best in place
28
+ javascripts in your application.js, and optionally specify jquery-ui if
29
+ you want to use jQuery UI datepickers:
30
+
31
+ //= require jquery
32
+ //= require best_in_place
33
+
34
+ //= require jquery-ui
35
+ //= require best_in_place.jquery-ui
36
+
37
+ If you want to use jQuery UI datepickers, you should also install and
38
+ load your preferred jquery-ui CSS file and associated assets.
39
+
40
+ Then, just add a binding to prepare all best in place fields when the document is ready:
41
+
42
+ $(document).ready(function() {
43
+ /* Activating Best In Place */
44
+ jQuery(".best_in_place").best_in_place();
45
+ });
46
+
47
+ You are done!
48
+
18
49
  ##Features
19
50
 
20
51
  - Compatible with text **inputs**
@@ -46,7 +77,7 @@ Params:
46
77
  Options:
47
78
 
48
79
  - **:type** It can be only [:input, :textarea, :select, :checkbox, :date (>= 1.0.4)] or if undefined it defaults to :input.
49
- - **:collection**: In case you are using the :select type then you must specify the collection of values it takes. In case you are
80
+ - **:collection**: If you are using the :select type then you must specify the collection of values it takes as a hash where keys represent the display text and values are the option's value when selected. If you are
50
81
  using the :checkbox type you can specify the two values it can take, or otherwise they will default to Yes and No.
51
82
  - **:url**: URL to which the updating action will be sent. If not defined it defaults to the :object path.
52
83
  - **:place_holder**: The nil param defines the content displayed in case no value is defined for that field. It can be something like "click me to edit".
@@ -92,8 +123,7 @@ It is a very useful feature to use with, for example, [Ryan Bates](https://githu
92
123
 
93
124
  ---
94
125
 
95
- ##TestApp and examples
96
- A [test_app](https://github.com/bernat/best_in_place/tree/master/test_app) was created, and can be seen in action in a [running demo on heroku](http://bipapp.heroku.com).
126
+ ##Examples
97
127
 
98
128
  Examples (code in the views):
99
129
 
@@ -111,10 +141,10 @@ Examples (code in the views):
111
141
 
112
142
  ### Select
113
143
 
114
- <%= best_in_place @user, :country, :type => :select, :collection => {{"1", "Spain"}, {"2", "Italy"}, {"3", "Germany"}, {"4", "France"}} %>
144
+ <%= best_in_place @user, :country, :type => :select, :collection => {"1" => "Spain", "2" => "Italy", "3" => "Germany", "4" => "France"} %>
115
145
 
116
- Of course it can take an instance or global variable for the collection, just remember the structure `{{key, value}, {key, value},...}`.
117
- The key has to be a string. *
146
+ Of course it can take an instance or global variable for the collection, just remember the structure is a hash.
147
+ The key will always be converted to a string for display. *
118
148
 
119
149
  ### Checkbox
120
150
 
@@ -285,38 +315,6 @@ And finally we need a controller:
285
315
 
286
316
  And this is how it is done!
287
317
 
288
- ##Installation
289
-
290
- ###Rails
291
-
292
- Installing *best_in_place* is very easy and straight-forward.
293
- Just begin including the gem in your Gemfile:
294
-
295
- gem "best_in_place"
296
-
297
- After that, specify the use of the jquery and best in place
298
- javascripts in your application.js, and optionally specify jquery-ui if
299
- you want to use jQuery UI datepickers:
300
-
301
- //= require jquery
302
- //= require best_in_place
303
-
304
- //= require jquery-ui
305
- //= require best_in_place.jquery-ui
306
-
307
- If you want to use jQuery UI datepickers, you should also install and
308
- load your preferred jquery-ui CSS file and associated assets.
309
-
310
- Then, just add a binding to prepare all best in place fields when the document is ready:
311
-
312
- $(document).ready(function() {
313
- /* Activating Best In Place */
314
- jQuery(".best_in_place").best_in_place();
315
- });
316
-
317
- You are done!
318
-
319
-
320
318
 
321
319
  ## Notification
322
320
 
data/config.ru CHANGED
@@ -3,5 +3,5 @@ require 'bundler'
3
3
 
4
4
  Bundler.require :default, :development
5
5
 
6
- Combustion.initialize!
6
+ Combustion.initialize! :active_record
7
7
  run Combustion::Application
@@ -1,6 +1,6 @@
1
1
  # This file was generated by Appraisal
2
2
 
3
- source "http://rubygems.org"
3
+ source "https://rubygems.org"
4
4
  source "https://rails-assets.org"
5
5
 
6
6
  gem "rails-assets-jquery", "1.11.1"
@@ -11,9 +11,10 @@ gem "nokogiri"
11
11
  gem "combustion"
12
12
  gem "sprockets-rails"
13
13
  gem "capybara"
14
- gem "selenium-webdriver"
14
+ gem "poltergeist"
15
15
  gem "sqlite3"
16
16
  gem "appraisal"
17
+ gem "launchy"
17
18
  gem "rails", "3.2.19"
18
19
  gem "strong_parameters"
19
20
 
@@ -1,6 +1,6 @@
1
1
  # This file was generated by Appraisal
2
2
 
3
- source "http://rubygems.org"
3
+ source "https://rubygems.org"
4
4
  source "https://rails-assets.org"
5
5
 
6
6
  gem "rails-assets-jquery", "1.11.1"
@@ -11,9 +11,10 @@ gem "nokogiri"
11
11
  gem "combustion"
12
12
  gem "sprockets-rails"
13
13
  gem "capybara"
14
- gem "selenium-webdriver"
14
+ gem "poltergeist"
15
15
  gem "sqlite3"
16
16
  gem "appraisal"
17
+ gem "launchy"
17
18
  gem "rails", "~> 4.0.0"
18
19
 
19
20
  platforms :mri_21 do
@@ -1,6 +1,6 @@
1
1
  # This file was generated by Appraisal
2
2
 
3
- source "http://rubygems.org"
3
+ source "https://rubygems.org"
4
4
  source "https://rails-assets.org"
5
5
 
6
6
  gem "rails-assets-jquery", "1.11.1"
@@ -11,9 +11,10 @@ gem "nokogiri"
11
11
  gem "combustion"
12
12
  gem "sprockets-rails"
13
13
  gem "capybara"
14
- gem "selenium-webdriver"
14
+ gem "poltergeist"
15
15
  gem "sqlite3"
16
16
  gem "appraisal"
17
+ gem "launchy"
17
18
  gem "rails", "~> 4.1.0"
18
19
 
19
20
  platforms :mri_21 do
@@ -1,6 +1,6 @@
1
1
  # This file was generated by Appraisal
2
2
 
3
- source "http://rubygems.org"
3
+ source "https://rubygems.org"
4
4
  source "https://rails-assets.org"
5
5
 
6
6
  gem "rails-assets-jquery", "1.11.1"
@@ -11,12 +11,12 @@ gem "nokogiri"
11
11
  gem "combustion"
12
12
  gem "sprockets-rails"
13
13
  gem "capybara"
14
- gem "selenium-webdriver"
14
+ gem "poltergeist"
15
15
  gem "sqlite3"
16
16
  gem "appraisal"
17
+ gem "launchy"
17
18
  gem "rails", :github => "rails/rails"
18
19
  gem "arel", :github => "rails/arel"
19
- gem "minitest"
20
20
 
21
21
  platforms :mri_21 do
22
22
  gem "byebug"
@@ -439,6 +439,7 @@ BestInPlaceEditor.forms = {
439
439
  'use strict';
440
440
  if (event.keyCode === 27) {
441
441
  event.data.editor.abort();
442
+ event.stopImmediatePropagation();
442
443
  }
443
444
  }
444
445
  },
@@ -518,6 +519,7 @@ BestInPlaceEditor.forms = {
518
519
  .attr('action', 'javascript:void(0);')
519
520
  .attr('style', 'display:inline');
520
521
  var textarea_elt = jQuery(document.createElement('textarea'))
522
+ .attr('name', this.attributeName)
521
523
  .val(this.sanitizeValue(this.display_value));
522
524
 
523
525
  if (this.inner_class !== null) {
@@ -18,19 +18,18 @@ module BestInPlace
18
18
 
19
19
  if opts[:collection] or type == :checkbox
20
20
  collection = opts[:collection]
21
+ value = value.to_s
21
22
  case type
22
23
  when :checkbox
23
- value = value.to_s
24
24
  if collection.blank?
25
25
  collection = best_in_place_default_collection
26
26
  else
27
- collection = best_in_place_collection_builder(collection)
27
+ collection = best_in_place_collection_builder(type, collection)
28
28
  end
29
29
  display_value = collection[value]
30
30
  collection = collection.to_json
31
31
  else # :select
32
- value = value.to_s
33
- collection = best_in_place_collection_builder(collection)
32
+ collection = best_in_place_collection_builder(type, collection)
34
33
  display_value = collection[value]
35
34
  collection = collection.to_json
36
35
  end
@@ -173,13 +172,22 @@ module BestInPlace
173
172
  end
174
173
  end
175
174
 
176
- def best_in_place_collection_builder(collection)
177
- case collection
175
+ def best_in_place_collection_builder(type, collection)
176
+ collection = case collection
178
177
  when Array
179
- Hash[collection.map { |x| [x.to_s, x.to_s] }]
178
+ if type == :checkbox
179
+ if collection.length == 2
180
+ {'false' => collection[0], 'true' => collection[1]}
181
+ else
182
+ fail ArgumentError, '[Best_in_place] :collection array should have 2 values'
183
+ end
184
+ else # :select
185
+ Hash[(1...collection.size+1).zip collection]
186
+ end
180
187
  else
181
- collection.stringify_keys
188
+ collection
182
189
  end
190
+ collection.stringify_keys
183
191
  end
184
192
 
185
193
  def best_in_place_default_collection
@@ -4,38 +4,58 @@ module BestInPlace
4
4
 
5
5
  def bip_area(model, attr, new_value)
6
6
  id = BestInPlace::Utils.build_best_in_place_id model, attr
7
- page.execute_script <<-JS
8
- jQuery("##{id}").click();
9
- jQuery("##{id} form textarea").val('#{escape_javascript new_value.to_s}');
10
- jQuery("##{id} form textarea").blur();
11
- jQuery("##{id} form textarea").blur();
7
+ find("##{id}").trigger('click')
8
+ execute_script <<-JS
9
+ $("##{id} form textarea").val('#{escape_javascript new_value.to_s}');
10
+ $("##{id} form textarea").blur();
12
11
  JS
12
+ wait_for_ajax
13
13
  end
14
14
 
15
15
  def bip_text(model, attr, new_value)
16
16
  id = BestInPlace::Utils.build_best_in_place_id model, attr
17
- page.execute_script <<-JS
18
- jQuery("##{id}").click();
19
- jQuery("##{id} input[name='#{attr}']").val('#{escape_javascript new_value.to_s}');
20
- jQuery("##{id} form").submit();
17
+ find("##{id}").click
18
+ execute_script <<-JS
19
+ $("##{id} input[name='#{attr}']").val('#{escape_javascript new_value.to_s}');
20
+ $("##{id} form").submit();
21
21
  JS
22
+ wait_for_ajax
22
23
  end
23
24
 
24
25
  def bip_bool(model, attr)
25
26
  id = BestInPlace::Utils.build_best_in_place_id model, attr
26
- page.execute_script("jQuery('##{id}').click();")
27
+ find("##{id}").trigger('click')
28
+ wait_for_ajax
27
29
  end
28
30
 
29
31
  def bip_select(model, attr, name)
30
32
  id = BestInPlace::Utils.build_best_in_place_id model, attr
31
- page.execute_script <<-JS
32
- (function() {
33
- jQuery("##{id}").click();
34
- var opt_value = jQuery("##{id} select option:contains('#{name}')").attr('value');
35
- jQuery("##{id} select option[value='" + opt_value + "']").attr('selected', true);
36
- jQuery("##{id} select").change();
37
- })();
38
- JS
33
+ find("##{id}").trigger('click')
34
+ find("##{id}").select(name)
35
+ wait_for_ajax
36
+ end
37
+
38
+ def wait_for_ajax
39
+ return unless respond_to?(:evaluate_script)
40
+ wait_until { finished_all_ajax_requests? }
41
+ end
42
+
43
+ def finished_all_ajax_requests?
44
+ evaluate_script('!window.jQuery') || evaluate_script('jQuery.active').zero?
45
+ end
46
+
47
+ def wait_until(max_execution_time_in_seconds = Capybara.default_wait_time)
48
+ Timeout.timeout(max_execution_time_in_seconds) do
49
+ loop do
50
+ if yield
51
+ return true
52
+ else
53
+ sleep(0.1)
54
+ next
55
+ end
56
+ end
57
+ end
39
58
  end
59
+
40
60
  end
41
61
  end
@@ -1,3 +1,3 @@
1
1
  module BestInPlace
2
- VERSION = '3.0.0'
2
+ VERSION = '3.0.1'
3
3
  end
data/spec/helper_spec.rb CHANGED
@@ -326,9 +326,9 @@ describe BestInPlace::Helper, type: :helper do
326
326
  expect(@span.text).to eq("No")
327
327
  end
328
328
 
329
- describe "custom collection" do
329
+ describe "custom hash collection" do
330
330
  before do
331
- @collection = {'false' => 'Nain', 'true' => 'Da'}
331
+ @collection = {false: 'Nain', true: 'Da'}
332
332
  nk = Nokogiri::HTML.parse(helper.best_in_place @user, :receive_email, as: :checkbox, collection: @collection)
333
333
  @span = nk.css("span")
334
334
  end
@@ -342,40 +342,120 @@ describe BestInPlace::Helper, type: :helper do
342
342
  end
343
343
  end
344
344
 
345
+ describe "custom array collection" do
346
+ before do
347
+ @good_collection = ['Net', 'Da']
348
+ @bad_collection = ['Maybe']
349
+ nk = Nokogiri::HTML.parse(helper.best_in_place @user, :receive_email, as: :checkbox, collection: @good_collection)
350
+ @span = nk.css("span")
351
+ end
352
+
353
+ it "should show the message with the custom values" do
354
+ expect(@span.text).to eq("Net")
355
+ end
356
+
357
+ it "should render the proper data-bip-collection" do
358
+ expect(@span.attribute("data-bip-collection").value).to eq({false: @good_collection[0], true: @good_collection[1]}.to_json)
359
+ end
360
+
361
+ it "should raise an argument error on bad collection" do
362
+ expect { helper.best_in_place @user, :receive_email, as: :checkbox, collection: @bad_collection }.to raise_error(ArgumentError)
363
+ end
364
+ end
365
+
345
366
  end
346
367
 
347
- context "with a select attribute" do
368
+ context 'with a select attribute' do
348
369
  before do
349
- @countries = COUNTRIES
350
- nk = Nokogiri::HTML.parse(helper.best_in_place @user, :country, as: :select, :collection => @countries)
351
- @span = nk.css("span")
370
+ @countries_hash = COUNTRIES_HASH
371
+ @countries_hash_string_keys = COUNTRIES_HASH_STRING_KEYS
372
+ @countries_array = COUNTRIES_ARRAY
373
+ @apostrophe_countries_hash = COUNTRIES_APOSTROPHE_HASH
374
+ @apostrophe_countries_array = COUNTRIES_APOSTROPHE_ARRAY
352
375
  end
353
376
 
354
- it 'should have a select data-bip-type' do
355
- expect(@span.attribute('data-bip-type').value).to eq('select')
356
- end
377
+ describe 'with a hash parameter' do
378
+ before do
379
+ nk = Nokogiri::HTML.parse(helper.best_in_place @user, :country, as: :select, collection: @countries_hash)
380
+ @span = nk.css('span')
381
+ end
357
382
 
358
- it "should have a proper data collection" do
359
- expect(@span.attribute('data-bip-collection').value).to eq(@countries.to_json)
360
- end
383
+ it 'should have a select data-bip-type' do
384
+ expect(@span.attribute('data-bip-type').value).to eq('select')
385
+ end
361
386
 
362
- it "should show the current country" do
363
- expect(@span.text).to eq("Italy")
364
- end
387
+ it 'should have a proper data collection' do
388
+ expect(@span.attribute('data-bip-collection').value).to eq(@countries_hash.to_json)
389
+ end
390
+
391
+ it 'should show the current country' do
392
+ expect(@span.text).to eq('Italy')
393
+ end
394
+
395
+ it 'should include the proper data-bip-value' do
396
+ expect(@span.attribute('data-bip-value').value).to eq('2')
397
+ end
398
+
399
+ context 'with hash string keys' do
400
+ before do
401
+ @user.country = 'it'
402
+ @user.save
403
+ nk = Nokogiri::HTML.parse(helper.best_in_place @user, :country, as: :select, collection: @countries_hash_string_keys)
404
+ @span = nk.css('span')
405
+ end
406
+
407
+ it 'should have a proper data collection' do
408
+ expect(@span.attribute('data-bip-collection').value).to eq(@countries_hash_string_keys.to_json)
409
+ end
410
+
411
+ it 'should show the current country' do
412
+ expect(@span.text).to eq('Italy')
413
+ end
414
+
415
+ it 'should include the proper data-bip-value' do
416
+ expect(@span.attribute('data-bip-value').value).to eq('it')
417
+ end
418
+ end
419
+
420
+ context 'with an apostrophe in it' do
421
+ before do
422
+ nk = Nokogiri::HTML.parse(helper.best_in_place @user, :country, as: :select, collection: @apostrophe_countries_hash)
423
+ @span = nk.css('span')
424
+ end
365
425
 
366
- it 'should include the proper data-bip-value' do
367
- expect(@span.attribute('data-bip-value').value).to eq('2')
426
+ it 'should have a proper data collection' do
427
+ expect(@span.attribute('data-bip-collection').value).to eq(@apostrophe_countries_hash.to_json)
428
+ end
429
+ end
368
430
  end
369
431
 
370
- context "with an apostrophe in it" do
432
+ describe 'with an array parameter' do
371
433
  before do
372
- @apostrophe_countries = {1=> "Joe's Country", 2 => "Bob's Country"}
373
- nk = Nokogiri::HTML.parse(helper.best_in_place @user, :country, as: :select, :collection => @apostrophe_countries)
374
- @span = nk.css("span")
434
+ nk = Nokogiri::HTML.parse(helper.best_in_place @user, :country, as: :select, collection: @countries_array)
435
+ @span = nk.css('span')
436
+ end
437
+
438
+ it 'should have a proper data collection' do
439
+ expect(@span.attribute('data-bip-collection').value).to eq(@countries_hash.to_json)
440
+ end
441
+
442
+ it 'should show the current country' do
443
+ expect(@span.text).to eq('Italy')
444
+ end
445
+
446
+ it 'should include the proper data-bip-value' do
447
+ expect(@span.attribute('data-bip-value').value).to eq('2')
375
448
  end
376
449
 
377
- it "should have a proper data collection" do
378
- expect(@span.attribute('data-bip-collection').value).to eq(@apostrophe_countries.to_json)
450
+ context 'with an apostrophe in it' do
451
+ before do
452
+ nk = Nokogiri::HTML.parse(helper.best_in_place @user, :country, as: :select, collection: @apostrophe_countries_array)
453
+ @span = nk.css('span')
454
+ end
455
+
456
+ it 'should have a proper data collection' do
457
+ expect(@span.attribute('data-bip-collection').value).to eq(@apostrophe_countries_hash.to_json)
458
+ end
379
459
  end
380
460
  end
381
461
  end