2008/06/18

Plugin released - records_sequence

In my last post, I wanted to have some flexible next or previous functions to find a object's neighbors. To fulfill that I just finished a plugin called "records_sequence" which could be found here.

After installed, records_sequence could mixin two methods "next/previous" to every ActiveRecord objects.

INSTALL
This plugin requires Rails 2.1+ currently. Install it by below command:
ruby script/plugin install git://github.com/Aaron2Ti/records_sequence.git

USAGE
After installed, your models could:
foo = User.first
bar = foo.next
foo_neighbour = foo.previous # returns nil if foo has no previous neighbour

#The sequence's sorted column is defined by the new option :sorted_by,
# default is the 'id' column.
foo = User.last
pre_foo_sorted_by_id = foo.previous
pre_foo_sorted_by_age = foo.previous(:sorted_by => 'age')

#Also works fine with most other ActiveRecord's find options:
foo = User.find 30
foo.next(:conditions => ['age < ?', 20]) foo.previous(:offset => 2)
foo.next(:sorted_by => 'address', :order => 'name DESC, age')
foo.previous( :sorted_by => 'age', :conditions => ['age < ?', 20],
:order => 'name DESC, address', :offset => 2 )


2008/06/13

Next & Previous Extension to ActiveRecord

Within Rails 2.1, ActiveRecord was added lots of flexibility, but I also want to have some next or previous functions built with the ActiveRecord model like below:

# find first foo
foo = Foo.first

# find next foo
next_foo = foo.next

# find next foo with defined sort column or conditions
next_foo = foo.next(:sort => :age, :conditions ...)

# previous function also.
previous_foo = foo.previous

#...

2007/09/16

Parameterized module design Using Ruby on Rails

   A long time I wrote noting. Last month I had my first holiday vocation after 'working' , that's really precious to me.

  Back to work, I have a mission to do some parameterized module design through the web. It means that, someone submit some params through their browsers, then the server renders the submitted params to the 3D model and changes the model, the users could retrieve the changed 3D models then.

I try to use ruby on rails and Solidworks to make a demo a demo of this application.

One difficult of this solution is how data flow through the application.

In my mind, firstly the user submit data to the server, then the server transfer the data to the ruby app.    Secondly, ruby could manipulate the Solidworks through 'win32ole' and fill the data.

After Solidworks changes the model doc file and saves it to somewhere user could download from in the server.

Followed is the lib/solidworks.rb I wrote in my rails application:

1 require 'win32ole'
2
3 module ModelDoc
4 def saveAs(filename)
5 self.SaveAs3(filename, 0, 0)
6 end
7 def save
8 self.EditRebuild3
9 self.ViewZoomtofit2
10 self.Save2 false
11 end
12 def setValue(parameter, value = 0.01)
13 self.Parameter(parameter).SystemValue = value
14 end
15 def closeDoc(docName)
16 self.CloseDoc docName
17 end
18 end
19
20 module SldWorks
21 def openDoc(filename, filetype = 1)
22 self.OpenDoc(filename, filetype)
23 activeDoc
24 end
25 def activeDoc()
26 self.ActiveDoc().extend ModelDoc
27 end
28 def exit
29 self.ExitApp()
30 end
31 end
32
33 def solidworks
34 begin
35 @sldworks = WIN32OLE.connect('SldWorks.Application')
36 rescue WIN32OLERuntimeError
37 @sldworks = WIN32OLE.new('SldWorks.Application')
38 end
39 @sldworks.extend SldWorks
40 end



The most suck problem to use Soildworks API is the pointer params, most APIs have parameters like followed:

OpenDoc6 ( filename, type, options, configuration, &Errors, &Warnings )

&Errors and &Warnings are pointer type, but Ruby don't support pointer type that may be a big problem!


For some other suggestions look this guy's  blog.


2007/08/23

Rails Restful routing

Just now I tried to write some some restful application using rails, then I found there is some thing wront with Rails' routing.

In my application , there were three models: book , person and library, a person or library may have many books, but the book can only have one owner----person or library.

After setup other relationships in the models, I wrote this in the route.rb:

map.resources :books,
:path_prefix => '/person/:person_id',
:name_prefix => 'person_'

map.resources :books,
:path_prefix => '/library/:library_id',
:name_prefix => 'library_'
And in the controller , I done something like this:
if params[:person_id]
........
elsif params[:library_id]
.......
end

That worked well.

But I felt that either person or library was the book's owner, so I refactor the routing to this one for cleaning the url:
map.resources :books,
:path_prefix => '/owner/:person_id',
:name_prefix => 'person_'

map.resources :books,
:path_prefix => '/owner/:library_id',
:name_prefix => 'library_'
Controllers changed nothing, then something wrong still.
It's impossible to pass the "params[:library_id]" using any of the helper the methods like "library_new_book, library_...." etc.
In the development.log , I found Parameters from the "library" name-prefix url helper had the same parameter---"Parameters:{"action"=>..., "controller"=>..., "person_id"=>3 }".
The "person_id" was excepted to "library_id"!

Then I changed the order of the routing :
map.resources :books,
:path_prefix => '/owner/:library_id',
:name_prefix => 'library_'

map.resources :books,
:path_prefix => '/owner/:person_id',
:name_prefix => 'person_'

This time the parameter "params[:person_id]" didn't work at all when using the helper like "person_new_book(1)" , "person_books (1)",etc.

Seemed in this case , Rails may use the first path-prefix's params for all the next routings whose path-prefix has the same path.

That's quite confused!!


2007/08/19

About Me

I am Aaron Tian(Chinese name: Tian Baoqing), 24 years old.