Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 9f7fb0d481e384be76307fa56baa852c329b559e @spangenberg spangenberg committed Oct 14, 2010
Showing with 843 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +4 −0 Gemfile
  3. +17 −0 README.md
  4. +10 −0 Rakefile
  5. +33 −0 lib/generators/mootools/install/install_generator.rb
  6. +13 −0 lib/mootools-rails.rb
  7. +79 −0 lib/mootools-rails/assert_select_mootools.rb
  8. +5 −0 lib/mootools-rails/version.rb
  9. +24 −0 mootools-rails.gemspec
  10. +20 −0 spec/lib/generators/mootools/install_generator_spec.rb
  11. +25 −0 spec/lib/mootools-rails_spec.rb
  12. +20 −0 spec/spec_helper.rb
  13. +4 −0 spec/support/custom_app/.gitignore
  14. +31 −0 spec/support/custom_app/Gemfile
  15. +4 −0 spec/support/custom_app/config.ru
  16. +42 −0 spec/support/custom_app/config/application.rb
  17. +13 −0 spec/support/custom_app/config/boot.rb
  18. +22 −0 spec/support/custom_app/config/database.yml
  19. +5 −0 spec/support/custom_app/config/environment.rb
  20. +26 −0 spec/support/custom_app/config/environments/development.rb
  21. +49 −0 spec/support/custom_app/config/environments/production.rb
  22. +35 −0 spec/support/custom_app/config/environments/test.rb
  23. +58 −0 spec/support/custom_app/config/routes.rb
  24. +6 −0 spec/support/custom_app/script/rails
  25. +4 −0 spec/support/default_app/.gitignore
  26. +31 −0 spec/support/default_app/Gemfile
  27. +4 −0 spec/support/default_app/config.ru
  28. +42 −0 spec/support/default_app/config/application.rb
  29. +13 −0 spec/support/default_app/config/boot.rb
  30. +22 −0 spec/support/default_app/config/database.yml
  31. +5 −0 spec/support/default_app/config/environment.rb
  32. +26 −0 spec/support/default_app/config/environments/development.rb
  33. +49 −0 spec/support/default_app/config/environments/production.rb
  34. +35 −0 spec/support/default_app/config/environments/test.rb
  35. +58 −0 spec/support/default_app/config/routes.rb
  36. +6 −0 spec/support/default_app/script/rails
