Super InPlace Controls upgraded. Now with Rails 2.3 support
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
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
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:
Example4.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
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:
- cd vendor/plugins/acts_as_noteable
- 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 => true do |t|
t.column "from", :string, :limit => 50, :default => ""
t.column "body", :text, :default => ""
t.column "created_at", :datetime, :null => false
t.column "noteable_id", :integer, :default => 0, :null => false
t.column "noteable_type", :string, :limit => 15, :default => "", :null => 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 < 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:
<%= display_notes(object) %>
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:
<%= display_notes(obj, partial_name = "notes/note", controller_name = "notes") %>
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