README.md in dynaspan-0.1.1 vs README.md in dynaspan-0.1.2.beta1

- old
+ new

@@ -1,49 +1,84 @@ ##Dynaspan [![Gem Version](https://badge.fury.io/rb/dynaspan.svg)](http://badge.fury.io/rb/dynaspan) +[![Code Climate](https://codeclimate.com/github/danielpclark/dynaspan/badges/gpa.svg)](https://codeclimate.com/github/danielpclark/dynaspan) #####[JSFiddle Demo](http://jsfiddle.net/680v09y8/) Dynaspan is an AJAX tool for Rails to update one field of any object without interfering with your website experience. The user will see the web page as normal text. Where ever you've placed a Dynaspan field people can click on the text and it transforms into text entry. As soon as the person moves away from that entry it sends the update to the server. Dynaspan also accepts updating an attribute for a nested object, but only 1 level deep. ###Installation - - [ ] Add `gem 'dynaspan'` to your Gemfile. - - [ ] Run `bundle`. - - [ ] Next add `include Dynaspan::ApplicationHelper` inside your **ApplicationHelper** module. - - [ ] Add `//= require dynaspan/dynaspan` to your **application.js** file. - - [ ] And it's installed! + - [ ] Add `gem 'dynaspan'` to your Gemfile + - [ ] Run `bundle` + - [ ] Next add `include Dynaspan::ApplicationHelper` inside your **ApplicationHelper** module + - [ ] Add `//= require dynaspan/dynaspan` to your **application.js** file +And it's installed! + ###Usage -Example #1: +Simple example: +```ruby +dynaspan_text_field(user, :name) +``` +And that's it. As long as you have a User object with a name field, this will update through +the UserController's update method. **user** is an User Object instance eg: `user = User.first`. - dynaspan_text_field(@article, comment, :note, '[edit]') - -Example #2: +--- - dynaspan_text_field(profile, profile.websites, :url, '[edit]', - { - hidden_fields: {page_name: 'page2'}, - callback_on_update: "alert('Awesome!');" - } - ) - +Polymorphic/Nested Example #1: +```ruby +dynaspan_text_field(@article, comment, :note, '[edit]') +``` +Polymorphic/Nested Example #2: +```ruby +dynaspan_text_field(profile, profile.websites, :url, '[edit]', + { + hidden_fields: {page_name: 'page2'}, + callback_on_update: "alert('Awesome!');" + } + ) +``` This will show the value of note in the comment object as plain text. It can be clicked on to instantly become a text field input. And once unselected the `@article` object will update with its nested attribute object `comment` and its new value in the `note` attribute. You can use either `dynaspan_text_field` or `dynaspan_text_area` in any of your views. There are two mandatory parameters. The first is a the main Object model instance you will be updating. And the other mandatory field is the symbol of the attribute to update. There are two optional fields. The first is the nested attribute object which will have its field updated. And the last is the optional text for `[edit]`-ing (clicking on to edit which is useful for blank fields). +```ruby +dynaspan_text_field(Object,OptionalNestedObject,SymField,OptionalEditText,OptionalOptionsHash) +dynaspan_text_area(Object,OptionalNestedObject,SymField,OptionalEditText,OptionalOptionsHash) +``` +The order is important. And yes it does NOT change even if you just do: +```ruby +dynaspan_text_field(Object,SymField) +``` +It is unconventional but the order remains the same despite the optional fields. - dynaspan_text_field(Object,OptionalNestedObject,SymField,OptionalEditText,OptionalOptionsHash) - dynaspan_text_area(Object,OptionalNestedObject,SymField,OptionalEditText,OptionalOptionsHash) +###Parameters -The order is important. And yes it does NOT change even if you just do: +The **first** parameter will always be the Object that will have its update method called. It must be an instance of the Object. +For example current_user being an instance of User. - dynaspan_text_field(Object,SymField) +The **second** parameter can be a symbol of the field you want to update on the main Object from the first parameter. -It is unconventional but the order remains the same despite the optional fields. +The **second** field can also be a has_one or has_many subset of the first argument moving the symbol to modify to the **third** argument. +For example **dynaspan_text_field(author, author.stories, :title)**. This works as a nested attribute so it includes Polymorphic Objects. +The last two parameters can be edit text, and then additional options (in that order). Both are optional. The edit text +is a way to be able to click somewhere to open up the input to initially enter text. + +The options Hash currently has three options. + + - **:hidden_fields** will put in as many hidden fields as you include in a Hash with key->value matching to name->value + - **:callback_on_update** is a no frills callback. It runs whatever command you give it whenever Dynaspan submits an update + to the server + - **:callback_with_values** will allow you to put a JavaScript command you want called on update and include as many parameters + as you'd like. It will dynamically append a last parameter which is a Hash of two values. The first is the CSS selector id + of the Dynaspan block that just performed the action, the second value is the actual text that was entered. The keys in this + Hash are **ds_selector** and **ds_input** + + ###How it updates The AJAX call will call the update method on your first Object parameter via PATCH. The optional nested attribute and the symbol for the field are all part of the main Object being updated. There is no expected AJAX reply. It's a silent set it and forget it method. If you don't have your update method configured with a `.js` response then it will successfully perform the update on the object, and then send a complaint about a response but no one will notice (unless maybe you look at the server logs). In other words the client experience is only good, and the server won't hiccup over it. ###It's too easy! @@ -79,55 +114,55 @@ ####Version 0.1.1 Added a JavaScript callback that will **append** a Hash/Dictionary of the updated Dynaspan Object to the end of your functions parameters. The method is named **callback_with_values**. - - { - callback_with_values: "console.log();" - } - +```ruby +{ + callback_with_values: "console.log();" +} +``` This will be called everytime the Dynaspan field submits and it will **inject** the following result **as the last parameter**: - - { - ds_selector: "dyna_span_unique_label<#>", - ds_input: "the entered text from the input field" - } - +```ruby +{ + ds_selector: "dyna_span_unique_label<#>", + ds_input: "the entered text from the input field" +} +``` ####Version 0.1.0 Added the same hidden_fields from version 0.0.8 to support non-nested Objects. You can use them now on anything. ####Version 0.0.9 JavaScript callback option now available. Whenever the Dynaspan field is submitted you can have Dynaspan call your own JavaScript method. - - { - callback_on_update: "someMethod('some-relative-instance-value');" - } - +```ruby +{ + callback_on_update: "someMethod('some-relative-instance-value');" +} +``` ####Version 0.0.8 You can now provide an option hash as a last parameter. Current valid options only include: - - { - hidden_fields: { label: "value" } - } - +```ruby +{ + hidden_fields: { label: "value" } +} +``` You can add as many hidden fields to your Dynaspan objects as you'd like. ->NOTE: In this version hidden fields only apply for nested attributes. +>NOTE: In this version hidden fields only applies to nested attributes. Also the id parameter will only be passed to the server if it exists. (No more empty string for id.) This allows you to create "new" polymorphic child objects with Dynaspan. ###License The MIT License (MIT) -Copyright (C) 2014 by Daniel P. Clark +Copyright (C) 2014-2015 by Daniel P. Clark Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell