Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Update batman-rails so we can help people get started with Batman [WIP] #39

Merged
merged 30 commits into from

6 participants

@matthelm
Collaborator

Batman.js

A client-side framework for Rails developers.

Batman.js is a framework for building rich web applications with CoffeeScript. App code is concise and favors convention over configuration, and packs a powerful system of view bindings and observable properties. The API is designed with developer and designer happiness as its first priority, and while it has no real dependencies, it works quite well with Rails.

The goal of this Pull Request is to make it easy for Rails developers to use Batman by building conventions.

I'm working on updating the batman-rails gem so that we can provide an easy install path for Rails developers. @nickjs @craigdavey and I have talked through how we want to layout the default Batman app when using Rails. I've compleatly rewritten the gem but used a lot of the existing tasks that @jduff had written. Everything has been revisited. In doing so @nickjs, @kristianpd and I are leaning towards avoiding working on the test suite such that I can move forward working on other really important improvements.

In this commit I'll also be taking ownership of this gem and bumping to 0.1.0. In order to do this I'll need to be added to the rubygems project.

TODOS

  • Get rid of Ruby's yeild in the layout
  • Don't remove index.html but add to the readme
  • Application Controller
  • Change view prefix to pathToHTML
  • Use Push State as default in Batman
  • Main controller
  • Main HTML directory
  • Styling for main/index.html
  • When calling rails g controller don't pluralize
  • No Shim option
  • No jQuery option
  • Existing tests
  • More route examples
  • What should happen when calling rails g view in terms of pluralization
  • Content for the welcome page main/index.html
  • Explain in the README that you have to remove public/index.html
  • rails g batman:controller posts index show edit should generate HTML files.
  • Generated HTML files should contain default content. This is difficult because the model generator is passed the actions and not the attributes so we can't build out the table columns.
  • Try and remove the need for resourceName and storageKey.
  • Create a nicer console output after generators are run.
  • Add basic CSRF support.

/cc @jduff

@ghost

From here this looks like a solid commit Matt. You’re on a roll – keep moving.

I think you might want to drop a few to-dos that we don’t need right now. We will get them done. Leave some crumbs for the rest of us :)

I suggest dopping:

• More route examples
• rails g batman:controller ... (we can finish comprehensive controller generation tasks later)
• When calling rails g controller don't pluralize
• No shim option
• jQuery option

@jduff
Collaborator

Run with it.

Please don't drop the tests though, I don't see why that would be necessary. What email do you use for rubygems?

@kristianpd kristianpd commented on the diff
lib/generators/batman/app_generator.rb
((17 lines not shown))
+ template "rails/layout.html", "app/views/layouts/#{app_name}.html.erb"
+
+ inject_into_file "config/routes.rb", :after => "#{js_application_name}::Application.routes.draw do\n" do
+ route_catchall
+ end
+ end
+ end
+
+ def create_app
+ with_app_name do
+ template "batman/batman_app.coffee", "#{app_path}/#{application_name}.js.coffee"
+ end
+ end
+
+ def create_directories
+ %w(models views controllers html lib).each do |dir|
@kristianpd Owner

what's html?

@nickjs Owner
nickjs added a note

It's a new directory/convention we're pushing with the new version of Batman. We're going to separate .coffee views from .html views. The latter goes into /html while the former stays in /views. This makes it more sensible to logically separate your view classes, e.g. views/products/index.coffee and views/products/show.coffee. Right now, we dump all those classes in one products.coffee file in order to avoid file glut.

@kristianpd Owner

This is a good idea

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/generators/batman/app_generator.rb
((47 lines not shown))
+
+ def inject_batman
+ with_app_name do
+ application_file = File.join(app_path, "#{application_name}.js.coffee")
+
+ prepend_file application_file do
+ batman_requires
+ end
+
+ end
+ end
+
+ private
+
+ def route_catchall
+# \n match ":controller(/:action(/:id))", to: "#{app_name}\#index", :constraints => { :format => 'html' }\n
@kristianpd Owner