@@ -0,0 +1,3 @@
+pkg/*
+tmp
+spec/support/*/Gemfile.lock
@@ -0,0 +1,4 @@
+source :gemcutter
+
+# Specify your gem's dependencies in mootols-rails.gemspec
+gemspec
@@ -0,0 +1,17 @@
+# Mootools-rails
+
+This gem adds a single generator to Rails 3, mootools:install. Running the generator will remove any Prototype JS files you may happen to have, fetch MooTools Core and the MooTools-ujs driver for Rails.
+
+The gem will also hook into the Rails configuration process, removing Prototype and adding MooTools to the javascript files included by the `javascript_include_tag(:defaults)` call. While the plugin downloads minified and un-minified version of MooTools, only the minified versions are included in :default.
+
+### Installation
+
+In your Gemfile, add this line:
+
+ gem "mootools-rails"
+
+Then, run `bundle install`. To invoke the generator, run:
+
+ rails generate mootools:install --version to install specific version of MooTools (default is 1.2.5)
+
+You're done! Don't forget to output `csrf_meta_tag` somewhere inside your `<head>` tag in your layout!
@@ -0,0 +1,10 @@
+require 'bundler'
+Bundler::GemHelper.install_tasks
+
+require "rspec/core/rake_task"
+desc "Run all examples"
+RSpec::Core::RakeTask.new(:spec) do |t|
+ t.ruby_opts = ['-r test/unit']
+ t.rspec_opts = %w[--color]
+end
+task :default => :spec
@@ -0,0 +1,33 @@
+module Mootools
+ module Generators
+ class InstallGenerator < ::Rails::Generators::Base
+ desc "This generator downloads and installs MooTools, MooTools-ujs HEAD"
+ class_option :version, :type => :string, :default => "1.2.5", :desc => "Which version of MooTools to fetch"
+ @@versions = %w( 1.2.5 1.2.4 1.2.3 1.2.2 1.2.1 1.1.2 1.1.1 )
+
+ def remove_prototype
+ %w(controls.js dragdrop.js effects.js prototype.js).each do |js|
+ remove_file "public/javascripts/#{js}"
+ end
+ end
+
+ def download_mootools
+ # Downloading latest MooTools
+ if @@versions.include?(options.version)
+ puts "Fetching MooTools version #{options.version}!"
+ get "http://ajax.googleapis.com/ajax/libs/mootools/#{options.version}/mootools-yui-compressed.js", "public/javascripts/mootools.min.js"
+ get "http://ajax.googleapis.com/ajax/libs/mootools/#{options.version}/mootools.js", "public/javascripts/mootools.js"
+ else
+ puts "MooTools #{options.version} is invalid; fetching #{@@versions[1]} instead."
+ get "http://ajax.googleapis.com/ajax/libs/mootools/#{@@versions[1]}/mootools-yui-compressed.js", "public/javascripts/mootools.min.js"
+ get "http://ajax.googleapis.com/ajax/libs/mootools/#{@@versions[1]}/mootools.js", "public/javascripts/mootools.js"
+ end
+ end
+
+ def download_ujs_driver
+ # Downloading latest MooTOols drivers
+ get "http://github.com/neonlex/mootools-ujs/raw/master/Source/rails.js", "public/javascripts/rails.js"
+ end
+ end
+ end
+end
@@ -0,0 +1,13 @@
+module Mootools
+ module Rails
+ class Railtie < ::Rails::Railtie
+ config.before_configuration do
+ require "mootools-rails/assert_select_mootools" if ::Rails.env.test?
+
+ jq_defaults = ::Rails.env.production? ? %w(mootools.min) : %w(mootools)
+
+ config.action_view.javascript_expansions[:defaults] = jq_defaults + %w(rails)
+ end
+ end
+ end
+end
@@ -0,0 +1,79 @@
+module ActionDispatch
+ module Assertions
+ module SelectorAssertions
+ # Selects content from a MooTools response. Patterned loosely on
+ # assert_select_rjs.
+ #
+ # === Narrowing down
+ #
+ # With no arguments, asserts that one or more method calls are made.
+ #
+ # Use the +method+ argument to narrow down the assertion to only
+ # statements that call that specific method.
+ #
+ # Use the +opt+ argument to narrow down the assertion to only statements
+ # that pass +opt+ as the first argument.
+ #
+ # Use the +id+ argument to narrow down the assertion to only statements
+ # that invoke methods on the result of using that identifier as a
+ # selector.
+ #
+ # === Using blocks
+ #
+ # Without a block, +assert_select_mootools_ merely asserts that the
+ # response contains one or more statements that match the conditions
+ # specified above
+ #
+ # With a block +assert_select_mootools_ also asserts that the method call
+ # passes a javascript escaped string containing HTML. All such HTML
+ # fragments are selected and passed to the block. Nested assertions are
+ # supported.
+ #
+ # === Examples
+ #
+ # # asserts that the #notice element is hidden
+ # assert_select :hide, '#notice'
+ #
+ # # asserts that the #cart element is shown with a blind parameter
+ # assert_select :show, :blind, '#cart'
+ #
+ # # asserts that #cart content contains a #current_item
+ # assert_select :html, '#cart' do
+ # assert_select '#current_item'
+ # end
+
+ def assert_select_mootools(*args, &block)
+ mootools_method = args.first.is_a?(Symbol) ? args.shift : nil
+ mootools_opt = args.first.is_a?(Symbol) ? args.shift : nil
+ id = args.first.is_a?(String) ? args.shift : nil
+
+ pattern = "\\.#{mootools_method || '\\w+'}\\("
+ pattern = "#{pattern}['\"]#{mootools_opt}['\"],?\\s*" if mootools_opt
+ pattern = "#{pattern}#{RJS_PATTERN_HTML}" if block
+ pattern = "(?:MooTools|\\$)\\(['\"]#{id}['\"]\\)#{pattern}" if id
+
+ fragments = []
+ response.body.scan(Regexp.new(pattern)).each do |match|
+ doc = HTML::Document.new(unescape_rjs(match.first))
+ doc.root.children.each do |child|
+ fragments.push child if child.tag?
+ end
+ end
+
+ if fragments.empty?
+ opts = [mootools_method, mootools_opt, id].compact
+ flunk "No MooTools call matches #{opts.inspect}"
+ end
+
+ if block
+ begin
+ in_scope, @selected = @selected, fragments
+ yield
+ ensure
+ @selected = in_scope
+ end
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,5 @@
+module Mootools
+ module Rails
+ VERSION = "0.1"
+ end
+end
@@ -0,0 +1,24 @@
+# -*- encoding: utf-8 -*-
+require File.expand_path('../lib/mootools-rails/version', __FILE__)
+
+Gem::Specification.new do |s|
+ s.name = "mootools-rails"
+ s.version = Mootools::Rails::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["Daniel Spangenberg"]
+ s.email = ["daniel.spangenberg@gmail.com"]
+ s.homepage = "http://rubygems.org/gems/mootools-rails"
+ s.summary = "Use MooTools with Rails 3"
+ s.description = "This gem provides a Rails generator to install MooTools and the MooTools-ujs driver into your Rails 3 application, and then have them included automatically instead of Prototype."
+
+ s.required_rubygems_version = ">= 1.3.6"
+ s.rubyforge_project = "mootools-rails"
+
+ s.add_dependency "rails", "~> 3.0"
+ s.add_development_dependency "bundler", "~> 1.0.0"
+ s.add_development_dependency "rspec", "~> 1.3"
+
+ s.files = `git ls-files`.split("\n")
+ s.executables = `git ls-files`.split("\n").select{|f| f =~ /^bin/}
+ s.require_path = 'lib'
+end
@@ -0,0 +1,20 @@
+require 'spec/test/unit'
+require 'spec_helper'
+require 'generators/mootools/install/install_generator'
+
+class Mootools::Generators::InstallGeneratorTest < Rails::Generators::TestCase
+ describe "The MooTools generator"
+
+ destination File.join(Rails.root)
+ tests Mootools::Generators::InstallGenerator
+ arguments []
+
+ setup :prepare_destination
+
+ it "should install mootools" do
+ run_generator
+
+ %w(mootools.min.js mootools.js rails.js).each { |js| assert_file "public/javascripts/#{js}" }
+ %w(controls.js dragdrop.js effects.js prototype.js).each { |js| assert_no_file "public/javascripts/#{js}" }
+ end
+end
@@ -0,0 +1,25 @@
+require 'spec_helper'
+
+describe "The MooTools-Rails railtie" do
+
+ it "changes the default javascript expansion" do
+ get_js_defaults("default").should == ["mootools.min", "rails"].inspect
+ end
+
+ it "uses non-minified js in development" do
+ get_js_defaults("default", "development").should == %w(mootools rails).inspect
+ end
+
+ it "changes allows overriding the javascript expansion" do
+ get_js_defaults("custom").should == ["foo", "bar", "baz"].inspect
+ end
+
+ def get_js_defaults(name, env = "production")
+ dir = File.expand_path("../../support/#{name}_app", __FILE__)
+ Dir.chdir(dir) do
+ `bundle install --local`
+ `rails runner -e #{env} 'puts Rails.application.config.action_view.
+ javascript_expansions[:defaults].inspect'`.chomp
+ end
+ end
+end
@@ -0,0 +1,20 @@
+# Setup for generator tests
+require 'rails/all'
+require 'rails/generators'
+require 'rails/generators/test_case'
+
+class TestApp < Rails::Application
+ config.root = File.dirname(__FILE__)
+end
+Rails.application = TestApp
+
+module Rails
+ def self.root
+ @root ||= File.expand_path("../../tmp/rails", __FILE__)
+ end
+end
+Rails.application.config.root = Rails.root
+
+# Call configure to load the settings from
+# Rails.application.config.generators to Rails::Generators
+Rails::Generators.configure!
@@ -0,0 +1,4 @@
+.bundle
+db/*.sqlite3
+log/*.log
+tmp/**/*
@@ -0,0 +1,31 @@
+source 'http://rubygems.org'
+
+gem 'rails', '3.0.0'
+gem 'mootools-rails', :path => "../../.."
+
+# Bundle edge Rails instead:
+# gem 'rails', :git => 'git://github.com/rails/rails.git'
+
+gem 'sqlite3-ruby', :require => 'sqlite3'
+
+# Use unicorn as the web server
+# gem 'unicorn'
+
+# Deploy with Capistrano
+# gem 'capistrano'
+
+# To use debugger
+# gem 'ruby-debug'
+
+# Bundle the extra gems:
+# gem 'bj'
+# gem 'nokogiri'
+# gem 'sqlite3-ruby', :require => 'sqlite3'
+# gem 'aws-s3', :require => 'aws/s3'
+
+# Bundle gems for the local environment. Make sure to
+# put test-only gems in this group so their generators
+# and rake tasks are available in development mode:
+# group :development, :test do
+# gem 'webrat'
+# end
@@ -0,0 +1,4 @@
+# This file is used by Rack-based servers to start the application.
+
+require ::File.expand_path('../config/environment', __FILE__)
+run CustomApp::Application
@@ -0,0 +1,42 @@
+require File.expand_path('../boot', __FILE__)
+
+require 'rails/all'
+
+# If you have a Gemfile, require the gems listed there, including any gems
+# you've limited to :test, :development, or :production.
+Bundler.require(:default, Rails.env) if defined?(Bundler)
+
+module CustomApp
+ class Application < Rails::Application
+ # Settings in config/environments/* take precedence over those specified here.
+ # Application configuration should go into files in config/initializers
+ # -- all .rb files in that directory are automatically loaded.
+
+ # Custom directories with classes and modules you want to be autoloadable.
+ # config.autoload_paths += %W(#{config.root}/extras)
+
+ # Only load the plugins named here, in the order given (default is alphabetical).
+ # :all can be used as a placeholder for all plugins not explicitly named.
+ # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
+
+ # Activate observers that should always be running.
+ # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
+
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
+ # config.time_zone = 'Central Time (US & Canada)'
+
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
+ # config.i18n.default_locale = :de
+
+ # JavaScript files you want as :defaults (application.js is always included).
+ config.action_view.javascript_expansions[:defaults] = %w(foo bar baz)
+
+ # Configure the default encoding used in templates for Ruby 1.9.
+ config.encoding = "utf-8"
+
+ # Configure sensitive parameters which will be filtered from the log file.
+ config.filter_parameters += [:password]
+ end
+end
@@ -0,0 +1,13 @@
+require 'rubygems'
+
+# Set up gems listed in the Gemfile.
+gemfile = File.expand_path('../../Gemfile', __FILE__)
+begin
+ ENV['BUNDLE_GEMFILE'] = gemfile
+ require 'bundler'
+ Bundler.setup
+rescue Bundler::GemNotFound => e
+ STDERR.puts e.message
+ STDERR.puts "Try running `bundle install`."
+ exit!
+end if File.exist?(gemfile)
@@ -0,0 +1,22 @@
+# SQLite version 3.x
+# gem install sqlite3-ruby (not necessary on OS X Leopard)
+development:
+ adapter: sqlite3
+ database: db/development.sqlite3
+ pool: 5
+ timeout: 5000
+
+# Warning: The database defined as "test" will be erased and
+# re-generated from your development database when you run "rake".
+# Do not set this db to the same as development or production.
+test:
+ adapter: sqlite3
+ database: db/test.sqlite3
+ pool: 5
+ timeout: 5000
+
+production:
+ adapter: sqlite3
+ database: db/production.sqlite3
+ pool: 5
+ timeout: 5000
@@ -0,0 +1,5 @@
+# Load the rails application
+require File.expand_path('../application', __FILE__)
+
+# Initialize the rails application
+CustomApp::Application.initialize!
Oops, something went wrong. Retry.

0 comments on commit 9f7fb0d

Please sign in to comment.