batman-rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (77) hide show
  1. data/.gitignore +5 -0
  2. data/Gemfile +4 -0
  3. data/README.md +73 -0
  4. data/Rakefile +18 -0
  5. data/batman-rails.gemspec +27 -0
  6. data/lib/batman-rails.rb +1 -0
  7. data/lib/batman/rails.rb +6 -0
  8. data/lib/batman/rails/engine.rb +6 -0
  9. data/lib/batman/rails/version.rb +6 -0
  10. data/lib/generators/batman/common.rb +45 -0
  11. data/lib/generators/batman/controller_generator.rb +33 -0
  12. data/lib/generators/batman/helper_generator.rb +14 -0
  13. data/lib/generators/batman/install_generator.rb +58 -0
  14. data/lib/generators/batman/model_generator.rb +29 -0
  15. data/lib/generators/batman/scaffold_generator.rb +19 -0
  16. data/lib/generators/batman/templates/batman_app.coffee +25 -0
  17. data/lib/generators/batman/templates/controller.coffee +5 -0
  18. data/lib/generators/batman/templates/helper.coffee +5 -0
  19. data/lib/generators/batman/templates/model.coffee +7 -0
  20. data/test/controller_generator_test.rb +40 -0
  21. data/test/fixtures/application.js +9 -0
  22. data/test/install_generator_test.rb +82 -0
  23. data/test/model_generator_test.rb +44 -0
  24. data/test/sample/.gitignore +5 -0
  25. data/test/sample/Gemfile +30 -0
  26. data/test/sample/README +261 -0
  27. data/test/sample/Rakefile +7 -0
  28. data/test/sample/app/assets/images/rails.png +0 -0
  29. data/test/sample/app/assets/javascripts/application.js +9 -0
  30. data/test/sample/app/assets/stylesheets/application.css +7 -0
  31. data/test/sample/app/controllers/application_controller.rb +3 -0
  32. data/test/sample/app/helpers/application_helper.rb +2 -0
  33. data/test/sample/app/mailers/.gitkeep +0 -0
  34. data/test/sample/app/models/.gitkeep +0 -0
  35. data/test/sample/app/views/layouts/application.html.erb +14 -0
  36. data/test/sample/config.ru +4 -0
  37. data/test/sample/config/application.rb +48 -0
  38. data/test/sample/config/boot.rb +6 -0
  39. data/test/sample/config/database.yml +25 -0
  40. data/test/sample/config/environment.rb +5 -0
  41. data/test/sample/config/environments/development.rb +30 -0
  42. data/test/sample/config/environments/production.rb +60 -0
  43. data/test/sample/config/environments/test.rb +42 -0
  44. data/test/sample/config/initializers/backtrace_silencers.rb +7 -0
  45. data/test/sample/config/initializers/inflections.rb +10 -0
  46. data/test/sample/config/initializers/mime_types.rb +5 -0
  47. data/test/sample/config/initializers/secret_token.rb +7 -0
  48. data/test/sample/config/initializers/session_store.rb +8 -0
  49. data/test/sample/config/initializers/wrap_parameters.rb +14 -0
  50. data/test/sample/config/locales/en.yml +5 -0
  51. data/test/sample/config/routes.rb +58 -0
  52. data/test/sample/db/seeds.rb +7 -0
  53. data/test/sample/doc/README_FOR_APP +2 -0
  54. data/test/sample/lib/assets/.gitkeep +0 -0
  55. data/test/sample/lib/tasks/.gitkeep +0 -0
  56. data/test/sample/log/.gitkeep +0 -0
  57. data/test/sample/public/404.html +26 -0
  58. data/test/sample/public/422.html +26 -0
  59. data/test/sample/public/500.html +26 -0
  60. data/test/sample/public/favicon.ico +0 -0
  61. data/test/sample/public/index.html +241 -0
  62. data/test/sample/public/robots.txt +5 -0
  63. data/test/sample/script/rails +6 -0
  64. data/test/sample/test/fixtures/.gitkeep +0 -0
  65. data/test/sample/test/functional/.gitkeep +0 -0
  66. data/test/sample/test/integration/.gitkeep +0 -0
  67. data/test/sample/test/performance/browsing_test.rb +12 -0
  68. data/test/sample/test/test_helper.rb +13 -0
  69. data/test/sample/test/unit/.gitkeep +0 -0
  70. data/test/sample/vendor/assets/stylesheets/.gitkeep +0 -0
  71. data/test/sample/vendor/plugins/.gitkeep +0 -0
  72. data/test/test_helper.rb +24 -0
  73. data/vendor/assets/javascripts/batman/batman.jquery.js +72 -0
  74. data/vendor/assets/javascripts/batman/batman.js +5193 -0
  75. data/vendor/assets/javascripts/batman/batman.rails.js +58 -0
  76. data/vendor/assets/javascripts/batman/batman.solo.js +414 -0
  77. metadata +285 -0
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ tmp/**/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in batman-rails.gemspec
4
+ gemspec
@@ -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:
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+
8
+ require 'bundler/gem_tasks'
9
+ require 'rake/testtask'
10
+
11
+ Rake::TestTask.new(:test) do |t|
12
+ t.libs << 'lib'
13
+ t.libs << 'test'
14
+ t.pattern = 'test/*_test.rb'
15
+ t.verbose = false
16
+ end
17
+
18
+ task :default => :test
@@ -0,0 +1,27 @@
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 = ["john.duff@jadedpixel.com"]
10
+ s.homepage = "https://github.com/Shopify/batman-rails"
11
+ s.summary = %q{Use Batman.js with Rails 3.1}
12
+ s.description = %q{Quickly get started with Batman.js in a Rails 3.1 app. Provides generators to get started and bundles in the latest version of Batman.js.}
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
+ s.add_development_dependency "sqlite3"
22
+
23
+ s.files = `git ls-files`.split("\n")
24
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
25
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
26
+ s.require_paths = ["lib"]
27
+ end
@@ -0,0 +1 @@
1
+ require "batman/rails"
@@ -0,0 +1,6 @@
1
+ require "batman/rails/version"
2
+ require "batman/rails/engine"
3
+ module Batman
4
+ module Rails
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Batman
2
+ module Rails
3
+ class Engine < ::Rails::Engine
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Batman
2
+ module Rails
3
+ VERSION = "0.0.1"
4
+ BATMAN_VERSION = "0.7.0"
5
+ end
6
+ end
@@ -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
@@ -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
@@ -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
@@ -0,0 +1,58 @@
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
+ require_tree_pattern = /\/\/=(?!.*\/\/=).*?$/m
29
+
30
+ inject_into_file "#{js_path}/application.js", :before=>require_tree_pattern do
31
+ <<-CODE
32
+ \n// Batman.js and its adapters
33
+ //= require batman/batman
34
+ //= require batman/batman.jquery
35
+ //= require batman/batman.rails
36
+
37
+ //= require #{app_name}
38
+
39
+ //= require_tree ./models
40
+ //= require_tree ./controllers
41
+ //= require_tree ./helpers
42
+ \n
43
+ CODE
44
+ end
45
+
46
+ inject_into_file "#{js_path}/application.js", :after=>require_tree_pattern do
47
+ <<-CODE
48
+ \n// Run the Batman app
49
+ $(document).ready(function(){
50
+ #{js_app_name}.run();
51
+ });
52
+ CODE
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -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
@@ -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
@@ -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
@@ -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 -%>
@@ -0,0 +1,5 @@
1
+ # <%= plural_name.camelize %> helper file
2
+
3
+ # Batman.mixin Batman.Filters,
4
+ # helper: (input) ->
5
+ # return input
@@ -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,0 +1,40 @@
1
+ require 'test_helper'
2
+ require 'generators/batman/controller_generator'
3
+
4
+ class ControllerGeneratorTest < Rails::Generators::TestCase
5
+ tests Batman::Generators::ControllerGenerator
6
+
7
+ test "simple controller" do
8
+ run_generator %w(Task index show)
9
+
10
+ assert_file "#{javascripts_path}/controllers/tasks_controller.js.coffee" do |controller|
11
+ controller_class = Regexp.escape("class Sample.TasksController extends Batman.Controller")
12
+
13
+ assert_match /#{controller_class}/, controller
14
+ assert_match %r{ index: \(params\) ->}, controller
15
+ assert_match %r{ show: \(params\) ->}, controller
16
+ end
17
+ end
18
+
19
+ test "two word controller is camelcased" do
20
+ run_generator %w(RegularUser index)
21
+
22
+ assert_file "#{javascripts_path}/controllers/regular_users_controller.js.coffee" do |controller|
23
+ controller_class = Regexp.escape("class Sample.RegularUsersController extends Batman.Controller")
24
+
25
+ assert_match /#{controller_class}/, controller
26
+ assert_match %r{ index: \(params\) ->}, controller
27
+ end
28
+ end
29
+
30
+ test "simple controller with app_name" do
31
+ run_generator %w(Task index --app_name MyApp)
32
+
33
+ assert_file "#{javascripts_path}/controllers/tasks_controller.js.coffee" do |controller|
34
+ controller_class = Regexp.escape("class MyApp.TasksController extends Batman.Controller")
35
+
36
+ assert_match /#{controller_class}/, controller
37
+ assert_match %r{ index: \(params\) ->}, controller
38
+ end
39
+ end
40
+ end