Skip to content

Commit 185d018

Browse files
author
John Duff
committed
Initial commit
0 parents  commit 185d018

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+6859
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.gem
2+
.bundle
3+
Gemfile.lock
4+
pkg/*

Gemfile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source "http://rubygems.org"
2+
3+
# Specify your gem's dependencies in batman-rails.gemspec
4+
gemspec

README.md

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Batman-Rails
2+
3+
Easily setup and use batman.js (0.6.0) with rails 3.1
4+
5+
## Rails 3.1 setup
6+
This gem requires the use of rails 3.1, coffeescript and the new rails asset pipeline provided by sprockets.
7+
8+
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.
9+
10+
### Installation
11+
12+
In your Gemfile, add this line:
13+
14+
gem "batman-rails"
15+
16+
Then run the following commands:
17+
18+
bundle install
19+
rails g batman:install
20+
21+
### Layout and namespacing
22+
23+
Running `rails g batman:install` will create the following directory structure under `app/assets/javascripts/`:
24+
25+
controllers/
26+
models/
27+
helpers/
28+
29+
It will also create a toplevel app_name.coffee file to setup namespacing and setup initial requires.
30+
31+
## Generators
32+
batman-rails provides 3 simple generators to help get you started using batman.js with rails 3.1.
33+
The generators will only create client side code (javascript).
34+
35+
### Model Generator
36+
37+
rails g batman:model
38+
39+
This generator creates a batman model and collection inside `app/assets/javascript/models` to be used to talk to the rails backend.
40+
41+
### Controllers
42+
43+
rails g batman:controller
44+
45+
This generator creates a batman controller for the given actions provided.
46+
47+
### Scaffolding
48+
49+
rails g batman:scaffold
50+
51+
This generator creates a controller, helper and mode to create a simple crud single page app
52+
53+
## Example Usage
54+
55+
Created a new rails 3.1 application called `blog`.
56+
57+
rails new blog
58+
59+
Edit your Gemfile and add
60+
61+
gem 'batman-rails'
62+
63+
Install the gem and generate scaffolding.
64+
65+
bundle install
66+
rails g batman:install
67+
rails g scaffold Post title:string content:string
68+
rake db:migrate
69+
rails g batman:scaffold Post title:string content:string
70+
71+
You now have installed the batman-rails gem, setup a default directory structure for your frontend batman code.
72+
Then you generated the usual rails server side crud scaffolding and finally generated batman.js code to provide a simple single page crud app.
73+
You have one last step:

Rakefile

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require 'bundler/gem_tasks'

batman-rails.gemspec

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# -*- encoding: utf-8 -*-
2+
$:.push File.expand_path("../lib", __FILE__)
3+
require "batman/rails/version"
4+
5+
Gem::Specification.new do |s|
6+
s.name = "batman-rails"
7+
s.version = Batman::Rails::VERSION
8+
s.authors = ["John Duff"]
9+
s.email = ["[email protected]"]
10+
s.homepage = ""
11+
s.summary = %q{}
12+
s.description = %q{}
13+
14+
s.rubyforge_project = "batman-rails"
15+
16+
s.add_dependency "railties", "~> 3.1.0"
17+
s.add_dependency "thor", "~> 0.14"
18+
s.add_development_dependency "bundler", "~> 1.0.0"
19+
s.add_development_dependency "rails", "~> 3.1.0"
20+
s.add_development_dependency "mocha"
21+
22+
s.files = `git ls-files`.split("\n")
23+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
24+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
25+
s.require_paths = ["lib"]
26+
end

lib/batman-rails.rb

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require "batman/rails"

lib/batman/rails.rb

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require "batman/rails/version"
2+
require "batman/rails/engine"
3+
module Batman
4+
module Rails
5+
end
6+
end

lib/batman/rails/engine.rb

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Batman
2+
module Rails
3+
class Engine < ::Rails::Engine
4+
end
5+
end
6+
end

lib/batman/rails/version.rb

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Batman
2+
module Rails
3+
VERSION = "0.0.1"
4+
BATMAN_VERSION = "0.6.0"
5+
end
6+
end

lib/generators/batman/common.rb

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
module Batman
2+
module Generators
3+
module Common
4+
def self.included(base)
5+
base.send(:extend, ClassMethods)
6+
base.source_root File.expand_path("../templates", __FILE__)
7+
end
8+
9+
protected
10+
def with_app_name
11+
raise "Batman application name must be given" unless app_name
12+
yield
13+
end
14+
15+
def js_app_name
16+
app_name.camelize
17+
end
18+
19+
def app_name
20+
@app_name ||= options[:app_name] || application_name
21+
end
22+
23+
def application_name
24+
if defined?(::Rails) && ::Rails.application
25+
::Rails.application.class.name.split('::').first.underscore
26+
end
27+
end
28+
29+
def js_path
30+
"app/assets/javascripts"
31+
end
32+
33+
def singular_model_name
34+
singular_name.camelize
35+
end
36+
37+
module ClassMethods
38+
def requires_app_name
39+
class_option :app_name, :type => :string, :optional => true,
40+
:desc => "Name of the Batman app (defaults to the Rails app name"
41+
end
42+
end
43+
end
44+
end
45+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
require 'generators/batman/common'
2+
module Batman
3+
module Generators
4+
class ControllerGenerator < ::Rails::Generators::NamedBase
5+
include Common
6+
requires_app_name
7+
8+
desc "This generator creates a Batman controller"
9+
argument :actions, :type => :array, :default => [], :banner => "action action"
10+
11+
12+
RESERVED_JS_WORDS = %w{
13+
break case catch continue debugger default delete do else finally for
14+
function if in instanceof new return switch this throw try typeof var void while with
15+
}
16+
17+
def validate_no_reserved_words
18+
actions.each do |action|
19+
if RESERVED_JS_WORDS.include? action
20+
raise Thor::Error, "The name '#{action}' is reserved by javascript " <<
21+
"Please choose an alternative action name and run this generator again."
22+
end
23+
end
24+
end
25+
26+
def create_batman_controller
27+
with_app_name do
28+
template "controller.coffee", "#{js_path}/controllers/#{plural_name.downcase}_controller.js.coffee"
29+
end
30+
end
31+
end
32+
end
33+
end
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require 'generators/batman/common'
2+
module Batman
3+
module Generators
4+
class HelperGenerator < ::Rails::Generators::NamedBase
5+
include Common
6+
7+
desc "This generator creates a Batman helper"
8+
9+
def create_batman_helper
10+
template "helper.coffee", "#{js_path}/helpers/#{plural_name.downcase}_helper.js.coffee"
11+
end
12+
end
13+
end
14+
end
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
require 'generators/batman/common'
2+
module Batman
3+
module Generators
4+
class InstallGenerator < ::Rails::Generators::Base
5+
include Common
6+
requires_app_name
7+
8+
desc "This generator installs Batman.js with a default folder layout"
9+
10+
class_option :skip_git, :type => :boolean, :aliases => "-G", :default => false,
11+
:desc => "Skip Git ignores and keeps"
12+
13+
def create_batman_app
14+
with_app_name do
15+
template "batman_app.coffee", "#{js_path}/#{app_name}.js.coffee"
16+
end
17+
end
18+
19+
def create_directories
20+
%w(models controllers helpers).each do |dir|
21+
empty_directory "#{js_path}/#{dir}"
22+
create_file "#{js_path}/#{dir}/.gitkeep" unless options[:skip_git]
23+
end
24+
end
25+
26+
def inject_batman
27+
with_app_name do
28+
inject_into_file "#{js_path}/application.js", :after=>/\/\/=(?!.*\/\/=).*?$/m do
29+
<<-CODE
30+
\n\n//= require batman/batman
31+
//= require batman/batman.jquery
32+
//= require batman/batman.rails
33+
34+
//= require #{app_name}
35+
36+
//= require_tree ./models
37+
//= require_tree ./controllers
38+
//= require_tree ./helpers
39+
40+
$(document).ready(function(){
41+
#{js_app_name}.run();
42+
});
43+
CODE
44+
end
45+
end
46+
end
47+
end
48+
end
49+
end
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require 'generators/batman/common'
2+
module Batman
3+
module Generators
4+
class ModelGenerator < ::Rails::Generators::NamedBase
5+
include Common
6+
requires_app_name
7+
8+
desc "This generator creates a Batman model"
9+
argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
10+
11+
def create_batman_model
12+
with_app_name do
13+
template "model.coffee", "#{js_path}/models/#{file_name.downcase}.js.coffee"
14+
end
15+
end
16+
17+
protected
18+
def render_attribute(attribute)
19+
type = case attribute.type.to_s
20+
when 'date', 'datetime'
21+
"Batman.Encoders.railsDate"
22+
when 'string', 'integer', 'float', 'decimal', 'boolean', 'text'
23+
end
24+
25+
["'#{attribute.name}'", type].compact.join(', ')
26+
end
27+
end
28+
end
29+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require 'generators/batman/common'
2+
module Batman
3+
module Generators
4+
class ScaffoldGenerator < ::Rails::Generators::NamedBase
5+
include Common
6+
requires_app_name
7+
8+
desc "This generator creates the client side CRUD scaffolding"
9+
10+
def create_batman_model
11+
with_app_name do
12+
generate "batman:model #{singular_model_name} --app_name #{app_name}"
13+
generate "batman:controller #{singular_model_name} index show create update destroy --app_name #{app_name}"
14+
generate "batman:helper #{singular_model_name}"
15+
end
16+
end
17+
end
18+
end
19+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
window.<%= js_app_name %> = class <%= js_app_name %> extends Batman.App
2+
3+
# @root 'controller#all'
4+
# @route '/controller/:id', 'controller#show', resource: 'model', action: 'show'
5+
6+
@run ->
7+
console.log "Running..."
8+
true
9+
10+
@ready ->
11+
console.log "<%= js_app_name %> ready for use."
12+
13+
@flash: Batman()
14+
@flash.accessor
15+
get: (key) -> @[key]
16+
set: (key, value) ->
17+
@[key] = value
18+
if value isnt ''
19+
setTimeout =>
20+
@set(key, '')
21+
, 2000
22+
value
23+
24+
@flashSuccess: (message) -> @set 'flash.success', message
25+
@flashError: (message) -> @set 'flash.error', message
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class <%= js_app_name %>.<%= plural_name.camelize %>Controller extends Batman.Controller
2+
<% actions.each do |action| -%>
3+
<%= action %>: (params) ->
4+
5+
<% end -%>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# <%= plural_name.camelize %> helper file
2+
3+
# Batman.mixin Batman.Filters,
4+
# helper: (input) ->
5+
# return input
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class <%= js_app_name %>.<%= singular_model_name %> extends Batman.Model
2+
@storageKey: '<%= plural_name %>'
3+
@persist Batman.RailsStorage
4+
5+
<% attributes.each do |attribute| -%>
6+
@encode <%= render_attribute(attribute) %>
7+
<% end -%>

0 commit comments

Comments
 (0)