CategoryDevelopment

Source code websites

http://www.refactormycode.com

http://snippets.dzone.com/tag/split

http://www.splunk.com

Articles: http://thedailywtf.com/

Rails caching notes

General Notes

Very nice tutorial for rails 2.1 caching.

API references: Fragments, Sweepers, the Store

Post how to move the page cache in a folder with some server examples

Observing Controllers with Sweepers

Here is described how you can use the Sweepers to observe controller actions. I totaly agree that the documenation is very bad on the sweepers and it is like a rule book of MTG than usefull rdoc.

Here is what I have found on the naming of the callbacks.

Checking the source code in sweeping.rb shows that the callbacks that are used for the controller are constructed in this way:

controller_callback_method_name = "#{timing}_#{controller.controller_name.underscore}"
action_callback_method_name = "#{controller_callback_method_name}_#{controller.action_name}"

This means that the method which will be called in the sweeper will be named

  • after/before_user – for the controller UserController
  • after/before_user_login – for the action login in the controller UserController

Here is an example.

class Dagens::UserController < Dagens::BaseController
  cache_sweeper DagensAgentSweeper, :only => [:do_login, :do_delete_account, :do_logout]
  def do_login
  end
  ....
end

class DagensAgentSweeper < ActionController::Caching::Sweeper
  observe DagensAgent

  def after_user_do_login
    puts "after do login \n \n \n \n \n \n \n "
    expire_cache_for(@logged_user)
  end
end

Skipping callbacks from an observer

I know that it is not good idea, but some times there is no time/way to do skip it. I have found a plugin and this post here. I have choose to use the second one in my project. Here is the example

class DagensAgentSweeper < ActionController::Caching::Sweeper
  observe DagensAgent

  def self.skip_callback(callback, &block)
    method = instance_method(callback)
    remove_method(callback) if respond_to?(callback)
    define_method(callback) { true }
    result = yield
    remove_method(callback)
    define_method(callback, method)
    result
  end
........
end

And later in the controller….model…

      DagensAgentSweeper.skip_callback(:after_update) do
        @agent.shown_times += 1
        @agent.save
      end

timed_fragment_cache

Some patches on the timed_fragment_cache plugin.

Note how Jolyon Says suggest to expire the cache when you have time zone : when_fragment_expired ‘tags_home_page’, Time.now + 10.minutes do

I have notice that timed_fragment_cache (or the patch) have a problem with expiring the cache from the controller. In the documenation it is stated that it is ok to expire the cache only in the controller with

when_fragment_expired  "_last_update",  Time.now + 1.minutes  do

and there is no need to expire it in the view, but you MUST expire it in the view.

cache "_last_update" do

must be

cache "_last_update", Time.now + 1.minutes  do



		
	

Nginx with Rails

downloaded from here
documentation here
If you want to try different solution check it haproxy Continue reading

Semantic web

http://www.powerset.com/

http://www.trueknowledge.com/

http://www.spock.com/

good blog (bg)

lecutre in bg

Forum based search engine http://www.omgili.com/

Do you need to change a filename before sending it ?

Rails action to send binary data

I have one old application where the attachment is store as an id in the filesystem. The customer wants to have the attachment with the right name. So I have made this small action which accepts the id of the requested object and send the file data.


  def send_bios
    redirect_to :action => 'bios' if params[:id].blank?
    @artist = Artist.find(params[:id]);
    contents = File.open(@artist.bios_path,"rb") {|io| io.read}
    send_data contents, :filename => "#{@artist.name}.pdf", :type => "application/pdf", :disposition => 'attachment'
  end

Bulgarian stopwords

I want to start such a list so I will become maintain a Bulgarian list here which I will use for my website www.cenite.com

As a base source I will use this list here

Please submit your suggestions.

Note: I have removed a lot of words from the starting list.

 Continue reading

Javascript in the window.name

Wow. What a nice idea.

http://code.google.com/p/quipt/

Note: if you want to make the test.html to work checkout and test on http://… it is some beta but it works great only in Firefox

Random images helper for Rails

Here is how to make random images generation for your website.

First you must put your images in a folder /public/images/random_images or what ever.

Second you put images there and mark the filenames with a prefix, which image to which section will be visible on.

index_bottom_1.jpg -> for the index page on the bottom :)

index_bottom_2.jpg -> for the index page on the bottom

featured_art_1.jpg -> some panel.

featured_art_2.jpg -> some panel.

