Showing posts with label rails. Show all posts
Showing posts with label rails. Show all posts

Friday, April 8, 2011

Ruby: Stringception - a string within a string

good reference on all the various ways to define strings in ruby
http://www.techotopia.com/index.php/Ruby_Strings_-_Creation_and_Basics

So that you can run testing like:

assert_difference( %[Story.count :conditions => 'state = "P"']) do
put :drop, :id => story.to_param, :state => 'P'
end

Where the query string contains a string.

Thursday, April 7, 2011

jquery w/ rails : ajax update of drag & drop item

A script added to my html.erb file, at page load it establishes sortable columns, and ajax call to automatically update an item when it is dragged from one column to another:

$(function() {
$(".sortcol").sortable({
connectWith: ".sortcol",
receive: function(event, ui) {
$.ajax({
url: "<%= drop_url() %>",
type: 'PUT',
data: $.param({id : ui.item.attr('id'), state : ui.item.parent().attr('id')}),
success: function(d) { ui.item.effect("pulsate", {} , 500 ) }
})//end get
} //end receive function
}); //end sortable
}); // end function



The drop_url references as routes.rb entry /board/drop to a specific board_controller method. Upon retrospect, I'm thinking this ought to work with the default update method on my :story resource. Note to self, if your update method doesn't update... check that you haven't forgotten your parameter naming conventions. status != state no matter how you write the method to update.

Saturday, March 12, 2011

Rails - Ruby: lesson in MVC

So turns out if you apply MVC correctly you save yourself from lots of unnecessary objects!

Originally I thought I needed a Board --> Panel --> Story. But as mentioned last post, using poro instead of ActiveRecord was getting 'interesting', as I had decided I didn't need any state for Board and Panel. As I was fixing tests... suddenly realized how all this was very unnecessary work:

The better solution is to create a BoardController, and then define the panels as simple attributes of that controller that reference the appropriate collection of stories:

class BoardController < ApplicationController
 def show
  @backlog = Story.find :all, :conditions => 'state IS NULL'
   respond_to do |format|
    format.html # show.html.erb
    format.xml { render :xml => @stories }
    end
  end
end

Now instead of needing to keep a bunch of keys, names in alignment between the view, board, panel objects... I just have simple associations. Each named collection I want is an attribute in the board controller, available to the view. The view simply needs to render each attribute appropriately. This approach also seem to have simplified my views, since now I just have the 'board' view, and a '_story' partial view to render each collection element.

Ruby-Rails : lessons on rails ActiveRecord vs plain old rails objects

Got distracted from doing the Agile-Web-Development-With-Rails examples, am now starting from scratch on an experiment with jquery and rails. I'm looking to see what I can achieve from a robust client app with rails based data & services via ajax.

1) Unit Tests throwing " ActiveRecord::StatementInvalid: Could not find table 'panels' "

Kept seeing this error for several basic assert statements in the model unit tests.
> assert p.is_a(Panel)
> assert p.display_text == 'backlog'

Noob mistake here... using 'rails g model' for objects that will not be stored in the database. Caused the objects to inherit from ActiveRecord class, and when ActiveRecord triggers automagic that expects a table to exist for the Panel class, and complains when it doesn't.

Instead, I really wanted to be dealing with plain old ruby objects, and viola! once I deleted the ActiveRecord reference in the model object.

> couldn't use is_a, instead had to use ruby method is_a?
> have to define the accessor methods for private instance variables.