Found the following pages that are tagged with "rails".
<< Return to blog
Oct 17, 2008
New as of Rails 2.1.1 the before_save filter and the after_save filter stack has been modified. If you have to save the object associations again the after_save filter you can create the infinite loop of cpu love. Hence there needs to be some intervening step to keep it from looping.
SomeModel < ActiveRecord::Base
...
before_save :do_something
after_save :something_else
...
def do_something
self.manipulate
end
def something_else
self.association.manipulate
self.association.save
end
end
This worked up as above until rails 2.1.0, I would like to note that the above method although it worked, should not have used. Now you maybe wondering why I must do something else after and not before. The action I am taking requires data generated during the save. In order to keep things DRY, I allow the system to perform the necessary actions in the after_save so as to simplify the process. The way to deal with this is:
SomeModel < ActiveRecord::Base
...
before_save :do_something
after_save :something_else
...
As before the before_save is the same
...
def do_something
self.manipulate
end
...
Instantiating a new object from the association out will bypass the manipulation of self, thus preventing the infinite loop of cpu love
...
def something_else
obj = self.association
obj.manipulate
obj.save!
end
end
The result is that rails doesn’t loop. If you should need to manipulate self in after_save, I would recommend, putting some conditionals in place that will bypass the before_save and after_save filters as necessary based on the changes or required data. One caveat is the ActiveRecord::Dirty will only work for unsaved content that has been saved. Once the object is saved, you loose that. It is better to check for the existence/ non existence of attributes to test as conditionals.
Posted by
Tags: rails
© Copyright 2008 Eight Square Limited. All Rights Reserved.
Oct 11, 2008
A refresher for passing information only available to the controller to a model.
Firstly you will need set up attr_accessor on the model to receive the object or value from the controller.
class Model < ActiveRecord::Base
...
attr_accessor :current_user
...
end
The controller can easily pass an object or value as set up with attr_accessor by taking an instances of the model and using the virtual attribute pass in the object.
class CompaniesController < ApplicationController
...
def create
@company = Company.new
@company.current_user = current_user
...
end
end
The value is passed in from the controller can now be accessed within the model using the virtual attribute as the instance variable @current_user. Please note: This can also be done by defining methods
def current_user
...
end
def current_user=(user)
@current_user = user
end
Posted by Thomas Cowell
Tags: rails
© Copyright 2008 Eight Square Limited. All Rights Reserved.
Jul 20, 2009
Recently I have been trying to prototype an application and have needed a way to set the navigation bar during the initial development. So, here is a simple menubar I use while I am building apps.
In my rails app
lib/eightsquarestudio/menu_bar_builder.rb
module EightSquareStudio::MenuBarBuilder
protected
def self.controller_list(skip_routes=[])
list = ActionController::Routing.possible_controllers
skip_routes.concat(%w( rails/info rails_info application)).each do |item|
list.delete(item)
end
return list
end
end
module ApplicationHelper
def menubar_builder(controller, *skip_routes, &block)
result = []
block ||= lambda do |c|
link_to_unless(controller.controller_name == c, c.singularize, {:controller => c}) do |name|
link_to(name, {:controller => c}, {:class => 'current'})
end
end
EightSquareStudio::MenuBarBuilder.controller_list(skip_routes).each do |c|
result << block.call(c)
end
return "<div class='tab'>#{result.join("</div><div class='tab'>")}</div>"
end
end
If I get a chance I will wrap it into a plugin. If you have any comments or suggestions. Please email me mail at eightsquarestudio dot com
Posted by Thomas Cowell
Tags: rails
© Copyright 2009 Eight Square Limited. All Rights Reserved.
Nov 21, 2008
Today I upgraded my gems and all of a sudden all my sqlite3 db’s failed.
All the tasks using rake db:**** rake aborted! no such file to load -- sqlite3 This all includes all script/server and passenger uses of the sqlite db.
The gem datamapper do_sqlite3 somehow interfers with the sqlite3-ruby gem. Simple answer gem uninstall do_sqlite3 -v=0.9.7. I uninstalled versions 0.9.7, as there is not a problem with the previous version 0.9.6.
http://wm.lighthouseapp.com/projects/4819/tickets/660-require-sqlite3-fails-with-do_sqlite3-097
*****UPDATE*****
This has been fixed as of 0.9.9 – Thank you
Posted by Thomas Cowell
Tags: rails
© Copyright 2008 Eight Square Limited. All Rights Reserved.
Jan 01, 2010
Having taken over the maintenance of the Radiant Gallery here is a description of the project.
The Gallery extension allows to manage galleries of images/videos/files using Radiant CMS.
Features:
- Multiple nested galleries
- Multiple upload
- Import from ftp upload
- Images/Files sorting by drag-n-drop
- Automatic thumbnail generation for images after upload
- Preview in lightwindow for images
- Content fetching by keywords
- Custom geometry for thumbnail generation
Installation:
After installing radiant go to the radiant folder and check out the attachment_fu plugin under vendor/plugins and the radiant-gallery extension under the vendor/extensions folder.
cd /path/to/radiant
git clone git://github.com/technoweenie/attachment_fu.git vendor/plugins/attachment_fu
git clone git://github.com/hairballopolis/radiant-gallery.git vendor/extensions/gallery
In config/environment.rb add to the config.after_initialize block
config.after_initialize do
...
Radiant::Config["gallery.storage"] = "file_system"
end
Finally run the gallery install rake task
rake RAILS_ENV=production radiant:extensions:gallery:install
There is more information on the github radiant gallery project page
Posted by Thomas Cowell
Tags: rails
© Copyright 2010 Eight Square Limited. All Rights Reserved.