Skip to content

Commit

Permalink
Added all functionality for the gem.
Browse files Browse the repository at this point in the history
  • Loading branch information
tvw committed Feb 28, 2012
1 parent b4fe8ba commit 8008a65
Show file tree
Hide file tree
Showing 18 changed files with 338 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.bundle
.rvmrc
*~

/pkg
4 changes: 2 additions & 2 deletions hsc-twitter-bootstrap-rails.gemspec
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "hsctwitter/bootstrap/rails/version"
require "hsc-twitter-bootstrap/rails/version"

Gem::Specification.new do |s|
s.name = "hsc-twitter-bootstrap-rails"
s.version = HSCTwitter::Bootstrap::Rails::VERSION::STRING
s.version = HSCTwitterBootstrap::Rails::VERSION::STRING
s.platform = Gem::Platform::RUBY
s.authors = ["Thomas Volkmar Worm"]
s.email = ["tvw@s4r.de"]
Expand Down
48 changes: 48 additions & 0 deletions lib/generators/bootstrap/install/install_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'rails/generators'

module Bootstrap
module Generators
class InstallGenerator < ::Rails::Generators::Base

source_root File.expand_path("../templates", __FILE__)
desc "This generator installs Twitter Bootstrap to Asset Pipeline"

def add_assets

if File.exist?('app/assets/javascripts/application.js.coffee')
insert_into_file "app/assets/javascripts/application.js.coffee", "//= require twitter/bootstrap\n", :after => "jquery_ujs\n"
else
copy_file "application.js.coffee", "app/assets/javascripts/application.js.coffee"
end

if File.exist?('app/assets/stylesheets/application.css.sass')
# Add our own require:
content = File.read("app/assets/stylesheets/application.css.sass")
if content.match(/require_tree\s+\./)
# Good enough - that'll include our bootstrap_and_overrides.css.less
else
style_require_block = " *= require bootstrap_and_overrides\n"
insert_into_file "app/assets/stylesheets/application.css.sass", style_require_block, :after => "require_self\n"
end
else
copy_file "application.css.sass", "app/assets/stylesheets/application.css.sass"
end

end

def cleanup_legacy
# Remove old requires (if any) that included twitter/bootstrap directly:
gsub_file("app/assets/stylesheets/application.css.sass", %r|\s*\*=\s*twitter/bootstrap\s*\n|, "")
gsub_file("app/assets/stylesheets/application.css.sass", %r|\s*\*=\s*twitter/bootstrap_responsive\s*\n|, "")
if File.exist?('app/assets/stylesheets/bootstrap_override.css.sass.less')
puts <<-EOM
Warning:
app/assets/stylesheets/bootstrap_override.css.sass.less exists
It should be removed, as it has been superceded by app/assets/stylesheets/bootstrap_and_overrides.css.sass.less
EOM
end
end

end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import "twitter/bootstrap"
10 changes: 10 additions & 0 deletions lib/generators/bootstrap/install/templates/application.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This is a manifest file that'll be compiled into including all the files listed below.
# Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
# be included in the compiled file accessible from http:##example.com/assets/application.js
# It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
# the compiled file.
#
#= require jquery
#= require jquery_ujs
#= require twitter/bootstrap
#= require_tree .
25 changes: 25 additions & 0 deletions lib/generators/bootstrap/layout/layout_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'rails/generators'

module Bootstrap
module Generators
class LayoutGenerator < ::Rails::Generators::Base
source_root File.expand_path("../templates", __FILE__)
desc "This generator generates layout file with navigation."
argument :layout_name, :type => :string, :default => "application"
argument :layout_type, :type => :string, :default => "fixed",
:banner => "*fixed or fluid"

attr_reader :app_name, :container_class

def generate_layout
app = ::Rails.application
@app_name = app.class.to_s.split("::").first
@container_class = layout_type == "fluid" ? "container-fluid" : "container"
template "layout.html.haml", "app/views/layouts/#{layout_name}.html.haml"
if File.exists?("app/views/layouts/#{layout_name}.html.erb")
puts "Remove app/views/layouts/#{layout_name}.html.erb to make haml-layout work."
end
end
end
end
end
76 changes: 76 additions & 0 deletions lib/generators/bootstrap/layout/templates/layout.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
!!! 5
%html{:lang => "en"}
%head
%meta{:charset => "utf-8"}/
%title= content_for?(:title) ? yield(:title) : "<%= app_name %>"
= csrf_meta_tags
/ Le HTML5 shim, for IE6-8 support of HTML elements
/[if lt IE 9]
= javascript_include_tag "http://html5shim.googlecode.com/svn/trunk/html5.js"
/ Le styles
:css
body {
padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
}
= stylesheet_link_tag "application", :media => "all"
/ Le fav and touch icons
%link{:href => "images/favicon.ico", :rel => "shortcut icon"}/
%link{:href => "images/apple-touch-icon.png", :rel => "apple-touch-icon"}/
%link{:href => "images/apple-touch-icon-72x72.png", :rel => "apple-touch-icon", :sizes => "72x72"}/
%link{:href => "images/apple-touch-icon-114x114.png", :rel => "apple-touch-icon", :sizes => "114x114"}/


