Skip to content

Commit

Permalink
implement :type => :date
Browse files Browse the repository at this point in the history
  • Loading branch information
taavo committed Dec 17, 2011
1 parent 92e7310 commit 3182274
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
40 changes: 40 additions & 0 deletions lib/assets/javascripts/best_in_place.js
Expand Up @@ -266,6 +266,46 @@ 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 + '"';
}
output += '></form>'
this.element.html(output);
this.setHtmlAttributes();
this.element.find('input')[0].select();
this.element.find("form").bind('submit', {editor: this}, BestInPlaceEditor.forms.input.submitHandler);
this.element.find("input").bind('keyup', {editor: this}, BestInPlaceEditor.forms.input.keyupHandler);

this.element.find('input')
.datepicker({
dateFormat: 'yy-mm-dd',
onClose: function() {
that.update();
}
})
.datepicker('show');
},

getValue : function() {
return this.sanitizeValue(this.element.find("input").val());
},

submitHandler : function(event) {
event.data.editor.update();
},

keyupHandler : function(event) {
if (event.keyCode == 27) {
event.data.editor.abort();
}
}
},

"select" : {
activateForm : function() {
var output = "<form action='javascript:void(0)' style='display:inline;'><select>";
Expand Down
20 changes: 20 additions & 0 deletions spec/helpers/best_in_place_spec.rb
Expand Up @@ -11,6 +11,7 @@
:zip => "25123",
:country => "2",
:receive_email => false,
:birth_date => Date.today,
: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."
end

Expand Down Expand Up @@ -155,6 +156,25 @@
end
end

context "with a date attribute" do
before do
nk = Nokogiri::HTML.parse(helper.best_in_place @user, :birth_date, :type => :date)
@span = nk.css("span")
end

it "should render the date as text" do
@span.text.should == Date.today.to_s
end

it "should have a date data-type" do
@span.attribute("data-type").value.should == "date"
end

it "should have no data-collection" do
@span.attribute("data-collection").should be_nil
end
end

context "with a boolean attribute" do
before do
nk = Nokogiri::HTML.parse(helper.best_in_place @user, :receive_email, :type => :checkbox)
Expand Down
35 changes: 35 additions & 0 deletions spec/integration/js_spec.rb
Expand Up @@ -10,6 +10,7 @@
:zip => "25123",
:country => "2",
:receive_email => false,
:birth_date => Date.today,
: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."
end

Expand Down Expand Up @@ -105,6 +106,40 @@
end
end

it "should be able to use bip_text to change a date field" do
@user.save!
visit user_path(@user)
within("#birth_date") do
page.should have_content(Date.today)
end

bip_text @user, :birth_date, (Date.today - 1.days)

visit user_path(@user)
within("#birth_date") do
page.should have_content(Date.today - 1.days)
end
end

it "should be able to use datepicker to change a date field" do
@user.save!
visit user_path(@user)
within("#birth_date") do
page.should have_content(Date.today)
end

id = BestInPlace::Utils.build_best_in_place_id @user, :birth_date
page.execute_script <<-JS
$("##{id}").click();
$(".ui-datepicker-calendar tbody td").not(".ui-datepicker-other-month").first().click()
JS

visit user_path(@user)
within("#birth_date") do
page.should have_content(Date.new(Date.today.year, Date.today.month, 1))
end
end

it "should be able to use bip_bool to change a boolean value" do
@user.save!
visit user_path(@user)
Expand Down

0 comments on commit 3182274

Please sign in to comment.