best_in_place 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/best_in_place.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require "best_in_place/utils"
1
2
  require "best_in_place/helper"
2
3
 
3
4
  module BestInPlace
@@ -1,5 +1,6 @@
1
1
  module BestInPlace
2
2
  module BestInPlaceHelpers
3
+
3
4
  def best_in_place(object, field, opts = {})
4
5
  opts[:type] ||= :input
5
6
  opts[:collection] ||= []
@@ -20,7 +21,7 @@ module BestInPlace
20
21
  collection = opts[:collection].to_json
21
22
  end
22
23
  out = "<span class='best_in_place'"
23
- out << " id='best_in_place_#{object.class.to_s.gsub("::", "_").underscore}_#{field}'"
24
+ out << " id='#{BestInPlace::Utils.build_best_in_place_id(object, field)}'"
24
25
  out << " data-url='#{opts[:path].blank? ? url_for(object).to_s : url_for(opts[:path])}'"
25
26
  out << " data-object='#{object.class.to_s.gsub("::", "_").underscore}'"
26
27
  out << " data-collection='#{collection}'" unless collection.blank?
@@ -1,24 +1,28 @@
1
1
  module BestInPlace
2
2
  module TestHelpers
3
+
3
4
  def bip_text(model, attr, new_value)
5
+ id = BestInPlace::Utils.build_best_in_place_id model, attr
4
6
  page.execute_script <<-JS
5
- $("#best_in_place_#{model}_#{attr}").click();
6
- $("#best_in_place_#{model}_#{attr} input[name='#{attr}']").val('#{new_value}');
7
- $("#best_in_place_#{model}_#{attr} form").submit();
7
+ $("##{id}").click();
8
+ $("##{id} input[name='#{attr}']").val('#{new_value}');
9
+ $("##{id} form").submit();
8
10
  JS
9
11
  end
10
12
 
11
13
  def bip_bool(model, attr)
12
- page.execute_script("$('#best_in_place_#{model}_#{attr}').click();")
14
+ id = BestInPlace::Utils.build_best_in_place_id model, attr
15
+ page.execute_script("$('##{id}').click();")
13
16
  end
14
17
 
15
18
  def bip_select(model, attr, name)
19
+ id = BestInPlace::Utils.build_best_in_place_id model, attr
16
20
  page.execute_script <<-JS
17
21
  (function() {
18
- $("#best_in_place_#{model}_#{attr}").click();
19
- var opt_value = $("#best_in_place_#{model}_#{attr} select option:contains('#{name}')").attr('value');
20
- $("#best_in_place_#{model}_#{attr} select option[value='" + opt_value + "']").attr('selected', true);
21
- $("#best_in_place_#{model}_#{attr} select").change();
22
+ $("##{id}").click();
23
+ var opt_value = $("##{id} select option:contains('#{name}')").attr('value');
24
+ $("##{id} select option[value='" + opt_value + "']").attr('selected', true);
25
+ $("##{id} select").change();
22
26
  })();
23
27
  JS
24
28
  end