get rid of this line?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/generators/batman/html_generator.rb
((4 lines not shown))
+ class HtmlGenerator < ::Rails::Generators::NamedBase
+ include Common
+ requires_app_name
+
+ desc "This generator creates a the HTML files needed for a Batman.Controller"
+
+ def create_directory
+ with_app_name do
+ empty_directory "#{app_path}/html/#{plural_name.downcase}"
+ end
+ end
+
+ def create_files
+ with_app_name do
+ %w(index show).each do |file|
+ template "batman/html/#{file}.html", "#{app_path}/html/#{plural_name.downcase}/#{file}.html"
@kristianpd Owner

Should these be .js.htm for the asset pipeline?

@nickjs Owner
nickjs added a note

No. First off, that's just a hack we use in Shopify to tell our custom asset pipeline to do the extra special bundling with our view files. Real Sprockets should handle it more gracefully. Secondly, and more importantly, what we used the hack for was to enable bundling HTML views in the js bundle. For simplicity's sake, we've decided to make the default behaviour for batman-rails to request the HTML views separately. Batman defaults to this behaviour, with additional hooks for bundling the HTML. For any given app, that bundling process is going to require custom code. It would be difficult to do it in a generic way in the batman-rails gem. So we use the simpler, default Batman behaviour, and just leave those hooks in if an app wants to implement their own view bundling.

@nickjs Owner
nickjs added a note

Plust .html is way nicer looking :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/batman-rails/version.rb
@@ -0,0 +1,5 @@
+module Batman
+ module Rails
+ VERSION = "0.1.0"
@nickjs Owner
nickjs added a note

Should the version number coincide with the Batman version number? Either way, this should definitely be greater than 0.1, since this is definitely a new version.

@jduff Collaborator
jduff added a note

This shouldn't change until you're ready to release. Your improvements should go in this pr and that should be merge, you would then increment the version when you are ready to release.

Since the previous version was 0.0.9 going to 0.1.0 is legit. But that is t a decision you can make right now. Maybe you merge this an do a bunch more work and when we decide to release this is the real deal 1.0. Can't make version decisions until release time. Also check out http://semver.org/ for details on how to version and how version bumps should progress.

@matthelm Collaborator

Sorry John :cry:

I :heart: semver and use semver but @nickjs pulled out the :b:IG :gun:s so I folded. I'll revert that line.

@jduff Collaborator
jduff added a note
@nickjs Owner
nickjs added a note

Don't forget the other rule of semver: before 1.0, all other rules are useless.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@matthelm
Collaborator

I've added styles and images for the welcome page. This is shown when the user installs Batman, boots and goes to localhost:3000. The images will sit in the gem and, just like rails, the CSS and HTML will be in a single file.

@matthelm
Collaborator

@jduff my username is matthelm and my email is matt@matthelm.ca

lib/templates/batman/batman_app.coffee
@@ -0,0 +1,25 @@
+Batman.extend Batman.config,
+ usePushState: true
+ viewPrefix: '/assets/batman/html'
@matthelm Collaborator

@nickjs What do you think about moving the viewPrefix part of this into Batman.rails? With my pushState change it'll mean we don't need a config block.

@nickjs Owner
nickjs added a note

Yes, yes, a thousand times yes.

@matthelm Collaborator

In order to do this I need to change batman, vendor batman and then update the version of batman.rails.js used in batman-rails. Doing that now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@matthelm
Collaborator
  • I need to make the controller generate HTML files when you create those actions. The reason I've been holding off is because I expect testing this will be hard. Long story.
  • I need to get CSRF working. Shopify's implementation uses jQuery so I'm going to have to do it with plain JS.
  • Nick is going to review the content for main/index.html and after app creation.

The let's :shipit:

@jduff
Collaborator
@nickjs
Owner

Matt, what's the status on CSRF and testing?

@matthelm
Collaborator

Testing is solid. Shopify's CSRF filter uses the jQuery ajaxPrefilter. I need to figure out how to parse the meta element with name csrf-param and include that in the request header in batman.solo. The jQuery version is easy.

@nickjs
Owner
  • If you pass model attributes to the scaffold generator, they get ignored.
  • Scaffolds don't include an edit action by default.
  • Scaffolds (and controllers?) need to add @resources to batman_app.js.coffee
  • Scaffolds should pluralize their controllers and html.
  • When passing key names to the model generator, if you don't append a :type, it should default to string, like in Rails.

It'd also be great to be able to get rid of the need for resourceName and storageKey, but there may still be a browser limitation with that.

@seivan

Should an API really use CSRF?

@matthelm
Collaborator

@andrewdunbar Do you have any insight into this? batman-rails is a gem that adds rails-like generators to a Rails project to help developers get started with Batman. I'm wondering if you think it's a standard thing to use CSRF protection for APIs in the same way we do with Shopify 2. If it is I'll add some code to batman-rails that will be inserted when the user installs Batman into their app. This would take the CSRF token from the <meta> tag and adds it as a header to all our requests. Should we assume users are using CSRF for their Rails backed APIs or not. Ping @jduff. You're another guy to weight in on this sort of stuff. Thanks to you both.

@jduff
Collaborator
@matthelm
Collaborator

@nickjs I guess we're going to have to do this and the other parts at some point :P

@jamesmacaulay
Collaborator

I was just playing around with this gem over the weekend and noticed this branch. It seems like there are lots of valuable improvements here that could be merged in piecemeal without waiting for everything to be finished. Could we do that?

Also:

CSRF protection definitely needs to be in place for all requests, no matter what format they are in. Every Rails app should have protect_from_forgery turned on, so batman-rails definitely needs to accommodate that.

The only way that "API requests" shouldn't require a security token is when they use some other kind of non-cookie, non-session-based authentication like basic auth or oauth or whatever. In that case, protect_from_forgery still does what it always does when there's no security token (since 3.0.4): it resets the session...which is okay, since the session wasn't being used for auth.

@nickjs
Owner

I agree, this should be merged in ASAP, especially now that Rails 4 is out. @matthelm, what do you think?

@nickjs nickjs merged commit 4041e63 into from
@jamesmacaulay
Collaborator

Woot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 21, 2013
  1. @matthelm
  2. @matthelm

    Update Batman Files.

    matthelm authored
  3. @matthelm
  4. @matthelm

    Update README and LICENSE.

    matthelm authored
Commits on Feb 22, 2013
  1. @matthelm

    Update Tests.

    matthelm authored
  2. @matthelm

    Update assets for index page.

    matthelm authored
  3. @matthelm
  4. @matthelm
  5. @matthelm
  6. @matthelm

    Include basic route examples

    matthelm authored
  7. @matthelm
  8. @matthelm

    Remove unneeded route

    matthelm authored
Commits on Feb 23, 2013
  1. @matthelm
Commits on Feb 24, 2013
  1. @matthelm
  2. @matthelm
  3. @matthelm
Commits on Feb 28, 2013
  1. @nickjs
  2. @nickjs
  3. @nickjs
  4. @nickjs
  5. @nickjs
  6. @nickjs
Commits on Mar 1, 2013
  1. @matthelm
  2. @matthelm
Commits on Aug 15, 2013
  1. @nickjs

    Vendor batman to a539b21.

    nickjs authored
Commits on Aug 16, 2013
  1. @nickjs

    Fix batman route constraint

    nickjs authored
  2. @nickjs

    General clean up

    nickjs authored
  3. @nickjs
  4. @nickjs
  5. @nickjs

    Update gemspec

    nickjs authored
Something went wrong with that request. Please try again.