2014年4月23日 星期三

t.belongs_to in migration


reference http://stackoverflow.com/questions/9471091/t-belongs-to-in-migration

auto create  *_id , *_type
       # Adds a reference. Optionally adds a +type+ column, if <tt>:polymorphic</tt> option is provided.
      # <tt>references</tt> and <tt>belongs_to</tt> are acceptable.
      # ===== Examples
      # t.references(:goat)
      # t.references(:goat, :polymorphic => true)
      # t.belongs_to(:goat)
      def references(*args)
        options = args.extract_options!
        polymorphic = options.delete(:polymorphic)
        args.each do |col|
          @base.add_column(@table_name, "#{col}_id", :integer, options)
          @base.add_column(@table_name, "#{col}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : options) unless polymorphic.nil?
        end
      end
      alias :belongs_to :references

Polymorphic Associations

Polymorphic Association Diagram

rails data-method changed by javascript

reference http://stackoverflow.com/questions/9935956/changing-data-method-with-javascript-does-not-change-what-method-the-ajax-calls

$(this).data('method', 'post'); # sets "data-method" attribute to "post"
$(this).data('method', 'delete'); # sets "data-method" attribute to "delete"
For example, given the following HTML:
<div data-role="page" data-last-value="43" data-hidden="true" data-options='{"name":"John"}'></div>

All of the following jQuery code will work.
$( "div" ).data( "role" ) === "page";
$( "div" ).data( "lastValue" ) === 43;
$( "div" ).data( "hidden" ) === true;
$( "div" ).data( "options" ).name === "John";





2014年4月1日 星期二

Page Caching - rails 3

Source : http://railscasts.com/episodes/89-page-caching-revised?view=asciicast

Page caching is one of the most efficient forms of caching  in Rails app

1.
enable perform_caching

development.rb
config.action_controller.perform_caching = true

2.
add page caching to the actions we want to cache

class ProductsController < ApplicationController
  
  caches_page :index, :show
  
  def index
    @products = Product.page(params[:page]).per_page(5)
  end
  def show
    @product = Product.find(params[:id])
  end
.....
3.
then , refresh the page , we will find in the folder "public" , it auto create a file named "product.html" contains the full response for the /products page

4.
one issue we will have is that any URL parameters we pass are ignored .
so include the page as part of the URL path instead of as a query parameter

get 'products/page/:page', to: 'products#index'
5.cache expiration
when the content of the cache changes , we will need to flush the cache
    expire_page products_path
    expire_page product_path(product)
    expire_page "/"
    FileUtils.rm_rf "#{page_cache_directory}/products/page"
6.Flash message is stored in the cache , so it's shown everytime
   we need a way to hide these when we're caching the page
  before_filter(only: [:index, :show]) { @page_caching = true }  
<%= csrf_meta_tag unless @page_caching %>
<% unless @page_caching %>
                <% flash.each do |name, msg| %>
            <%= content_tag :div, msg, id: "flash_#{name}" %>
          <% end %>