@@ -0,0 +1,15 @@
1
+ module BestInPlace
2
+ class Utils
3
+
4
+ def self.build_best_in_place_id(object, field)
5
+ if object.is_a?(Symbol) || object.is_a?(String)
6
+ return "best_in_place_#{object}_#{field}"
7
+ end
8
+
9
+ id = "best_in_place_#{object.class.to_s.demodulize.underscore}"
10
+ id << "_#{object.id}" if object.class.ancestors.include?(ActiveRecord::Base)
11
+ id << "_#{field}"
12
+ id
13
+ end
14
+ end
15
+ end
@@ -1,3 +1,3 @@
1
1
  module BestInPlace
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -339,7 +339,9 @@ BestInPlaceEditor.forms = {
339
339
 
340
340
  jQuery.fn.best_in_place = function() {
341
341
  this.each(function(){
342
- jQuery(this).data('bestInPlaceEditor', new BestInPlaceEditor(this));
342
+ if (!jQuery(this).data('bestInPlaceEditor')) {
343
+ jQuery(this).data('bestInPlaceEditor', new BestInPlaceEditor(this));
344
+ }
343
345
  });
344
346
  return this;
345
347
  };
@@ -26,8 +26,18 @@ describe BestInPlace::BestInPlaceHelpers do
26
26
  @span = nk.css("span")
27
27
  end
28
28
 
29
- it "should have a proper id" do
30
- @span.attribute("id").value.should == "best_in_place_user_name"
29
+ context "when it's an ActiveRecord model" do
30
+ it "should have a proper id" do
31
+ @span.attribute("id").value.should == "best_in_place_user_#{@user.id}_name"
32
+ end
33
+ end
34
+
35
+ context "when it's not an AR model" do
36
+ it "shold generate an html id without any id" do
37
+ nk = Nokogiri::HTML.parse(helper.best_in_place [1,2,3], :first, :path => @user)
38
+ span = nk.css("span")
39
+ span.attribute("id").value.should == "best_in_place_array_first"
40
+ end
31
41
  end
32
42
 
33
43
  it "should have the best_in_place class" do
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
  require "spec_helper"
3
3
 
4
- describe "Double initialization bug", :js => true, :pending => true do
4
+ describe "Double initialization bug", :js => true do
5
5
  before do
6
6
  @user = User.new :name => "Lucia",
7
7
  :last_name => "Napoli",
@@ -21,7 +21,7 @@ describe "Double initialization bug", :js => true, :pending => true do
21
21
  page.should have_content("No thanks")
22
22
  end
23
23
 
24
- bip_bool :user, :receive_email
24
+ bip_bool @user, :receive_email
25
25
 
26
26
  visit double_init_user_path(@user)
27
27
  within("#receive_email") do
@@ -35,14 +35,14 @@ describe "JS behaviour", :js => true do
35
35
  end
36
36
  end
37
37
 
38
- it "should be able to use the bip_text to update a text field" do
38
+ it "should be able to use bip_text to update a text field" do
39
39
  @user.save!
40
40
  visit user_path(@user)
41
41
  within("#email") do
42
42
  page.should have_content("lucianapoli@gmail.com")
43
43
  end
44
44
 
45
- bip_text :user, :email, "new@email.com"
45
+ bip_text @user, :email, "new@email.com"
46
46
 
47
47
  visit user_path(@user)
48
48
  within("#email") do
@@ -54,13 +54,13 @@ describe "JS behaviour", :js => true do
54
54
  @user.save!
55
55
  visit user_path(@user)
56
56
 
57
- bip_text :user, :email, "new@email.com"
57
+ bip_text @user, :email, "new@email.com"
58
58
 
59
59
  within("#email") do
60
60
  page.should have_content("new@email.com")
61
61
  end
62
62
 
63
- bip_text :user, :email, "new_two@email.com"
63
+ bip_text @user, :email, "new_two@email.com"
64
64
 
65
65
  within("#email") do
66
66
  page.should have_content("new_two@email.com")
@@ -76,10 +76,10 @@ describe "JS behaviour", :js => true do
76
76
  @user.save!
77
77
  visit user_path(@user)
78
78
 
79
- bip_text :user, :email, "wrong format"
79
+ bip_text @user, :email, "wrong format"
80
80
  page.should have_content("Email has wrong email format")
81
81
 
82
- bip_text :user, :email, "another@email.com"
82
+ bip_text @user, :email, "another@email.com"
83
83
  within("#email") do
84
84
  page.should have_content("another@email.com")
85
85
  end
@@ -97,7 +97,7 @@ describe "JS behaviour", :js => true do
97
97
  page.should have_content("Italy")
98
98
  end
99
99
 
100
- bip_select :user, :country, "France"
100
+ bip_select @user, :country, "France"
101
101
 
102
102
  visit user_path(@user)
103
103
  within("#country") do
@@ -113,7 +113,7 @@ describe "JS behaviour", :js => true do
113
113
  page.should have_content("No thanks")
114
114
  end
115
115
 
116
- bip_bool :user, :receive_email
116
+ bip_bool @user, :receive_email
117
117
 
118
118
  visit user_path(@user)
119
119
  within("#receive_email") do
@@ -125,7 +125,7 @@ describe "JS behaviour", :js => true do
125
125
  @user.save!
126
126
  visit user_path(@user)
127
127
 
128
- bip_text :user, :address, ""
128
+ bip_text @user, :address, ""
129
129
  page.should have_content("Address can't be blank")
130
130
  within("#address") do
131
131
  page.should have_content("Via Roma 99")
@@ -136,7 +136,7 @@ describe "JS behaviour", :js => true do
136
136
  @user.save!
137
137
  visit user_path(@user)
138
138
 
139
- bip_text :user, :last_name, "a"
139
+ bip_text @user, :last_name, "a"
140
140
  page.should have_content("last_name has invalid length")
141
141
  end
142
142
 
@@ -144,13 +144,13 @@ describe "JS behaviour", :js => true do
144
144
  @user.save!
145
145
  visit user_path(@user)
146
146
 
147
- bip_text :user, :last_name, "a"
147
+ bip_text @user, :last_name, "a"
148
148
 
149
149
  within("#last_name") do
150
150
  page.should have_content("Napoli")
151
151
  end
152
152
 
153
- bip_text :user, :last_name, "Another"
153
+ bip_text @user, :last_name, "Another"
154
154
 
155
155
  within("#last_name") do
156
156
  page.should have_content("Another")
@@ -178,6 +178,8 @@ BestInPlaceEditor.prototype = {
178
178
 
179
179
  loadSuccessCallback : function(data) {
180
180
  this.element.html(data[this.objectName]);
181
+ this.element.trigger($.Event("ajax:success"), data);
182
+
181
183
  // Binding back after being clicked
182
184
  $(this.activator).bind('click', {editor: this}, this.clickHandler);
183
185
  },
@@ -337,7 +339,9 @@ BestInPlaceEditor.forms = {
337
339
 
338
340
  jQuery.fn.best_in_place = function() {
339
341
  this.each(function(){
340
- jQuery(this).data('bestInPlaceEditor', new BestInPlaceEditor(this));
342
+ if (!jQuery(this).data('bestInPlaceEditor')) {
343
+ jQuery(this).data('bestInPlaceEditor', new BestInPlaceEditor(this));
344
+ }
341
345
  });
342
346
  return this;
343
347
  };
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: best_in_place
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-26 00:00:00.000000000 Z
12
+ date: 2011-11-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &84765280 !ruby/object:Gem::Requirement
16
+ requirement: &75417920 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.0.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *84765280
24
+ version_requirements: *75417920
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec-rails
27
- requirement: &84764870 !ruby/object:Gem::Requirement
27
+ requirement: &75415930 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 2.7.0
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *84764870
35
+ version_requirements: *75415930
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: nokogiri
38
- requirement: &84764560 !ruby/object:Gem::Requirement
38
+ requirement: &75435180 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 1.5.0
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *84764560
46
+ version_requirements: *75435180
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: capybara
49
- requirement: &84763970 !ruby/object:Gem::Requirement
49
+ requirement: &75431500 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: 1.0.1
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *84763970
57
+ version_requirements: *75431500
58
58
  description: BestInPlace is a jQuery script and a Rails 3 helper that provide the
59
59
  method best_in_place to display any object field easily editable for the user by
60
60
  just clicking on it. It supports input data, text data, boolean data and custom
@@ -75,6 +75,7 @@ files:
75
75
  - lib/best_in_place.rb
76
76
  - lib/best_in_place/helper.rb
77
77
  - lib/best_in_place/test_helpers.rb
78
+ - lib/best_in_place/utils.rb
78
79
  - lib/best_in_place/version.rb
79
80
  - lib/generators/best_in_place/setup_generator.rb
80
81
  - public/javascripts/best_in_place.js
@@ -160,7 +161,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
160
161
  version: '0'
161
162
  segments:
162
163
  - 0
163
- hash: 982211869
164
+ hash: -404382943
164
165
  required_rubygems_version: !ruby/object:Gem::Requirement
165
166
  none: false
166
167
  requirements:
@@ -169,7 +170,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
169
170
  version: '0'
170
171
  segments:
171
172
  - 0
172
- hash: 982211869
173
+ hash: -404382943
173
174
  requirements: []
174
175
  rubyforge_project: best_in_place
175
176
  rubygems_version: 1.8.10