and so on…

Paste this method in your base controller. This method will be called to make list for a specific image folder and “filter” the images that are supposed to be showed in the different regions in @variables.

  before_filter :init_random_images
  protected # :) be carefull for this if you copy paste
  def init_random_images
    # collect some static images
    web_root = "images/imagestorage/random_images"

    all_images = Dir["#{RAILS_ROOT}/public/#{web_root}/*.jpg"]

    @random_head_images = []
    @random_indexbtm = []
    @random_featured = []
    @random_sales = []
    @random_services = []
    @random_framing = []
    @random_artistcollection = []
    all_images.each do |f|
      if File.stat(f).file?
        @random_head_images << ("/#{web_root}/" + File.basename(f)) if f =~ /head_images_/
        @random_indexbtm << ("/#{web_root}/" + File.basename(f)) if f =~ /indexbtm_/
        @random_featured << ("/#{web_root}/" + File.basename(f)) if f =~ /featured_/
        @random_sales << ("/#{web_root}/" + File.basename(f)) if f =~ /sales_/
        @random_services << ("/#{web_root}/" + File.basename(f)) if f =~ /services_/
        @random_framing << ("/#{web_root}/" + File.basename(f)) if f =~ /framing_/
        @random_artistcollection << ("/#{web_root}/" + File.basename(f)) if f =~ /artistcollection_/
      end
    end

Then paste this as a helper or in the base controller, to use it in your views. The method will give you from the images array, count number of images.

  helper_method :get_random_image
  def get_random_image(images, count)
    requested_images = []
    images_copy = images.clone
    [images_copy.size, count].min.times do
      index = rand(images_copy.size)
      requested_images << images_copy[index]
      images_copy.delete_at(index)
    end
    requested_images.flatten.compact
  end

here is an example how to use it.

<div id="static_logo" style="background:url(<%= get_random_image(@random_sales, 1) %>) no-repeat scroll left top;">
 

Future optimization

Put some caching, not to make

Dir["#{RAILS_ROOT}/public/#{web_root}/*.jpg"]
 

on every request.

10x Bl8cki to the nice code optimizations :)

Opera beautifully engineered…for my desktop

I was nice surprised when I tried Opera browser for PC after aprx. one year Firefox usage.

Zooming is faster and smarter than in every browser that I have used.

Firefox boot like MS Windows, and is almost stable like it.

Opera is faaaaast. Gmail is fast. And my work is going fast because all webpages are opening fast.

What? Why you need faster browser if it doesnt support right CSS, DOM, or what ever?

Who said this ? Check the tests for CSS2 and see that Opera and FF are failing apx on the same tests on the same places I am not sure even who is the winner. Check also this those acid tests also

Does someone has tried the dragonfly plugin – analog for the firefox firebug ?

I must agree that the Opera release 9.50 (Build 10063) has some strange javascript support. It happends that when I paste an URL for RSS in Opera, the current page disapears, maybe this is a feature and not a bug :)

Also I am very very disapointed that I should write this article in FF because the build-in WordPress editordoesnt work for Opera :)

Copy checkboxes from one form to another with Prototype

I have a case where the user must select items from a basket and use them with different forms.

Example:
Some items in this form
  • Item:
  • Item:
  • Item:
  • Item:
Another form here
(I will create here hidden fields from the the checkboxes in the first box)

I will show you how to use the checked boxes from the first form with the submit action in the second form

It would be easy if the form could be one – less support.

I am starting with those two forms

  • Helper form – Form with checkboxes
  • Action form – the form that will do the real action based on the selected items in the helper form

Here is how it looks the helper form. All the checkboxes are tagged with the class=”selectable”. Here is rails example but you can implement it pure html.

<%= check_box_tag('selected[]', true, image.selected?, :class=>"selectable") %>

Then we can select all the checkboxes with this javascript.

var make_to_hidden = $$('input.selectable');

Put on your action form an id so we can access it easy in this case:

<form id="mail_form" onsubmit="import_selected_basket()"> ... </form>

Lets add those checkboxes from the first form to the real action form with this javascript function

      function import_selected_basket() {
          var make_to_hidden = $$('input.selectable');
          make_to_hidden.each(function(item) {
              if (item.checked) {
                var new_item = new Element('input', { 'type': 'hidden', name: item.name, value: item.value })
                $('mail_form').appendChild(new_item)
              }
          });

© 2025 Gudasoft

Theme by Anders NorénUp ↑