Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'dom'

  • Loading branch information...
commit f7a7eb425383ee13cdad9dd096d7752d4b24da4b 2 parents 6b8db61 + 485a9b3
Roger Campos rogercampos authored
110 lib/assets/javascripts/best_in_place.js
View
@@ -254,19 +254,33 @@ BestInPlaceEditor.prototype = {
BestInPlaceEditor.forms = {
"input" : {
activateForm : function() {
- var output = '<form class="form_in_place" action="javascript:void(0)" style="display:inline;">';
- output += '<input type="text" name="'+ this.attributeName + '" value="' + this.sanitizeValue(this.display_value) + '"';
- if (this.inner_class !== null) {
- output += ' class="' + this.inner_class + '"';
+ var output = $(document.createElement('form'))
+ .addClass('form_in_place')
+ .attr('action', 'javascript:void(0);')
+ .attr('style', 'display:inline');
+ var input_elt = $(document.createElement('input'))
+ .attr('type', 'text')
+ .attr('name', this.attributeName)
+ .val(this.display_value);
+ if(this.inner_class !== null) {
+ input_elt.addClass(this.inner_class);
}
- output += '>';
- if (this.okButton) {
- output += '<input type="submit" value="' + this.okButton + '" />';
+ output.append(input_elt);
+ if(this.okButton) {
+ output.append(
+ $(document.createElement('input'))
+ .attr('type', 'submit')
+ .attr('value', this.okButton)
+ )
}
- if (this.cancelButton) {
- output += '<input type="button" value="' + this.cancelButton + '" />';
+ if(this.cancelButton) {
+ output.append(
+ $(document.createElement('input'))
+ .attr('type', 'button')
+ .attr('value', this.cancelButton)
+ )
}
- output += '</form>';
+
this.element.html(output);
this.setHtmlAttributes();
this.element.find("input[type='text']")[0].select();
@@ -327,13 +341,20 @@ BestInPlaceEditor.forms = {
"date" : {
activateForm : function() {
- var that = this,
- output = '<form class="form_in_place" action="javascript:void(0)" style="display:inline;">';
- output += '<input type="text" name="'+ this.attributeName + '" value="' + this.sanitizeValue(this.display_value) + '"';
- if (this.inner_class !== null) {
- output += ' class="' + this.inner_class + '"';
+ var that = this,
+ output = $(document.createElement('form'))
+ .addClass('form_in_place')
+ .attr('action', 'javascript:void(0);')
+ .attr('style', 'display:inline'),
+ input_elt = $(document.createElement('input'))
+ .attr('type', 'text')
+ .attr('name', this.attributeName)
+ .attr('value', this.sanitizeValue(this.display_value));
+ if(this.inner_class !== null) {
+ input_elt.addClass(this.inner_class);
}
- output += '></form>';
+ output.append(input_elt)
+
this.element.html(output);
this.setHtmlAttributes();
this.element.find('input')[0].select();
@@ -366,14 +387,24 @@ BestInPlaceEditor.forms = {
"select" : {
activateForm : function() {
- var output = "<form action='javascript:void(0)' style='display:inline;'><select>";
- var selected = "";
- var oldValue = this.oldValue;
- jQuery.each(this.values, function(index, value) {
- selected = (value[1] == oldValue ? "selected='selected'" : "");
- output += "<option value='" + value[0] + "' " + selected + ">" + value[1] + "</option>";
- });
- output += "</select></form>";
+ var output = $(document.createElement('form'))
+ .attr('action', 'javascript:void(0)')
+ .attr('style', 'display:inline');
+ selected = '',
+ oldValue = this.oldValue,
+ select_elt = $(document.createElement('select'));
+ jQuery.each(this.values, function (index, value) {
+ var option_elt = $(document.createElement('option'))
+ // .attr('value', value[0])
+ .val(value[0])
+ .html(value[1]);
+ if(value[1] == oldValue) {
+ option_elt.attr('selected', 'selected');
+ }
+ select_elt.append(option_elt);
+ });
+ output.append(select_elt);
+
this.element.html(output);
this.setHtmlAttributes();
this.element.find("select").bind('change', {editor: this}, BestInPlaceEditor.forms.select.blurHandler);
@@ -384,6 +415,7 @@ BestInPlaceEditor.forms = {
getValue : function() {
return this.sanitizeValue(this.element.find("select").val());
+ // return this.element.find("select").val();
},
blurHandler : function(event) {
@@ -414,18 +446,28 @@ BestInPlaceEditor.forms = {
// grab width and height of text
width = this.element.css('width');
height = this.element.css('height');
-
- // construct the form
- var output = '<form action="javascript:void(0)" style="display:inline;"><textarea>';
- output += this.sanitizeValue(this.display_value);
- output += '</textarea>';
- if (this.okButton) {
- output += '<input type="submit" value="' + this.okButton + '" />';
+
+ // construct form
+ var output = $(document.createElement('form'))
+ .attr('action', 'javascript:void(0)')
+ .attr('style', 'display:inline')
+ .append($(document.createElement('textarea'))
+ .val(this.sanitizeValue(this.display_value)));
+ if(this.okButton) {
+ output.append(
+ $(document.createElement('input'))
+ .attr('type', 'submit')
+ .attr('value', this.okButton)
+ );
}
- if (this.cancelButton) {
- output += '<input type="button" value="' + this.cancelButton + '" />';
+ if(this.cancelButton) {
+ output.append(
+ $(document.createElement('input'))
+ .attr('type', 'button')
+ .attr('value', this.cancelButton)
+ )
}
- output += '</form>';
+
this.element.html(output);
this.setHtmlAttributes();
6 lib/best_in_place/helper.rb
View
@@ -20,7 +20,7 @@ def best_in_place(object, field, opts = {})
collection = nil
if opts[:type] == :select && !opts[:collection].blank?
v = real_object.send(field)
- value = Hash[opts[:collection]][!!(v =~ /^[0-9]+$/) ? v.to_i : v]
+ value = Hash[opts[:collection]].stringify_keys[v.to_s]
collection = opts[:collection].to_json
end
if opts[:type] == :checkbox
@@ -55,9 +55,9 @@ def best_in_place(object, field, opts = {})
end
if !opts[:sanitize].nil? && !opts[:sanitize]
out << " data-sanitize='false'>"
- out << sanitize(value, :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))
+ 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))
else
- out << ">#{sanitize(value, :tags => nil, :attributes => nil)}"
+ out << ">#{sanitize(value.to_s, :tags => nil, :attributes => nil)}"
end
out << "</span>"
raw out
2  spec/helpers/best_in_place_spec.rb
View
@@ -7,6 +7,7 @@
@user = User.new :name => "Lucia",
:last_name => "Napoli",
:email => "lucianapoli@gmail.com",
+ :height => "5' 5\"",
:address => "Via Roma 99",
:zip => "25123",
:country => "2",
@@ -336,6 +337,7 @@
@user = User.new :name => "Lucia",
:last_name => "Napoli",
:email => "lucianapoli@gmail.com",
+ :height => "5' 5\"",
:address => "Via Roma 99",
:zip => "25123",
:country => "2",
1  spec/integration/double_init_spec.rb
View
@@ -6,6 +6,7 @@
@user = User.new :name => "Lucia",
:last_name => "Napoli",
:email => "lucianapoli@gmail.com",
+ :height => "5' 5\"",
:address => "Via Roma 99",
:zip => "25123",
:country => "2",
39 spec/integration/js_spec.rb
View
@@ -6,6 +6,7 @@
@user = User.new :name => "Lucia",
:last_name => "Napoli",
:email => "lucianapoli@gmail.com",
+ :height => "5' 5\"",
:address => "Via Roma 99",
:zip => "25123",
:country => "2",
@@ -677,4 +678,42 @@
page.should have_link("link in this text", :href => "http://google.es")
end
end
+
+ it "should display single- and double-quotes in values appropriately" do
+ @user.height = %{5' 6"}
+ @user.save!
+
+ retry_on_timeout do
+ visit user_path(@user)
+
+ id = BestInPlace::Utils.build_best_in_place_id @user, :height
+ page.execute_script <<-JS
+ $("##{id}").click();
+ JS
+
+ page.find("##{id} select").value.should eq(%{5' 6"})
+ end
+ end
+
+ it "should save single- and double-quotes in values appropriately" do
+ @user.height = %{5' 10"}
+ @user.save!
+
+ retry_on_timeout do
+ visit user_path(@user)
+
+ id = BestInPlace::Utils.build_best_in_place_id @user, :height
+ page.execute_script <<-JS
+ $("##{id}").click();
+ $("##{id} select").val("5' 7\\\"");
+ $("##{id} select").blur();
+ JS
+
+ sleep 1
+
+ @user.reload
+ @user.height.should eq(%{5' 7"})
+ end
+ end
+
end
1  spec/integration/live_spec.rb
View
@@ -6,6 +6,7 @@
@user = User.new :name => "Lucia",
:last_name => "Napoli",
:email => "lucianapoli@gmail.com",
+ :height => "5' 5\"",
:address => "Via Roma 99",
:zip => "25123",
:country => "2",
1  spec/integration/text_area_spec.rb
View
@@ -6,6 +6,7 @@
@user = User.new :name => "Lucia",
:last_name => "Napoli",
:email => "lucianapoli@gmail.com",
+ :height => "5' 5\"",
:address => "Via Roma 99",
:zip => "25123",
:country => "2",
22 test_app/app/helpers/users_helper.rb
View
@@ -1,2 +1,24 @@
module UsersHelper
+ def height_collection
+ [
+ %{5' 1"} ,
+ %{5' 2"} ,
+ %{5' 3"} ,
+ %{5' 4"} ,
+ %{5' 5"} ,
+ %{5' 6"} ,
+ %{5' 7"} ,
+ %{5' 8"} ,
+ %{5' 9"} ,
+ %{5' 10"},
+ %{5' 11"},
+ %{6' 0"} ,
+ %{6' 1"} ,
+ %{6' 2"} ,
+ %{6' 3"} ,
+ %{6' 4"} ,
+ %{6' 5"} ,
+ %{6' 6"}
+ ]
+ end
end
4 test_app/app/views/users/_form.html.erb
View
@@ -20,6 +20,10 @@
<%= f.text_field :last_name %>
</div>
<div class="field">
+ <%= f.label :height %><br />
+ <%= f.select :height, height_collection %>
+ </div>
+ <div class="field">
<%= f.label :address %><br />
<%= f.text_field :address %>
</div>
6 test_app/app/views/users/show.html.erb
View
@@ -17,6 +17,12 @@
</td>
</tr>
<tr>
+ <td>Height</td>
+ <td id="height">
+ <%= best_in_place @user, :height, :type => :select, :collection => height_collection.zip(height_collection), :sanitize => false %>
+ </td>
+ </tr>
+ <tr>
<td>Email</td>
<td id="email">
<%= best_in_place @user, :email %>
5 test_app/db/migrate/20120620165212_add_height_to_user.rb
View
@@ -0,0 +1,5 @@
+class AddHeightToUser < ActiveRecord::Migration
+ def change
+ add_column :users, :height, :string
+ end
+end
3  test_app/db/schema.rb
View
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120616170454) do
+ActiveRecord::Schema.define(:version => 20120620165212) do
create_table "cars", :force => true do |t|
t.string "model"
@@ -33,6 +33,7 @@
t.datetime "birth_date"
t.float "money"
t.float "money_proc"
+ t.string "height"
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.