Railsコードフラグメント


1Debugging Views in Development
rubyコード
  
  
  
    "debug" style="margin: 40px 5px 5px 5px;">   
        "#" onclick="Element.toggle('debug_info');return false" style="text-decoration: none; color: #ccc;">Show Debug Info ➲   
        "debug_info" style="display : none;">   
               
               
        
もう1つの関連プラグイン:TextmateFootnotesPlugin
rubyコード
class << Dispatcher   
  def dispatch(cgi = CGI.new,   
               session_options = ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS)   
    begin  
      request, response =   
        ActionController::CgiRequest.new(cgi, session_options),   
        ActionController::CgiResponse.new(cgi)   
      prepare_application   
      ActionController::Routing::Routes.recognize!(request).process(request, response).out   
    rescue Object => exception   
      begin  
        ActionController::Base.process_with_exception(request, response, exception).out   
      rescue  
        # The rescue action above failed also, probably for the same reason   
        # the original action failed.  Do something simple which is unlikely   
        # to fail.  You might want to redirect to a static page instead of this.   
        e = exception   
        cgi.header("type" => "text/html")   
        cgi.out('cookie' => '') do  
          <<-RESPONSE   
           
             
             
             Application Error   
            
 
#{e.class}: #{e.message}  
  
            
 
#{e.backtrace.join("")}              
          RESPONSE   
        end  
      end  
    ensure  
      reset_application   
    end  
  end  
end  

  3 SQL Logging in Rails
rubyコード
SQL_LOGGING = true  
SQL_LOG_MAX_LINES = 5000   
SQL_LOG_FILE = File::join(RAILS_ROOT, '/log/sql_log.txt')   
# $sql_log is a global var that will hold the results of the last sql statement executed   
$sql_log = ""  
# permit logging if we are in development environment   
if RAILS_ENV_DEV || RAILS_ENV_TEST   
  connection = ActiveRecord::Base.connection   
  class << connection   
    alias :original_exec :execute  
    def execute(sql, *name)   
      # try to log sql command but ignore any errors that occur in this block   
      # we log before executing, in case the execution raises an error   
      begin  
        if SQL_LOGGING   
          if File::exists?(SQL_LOG_FILE) : lines = IO::readlines(SQL_LOG_FILE)   
          else lines = Array.new(); end  
          log = File.new(SQL_LOG_FILE, "w+")   
          # keep the log to specified max lines   
          if lines.length > SQL_LOG_MAX_LINES   
            lines.slice!(0..(lines.length-SQL_LOG_MAX_LINES))   
          end  
          lines << Time.now.strftime("%x %I:%M:%S %p")+": "+sql+"n"  
          log.write(lines)   
          log.close   
          $sql_log = sql   
        end # if   
      rescue Exception => e   
        ;   
      end  
      # execute original statement   
      original_exec(sql, *name)   
    end # def execute   
  end # class <<   
end # if RAILS_ENV_DEV   
  
Here's the constant setting code (put in application.rb or something it requires before the code above gets run):   
  
  
class CoreERR_RailsEnvironment < StandardError; end  
#RAILS specific constants   
  #setup global constants for manipulating states   
  RAILS_ENV_VAR = "RAILS_ENV"  
  #set ENV to development if explicit or not present   
  RAILS_ENV_DEV = (ENV[RAILS_ENV_VAR]=="development" || (not ENV[RAILS_ENV_VAR]))   
  RAILS_ENV_LIVETEST = ENV[RAILS_ENV_VAR]=="livetest"  
  RAILS_ENV_TEST = ENV[RAILS_ENV_VAR]=="test"  
  if (RAILS_ENV_DEV) or (RAILS_ENV_TEST)   
    RAILS_ENV_PRODUCTION = false  
  else  
    RAILS_ENV_PRODUCTION = true  
  end  
  # check positively: if production environment is implied because test and development are not found   
  # but production doesn't show up positively, raise exception   
  if RAILS_ENV_PRODUCTION and (RAILS_ENV_PRODUCTION != (ENV[RAILS_ENV_VAR]=="production"))   
    raise CoreERR_RailsEnvironment, "Production environment implied but not detected: "+ENV[RAILS_ENV_VAR]   
  end  
  RAILS_DB_SU = ENV[RAILS_ENV_VAR]+'_su'   
  RAILS_ENV_DEV.freeze   
  RAILS_ENV_TEST.freeze   
  RAILS_ENV_PRODUCTION.freeze   
 
3 Digest MD5 & SHA1
DigestはMD 5とSHA 1の2種類の符号化を支援し、パスワードを格納する必要があれば使用し、一般的にSHA 1を使用します.
  • MD 5計算
    
    require 'digest/md5'
    
    puts Digest::MD5.hexdigest("Hello World!")
    
  • は、アーカイブのMD 5を計算し、アーカイブが
    
    require 'digest/md5'
    
    #method 1
    puts Digest::MD5.hexdigest(File.read("o.rb"))
    
    #method 2
    class Digest::MD5
      def self.open(path)
        o = new
        File.open(path) { |f|
          buf = "" 
          while f.read(256, buf)
            o << buf
          end
        }
        o
      end
    end
    puts Digest::MD5.open("o.rb").hexdigest
    
  • に変更されていないことを確認することができる.
  • SHA 1計算
    
    require 'digest/sha1'
    
    puts Digest::SHA1.hexdigest("Hello World!")