%body
.navbar.navbar-fixed-top
.navbar-inner
<%- if layout_type == "fluid" -%>
.container-fluid
<%- else -%>
.container
<%- end -%>
%a.btn.btn-navbar{"data-target" => ".nav-collapse", "data-toggle" => "collapse"}
%span.icon-bar
%span.icon-bar
%span.icon-bar
%a.brand{:href => "#"}<%= app_name %>
.<%=container_class%>.nav-collapse
%ul.nav
%li= link_to "Link 1", "/path1"
%li= link_to "Link 2", "/path2"
%li= link_to "Link 3", "/path3"

.<%= container_class %>
<%- if layout_type == "fluid" -%>

.row-fluid
.span3
.well.sidebar-nav
%ul.nav.nav-list
%li.nav-header Sidebar
%li= link_to "Link 1", "/path1"
%li= link_to "Link 2", "/path2"
%li= link_to "Link 3", "/path3"
.span9
= yield
<% else %>
.content
.row
.span9
= yield
.span1 &nbsp;
.span3
.well.sidebar-nav
%h3 Sidebar
%ul.nav.nav-list
%li.nav-header Sidebar
%li= link_to "Link 1", "/path1"
%li= link_to "Link 2", "/path2"
%li= link_to "Link 3", "/path3"
<% end %>
%footer
%p &copy; Company 2012
/
Le javascript
\==================================================
/ Placed at the end of the document so the pages load faster
= javascript_include_tag "application"
11 changes: 11 additions & 0 deletions lib/generators/bootstrap/themed/templates/_form.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<%- columns.each do |column| -%>
.clearfix
= f.label :<%= column.name %>, t("activerecord.attributes.<%= model_name.underscore %>.<%= column.name %>", :default => "<%= column.name.humanize %>"), :class => :label
.input
= f.<%= column.field_type %> :<%= column.name %>, :class => '<%= column.field_type %>'
<%- end -%>

.form-actions
%button{:class => "btn primary", :type => "submit"} Save
or
= link_to "Cancel", <%= controller_routing_path %>_path
3 changes: 3 additions & 0 deletions lib/generators/bootstrap/themed/templates/edit.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
= form_for @<%= model_name.underscore %>, :url => <%= singular_controller_routing_path %>_path(@<%= resource_name %>), :html => { :class => "edit_<%= model_name.underscore %>", :id => "edit_<%= model_name.underscore %>" } do |f|
%input{:name => "_method", :type => "hidden", :value =>"put"}
= render :partial => "form", :locals => {:f => f}
25 changes: 25 additions & 0 deletions lib/generators/bootstrap/themed/templates/index.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
%h1 <%= resource_name.titleize %>s
%table{:class => "table table-striped"}
%thead
%tr
%th ID
<%- unless columns.empty? -%>
%th
= t("activerecord.attributes.<%= singular_controller_routing_path %>.<%= columns.first.name %>", :default => t("activerecord.labels.<%= columns.first.name %>", :default => "<%= columns.first.name.capitalize %>"))
<%- end -%>
%th Created at
%th Actions
%tbody
- @<%= plural_resource_name %>.each do |<%= resource_name %>|
%tr
%td= <%= resource_name %>.id
<%- unless columns.empty? -%>
%td= link_to <%= resource_name %>.<%= columns.first.name %>, <%= singular_controller_routing_path %>_path(<%= resource_name %>)
<%- end -%>
%td= <%= resource_name %>.created_at
%td
= link_to "Show", <%= singular_controller_routing_path %>_path(<%= resource_name %>)
= link_to "Edit", edit_<%= singular_controller_routing_path %>_path(<%= resource_name %>)
= link_to "Destroy", <%= singular_controller_routing_path %>_path(<%= resource_name %>), :method => :delete, :confirm => "#{t("web-app-theme.confirm", :default => "Are you sure?")}"

= link_to "New", new_<%= singular_controller_routing_path %>_path, :class => 'btn btn-primary'
2 changes: 2 additions & 0 deletions lib/generators/bootstrap/themed/templates/new.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
= form_for @<%= model_name.underscore %>, :url => <%= controller_routing_path %>_path, :html => { :class => :form } do |f|
= render :partial => "form", :locals => {:f => f}
9 changes: 9 additions & 0 deletions lib/generators/bootstrap/themed/templates/show.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<%- columns.each do |column| -%>
%label{:class => "label"}= t("activerecord.attributes.<%= singular_controller_routing_path %>.<%= column.name %>", :default => t("activerecord.labels.<%= column.name %>", :default => "<%= column.name.humanize %>")) + ":"
%p= @<%= resource_name %>.<%= column.name %>
<%- end -%>

