best_in_placeish 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,27 +1,27 @@
1
- # -*- encoding: utf-8 -*-
2
- $:.push File.expand_path("../lib", __FILE__)
3
- require "best_in_placeish/version"
4
-
5
- Gem::Specification.new do |s|
6
- s.name = "best_in_placeish"
7
- s.version = BestInPlaceish::VERSION
8
- s.platform = Gem::Platform::RUBY
9
- s.authors = ["Bernat Farrero"]
10
- s.email = ["bernat@itnig.net"]
11
- s.homepage = "http://github.com/MilesRoberts/best_in_placeish"
12
- s.summary = %q{It makes any field in place editable by clicking on it, it works for inputs, textareas, select dropdowns and checkboxes}
13
- s.description = %q{Forked from the rails-3.0 branch of best_in_place, this includes a couple of key enhancements for Rails 3.0.x users}
14
-
15
- s.rubyforge_project = 'bestinplaceish'
16
-
17
- s.files = `git ls-files`.split("\n")
18
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
- s.require_paths = ["lib"]
21
-
22
- s.add_dependency "rails", "~> 3.0.0"
23
-
24
- s.add_development_dependency "rspec-rails", "~> 2.7.0"
25
- s.add_development_dependency "nokogiri", ">= 1.5.0"
26
- s.add_development_dependency "capybara", ">= 1.0.1"
27
- end
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "best_in_placeish/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "best_in_placeish"
7
+ s.version = BestInPlaceish::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Bernat Farrero"]
10
+ s.email = ["bernat@itnig.net"]
11
+ s.homepage = "http://github.com/MilesRoberts/best_in_placeish"
12
+ s.summary = %q{It makes any field in place editable by clicking on it, it works for inputs, textareas, select dropdowns and checkboxes}
13
+ s.description = %q{Forked from the rails-3.0 branch of best_in_place, this includes a couple of key enhancements for Rails 3.0.x users}
14
+
15
+ s.rubyforge_project = 'bestinplaceish'
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency "rails", "~> 3.0.0"
23
+
24
+ s.add_development_dependency "rspec-rails", "~> 2.7.0"
25
+ s.add_development_dependency "nokogiri", ">= 1.5.0"
26
+ s.add_development_dependency "capybara", ">= 1.0.1"
27
+ end
@@ -1,8 +1,8 @@
1
- require "best_in_placeish/utils"
2
- require "best_in_placeish/helper"
3
-
4
- module BestInPlaceish
5
- autoload :TestHelpers, "best_in_placeish/test_helpers"
6
- end
7
-
8
- ActionView::Base.send(:include, BestInPlaceish::BestInPlaceishHelpers)
1
+ require "best_in_placeish/utils"
2
+ require "best_in_placeish/helper"
3
+
4
+ module BestInPlaceish
5
+ autoload :TestHelpers, "best_in_placeish/test_helpers"
6
+ end
7
+
8
+ ActionView::Base.send(:include, BestInPlaceish::BestInPlaceishHelpers)
@@ -1,57 +1,57 @@
1
- module BestInPlaceish
2
- module BestInPlaceishHelpers
3
-
4
- def best_in_placeish(object, field, opts = {})
5
- opts[:type] ||= :input
6
- opts[:collection] ||= []
7
- field = field.to_s
8
- if opts[:display_as]
9
- value = object.send(opts[:display_as])
10
- else
11
- value = object.send(field).blank? ? "" : object.send(field)
12
- end
13
- collection = nil
14
- if opts[:type] == :select && !opts[:collection].blank?
15
- v = object.send(field)
16
- value = Hash[opts[:collection]][!!(v =~ /^[0-9]+$/) ? v.to_i : v]
17
- collection = opts[:collection].to_json
18
- end
19
- if opts[:type] == :checkbox
20
- fieldValue = !!object.send(field)
21
- if opts[:collection].blank? || opts[:collection].size != 2
22
- opts[:collection] = ["No", "Yes"]
23
- end
24
- value = fieldValue ? opts[:collection][1] : opts[:collection][0]
25
- collection = opts[:collection].to_json
26
- end
27
- out = "<span class='best_in_placeish'"
28
- out << " id='#{BestInPlaceish::Utils.build_best_in_placeish_id(object, field)}'"
29
- out << " data-url='#{opts[:path].blank? ? url_for(object).to_s : url_for(opts[:path])}'"
30
- out << " data-object='#{object.class.to_s.gsub("::", "_").underscore}'"
31
- out << " data-collection='#{collection}'" unless collection.blank?
32
- out << " data-attribute='#{field}'"
33
- out << " data-activator='#{opts[:activator]}'" unless opts[:activator].blank?
34
- out << " data-nil='#{opts[:nil].to_s}'" unless opts[:nil].blank?
35
- out << " data-type='#{opts[:type].to_s}'"
36
- out << " data-inner-class='#{opts[:inner_class].to_s}'" if opts[:inner_class]
37
- out << " data-html-attrs='#{opts[:html_attrs].to_json}'" unless opts[:html_attrs].blank?
38
- if !opts[:sanitize].nil? && !opts[:sanitize]
39
- out << " data-sanitize='false'>"
40
- out << sanitize(value.to_s, :tags => %w(b i u s a strong em p h1 h2 h3 h4 h5 ul li ol hr pre span img br), :attributes => %w(id class href))
41
- else
42
- out << ">#{sanitize(value.to_s, :tags => nil, :attributes => nil)}"
43
- end
44
- out << "</span>"
45
- raw out
46
- end
47
-
48
- def best_in_placeish_if(condition, object, field, opts={})
49
- if condition
50
- best_in_placeish(object, field, opts)
51
- else
52
- object.send field
53
- end
54
- end
55
- end
56
- end
57
-
1
+ module BestInPlaceish
2
+ module BestInPlaceishHelpers
3
+
4
+ def best_in_placeish(object, field, opts = {})
5
+ opts[:type] ||= :input
6
+ opts[:collection] ||= []
7
+ field = field.to_s
8
+ if opts[:display_as]
9
+ value = object.send(opts[:display_as])
10
+ else
11
+ value = object.send(field).blank? ? "" : object.send(field)
12
+ end
13
+ collection = nil
14
+ if opts[:type] == :select && !opts[:collection].blank?
15
+ v = object.send(field)
16
+ value = Hash[opts[:collection]][!!(v =~ /^[0-9]+$/) ? v.to_i : v]
17
+ collection = opts[:collection].to_json
18
+ end
19
+ if opts[:type] == :checkbox
20
+ fieldValue = !!object.send(field)
21
+ if opts[:collection].blank? || opts[:collection].size != 2
22
+ opts[:collection] = ["No", "Yes"]
23
+ end
24
+ value = fieldValue ? opts[:collection][1] : opts[:collection][0]
25
+ collection = opts[:collection].to_json
26
+ end
27
+ out = "<span class='best_in_placeish'"
28
+ out << " id='#{BestInPlaceish::Utils.build_best_in_placeish_id(object, field)}'"
29
+ out << " data-url='#{opts[:path].blank? ? url_for(object).to_s : url_for(opts[:path])}'"
30
+ out << " data-object='#{object.class.to_s.gsub("::", "_").underscore}'"
31
+ out << " data-collection='#{collection}'" unless collection.blank?
32
+ out << " data-attribute='#{field}'"
33
+ out << " data-activator='#{opts[:activator]}'" unless opts[:activator].blank?
34
+ out << " data-nil='#{opts[:nil].to_s}'" unless opts[:nil].blank?
35
+ out << " data-type='#{opts[:type].to_s}'"
36
+ out << " data-inner-class='#{opts[:inner_class].to_s}'" if opts[:inner_class]
37
+ out << " data-html-attrs='#{opts[:html_attrs].to_json}'" unless opts[:html_attrs].blank?
38
+ if !opts[:sanitize].nil? && !opts[:sanitize]
39
+ out << " data-sanitize='false'>"
40
+ out << sanitize(value.to_s, :tags => %w(b i u s a strong em p h1 h2 h3 h4 h5 ul li ol hr pre span img br), :attributes => %w(id class href))
41
+ else
42
+ out << ">#{sanitize(value.to_s, :tags => nil, :attributes => nil)}"
43
+ end
44
+ out << "</span>"
45
+ raw out
46
+ end
47
+
48
+ def best_in_placeish_if(condition, object, field, opts={})
49
+ if condition
50
+ best_in_placeish(object, field, opts)
51
+ else
52
+ object.send field
53
+ end
54
+ end
55
+ end
56
+ end
57
+
@@ -1,39 +1,39 @@
1
- module BestInPlaceishish
2
- module TestHelpers
3
-
4
- def bip_area(model, attr, new_value)
5
- id = BestInPlaceishish::Utils.build_best_in_placeish_id model, attr
6
- page.execute_script <<-JS
7
- $("##{id}").click();
8
- $("##{id} form textarea").val('#{new_value}');
9
- $("##{id} form textarea").blur();
10
- JS
11
- end
12
-
13
- def bip_text(model, attr, new_value)
14
- id = BestInPlaceish::Utils.build_best_in_placeish_id model, attr
15
- page.execute_script <<-JS
16
- $("##{id}").click();
17
- $("##{id} input[name='#{attr}']").val('#{new_value}');
18
- $("##{id} form").submit();
19
- JS
20
- end
21
-
22
- def bip_bool(model, attr)
23
- id = BestInPlaceish::Utils.build_best_in_placeish_id model, attr
24
- page.execute_script("$('##{id}').click();")
25
- end
26
-
27
- def bip_select(model, attr, name)
28
- id = BestInPlaceish::Utils.build_best_in_placeish_id model, attr
29
- page.execute_script <<-JS
30
- (function() {
31
- $("##{id}").click();
32
- var opt_value = $("##{id} select option:contains('#{name}')").attr('value');
33
- $("##{id} select option[value='" + opt_value + "']").attr('selected', true);
34
- $("##{id} select").change();
35
- })();
36
- JS
37
- end
38
- end
39
- end
1
+ module BestInPlaceishish
2
+ module TestHelpers
3
+
4
+ def bip_area(model, attr, new_value)
5
+ id = BestInPlaceishish::Utils.build_best_in_placeish_id model, attr
6
+ page.execute_script <<-JS
7
+ $("##{id}").click();
8
+ $("##{id} form textarea").val('#{new_value}');
9
+ $("##{id} form textarea").blur();
10
+ JS
11
+ end
12
+
13
+ def bip_text(model, attr, new_value)
14
+ id = BestInPlaceish::Utils.build_best_in_placeish_id model, attr
15
+ page.execute_script <<-JS
16
+ $("##{id}").click();
17
+ $("##{id} input[name='#{attr}']").val('#{new_value}');
18
+ $("##{id} form").submit();
19
+ JS
20
+ end
21
+
22
+ def bip_bool(model, attr)
23
+ id = BestInPlaceish::Utils.build_best_in_placeish_id model, attr
24
+ page.execute_script("$('##{id}').click();")
25
+ end
26
+
27
+ def bip_select(model, attr, name)
28
+ id = BestInPlaceish::Utils.build_best_in_placeish_id model, attr
29
+ page.execute_script <<-JS
30
+ (function() {
31
+ $("##{id}").click();
32
+ var opt_value = $("##{id} select option:contains('#{name}')").attr('value');
33
+ $("##{id} select option[value='" + opt_value + "']").attr('selected', true);
34
+ $("##{id} select").change();
35
+ })();
36
+ JS
37
+ end
38
+ end
39
+ end
@@ -1,15 +1,15 @@
1
- module BestInPlaceish
2
- class Utils
3
-
4
- def self.build_best_in_placeish_id(object, field)
5
- if object.is_a?(Symbol) || object.is_a?(String)
6
- return "best_in_placeish_#{object}_#{field}"
7
- end
8
-
9
- id = "best_in_placeish_#{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
+ module BestInPlaceish
2
+ class Utils
3
+
4
+ def self.build_best_in_placeish_id(object, field)
5
+ if object.is_a?(Symbol) || object.is_a?(String)
6
+ return "best_in_placeish_#{object}_#{field}"
7
+ end
8
+
9
+ id = "best_in_placeish_#{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
- module BestInPlaceish
2
- VERSION = "0.2.4"
3
- end
1
+ module BestInPlaceish
2
+ VERSION = "0.2.5"
3
+ end
@@ -1,11 +1,11 @@
1
- module BestInPlaceish
2
-
3
- class SetupGenerator < Rails::Generators::Base
4
- source_root File.expand_path("../../../../public/javascripts", __FILE__)
5
- desc "Copies best_in_placeish.js to the /public/javascripts folder of your app."
6
-
7
- def copy_js
8
- copy_file "best_in_placeish.js", "public/javascripts/best_in_placeish.js"
9
- end
10
- end
11
- end
1
+ module BestInPlaceish
2
+
3
+ class SetupGenerator < Rails::Generators::Base
4
+ source_root File.expand_path("../../../../public/javascripts", __FILE__)
5
+ desc "Copies best_in_placeish.js to the /public/javascripts folder of your app."
6
+
7
+ def copy_js
8
+ copy_file "best_in_placeish.js", "public/javascripts/best_in_placeish.js"
9
+ end
10
+ end
11
+ end
@@ -258,6 +258,45 @@ BestInPlaceEditor.forms = {
258
258
  }
