2007-06-30

Rails源码研究之ActionView:二,partials

关键字: Rails ActionView partials 源码
partials是Rails模板重用的一项重要技术,让我们来读读partials.rb源码文件: module ActionView module Partials def render_partial(partial_path, local_assigns = nil, deprecated_local_assigns = nil) path, partial_name = partial_pieces(partial_path) object = extracting_object(partial_name, local_assigns, ...
2007-06-30

Rails源码研究之ActionView:一,基本架构和ERB

关键字: ActionView ERB 源码
先看源码再分析 1,action_view.rb $:.unshift(File.dirname(__FILE__) + "/action_view/vendor") require 'action_view/base' require 'action_view/partials' ActionView::Base.class_eval do include ActionView::Partials end ActionView::Base.load_helpers(File.dirname(__FILE__) + "/action_view/helpers/") ...
2007-06-27

Rails源码研究之ActionController:十,pagination

关键字: ActionController pagination 源码
1,action_controller\pagination.rb: module ActionController module Pagination def paginate(collection_id, options={}) Pagination.validate_options!(collection_id, options, true) paginator_and_collection_for(collection_id, options) end def self.validate_options!(co ...
2007-06-27

Rails源码研究之ActionController:九,mime_responds

关键字: ActionController mime_responds 源码
Rails从HTTP Accept header得到客户端需要的response format信息 默认的MIME types见mime_type.rb: ALL = Type.new "*/*", :all TEXT = Type.new "text/plain", :text HTML = Type.new "text/html", :html, %w( application/xhtml+xml ) JS = Type.new "text/javascript", :js, %w( application/javascript applicatio ...
2007-06-27

Rails源码研究之ActionController:八,resources

关键字: ActionController resources 源码
深入了解一下ActionController的Resources--RESTful Rails 1,ActionController的resources用来实现REST api,一个单独的resource基于HTTP verb(method)有不同的行为(action),如: map.resources :messages class MessagesController < ActionController::Base # GET messages_url def index # return all messages end # GE ...
2007-06-26

Rails源码研究之ActionController:七,filters

关键字: ActionController filters 源码
我们上次看过了ActiveRecord的callbacks,这次看看ActionController的filters 1,filter继承 先执行父类中的filter,再执行子类中的filter,如果父类中的filter返回false,则不执行子类中后续的filter 2,filter类型 1)method reference(symbol) class BankController < ActionController::Base before_filter :audit end 2)external class class OutputCompressionFi ...
2007-06-26

使用Notepad2做Ruby IDE

关键字: notepad2 Ruby IDE
Pablo Hoch给notepad2加上了Ruby/YAML语法高亮,我们可以把它当作windows下的轻量级Ruby IDE来使了 特点: green, fast, free 支持的schemes: HTML/XML//CSS/JavaScript/VBScript/Perl/CGI Script/ActionScript 2.0/C/C++/C#/Resource Script/Makefiles/Java/Visual Basic/Pascal/Assembler/SQL/Python/NSIS Script/Configuration Files/Batch Files/Diff ...
2007-06-26

ActiveRecord的lazy loading与eager loading

关键字: ActiveRecord lazy loading eager loading
看来大家还对ActiveRecord的lazy loading和eager loading不是很清楚 ActiveRecord默认是lazy loading的,而加上:include选项后可以指定eager loading一些字段 :include  - specify second-order associations that should be eager loaded when the collection is loaded. 看例子: Post.find(:all, :include => :comments) # => SELECT ... FROM po ...
2007-06-26

Rails源码研究之ActionController:六,request

关键字: actioncontroller request 源码
看看Rails的request/response源码吧,非常有趣,有些方法非常实用 1,request.rb: module ActionController class AbstractRequest def method @request_method ||= (!parameters[:_method].blank? && @env['REQUEST_METHOD'] == 'POST') ? parameters[:_method].to_s.downcase.to_sym : @env['REQUEST_MET ...
2007-06-25

Rails源码研究之ActionController:五,cookies

关键字: ActionController cookies 源码
cookies.rb: module ActionController module Cookies protected def cookies CookieJar.new(self) end end class CookieJar < Hash def initialize(controller) @controller, @cookies = controller, controller.request.cookies super() update(@ ...
2007-06-25

Rails源码研究之ActionController:四,session

关键字: ActionController session 源码
我们知道Rails默认使用file来存储session数据,放在tmp\sessions目录下 其实我们还可以使用数据库、drb_server、mem_cache甚至内存来存储session数据 方法就是更改environment.rb: config.action_controller.session_store = :active_record_store || :drb_store || :mem_cache_store || :memory_store 当然使用数据库存储session数据时要先创建数据库表 rake db:sessions:create 这在以前的Ra ...
2007-06-25

Rails源码研究之ActionController:三,scaffolding

关键字: ActionController scaffolding 源码
Rails里富有Magic的东西往往实现起来非常简单,比如scaffolding 看看代码先,scaffolding.rb: module ActionController module Scaffolding def self.included(base) base.extend(ClassMethods) end module ClassMethods def scaffold(model_id, options = {}) options.assert_valid_keys(:class ...
2007-06-24

Rails源码研究之ActionController:二,routing

关键字: ActionController routing 源码
满城尽带黄金甲,源码尽在routing.rb: module ActionController module Routing class Route attr_accessor :segments, :requirements, :conditions end class RouteBuilder def build(path, options) path = "/#{path}" unless path[0] == ?/ path = "#{path}/" unless path[ ...
2007-06-24

Rails源码研究之ActionController:一,基本架构、render、redirect

关键字: ActionController render redirect 源码
1,action_controller.rb: $:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__))) unless defined?(ActiveSupport) begin $:.unshift(File.dirname(__FILE__) + "/../../activesupport/lib") require 'active_sup ...
2007-06-22

Rails源码研究之ActiveRecord:六,Acts

关键字: ActiveRecord Acts 源码
ActiveRecord自带了三种数据结构关系:acts_as_tree、acts_as_list、acts_as_nested_set 1,tree.rb module ActiveRecord module Acts module Tree def self.included(base) base.extend(ClassMethods) end module ClassMethods def acts_as_tree(options = {}) configu ...
2007-06-22

Rails源码研究之ActiveRecord:五,Callbacks

关键字: ActiveRecord Callbacks 源码
Callbacks相关的源码在callbacks.rb文件里: module ActiveRecord module Callbacks CALLBACKS = %w( after_find after_initialize before_save after_save before_create after_create before_update after_update before_validation after_validation before_validation_on_create after_validation_on_cre ...
2007-06-21

Rails源码研究之ActiveRecord:四,Validations

关键字: ActiveRecord Validations 源码
Validations相关的源码全在validations.rb文件里: module ActiveRecord class Errors include Enumerable @@default_error_messages = { :inclusion => "is not included in the list", :exclusion => "is reserved", :invalid => "is invalid", :confirmation => "doesn't match confi ...
2007-06-21

Rails源码研究之ActiveRecord:三,Transactions

关键字: ActiveRecord 源码
这次我们分析一下Rails的事务支持 1,Rails默认将父子关系的表的save()和destroy()包装在一个事务里(见AWDWR一书的Transactions) 这保证了父子保存和删除的原子性,即ActiveRecord是级联保存和级联删除的,有源码为证 transactions.rb: module ActiveRecord module Transactions def self.included(base) base.extend(ClassMethods) base.class_eval do [:destroy, ...
2007-06-20

Rails源码研究之ActiveRecord:二,Associations

关键字: ActiveRecord 源码
今天学习一下ActiveRecord的Associations相关的源码,即了解一下我们常用的has_many、has_one、belongs_to、has_and_belongs_to_many的原理 1,activerecord-1.15.3\lib\active_record\associations.rb: require 'active_record/associations/association_proxy' require 'active_record/associations/association_collection' require 'active_recor ...
Rails的ORM框架ActiveRecord是马大叔的ActiveRecord模式的实现+associations+SingleTableInheritance ActiveRecord的作者也是Rails的作者--David Heinemeier Hansson ActiveRecord的key features: 1,零Meta Data,不需要XML配置文件 2,Database Support,现在支持mysql postgresql sqlite firebird sqlserver db2 oracle sybase openbase frontbase,写一个新的databas ...
顺序看了《Agile Web Development With Rails》、《Rails Recipes》和《Ruby for Rails》,我看的都是最新的英文版的,说说对这三本书的感受。 1,《Agile Web Development With Rails》,2ed 以实践为基础,一步步讲解Web程序开发Rails做法,非常简单易懂,对没有编程背景的人来看也不是什么难事,算是本不错的Rails入门书。 但既然作为入门书,只看了本书的朋友们就不要在简历中写什么“熟悉Ruby on Rails”了。 2,《Rails Recipes》 这本书是Rails开发的参考书,一篇一篇讲解Rai ...
2007-06-14

最近的Ruby for Rails读书笔记

关键字: dynamic ruby
1, 用"class <<"定义class method class User < ActiveRecord::Base class << self def authenticate(username, password) find_by_username(username, :conditions => [password_hash = ?", Digest::SHA1.hexdigest(passwword)]) end 2, 用self.included(class)方法作为include模块的hooks modul ...
2007-06-14

Ruby common time and date format specifiers

关键字: time date format
%Y Year(four digits) %y Year(last two digits) %b, %B Short month name, full month name %m Month(number) %d Day of month(left-padded with zeros) %e Day of month(left-padded with blanks) %a, %A Short day name, full day name %H, %I Hour(24-hour clock), hour(12-hour clock) %M Minute %S Second ...
2007-06-14

Ruby methoswith operator-style syntactic sugar calling notat

关键字: operator-style syntactic sugar calling notation
1, + Definition: def +(x) Calling: obj.+(x) Sugared notation: obj + x 2, - Definition: def -(x) Calling: obj.-(x) Sugared notation: obj - x 3, * Definition: def *(x) Calling: obj.*(x) Sugared notation: obj * x 4, / Definition: def /(x) Calling: obj./(x) ...
2007-06-14

Built-in Ruby classes that have literal constructors

关键字: Ruby literal constructors
1, String Quotation marks "new string" or 'new string' 2, Symbol Leading colon :symbol or :"symbol with spaces" 3, Array Square brackets [1, 2, 3, 4, 5] 4, Hash Curly braces {"New York" => "NY", "Oregon" => "OR"} 5, Range Two or three dots 0...10 or 0..9 6, Regexp Forward sl ...
2007-06-14

Ruby common exception

关键字: exception
1, RuntimeError Reason: This is the default exception raised by the raise method raise 2, NoMethodError Reason: An object is sent a message it can't resolve to a method name a = Object.new a.some_unkonw_method_name 3, NameError Reason: The interpreter hits an identifier it can't resolve a ...
2007-06-14

读Ruby for Rails的思考之method_missing

关键字: Rails method_missing
Ruby里方法的查找顺序是本类=>本类include的模块=>父类=>父类include的模块... 而当最终还是没有找到该方法时,会触发内建的method_missing方法的执行 默认的method_missing会触发异常,而我们可以通过override method_missing方法来提供一些特性 比如Rails的ActiveRecord,User.find_by_name()这个方法本来没有在User类里定义,但是可以通过override method_missing来匹配数据库fields 然后使用class_eval和send动态创建和调用查询方法 我们来看看C:\ruby\ ...
2007-06-13

MinGW+Eclipse+CDT搭建Windows下C/C++集成开发环境

关键字: IDE MinGW Eclipse CDT
虽然N年以前就知道有CDT这个东西 但是知道今晚试过CDT才知道安装Visual C++是个sb似的行为 感觉删除你电脑上的VC程序,下载以下三样东西: 1,MinGW 2,Eclipse 3,CDT 这三样东西最大的相同点就是开源免费 MinGW下载MinGW-3.1.0-1.exe文件然后安装即可 Eclipse、CDT的安装就不需多费口舌了 如果CDT安装后没效果,删除D:\eclipse\configuration\org.eclipse.osgi目录下的manifest文件夹、.bundledata和.state文件然后重启Eclipse即可 然后在系统环境变量path中加入Mi ...
  • 02:19
  • 浏览 (2620)
  • 评论 (0)
2007-06-12

每天一剂Rails良药之acts_as_ferret

关键字: rails acts_as_ferret
Ferret是Ruby的文本搜索引擎,它基于Apache Lucene 安装Ferret非常简单: gem install ferret Ferret是一堆C代码的Ruby代码封装,Ferret是针对Ruby的而不是RoR的 而Acts As Ferret则是针对RoR的 我们有两种方式安装Acts As Ferret: 1,以gem方式安装 gem install acts_as_ferret 然后在environment.rb里添加 require 'acts_as_ferret' 2,以plugin方式安装 ruby script/plugin inst ...
2007-06-12

每天一剂Rails良药之upload_progress

关键字: Rails upload_progress
今天来看看使用upload_progress插件监听文件上传status以及创建AJAX上传progress bar Requirements:http://sean.treadway.info/articles/2005/07/18/upload-progress-checklist 安装好该插件以及Requirements后,我们就可以在controller里使用了: class DocumentController < ApplicationController upload_status_for :create def create # ... Y ...
hideto
搜索本博客
我的相册
A6bdc31c-c66e-468e-961e-9cc721e82adc-thumb
screenshot
共 1 张
存档
最新评论