.form-actions
= link_to "Back", <%= controller_routing_path %>_path, :class => 'btn'
= link_to "Edit", edit_<%= singular_controller_routing_path %>_path(@<%= resource_name %>), :class => 'btn'
= link_to "Delete", <%= singular_controller_routing_path %>_path(@<%= resource_name %>), :method => "delete", :confirm => "#{t("web-app-theme.confirm", :default => "Are you sure?")}", :class => 'btn'
96 changes: 96 additions & 0 deletions lib/generators/bootstrap/themed/themed_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
require 'rails/generators'
require 'rails/generators/generated_attribute'

module Bootstrap
module Generators
class ThemedGenerator < ::Rails::Generators::Base
source_root File.expand_path('../templates', __FILE__)
argument :controller_path, :type => :string
argument :model_name, :type => :string, :required => false
argument :layout, :type => :string, :default => "application",
:banner => "Specify application layout"

def initialize(args, *options)
super(args, *options)
initialize_views_variables
end

def copy_views
generate_views
end

protected

def initialize_views_variables
@base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(controller_path)
@controller_routing_path = @controller_file_path.gsub(/\//, '_')
@model_name = @base_name.singularize unless @model_name
@model_name = @model_name.camelize
end

def controller_routing_path
@controller_routing_path
end

def singular_controller_routing_path
@controller_routing_path.singularize
end

def model_name
@model_name
end

def plural_model_name
@model_name.pluralize
end

def resource_name
@model_name.underscore
end

def plural_resource_name
resource_name.pluralize
end

def columns
begin
excluded_column_names = %w[id created_at updated_at]
Kernel.const_get(@model_name).columns.reject{|c| excluded_column_names.include?(c.name) }.collect{|c| ::Rails::Generators::GeneratedAttribute.new(c.name, c.type)}
rescue NoMethodError
Kernel.const_get(@model_name).fields.collect{|c| c[1]}.reject{|c| excluded_column_names.include?(c.name) }.collect{|c| ::Rails::Generators::GeneratedAttribute.new(c.name, c.type.to_s)}
end
end

def extract_modules(name)
modules = name.include?('/') ? name.split('/') : name.split('::')
name = modules.pop
path = modules.map { |m| m.underscore }
file_path = (path + [name.underscore]).join('/')
nesting = modules.map { |m| m.camelize }.join('::')
[name, path, file_path, nesting, modules.size]
end

def generate_views
views = {
"index.html.#{ext}" => File.join('app/views', @controller_file_path, "index.html.#{ext}"),
"new.html.#{ext}" => File.join('app/views', @controller_file_path, "new.html.#{ext}"),
"edit.html.#{ext}" => File.join('app/views', @controller_file_path, "edit.html.#{ext}"),
"_form.html.#{ext}" => File.join('app/views', @controller_file_path, "_form.html.#{ext}"),
"show.html.#{ext}" => File.join('app/views', @controller_file_path, "show.html.#{ext}")}
selected_views = views
options.engine == generate_erb(selected_views)
end

def generate_erb(views)
views.each do |template_name, output_path|
template template_name, output_path
end
end

def ext
::Rails.application.config.generators.options[:rails][:template_engine] || :erb
end

end
end
end
2 changes: 2 additions & 0 deletions lib/hsc-twitter-bootstrap-rails.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require "sass-twitter-bootstrap-rails"
require "hsc-twitter-bootstrap/rails"
6 changes: 6 additions & 0 deletions lib/hsc-twitter-bootstrap/rails.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module HSCTwitterBootstrap
module Rails
require 'hsc-twitter-bootstrap/rails/version'
require 'hsc-twitter-bootstrap/rails/engine'
end
end
6 changes: 6 additions & 0 deletions lib/hsc-twitter-bootstrap/rails/engine.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module HSCTwitterBootstrap
module Rails
class Engine < ::Rails::Engine
end
end
end
14 changes: 14 additions & 0 deletions lib/hsc-twitter-bootstrap/rails/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module HSCTwitterBootstrap
module Rails

module VERSION #:nodoc:
MAJOR = 0
MINOR = 1
TINY = 0

STRING = [MAJOR, MINOR, TINY].join('.')
end

end
end

15 changes: 0 additions & 15 deletions lib/hsctwitter/bootstrap/rails/version.rb

This file was deleted.

0 comments on commit 8008a65

Please sign in to comment.