Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
John Duff committed Oct 11, 2011
0 parents commit 185d018
Show file tree
Hide file tree
Showing 77 changed files with 6,859 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.gem
.bundle
Gemfile.lock
pkg/*
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source "http://rubygems.org"

# Specify your gem's dependencies in batman-rails.gemspec
gemspec
73 changes: 73 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Batman-Rails

Easily setup and use batman.js (0.6.0) with rails 3.1

## Rails 3.1 setup
This gem requires the use of rails 3.1, coffeescript and the new rails asset pipeline provided by sprockets.

This gem vendors the latest version of batman.js for Rails 3.1 and greater. The files will be added to the asset pipeline and available for you to use.

### Installation

In your Gemfile, add this line:

gem "batman-rails"

Then run the following commands:

bundle install
rails g batman:install

### Layout and namespacing

Running `rails g batman:install` will create the following directory structure under `app/assets/javascripts/`:

controllers/
models/
helpers/

It will also create a toplevel app_name.coffee file to setup namespacing and setup initial requires.

## Generators
batman-rails provides 3 simple generators to help get you started using batman.js with rails 3.1.
The generators will only create client side code (javascript).

### Model Generator

rails g batman:model

This generator creates a batman model and collection inside `app/assets/javascript/models` to be used to talk to the rails backend.

### Controllers

rails g batman:controller

This generator creates a batman controller for the given actions provided.

### Scaffolding

rails g batman:scaffold

This generator creates a controller, helper and mode to create a simple crud single page app

## Example Usage

Created a new rails 3.1 application called `blog`.

rails new blog

Edit your Gemfile and add

gem 'batman-rails'

Install the gem and generate scaffolding.

bundle install
rails g batman:install
rails g scaffold Post title:string content:string
rake db:migrate
rails g batman:scaffold Post title:string content:string

You now have installed the batman-rails gem, setup a default directory structure for your frontend batman code.
Then you generated the usual rails server side crud scaffolding and finally generated batman.js code to provide a simple single page crud app.
You have one last step:
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'bundler/gem_tasks'
26 changes: 26 additions & 0 deletions batman-rails.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "batman/rails/version"

Gem::Specification.new do |s|
s.name = "batman-rails"
s.version = Batman::Rails::VERSION
s.authors = ["John Duff"]
s.email = ["[email protected]"]
s.homepage = ""
s.summary = %q{}
s.description = %q{}

s.rubyforge_project = "batman-rails"

s.add_dependency "railties", "~> 3.1.0"
s.add_dependency "thor", "~> 0.14"
s.add_development_dependency "bundler", "~> 1.0.0"
s.add_development_dependency "rails", "~> 3.1.0"
s.add_development_dependency "mocha"

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
end
1 change: 1 addition & 0 deletions lib/batman-rails.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require "batman/rails"
6 changes: 6 additions & 0 deletions lib/batman/rails.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require "batman/rails/version"
require "batman/rails/engine"
module Batman
module Rails
end
end
6 changes: 6 additions & 0 deletions lib/batman/rails/engine.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Batman
module Rails
class Engine < ::Rails::Engine
end
end
end
6 changes: 6 additions & 0 deletions lib/batman/rails/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Batman
module Rails
VERSION = "0.0.1"
BATMAN_VERSION = "0.6.0"
end
end
45 changes: 45 additions & 0 deletions lib/generators/batman/common.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module Batman
module Generators
module Common
def self.included(base)
base.send(:extend, ClassMethods)
base.source_root File.expand_path("../templates", __FILE__)
end

protected
def with_app_name
raise "Batman application name must be given" unless app_name
yield
end

def js_app_name
app_name.camelize
end

def app_name
@app_name ||= options[:app_name] || application_name
end

def application_name
if defined?(::Rails) && ::Rails.application
::Rails.application.class.name.split('::').first.underscore
end
end

def js_path
"app/assets/javascripts"
end

def singular_model_name
singular_name.camelize
end

module ClassMethods
def requires_app_name
class_option :app_name, :type => :string, :optional => true,
:desc => "Name of the Batman app (defaults to the Rails app name"
end
end
end
end
end
33 changes: 33 additions & 0 deletions lib/generators/batman/controller_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'generators/batman/common'
module Batman
module Generators
class ControllerGenerator < ::Rails::Generators::NamedBase
include Common
requires_app_name

desc "This generator creates a Batman controller"
argument :actions, :type => :array, :default => [], :banner => "action action"


RESERVED_JS_WORDS = %w{
break case catch continue debugger default delete do else finally for
function if in instanceof new return switch this throw try typeof var void while with
}

def validate_no_reserved_words
actions.each do |action|
if RESERVED_JS_WORDS.include? action
raise Thor::Error, "The name '#{action}' is reserved by javascript " <<
"Please choose an alternative action name and run this generator again."
end
end
end

def create_batman_controller
with_app_name do
template "controller.coffee", "#{js_path}/controllers/#{plural_name.downcase}_controller.js.coffee"
end
end
end
end
end
14 changes: 14 additions & 0 deletions lib/generators/batman/helper_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'generators/batman/common'
module Batman
module Generators
class HelperGenerator < ::Rails::Generators::NamedBase
include Common

desc "This generator creates a Batman helper"

def create_batman_helper
template "helper.coffee", "#{js_path}/helpers/#{plural_name.downcase}_helper.js.coffee"
end
end
end
end
49 changes: 49 additions & 0 deletions lib/generators/batman/install_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require 'generators/batman/common'
module Batman
module Generators
class InstallGenerator < ::Rails::Generators::Base
include Common
requires_app_name

desc "This generator installs Batman.js with a default folder layout"

class_option :skip_git, :type => :boolean, :aliases => "-G", :default => false,
:desc => "Skip Git ignores and keeps"

def create_batman_app
with_app_name do
template "batman_app.coffee", "#{js_path}/#{app_name}.js.coffee"
end
end

def create_directories
%w(models controllers helpers).each do |dir|
empty_directory "#{js_path}/#{dir}"
create_file "#{js_path}/#{dir}/.gitkeep" unless options[:skip_git]
end
end

def inject_batman
with_app_name do
inject_into_file "#{js_path}/application.js", :after=>/\/\/=(?!.*\/\/=).*?$/m do
<<-CODE
\n\n//= require batman/batman
//= require batman/batman.jquery
//= require batman/batman.rails
//= require #{app_name}
//= require_tree ./models
//= require_tree ./controllers
//= require_tree ./helpers
$(document).ready(function(){
#{js_app_name}.run();
});
CODE
end
end
end
end
end
end
29 changes: 29 additions & 0 deletions lib/generators/batman/model_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'generators/batman/common'
module Batman
module Generators
class ModelGenerator < ::Rails::Generators::NamedBase
include Common
requires_app_name

desc "This generator creates a Batman model"
argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"

def create_batman_model
with_app_name do
template "model.coffee", "#{js_path}/models/#{file_name.downcase}.js.coffee"
end
end

protected
def render_attribute(attribute)
type = case attribute.type.to_s
when 'date', 'datetime'
"Batman.Encoders.railsDate"
when 'string', 'integer', 'float', 'decimal', 'boolean', 'text'
end

["'#{attribute.name}'", type].compact.join(', ')
end
end
end
end
19 changes: 19 additions & 0 deletions lib/generators/batman/scaffold_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'generators/batman/common'
module Batman
module Generators
class ScaffoldGenerator < ::Rails::Generators::NamedBase
include Common
requires_app_name

desc "This generator creates the client side CRUD scaffolding"

def create_batman_model
with_app_name do
generate "batman:model #{singular_model_name} --app_name #{app_name}"
generate "batman:controller #{singular_model_name} index show create update destroy --app_name #{app_name}"
generate "batman:helper #{singular_model_name}"
end
end
end
end
end
25 changes: 25 additions & 0 deletions lib/generators/batman/templates/batman_app.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
window.<%= js_app_name %> = class <%= js_app_name %> extends Batman.App

# @root 'controller#all'
# @route '/controller/:id', 'controller#show', resource: 'model', action: 'show'

@run ->
console.log "Running..."
true

@ready ->
console.log "<%= js_app_name %> ready for use."

@flash: Batman()
@flash.accessor
get: (key) -> @[key]
set: (key, value) ->
@[key] = value
if value isnt ''
setTimeout =>
@set(key, '')
, 2000
value

@flashSuccess: (message) -> @set 'flash.success', message
@flashError: (message) -> @set 'flash.error', message
5 changes: 5 additions & 0 deletions lib/generators/batman/templates/controller.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class <%= js_app_name %>.<%= plural_name.camelize %>Controller extends Batman.Controller
<% actions.each do |action| -%>
<%= action %>: (params) ->

<% end -%>
5 changes: 5 additions & 0 deletions lib/generators/batman/templates/helper.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# <%= plural_name.camelize %> helper file

# Batman.mixin Batman.Filters,
# helper: (input) ->
# return input
7 changes: 7 additions & 0 deletions lib/generators/batman/templates/model.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class <%= js_app_name %>.<%= singular_model_name %> extends Batman.Model
@storageKey: '<%= plural_name %>'
@persist Batman.RailsStorage

<% attributes.each do |attribute| -%>
@encode <%= render_attribute(attribute) %>
<% end -%>
Loading

0 comments on commit 185d018

Please sign in to comment.