Permalink
Browse files

Merge remote branch 'stray/master' into improved-events

Conflicts:
	lib/assets/javascripts/best_in_place.js
  • Loading branch information...
2 parents f330393 + d9f6d4c commit 11155341cf9838e923b89cc737d89ebb6e2baf3b @rogercampos rogercampos committed May 12, 2012
Showing with 41 additions and 4 deletions.
  1. +19 −0 README.md
  2. +8 −0 lib/best_in_place/helper.rb
  3. +8 −0 spec/helpers/best_in_place_spec.rb
  4. +6 −4 test_app/db/schema.rb
View
@@ -60,6 +60,7 @@ Options:
- **:display_as**: A model method which will be called in order to display
this field.
- **:object_name**: Used for overriding the default params key used for the object (the data-object attribute). Useful for e.g. STI scenarios where best_in_place should post to a common controller for different models.
+- **:data**: Hash of custom data attributes to be added to span. Can be used to provide data to the ajax:success callback.
###best_in_place_if
**best_in_place_if condition, object, field, OPTIONS**
@@ -180,6 +181,24 @@ You can also pass in a proc or lambda like this:
= best_in_place @post, :body, :display_with => lambda { |v| textilize(v).html_safe }
+## Ajax success callback
+
+### Binding to ajax:success
+
+The 'ajax:success' event is triggered upon success. Use bind:
+
+ $('.best_in_place').bind("ajax:success", function(){$(this).closest('tr').effect('highlight'));});
+
+### Providing data to the callback
+
+Use the :data option to add HTML5 data attributes to the best_in_place span. For example, in your view:
+
+ <%= best_in_place @user, :name, :data => {:user_name => @user.name} %>
+
+And in your javascript:
+
+ $('.best_in_place').bind("ajax:success", function(){ alert('Name updated for '+$(this).data('userName')); });
+
##Non Active Record environments
We are not planning to support other ORMs apart from Active Record, at least for now. So, you can perfectly consider the following workaround as *the right way* until a specific implementation is done for your ORM.
@@ -44,6 +44,14 @@ def best_in_place(object, field, opts = {})
out << " data-inner-class='#{opts[:inner_class]}'" if opts[:inner_class]
out << " data-html-attrs='#{opts[:html_attrs].to_json}'" unless opts[:html_attrs].blank?
out << " data-original-content='#{attribute_escape(object.send(field))}'" if opts[:display_as] || opts[:display_with]
+ if opts[:data] && opts[:data].is_a?(Hash)
+ opts[:data].each do |k, v|
+ if !v.is_a?(String) && !v.is_a?(Symbol)
+ v = v.to_json
+ end
+ out << %( data-#{k.to_s.dasherize}="#{v}")
+ end
+ 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))
@@ -161,6 +161,14 @@
end
end
+ it "should have html5 data attributes" do
+ out = helper.best_in_place @user, :name, :data => { :foo => "awesome", :bar => "nasty" }
+ nk = Nokogiri::HTML.parse(out)
+ span = nk.css("span")
+ span.attribute("data-foo").value.should == "awesome"
+ span.attribute("data-bar").value.should == "nasty"
+ end
+
describe "display_as" do
it "should render the address with a custom renderer" do
@user.should_receive(:address_format).and_return("the result")
View
@@ -12,20 +12,22 @@
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20111224181356) do
+
create_table "users", :force => true do |t|
t.string "name"
t.string "last_name"
t.string "address"
t.string "email", :null => false
t.string "zip"
t.string "country"
- t.datetime "created_at"
- t.datetime "updated_at"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
t.boolean "receive_email"
t.text "description"
- t.datetime "birth_date"
- t.float "money"
t.string "favorite_color"
t.text "favorite_books"
+ t.datetime "birth_date"
+ t.float "money"
end
+
end

0 comments on commit 1115534

Please sign in to comment.