Class: Ferret::Browser::Controller
Constants
| Name | Value |
|---|---|
| APP_DIR | File.expand_path(File.join(File.dirname(__FILE__), "browser/")) |
| STATIC_DIR | File.expand_path(File.join(APP_DIR, "s/")) |
Public Class Methods
new (reader, path, vars)
# File lib/ferret/browser.rb, line 89 89: def initialize(reader, path, vars) 90: @reader = reader 91: @path = path 92: vars.each_pair {|key, val| instance_eval("@#{key} = val")} 93: @controller_path = pathify(self.class.to_s.gsub(/.*:/, '')) 94: end
Public Instance Methods
method_missing (meth_id, *args)
# File lib/ferret/browser.rb, line 96 96: def method_missing(meth_id, *args) 97: render(:action => meth_id) 98: end
Protected Instance Methods
load_page (page)
# File lib/ferret/browser.rb, line 102 102: def load_page(page) 103: File.read(File.join(APP_DIR, page)) 104: end
paginate (idx, max, url, &b)
takes an optional block to set optional attributes in the links
# File lib/ferret/browser.rb, line 126 126: def paginate(idx, max, url, &b) 127: return '' if max == 0 128: url = url.gsub(%r{^/?(.*?)/?$}, '\1') 129: b ||= lambda{} 130: link = lambda {|*args| 131: i, title, text = args 132: "<a href=\"/#{url}/#{i}#{'?' + @query_string if @query_string}\" " + 133: "#{'onclick="return false;"' if (i == idx)} " + 134: "class=\"#{'disabled ' if (i == idx)}#{b.call(i)}\" " + 135: "title=\"#{title||"Go to page #{i}"}\">#{text||i}</a>" 136: } 137: res = '<div class="nav">' 138: if (idx > 0) 139: res << link.call(idx - 1, "Go to previous page", "« Previous") 140: else 141: res << "<a href=\"/#{url}/0\" onclick=\"return false;\" " + 142: "class=\"disabled\" title=\"Disabled\">« Previous</a>" 143: end 144: if idx < 10 145: idx.times {|i| res << link.call(i)} 146: else 147: (0..2).each {|i| res << link.call(i)} 148: res << ' … ' 149: ((idx-4)...idx).each {|i| res << link.call(i)} 150: end 151: res << link.call(idx, 'Current Page') 152: if idx > (max - 10) 153: ((idx+1)...max).each {|i| res << link.call(i)} 154: else 155: ((idx+1)..(idx+4)).each {|i| res << link.call(i)} 156: res << ' … ' 157: ((max-3)...max).each {|i| res << link.call(i)} 158: end 159: if (idx < (max - 1)) 160: res << link.call(idx + 1, "Go to next page", "Next »") 161: else 162: res << "<a href=\"/#{url}/#{max-1}\" onclick=\"return false;\" " + 163: "class=\"disabled\" title=\"Disabled\"}\">Next »</a>" 164: end 165: res << '</div>' 166: end
render (options = {})
# File lib/ferret/browser.rb, line 106 106: def render(options = {}) 107: options = { 108: :controller => @controller_path, 109: :action => :index, 110: :status => 200, 111: :content_type => 'text/html', 112: :env => nil, 113: :layout => 'views/layout.rhtml', 114: }.update(options) 115: 116: path = "views/#{options[:controller]}/#{options[:action]}.rhtml" 117: content = ERB.new(load_page(path)).result(lambda{}) 118: if options[:layout] 119: content = ERB.new(load_page(options[:layout])).result(lambda{}) 120: end 121: 122: return options[:status], options[:content_type], content 123: end