Skip to content
Browse files

first implement

  • Loading branch information...
1 parent b887cb3 commit fe300b5e625709cf3ef8b521e3b92dc4e07311f6 @xdite committed Dec 14, 2011
View
17 .gitignore
@@ -0,0 +1,17 @@
+*.gem
+*.rbc
+.bundle
+.config
+.yardoc
+Gemfile.lock
+InstalledFiles
+_yardoc
+coverage
+doc/
+lib/bundler/man
+pkg
+rdoc
+spec/reports
+test/tmp
+test/version_tmp
+tmp
View
6 Gemfile
@@ -0,0 +1,6 @@
+source 'http://rubygems.org'
+
+# Specify your gem's dependencies in bootstrap-rails.gemspec
+gemspec
+
+gem "simple_form", :require => "simple_form"
View
0 README
No changes.
121 README.md
@@ -0,0 +1,121 @@
+# Twitter Bootstrap Helper for Rails 3
+
+Bootstrap is a toolkit from Twitter designed to kickstart development of webapps and sites. It includes base CSS and HTML for typography, forms, buttons, tables, grids, navigation, and more.
+
+bootstrap_helper auto generates Bootstrap HTML codes.
+
+## Rails 3.1
+
+include Bootstrap Helper in Gemfile;
+
+``` ruby
+gem 'bootstrap_helper'
+```
+or you can install from latest build;
+
+``` ruby
+gem 'bootstrap_helper', :git => "git://github.com/xdite/bootstrap_helper.git"
+```
+
+## USAGE
+
+
+### render_page_title
+
+#### SETUP
+
+edit your `config/application.rb `
+
+ SITENAME = "YOUR SITE NAME"
+
+in `application.html.erb`, replace `<title>` with
+
+ <%= render_page_title %>
+
+define page title in your action
+
+ def index
+ page_title("Posts Index")
+ end
+
+will render
+
+ <title>Posts Index &raquo; YOUR SITE NAME</title>
+
+### render_list
+
+render_list generates ul & li, auto append: "first", "last" class . If link matches current controller and acttion, it will auto add "active" class. Perfact for "menu"
+
+ <%= render_list :class => "nav" do |li|
+ li << link_to(t("menu.topics"), topics_path)
+ li << link_to(t("menu.wiki"), wikis_path )
+ li << link_to(t("menu.sites"), sites_path )
+ li << link_to(t("menu.users"), users_path)
+ end %>
+
+### render_body_tag
+
+in `application.html.erb`, replace `<body>` with
+
+ <%= render_body_tag %>
+
+render_body_tag auto inserts "controller name" & "action name" in to body class, and generates IE conditional comment.
+
+ <!--[if lt IE 7 ]>
+ <body class="topics-controller index-action ie6"><![endif]-->
+ <!--[if gte IE 7 ]>
+ <body class="topics-controller index-action ie"><![endif]-->
+ <!--[if !IE]>-->
+ <body class="topics-controller index-action">
+ <!--<![endif]-->
+
+### breadcrumb
+
+in `application.html.erb`, place this helper
+
+ <%= render_breadcrumb %>
+
+drop breadcrumb in your action
+
+ def show
+ @post = Posts.find(params[:id])
+ drop_breadcrumb("Posts", posts_path)
+ drop_breadcrumb(@post.title)
+ end
+
+it will generate breadcrumb with link for you
+
+ Home / Post / YourPostTitle
+
+### notice_message
+
+in `application.html.erb`, place this helper
+
+ <%= notice_message %>
+
+write notice message in your action, will generate bootstrap style notice message
+
+ def create
+ # ….
+ redirect_to posts_path, :notice => "Create Success!"
+ end
+
+## Example
+
+see [example](bootstrap_helper/tree/master/example/application.html.erb)
+
+## Thanks
+
+Thanks Twitter for Bootstrap <http://twitter.github.com/bootstrap>
+
+Thanks Handlino for HandicraftHelper <https://github.com/handlino/handicraft_helper>
+
+## License
+
+Copyright (C) 2011 by xdite
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
41 Rakefile
@@ -0,0 +1,41 @@
+#!/usr/bin/env rake
+require "bundler/gem_tasks"
+
+desc "Update Twitter's Bootstrap"
+task "update-twitter" do
+ boostrap_version = "1.3.0"
+ Dir["vendor/assets/stylesheets/*.*"].each {|f| FileUtils.rm(f)}
+ Dir["vendor/twitter/lib/*.scss"].each do |file|
+ cp file, "vendor/assets/stylesheets/", :verbose => true
+ end
+ bootstrap_scss = File.read("vendor/assets/stylesheets/bootstrap.scss")
+
+ bootstrap_scss.gsub!(/@VERSION/, "v#{boostrap_version}")
+ bootstrap_scss.gsub!(/^.*@DATE.*/, " *")
+
+ File.open("vendor/assets/stylesheets/bootstrap.scss", "w") do |f|
+ f.write(bootstrap_scss)
+ end
+
+ Dir["vendor/assets/javascripts/*.*"].each {|f| FileUtils.rm(f)}
+ js_files = Dir["vendor/twitter/js/*.js"].map()
+ js_files.each do |file|
+ cp file, "vendor/assets/javascripts/", :verbose => true
+ end
+
+ js_priorities = {}
+ js_files.each {|f| js_priorities[File.basename(f)] = 0}
+
+ # popover depend on twipsy
+ js_priorities["bootstrap-twipsy.js"] = 1
+ js_priorities["bootstrap-popover.js"] = 2
+
+ js_list = js_priorities.to_a.sort {|a,b| a[1] <=> b[1]}.map{|p| p[0]}
+
+ File.open("vendor/assets/javascripts/bootstrap.js", "w") do |f|
+ f.write "// Bootstrap v#{boostrap_version}\n"
+ js_list.each do |js|
+ f.write "//= require #{js}\n"
+ end
+ end
+end
View
23 bootstrap_helper.gemspec
@@ -0,0 +1,23 @@
+# -*- encoding: utf-8 -*-
+require File.expand_path('../lib/bootstrap_helper/version', __FILE__)
+
+Gem::Specification.new do |gem|
+ gem.authors = ["xdite"]
+ gem.email = ["xuite.joke@gmail.com"]
+ gem.description = %q{Twitter Bootstrap HTML Helper}
+ gem.summary = %q{Twitter Bootstrap HTML Helper}
+ gem.homepage = "https://github.com/xdite/bootstrap_helper"
+
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ gem.files = `git ls-files`.split("\n")
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ gem.name = "bootstrap_helper"
+ gem.require_paths = ["lib"]
+ gem.version = "1.4.0"
+
+ gem.add_dependency "railties", "~> 3.0"
+ gem.add_dependency "thor", "~> 0.14"
+ gem.add_development_dependency "bundler", ">= 1.0.0"
+ gem.add_development_dependency "rails", "~> 3.0"
+end
+
View
47 example/application.html.erb
@@ -0,0 +1,47 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <%= render_page_title %>
+ <%= csrf_meta_tag %>
+ <%= stylesheet_link_tag "application"%>
+</head>
+<%= render_body_tag %>
+ <div class="topbar" data-dropdown="dropdown">
+ <div class="topbar-inner">
+ <div class="container">
+ <h3>
+ <a href="/"><%= Setting.app_name %></a>
+ </h3>
+ <%= render_list :class => "nav" do |li|
+ li << link_to(t("menu.topics"), topics_path)
+ li << link_to(t("menu.wiki"), wikis_path )
+ li << link_to(t("menu.sites"), sites_path )
+ li << link_to(t("menu.users"), users_path)
+ end %>
+ <%= render "common/search_form" %>
+ <%= render "common/user_navigation" %>
+ </div>
+ </div>
+ </div>
+ <div class="container">
+ <%= render_breadcrumb %>
+ <div id="main" class="container-fluid">
+ <%= notice_message %>
+ <%= yield %>
+ </div>
+ </div>
+ <footer class="footer">
+ <div class="container">
+ <%= render :partial => "common/footer" %>
+ </div>
+ </footer>
+
+ <%= javascript_include_tag "application" %>
+
+ <%= yield :page_specific_javascripts %>
+
+ <%= render :partial => "common/facebook_js" %>
+ <%= render :partial => "common/google_analytics"%>
+
+</body>
+</html>
View
11 lib/bootstrap_helper.rb
@@ -0,0 +1,11 @@
+require "bootstrap_helper/version"
+
+module BootstrapHelper
+ module Rails
+ if ::Rails.version < "3.1"
+ require "bootstrap_helper/railtie"
+ else
+ require "bootstrap_helper/engine"
+ end
+ end
+end
View
64 lib/bootstrap_helper/breadcrumb.rb
@@ -0,0 +1,64 @@
+module BootstrapHelper
+ 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 <= 0
+ prefix = "".html_safe
+ crumb = "".html_safe
+
+ @breadcrumbs.each_with_index do |c, i|
+ breadcrumb_class = []
+ breadcrumb_class << "first" if i == 0
+ breadcrumb_class << "last active" 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
16 lib/bootstrap_helper/engine.rb
@@ -0,0 +1,16 @@
+require "bootstrap_helper/helper"
+require "bootstrap_helper/breadcrumb"
+module BootstrapHelper
+ module Rails
+ class Engine < ::Rails::Engine
+ initializer "bootstrap_helper.view_helpers" do
+ ActionView::Base.send :include, BootstrapHelper::Helper
+ end
+
+ config.to_prepare do
+ ApplicationController.send :include, BootstrapHelper::Breadcrumb
+ end
+
+ end
+ end
+end
View
178 lib/bootstrap_helper/helper.rb
@@ -0,0 +1,178 @@
+# coding: utf-8
+module BootstrapHelper
+
+ module Helper
+ def yield_or_default(message, default_message = "")
+ message.nil? ? default_message : message
+ 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 notice_message
+ flash_messages = []
+
+ flash.each do |type, message|
+ type = :success if type == :notice
+ text = content_tag(:div, link_to("x", "#", :class => "close") + content_tag(:p,message), :class => "alert-message #{type}", "data-alert" => "alert")
+ flash_messages << text if message
+ end
+
+ flash_messages.join("\n").html_safe
+ 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
6 lib/bootstrap_helper/railtie.rb
@@ -0,0 +1,6 @@
+module BootstrapHelper
+ module Rails
+ class Railtie < ::Rails::Railtie
+ end
+ end
+end
View
5 lib/bootstrap_helper/version.rb
@@ -0,0 +1,5 @@
+module BootstrapHelper
+ module Rails
+ VERSION = "0.1.4"
+ end
+end

0 comments on commit fe300b5

Please sign in to comment.
Something went wrong with that request. Please try again.