259
259
  }
260
260
  },
261
+
262
+ "datepicker" : {
263
+ activateForm : function() {
264
+ var output = '<form class="form_in_place" action="javascript:void(0)" style="display:inline;">';
265
+ output += '<input type="text" name="'+ this.attributeName + '" id="'+ this.attributeName + '" value="' + this.sanitizeValue(this.oldValue) + '"';
266
+ if (this.inner_class != null) {
267
+ output += ' class="' + this.inner_class + '"';
268
+ }
269
+ output += '></form>'
270
+ this.element.html(output);
271
+ this.setHtmlAttributes();
272
+ $('input#' + this.attributeName).datepicker({
273
+ dateFormat: 'dd/M/yy',
274
+ onClose: function() { $('form.form_in_place').submit(); }
275
+ });
276
+ this.element.find('input')[0].select();
277
+ this.element.find("form").bind('submit', {editor: this}, BestInPlaceEditor.forms.datepicker.submitHandler);
278
+ //this.element.find("input").bind('blur', {editor: this}, BestInPlaceEditor.forms.datepicker.inputBlurHandler);
279
+ this.element.find("input").bind('keyup', {editor: this}, BestInPlaceEditor.forms.datepicker.keyupHandler);
280
+ },
281
+
282
+ getValue : function() {
283
+ return this.sanitizeValue(this.element.find("input").val());
284
+ },
285
+
286
+ inputBlurHandler : function(event) {
287
+ event.data.editor.update();
288
+ },
289
+
290
+ submitHandler : function(event) {
291
+ event.data.editor.update();
292
+ },
293
+
294
+ keyupHandler : function(event) {
295
+ if (event.keyCode == 27) {
296
+ event.data.editor.abort();
297
+ }
298
+ }
299
+ },
261
300
 
