Creating HTML in jquery is a tedious and, quite frankly, boring task. Coming from a Rails web development background, I always wanted a way to create HTML like Rails does, with a content_tag method helper that can take attributes as has and be passed function blocks to quickly generate HTML within ruby. This is where the inpsiration for EasyHTMLTags comes from.

Let’s just dive in.

This plugin isnt anything complicated, just useful, so let’s see how it works.

1. Download the plugin and install into the appropriate directory.

2. Link to the plugin from your HTML

  
<script src="/path/to/javascripts/jquery.easy_html.js" type="text/javascript" charset="utf-8"></script>


3. Start creating tags

 
$.tag("li")
# =>  "<li></li>"

$.tag("li", "some content")
# =>  "<li>some content</li>"

$.tag("li", "some content", { class: "my_class", id: "special_id"})
# =>  "<li class="my_class" id="special_id">some content</li>"

$.tag("li", "some content", { class: "my_class", id: "special_id"}, function() { return $.tag("span", "some extra stuff")})
# =>  "<li class="my_class" id="special_id">some content<span>some extra stuff</span></li>"

That’s it. Hope you find it useful.

Check out the docs for more information.

—jake


Super InPlace Controls has gone through some upgrades.

We’ve Moved

First and foremost, we have moved the code from google to github.

To install run


script/plugin install git://github.com/flvorful/super_inplace_controls.git

Rails 2.3 Support

Tested with Rails 2.3 and 2.1

Better Jquery Support

We have updated the code to work better with jQuery. Now, in_place_date_selector, uses the jQuery datepicker method if available. JRails is required for jquery support.

Better Validation

Validations were a problem with SIPC, but no longer. Now, if you dont have an error div, SIPC will skip over the rendering of the error box and just add the class “fieldWithError” to the proper input tag.

Overall cleanup

Cleaned up the code in general and made it less brittle.

Check out the demos and the docs at our Open Source Site and stay tuned for some new plugins we’ve been working on.

—jake


New Open Source site launched

Posted by admin 10/12/2007 at 17h09

We have just launched our new Open Source project site. We will centralize all of our OS projects into this site.

Check it out


Rails Plugin - Jake's Rails Toolbox

Posted by admin 10/07/2007 at 13h56

hello all,

Ive recreated my old plugin and updated with some new features.

I created this plugin as a hodgepodge of different methods/hacks that i have found useful over the course of developing Rails Apps. I packaged it up as one plugin because most of the methods have nothing to do with each other, but i find them are very useful going from project to project.

Lets talk about them:

ActiveRecord Hacks:

1. Dynamic “nice_date”

Basically, i got tired of typing object.created_at.strftime(“%m-%d-%Y”) so i came up with the following method_missing hack for any Time attribute in an ActiveRecord object. Just prepend “nice_” in front of any Datetime attribute and you will get a nicely formatted date string. Plus some other goodies :)

Examples:

object.created_at                                          
# => Mon Dec 04 12:36:55 CST 2006
object.nice_created_at                                
# => "12-04-2006"  (mm-dd-YYYY)
object.nice_created_at(:euro)                 
# => "04-12-2006"  (dd-mm-YYYY)

plus, for you guys (and gals) that like using slashes instead of dashes:
object.nice_created_at(:slash)                 
# => "12/04/2006"  (mm/dd/YYYY)
object.nice_created_at(:euro_slash)     
# => "04/12/2006"  (dd/mm/YYYY)

and finally:
object.nice_created_at(:rss)                     
# => "Mon, 04 Dec 2006 12:36:55 -0600"  (rfc822)

NOTE
The attribute must be an instance of Time or a NoMethodError will be thrown

2. Dynamic SEO formatting.

Any method that returns a string can use this dynamic method. This method will create an SEO friendly string out of the attribute chosen (for use mainly in URLS). It will append the objects id (not the object_id, but the id key from the database) so that you can use params[:id] and not have to create new routes.

Example:

puts object.id                         
# => 1
object.title = " Rails is freaking awesome, didn't you know that?  " 
puts object.seo_title         
# => "1-rails-is-freaking-awesome-didnt-you-know-that" 

3. nice_name

This one is pretty simple. I found myself adding this same method to alot of my applications. Basically it looks for an attribute called “first_name” and one called “last_name” and combines them together with a space in between.

Example:

object.first_name    = "jake" 
object.last_name     = "varghese" 
object.nice_name  
# => "jake varghese" 

NOTE
If the method doesn’t have EITHER first_name or last_name, then a NoMethodError
will be thrown. It must have both attributes

4. create/new from XML

Create an ActiveRecord Object from XML. This method relies on the basics of AR’s current to_xml method (basically, it must be formatted the same).The differences between new_from_xml and create_from_xml are the same as the differences between AR.new and AR.create

If your XML string contains multiple records, it will parse out each record and create an object. The “multiple record” call returns an array like find(:all)

