Permalink
Browse files

把 b-rails-app 搬到這裡面

  • Loading branch information...
1 parent ad5d093 commit 7e361a485f3e9ce3db7fc0eafc0860f6492d21ca @xdite committed Oct 22, 2011
View
2 Gemfile
@@ -2,3 +2,5 @@ source 'http://rubygems.org'
# Specify your gem's dependencies in bootstrap-rails.gemspec
gemspec
+
+gem "simple_form", :require => "simple_form"
View
2 bootstrap-rails.gemspec
@@ -12,7 +12,7 @@ Gem::Specification.new do |gem|
gem.files = `git ls-files`.split("\n")
gem.files.reject! { |fn| fn.include? "vendor/twitter" }
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
- gem.name = "anjlab-bootstrap-rails"
+ gem.name = "bootstrap-rails"
gem.require_paths = ["lib"]
gem.version = Bootstrap::Rails::VERSION
View
64 lib/bootstrap-rails/breadcrumb.rb
@@ -0,0 +1,64 @@
+module Bootstrap
+ module Breadcrumb
+ def self.included(receiver)
+ receiver.extend ClassMethods
+ receiver.send :include, InstanceMethods
+ receiver.send :helper, Helpers
+ receiver.send :before_filter, :set_breadcrumbs
+ end
+
+ module ClassMethods
+
+ end
+
+ module InstanceMethods
+ protected
+
+ def set_breadcrumbs
+ @breadcrumbs = ["<a href='/'>Home</a>".html_safe]
+ end
+
+ def drop_breadcrumb(title=nil, url=nil)
+ title ||= @page_title
+ url ||= url_for
+ if title
+ @breadcrumbs.push("<a href=\"#{url}\">#{title}</a> ".html_safe)
+ end
+ end
+
+ def drop_page_title(title)
+ @page_title = title
+ return @page_title
+ end
+
+ def no_breadcrumbs
+ @breadcrumbs = []
+ end
+ end
+
+ module Helpers
+
+ def render_breadcrumb
+ return "" if @breadcrumbs.size <= 1
+ prefix = "".html_safe
+ crumb = "".html_safe
+
+ @breadcrumbs.each_with_index do |c, i|
+ breadcrumb_class = []
+ breadcrumb_class << "first" if i == 0
+ breadcrumb_class << "last" if i == (@breadcrumbs.length - 1)
+
+ if i == (@breadcrumbs.length - 1)
+ breadcrumb_content = c
+ else
+ breadcrumb_content = c + content_tag(:span, "/", :class => "divider")
+ end
+
+ crumb += content_tag(:li, breadcrumb_content ,:class => breadcrumb_class )
+ end
+ return prefix + content_tag(:ul, crumb, :class => "breadcrumb menu clearfix")
+ end
+ end
+ end
+
+end
View
13 lib/bootstrap-rails/engine.rb
@@ -1,6 +1,19 @@
+require "bootstrap-rails/helper"
+require "bootstrap-rails/breadcrumb"
+require "bootstrap-rails/form_inputs/string_input"
+require "bootstrap-rails/form_inputs/text_input"
+require "bootstrap-rails/form_inputs/collection_input"
module Bootstrap
module Rails
class Engine < ::Rails::Engine
+ initializer "bootstrap_helper.view_helpers" do
+ ActionView::Base.send :include, Bootstrap::Helper
+ end
+
+ config.to_prepare do
+ ApplicationController.send :include, Bootstrap::Breadcrumb
+ end
+
end
end
end
View
5 lib/bootstrap-rails/form_inputs/collection_input.rb
@@ -0,0 +1,5 @@
+class CollectionInput < SimpleForm::Inputs::CollectionInput
+ def input
+ "<div class='input'>#{super}</div>".html_safe
+ end
+end
View
5 lib/bootstrap-rails/form_inputs/string_input.rb
@@ -0,0 +1,5 @@
+class StringInput < SimpleForm::Inputs::StringInput
+ def input
+ "<div class='input'>#{super}</div>".html_safe
+ end
+end
View
5 lib/bootstrap-rails/form_inputs/text_input.rb
@@ -0,0 +1,5 @@
+class TextInput < SimpleForm::Inputs::TextInput
+ def input
+ "<div class='input'>#{super}</div>".html_safe
+ end
+end
View
186 lib/bootstrap-rails/helper.rb
@@ -0,0 +1,186 @@
+# coding: utf-8
+module Bootstrap
+
+ class CustomFormBuilder < SimpleForm::FormBuilder
+ def input(attribute_name, options = {}, &block)
+ "<div class='clearfix'>#{super}</div>".html_safe
+ end
+
+ def button(type, *args, &block)
+ options = args.extract_options!
+ options[:class] = "btn primary"
+ args << options
+ "<div class='actions'>
+ #{super}
+ </div>".html_safe
+ end
+ end
+
+ module Helper
+ def yield_or_default(message, default_message = "")
+ message.nil? ? default_message : message
+ end
+
+ def bootstrap_form_for(object, *args, &block)
+ options = args.extract_options!
+ simple_form_for(object, *(args << options.merge(:builder => Bootstrap::CustomFormBuilder)), &block)
+ end
+
+
+ def render_page_title
+ title = @page_title ? "#{SITE_NAME} | #{@page_title}" : SITE_NAME rescue "SITE_NAME"
+ content_tag("title", title, nil, false)
+ end
+
+ def render_body_tag
+ class_attribute = ["#{controller_name}-controller","#{action_name}-action"].join(" ")
+ id_attribute = (@body_id)? " id=\"#{@body_id}-page\"" : ""
+
+ raw(%Q|<!--[if lt IE 7 ]>
+<body class="#{class_attribute} ie6"><![endif]-->
+<!--[if gte IE 7 ]>
+<body class="#{class_attribute} ie"><![endif]-->
+<!--[if !IE]>-->
+<body#{id_attribute} class="#{class_attribute}">
+<!--<![endif]-->|)
+
+ end
+
+ def s(html)
+ sanitize( html, :tags => %w(table thead tbody tr td th ol ul li div span font img sup sub br hr a pre p h1 h2 h3 h4 h5 h6), :attributes => %w(id class style src href size color) )
+ end
+
+ def render_table(rows, renderrers, table_options = {})
+ table_options = {
+ :has_header => true,
+ :has_row_info => false,
+ :caption => "",
+ :id => nil,
+ :class_name => "auto"
+ }.merge(table_options)
+
+ table_tag_options = table_options[:id] ? { :id => table_options[:id], :class => table_options[:class_name] } : { :class => table_options[:class_name] }
+
+ table = TagNode.new('table', table_tag_options)
+
+ if !table_options[:caption].blank?
+ table << caption = TagNode.new(:caption)
+ caption << table_options[:caption]
+ end
+
+ if table_options[:has_header] == true
+ table << thead = TagNode.new(:thead)
+ thead << tr = TagNode.new(:tr, :class => 'odd')
+
+ renderrers.each do |renderrer|
+ tr << th = TagNode.new(:th)
+ th << renderrer[0]
+ end
+ end
+
+ table << tbody = TagNode.new('tbody')
+ row_info = {}
+ row_info[:total] = rows.length
+ rows.each_with_index do |row,i|
+ row_info[:current] = i
+ tbody << tr = TagNode.new('tr', :class => cycle("","odd") )
+ renderrers.each do |renderrer|
+ tr << td = TagNode.new('td')
+
+ if renderrer[1].class == Proc
+ if table_options[:has_row_info] == true
+ td << renderrer[1].call(row, row_info)
+ else
+ td << renderrer[1].call(row)
+ end
+ else
+ td << renderrer[1]
+ end
+ end
+ end
+
+ return table.to_s
+ end
+
+ # .current will be added to current action, but if you want to add .current to another link, you can set @current = ['/other_link']
+ # TODO: hot about render_list( *args )
+ def render_list(list=[], options={})
+ if list.is_a? Hash
+ options = list
+ list = []
+ end
+
+ yield(list) if block_given?
+
+ list_type ||= "ul"
+
+ if options[:type]
+ if ["ul", "dl", "ol"].include?(options[:type])
+ list_type = options[:type]
+ end
+ end
+
+ ul = TagNode.new(list_type, :id => options[:id], :class => options[:class] )
+ ul.addClass("unstyled") if (options[:type] && options[:type] == "unstyled")
+
+ list.each_with_index do |content, i|
+ item_class = []
+ item_class << "first" if i == 0
+ item_class << "last" if i == (list.length - 1)
+
+ item_content = content
+ item_options = {}
+
+ if content.is_a? Array
+ item_content = content[0]
+ item_options = content[1]
+ end
+
+ if item_options[:class]
+ item_class << item_options[:class]
+ end
+
+ link = item_content.match(/href=(["'])(.*?)(\1)/)[2] rescue nil
+
+ if ( link && current_page?(link) ) || ( @current && @current.include?(link) )
+ item_class << "active"
+ end
+
+ item_class = (item_class.empty?)? nil : item_class.join(" ")
+ ul << li = TagNode.new('li', :class => item_class )
+ li << item_content
+ end
+
+ return ul.to_s
+ end
+
+ # Composite pattern
+ class TagNode
+ include ActionView::Helpers::TagHelper
+
+ def initialize(name, options = {})
+ @name = name.to_s
+ @attributes = options
+ @children = []
+ end
+
+ def addClass(x)
+ if @attributes[:class].blank?
+ @attributes[:class] = x.to_s
+ else
+ @attributes[:class] = @attributes[:class] + " #{x}"
+ end
+ end
+
+ def to_s
+ value = @children.each { |c| c.to_s }.join
+ content_tag(@name, value.to_s, @attributes, false)
+ end
+
+ def <<(tag_node)
+ @children << tag_node
+ end
+ end
+ end
+
+end
View
3 lib/bootstrap-rails/railtie.rb
@@ -1,5 +1,6 @@
module Bootstrap
module Rails
- class Railtie < ::Rails::Railtie; end
+ class Railtie < ::Rails::Railtie
+ end
end
end
View
6 lib/bootstrap/rails.rb
@@ -0,0 +1,6 @@
+require "bootstrap-rails/version"
+module Bootstrap
+ module Rails
+ require "bootstrap-rails/engine"
+ end
+end

0 comments on commit 7e361a4

Please sign in to comment.