262
301
  "select" : {
263
302
  activateForm : function() {
@@ -1,239 +1,239 @@
1
- # encoding: utf-8
2
- require "spec_helper"
3
-
4
- describe BestInPlace::BestInPlaceHelpers do
5
- describe "#best_in_place" do
6
- before do
7
- @user = User.new :name => "Lucia",
8
- :last_name => "Napoli",
9
- :email => "lucianapoli@gmail.com",
10
- :address => "Via Roma 99",
11
- :zip => "25123",
12
- :country => "2",
13
- :receive_email => false,
14
- :description => "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus a lectus et lacus ultrices auctor. Morbi aliquet convallis tincidunt. Praesent enim libero, iaculis at commodo nec, fermentum a dolor. Quisque eget eros id felis lacinia faucibus feugiat et ante. Aenean justo nisi, aliquam vel egestas vel, porta in ligula. Etiam molestie, lacus eget tincidunt accumsan, elit justo rhoncus urna, nec pretium neque mi et lorem. Aliquam posuere, dolor quis pulvinar luctus, felis dolor tincidunt leo, eget pretium orci purus ac nibh. Ut enim sem, suscipit ac elementum vitae, sodales vel sem."
15
- end
16
-
17
- it "should generate a proper span" do
18
- nk = Nokogiri::HTML.parse(helper.best_in_place @user, :name)
19
- span = nk.css("span")
20
- span.should_not be_empty
21
- end
22
-
23
- describe "general properties" do
24
- before do
25
- nk = Nokogiri::HTML.parse(helper.best_in_place @user, :name)
26
- @span = nk.css("span")
27
- end
28
-
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
41
- end
42
-
43
- it "should have the best_in_place class" do
44
- @span.attribute("class").value.should == "best_in_place"
45
- end
46
-
47
- it "should have the correct data-attribute" do
48
- @span.attribute("data-attribute").value.should == "name"
49
- end
50
-
51
- it "should have the correct data-object" do
52
- @span.attribute("data-object").value.should == "user"
53
- end
54
-
55
- it "should have no activator by default" do
56
- @span.attribute("data-activator").should be_nil
57
- end
58
-
59
- it "should have no inner_class by default" do
60
- @span.attribute("data-inner-class").should be_nil
61
- end
62
-
63
- describe "url generation" do
64
- it "should have the correct default url" do
65
- @user.save!
66
- nk = Nokogiri::HTML.parse(helper.best_in_place @user, :name)
67
- span = nk.css("span")
68
- span.attribute("data-url").value.should == "/users/#{@user.id}"
69
- end
70
-
71
- it "should use the custom url specified in string format" do
72
- out = helper.best_in_place @user, :name, :path => "/custom/path"
73
- nk = Nokogiri::HTML.parse(out)
74
- span = nk.css("span")
75
- span.attribute("data-url").value.should == "/custom/path"
76
- end
77
-
78
- it "should use the path given in a named_path format" do
79
- out = helper.best_in_place @user, :name, :path => helper.users_path
80
- nk = Nokogiri::HTML.parse(out)
81
- span = nk.css("span")
82
- span.attribute("data-url").value.should == "/users"
83
- end
84
-
85
- it "should use the given path in a hash format" do
86
- out = helper.best_in_place @user, :name, :path => {:controller => :users, :action => :edit, :id => 23}
87
- nk = Nokogiri::HTML.parse(out)
88
- span = nk.css("span")
89
- span.attribute("data-url").value.should == "/users/23/edit"
90
- end
91
- end
92
-
93
- describe "nil option" do
94
- it "should have no nil data by default" do
95
- @span.attribute("data-nil").should be_nil
96
- end
97
-
98
- it "should show '' if the object responds with nil for the passed attribute" do
99
- @user.stub!(:name).and_return(nil)
100
- nk = Nokogiri::HTML.parse(helper.best_in_place @user, :name)
101
- span = nk.css("span")
102
- span.text.should == ""
103
- end
104
-
105
- it "should show '' if the object responds with an empty string for the passed attribute" do
106
- @user.stub!(:name).and_return("")
107
- nk = Nokogiri::HTML.parse(helper.best_in_place @user, :name)
108
- span = nk.css("span")
109
- span.text.should == ""
110
- end
111
- end
112
-
113
- it "should have the given inner_class" do
114
- out = helper.best_in_place @user, :name, :inner_class => "awesome"
115
- nk = Nokogiri::HTML.parse(out)
116
- span = nk.css("span")
117
- span.attribute("data-inner-class").value.should == "awesome"
118
- end
119
-
120
- it "should have the given activator" do
121
- out = helper.best_in_place @user, :name, :activator => "awesome"
122
- nk = Nokogiri::HTML.parse(out)
123
- span = nk.css("span")
124
- span.attribute("data-activator").value.should == "awesome"
125
- end
126
- end
127
-
128
-
129
- context "with a text field attribute" do
130
- before do
131
- nk = Nokogiri::HTML.parse(helper.best_in_place @user, :name)
132
- @span = nk.css("span")
133
- end
134
-
135
- it "should render the name as text" do
136
- @span.text.should == "Lucia"
137
- end
138
-
139
- it "should have an input data-type" do
140
- @span.attribute("data-type").value.should == "input"
141
- end
142
-
143
- it "should have no data-collection" do
144
- @span.attribute("data-collection").should be_nil
145
- end
146
- end
147
-
148
- context "with a boolean attribute" do
149
- before do
150
- nk = Nokogiri::HTML.parse(helper.best_in_place @user, :receive_email, :type => :checkbox)
151
- @span = nk.css("span")
152
- end
153
-
154
- it "should have a checkbox data-type" do
155
- @span.attribute("data-type").value.should == "checkbox"
156
- end
157
-
158
- it "should have the default data-collection" do
159
- data = ["No", "Yes"]
160
- @span.attribute("data-collection").value.should == data.to_json
161
- end
162
-
163
- it "should render the current option as No" do
164
- @span.text.should == "No"
165
- end
166
-
167
- describe "custom collection" do
168
- before do
169
- nk = Nokogiri::HTML.parse(helper.best_in_place @user, :receive_email, :type => :checkbox, :collection => ["Nain", "Da"])
170
- @span = nk.css("span")
171
- end
172
-
173
- it "should show the message with the custom values" do
174
- @span.text.should == "Nain"
175
- end
176
-
177
- it "should render the proper data-collection" do
178
- @span.attribute("data-collection").value.should == ["Nain", "Da"].to_json
179
- end
180
- end
181
-
182
- end
183
-
184
- context "with a select attribute" do
185
- before do
186
- @countries = COUNTRIES.to_a
187
- nk = Nokogiri::HTML.parse(helper.best_in_place @user, :country, :type => :select, :collection => @countries)
188
- @span = nk.css("span")
189
- end
190
-
191
- it "should have a select data-type" do
192
- @span.attribute("data-type").value.should == "select"
193
- end
194
-
195
- it "should have a proper data collection" do
196
- @span.attribute("data-collection").value.should == @countries.to_json
197
- end
198
-
199
- it "should show the current country" do
200
- @span.text.should == "Italy"
201
- end
202
- end
203
- end
204
-
205
- describe "#best_in_place_if" do
206
- context "when the parameters are valid" do
207
- before(:each) do
208
- @output = "Some Value"
209
- @field = :somefield
210
- @object = mock("object", @field => @output)
211
- @options = mock("options")
212
- end
213
- context "when the condition is true" do
214
- before {@condition = true}
215
- context "when the options parameter is left off" do
216
- it "should call best_in_place with the rest of the parameters and empty options" do
217
- helper.should_receive(:best_in_place).with(@object, @field, {})
218
- helper.best_in_place_if @condition, @object, @field
219
- end
220
- end
221
- context "when the options parameter is included" do
222
- it "should call best_in_place with the rest of the parameters" do
223
- helper.should_receive(:best_in_place).with(@object, @field, @options)
224
- helper.best_in_place_if @condition, @object, @field, @options
225
- end
226
- end
227
- end
228
- context "when the condition is false" do
229
- before {@condition = false}
230
- it "should return the value of the field when the options value is left off" do
231
- helper.best_in_place_if(@condition, @object, @field).should eq @output
232
- end
233
- it "should return the value of the field when the options value is included" do
234
- helper.best_in_place_if(@condition, @object, @field, @options).should eq @output
235
- end
236
- end
237
- end
238
- end
239
- end
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe BestInPlace::BestInPlaceHelpers do
5
+ describe "#best_in_place" do
6
+ before do
7
+ @user = User.new :name => "Lucia",
8
+ :last_name => "Napoli",
9
+ :email => "lucianapoli@gmail.com",
10
+ :address => "Via Roma 99",
11
+ :zip => "25123",
12
+ :country => "2",
13
+ :receive_email => false,
14
+ :description => "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus a lectus et lacus ultrices auctor. Morbi aliquet convallis tincidunt. Praesent enim libero, iaculis at commodo nec, fermentum a dolor. Quisque eget eros id felis lacinia faucibus feugiat et ante. Aenean justo nisi, aliquam vel egestas vel, porta in ligula. Etiam molestie, lacus eget tincidunt accumsan, elit justo rhoncus urna, nec pretium neque mi et lorem. Aliquam posuere, dolor quis pulvinar luctus, felis dolor tincidunt leo, eget pretium orci purus ac nibh. Ut enim sem, suscipit ac elementum vitae, sodales vel sem."
15
+ end
16
+
17
+ it "should generate a proper span" do
18
+ nk = Nokogiri::HTML.parse(helper.best_in_place @user, :name)
19
+ span = nk.css("span")
20
+ span.should_not be_empty
21
+ end
22
+
23
+ describe "general properties" do
24
+ before do
25
+ nk = Nokogiri::HTML.parse(helper.best_in_place @user, :name)
26
+ @span = nk.css("span")
27
+ end
28
+
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
41
+ end
42
+
43
+ it "should have the best_in_place class" do
44
+ @span.attribute("class").value.should == "best_in_place"
45
+ end
46
+
47
+ it "should have the correct data-attribute" do
48
+ @span.attribute("data-attribute").value.should == "name"
49
+ end
50
+
51
+ it "should have the correct data-object" do
52
+ @span.attribute("data-object").value.should == "user"
53
+ end
54
+
55
+ it "should have no activator by default" do
56
+ @span.attribute("data-activator").should be_nil
57
+ end
58
+
59
+ it "should have no inner_class by default" do
60
+ @span.attribute("data-inner-class").should be_nil
61
+ end
62
+
63
+ describe "url generation" do
64
+ it "should have the correct default url" do
65
+ @user.save!
66
+ nk = Nokogiri::HTML.parse(helper.best_in_place @user, :name)
67
+ span = nk.css("span")
68
+ span.attribute("data-url").value.should == "/users/#{@user.id}"
69
+ end
70
+
71
+ it "should use the custom url specified in string format" do
72
+ out = helper.best_in_place @user, :name, :path => "/custom/path"
73
+ nk = Nokogiri::HTML.parse(out)
74
+ span = nk.css("span")
75
+ span.attribute("data-url").value.should == "/custom/path"
76
+ end
77
+
78
+ it "should use the path given in a named_path format" do
79
+ out = helper.best_in_place @user, :name, :path => helper.users_path
80
+ nk = Nokogiri::HTML.parse(out)
81
+ span = nk.css("span")
82
+ span.attribute("data-url").value.should == "/users"
83
+ end
84
+
85
+ it "should use the given path in a hash format" do
86
+ out = helper.best_in_place @user, :name, :path => {:controller => :users, :action => :edit, :id => 23}
87
+ nk = Nokogiri::HTML.parse(out)
88
+ span = nk.css("span")
89
+ span.attribute("data-url").value.should == "/users/23/edit"
90
+ end
91
+ end
92
+
93
+ describe "nil option" do
94
+ it "should have no nil data by default" do
95
+ @span.attribute("data-nil").should be_nil
96
+ end
97
+
98
+ it "should show '' if the object responds with nil for the passed attribute" do
99
+ @user.stub!(:name).and_return(nil)
100
+ nk = Nokogiri::HTML.parse(helper.best_in_place @user, :name)
101
+ span = nk.css("span")
102
+ span.text.should == ""
103
+ end
104
+
105
+ it "should show '' if the object responds with an empty string for the passed attribute" do
106
+ @user.stub!(:name).and_return("")
107
+ nk = Nokogiri::HTML.parse(helper.best_in_place @user, :name)
108
+ span = nk.css("span")
109
+ span.text.should == ""
110
+ end
111
+ end
112
+
113
+ it "should have the given inner_class" do
114
+ out = helper.best_in_place @user, :name, :inner_class => "awesome"
115
+ nk = Nokogiri::HTML.parse(out)
116
+ span = nk.css("span")
117
+ span.attribute("data-inner-class").value.should == "awesome"
118
+ end
119
+
120
+ it "should have the given activator" do
121
+ out = helper.best_in_place @user, :name, :activator => "awesome"
122
+ nk = Nokogiri::HTML.parse(out)
123
+ span = nk.css("span")
124
+ span.attribute("data-activator").value.should == "awesome"
125
+ end
126
+ end
127
+
128
+
129
+ context "with a text field attribute" do
130
+ before do
131
+ nk = Nokogiri::HTML.parse(helper.best_in_place @user, :name)
132
+ @span = nk.css("span")
133
+ end
134
+
135
+ it "should render the name as text" do
136
+ @span.text.should == "Lucia"
137
+ end
138
+
139
+ it "should have an input data-type" do
140
+ @span.attribute("data-type").value.should == "input"
141
+ end
142
+
143
+ it "should have no data-collection" do
144
+ @span.attribute("data-collection").should be_nil
145
+ end
146
+ end
147
+
148
+ context "with a boolean attribute" do
149
+ before do
150
+ nk = Nokogiri::HTML.parse(helper.best_in_place @user, :receive_email, :type => :checkbox)
151
+ @span = nk.css("span")
152
+ end
153
+
154
+ it "should have a checkbox data-type" do
155
+ @span.attribute("data-type").value.should == "checkbox"
156
+ end
157
+
158
+ it "should have the default data-collection" do
159
+ data = ["No", "Yes"]
160
+ @span.attribute("data-collection").value.should == data.to_json
161
+ end
162
+
163
+ it "should render the current option as No" do
164
+ @span.text.should == "No"
165
+ end
166
+
167
+ describe "custom collection" do
168
+ before do
169
+ nk = Nokogiri::HTML.parse(helper.best_in_place @user, :receive_email, :type => :checkbox, :collection => ["Nain", "Da"])
170
+ @span = nk.css("span")
171
+ end
172
+
173
+ it "should show the message with the custom values" do
174
+ @span.text.should == "Nain"
175
+ end
176
+
177
+ it "should render the proper data-collection" do
178
+ @span.attribute("data-collection").value.should == ["Nain", "Da"].to_json
179
+ end
180
+ end
181
+
182
+ end
183
+
184
+ context "with a select attribute" do
185
+ before do
186
+ @countries = COUNTRIES.to_a
187
+ nk = Nokogiri::HTML.parse(helper.best_in_place @user, :country, :type => :select, :collection => @countries)
188
+ @span = nk.css("span")
189
+ end
190
+
191
+ it "should have a select data-type" do
192
+ @span.attribute("data-type").value.should == "select"
193
+ end
194
+
195
+ it "should have a proper data collection" do
196
+ @span.attribute("data-collection").value.should == @countries.to_json
197
+ end
198
+
199
+ it "should show the current country" do
200
+ @span.text.should == "Italy"
201
+ end
202
+ end
203
+ end
204
+
205
+ describe "#best_in_place_if" do
206
+ context "when the parameters are valid" do
207
+ before(:each) do
208
+ @output = "Some Value"
209
+ @field = :somefield
210
+ @object = mock("object", @field => @output)
211
+ @options = mock("options")
212
+ end
213
+ context "when the condition is true" do
214
+ before {@condition = true}
215
+ context "when the options parameter is left off" do
216
+ it "should call best_in_place with the rest of the parameters and empty options" do
217
+ helper.should_receive(:best_in_place).with(@object, @field, {})
218
+ helper.best_in_place_if @condition, @object, @field
219
+ end
220
+ end
221
+ context "when the options parameter is included" do
222
+ it "should call best_in_place with the rest of the parameters" do
223
+ helper.should_receive(:best_in_place).with(@object, @field, @options)
224
+ helper.best_in_place_if @condition, @object, @field, @options
225
+ end
226
+ end
227
+ end
228
+ context "when the condition is false" do
229
+ before {@condition = false}
230
+ it "should return the value of the field when the options value is left off" do
231
+ helper.best_in_place_if(@condition, @object, @field).should eq @output
232
+ end
233
+ it "should return the value of the field when the options value is included" do
234
+ helper.best_in_place_if(@condition, @object, @field, @options).should eq @output
235
+ end
236
+ end
237
+ end
238
+ end
239
+ end