Example:

person = Person.new_from_xml(xml_string)
# => #<Project:0x2430758 @attributes={"prototype_url"=>nil, "company"=>"some value", 
"order_id"=>45564, "phone_number"=>555544455, "created_at"=>nil, 
"email_address"=>lkjlkjL@jfgg.com}, @new_record=true>

String Hacks:

1. SEO friendly.

This method is used in the AR seo hack. It takes a string and makes it seo friendly.

Example:


c.title = " Rails is freaking awesome, didn’t you know that? "
c.title.seo_friendly
# => “rails-is-freaking-awesome-didnt-you-know-that”

Float Hacks:

1. to_s override.

I really hate using sprintf, mainly because i always have to go online and look up the syntax. I figured i make that a little easier. Now you can print floats with different precision as easily as:

Example


4.123456.to_s(1) # => “4.1”
4.123456.to_s(3) # => “4.12”
4.123456.to_s(3) # => “4.123”
4.123456.to_s(4) # => “4.1235” (Note the auto rounding from 4.123456)
4.123456.to_s # => “4.123456”

Hash Hacks:

1. Method Missing hack to allow for easy hash referencing.

I can never remember whether the Hash i am playing with has symbols for keys or strings. I also dont like typing the brackets (not all text editors have the cool “auto close brace” feature). That’s why i came up with this method missing hack. Instead of explaining what it does, I’ll just show you.

Example:

hsh =  {"project"=>
        {
         "prototype_url"=>nil,
         "designer_id"=>2,
          "finished_at"=>nil,
          "phone_number"=>"512225555",
          "website"=>"http://www.ggg.com",
          "first_name"=>"test"
         }
        }
hsh.project                             
#=>  {"prototype_url"=>nil, "designer_id"=>2, "finished_at"=>nil, 
"phone_number"=>"512225555", "website"=>"http://www.ggg.com", 
"first_name"=>"test"}

hsh.project.prototype_url 
#=> nil
hsh.project.designer_id     
#=> 2
hsh.project.first_name         
#=> test

So instead of hsh[“project”] you can just write hsh.project

and that’s it. If you want to try it yourself, just run

script/plugin install http://jakes-rails-toolbox.googlecode.com/svn/trunk/jakes_toolbox 

You can also checkout the latest rev via SVN at:

svn checkout http://jakes-rails-toolbox.googlecode.com/svn/trunk/ vendor/plugins 

New Rails Plugin - Acts As Noteable

Posted by admin 10/06/2007 at 13h51

So I was at work and I needed to add notes to multiple models in the same project (before i only had to worry about one model with notes, so it was pretty straightforward). So i bit down and knocked out this little plugin.

I borrowed heavily from Acts As Rateable by Juixe

Installation

Run the following command:

script/plugin install http://acts-as-noteable.googlecode.com/svn/trunk/acts_as_noteable

The install will try and copy some scripts over, this works sometimes. If you get a rake error do the following:

  1. cd vendor/plugins/acts_as_noteable
  2. rake update_scripts

Create a new rails migration:

script/generate migration add_notes_to_project

and add the following self.up and self.down methods

    def self.up
        create_table "notes", :force =&gt; true do |t|
            t.column "from", :string, :limit =&gt; 50, :default =&gt; "" 
            t.column "body", :text, :default =&gt; "" 
            t.column "created_at", :datetime, :null =&gt; false 
            t.column "noteable_id", :integer, :default =&gt; 0, :null =&gt; false 
            t.column "noteable_type", :string, :limit =&gt; 15, :default =&gt; "", :null =&gt; false 
        end 
    end 

    def self.down
        drop_table :notes 
    end 
 

(one day i’ll add a migration script to the plugin to automatically add this :) )

After that run “rake migrate” to create the table.

Now all you need to do is call acts_as_noteable in your model:

class Model &lt; ActiveRecord::Base
  acts_as_noteable
end

Now your model is notable, but that’s not all.

For only 19.95 you get a Helper method to help you create a nice form. Ok, so i am not actually charging, but whatever.

Here’s how the helper works.

In the view you want to display notes in put the following:

&lt;%= display_notes(object) %&gt;

This will create a div filled with the note entries for the object and a AJAX form to add new notes to the object as well as delete notes from the object. The add and delete methods are stored in the notes_controller.rb that gets copied over into your app/controllers directory.

The full implementation for this method is:

&lt;%= display_notes(obj, partial_name = "notes/note", controller_name = "notes") %&gt;

I added the partial name and controller name in case you have multiple controllers in different folders, this way you can display notes for objects whose controllers are buried deep in a folder.

Example: I have an “admin” folder for all the backend stuff and I needed a way to reference the notes controller from different places, otherwise RAILS will assume you are trying to reference a controller from the current folder your in.

After you have called display_notes, your done.

Note** This plugin has not been tested with Rails 2.0 yet


peace

—jake