├── .gitignore ├── .travis.yml ├── Appraisals ├── CODE_OF_CONDUCT.md ├── Gemfile ├── LICENSE.txt ├── README.md ├── Rakefile ├── bin ├── console └── setup ├── gemfiles ├── rails_4_2.gemfile ├── rails_5_0.gemfile ├── rails_5_1.gemfile ├── rails_5_2.gemfile └── rails_latest.gemfile ├── hamlit-rails.gemspec ├── lib ├── generators │ └── haml │ │ ├── controller │ │ ├── controller_generator.rb │ │ └── templates │ │ │ └── view.html.haml │ │ ├── mailer │ │ ├── mailer_generator.rb │ │ └── templates │ │ │ ├── layout.html.haml │ │ │ ├── layout.text.haml │ │ │ ├── view.html.haml │ │ │ └── view.text.haml │ │ └── scaffold │ │ ├── scaffold_generator.rb │ │ └── templates │ │ ├── _form.html.haml │ │ ├── edit.html.haml │ │ ├── index.html.haml │ │ ├── new.html.haml │ │ └── show.html.haml ├── hamlit-rails.rb └── hamlit-rails │ ├── erb2haml.rake │ └── version.rb └── test ├── fixtures └── routes.rb ├── lib └── generators │ └── haml │ ├── controller_generator_test.rb │ ├── mailer_generator_test.rb │ └── scaffold_generator_test.rb └── minitest_helper.rb /.gitignore: -------------------------------------------------------------------------------- 1 | /.bundle/ 2 | /.yardoc 3 | /Gemfile.lock 4 | /_yardoc/ 5 | /coverage/ 6 | /doc/ 7 | /pkg/ 8 | /spec/reports/ 9 | /tmp/ 10 | *.gem 11 | *.lock 12 | gemfiles/*.lock 13 | /vendor/bundle 14 | .DS_Store -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | cache: bundler 3 | before_install: "which bundle || gem install bundler" 4 | matrix: 5 | include: 6 | - rvm: 2.4.5 7 | gemfile: gemfiles/rails_5_2.gemfile 8 | - rvm: 2.5.3 9 | gemfile: gemfiles/rails_5_2.gemfile 10 | - rvm: 2.6.1 11 | gemfile: gemfiles/rails_4_2.gemfile 12 | - rvm: 2.6.1 13 | gemfile: gemfiles/rails_5_0.gemfile 14 | - rvm: 2.6.1 15 | gemfile: gemfiles/rails_5_1.gemfile 16 | - rvm: 2.6.1 17 | gemfile: gemfiles/rails_5_2.gemfile 18 | - rvm: 2.6.1 19 | gemfile: gemfiles/rails_latest.gemfile 20 | script: "bundle exec rake test" 21 | -------------------------------------------------------------------------------- /Appraisals: -------------------------------------------------------------------------------- 1 | appraise "rails_4_2" do 2 | gem "rails", "4.2" 3 | end 4 | 5 | appraise "rails_5_0" do 6 | gem "rails", "5.0" 7 | end 8 | 9 | appraise "rails_5_1" do 10 | gem "rails", "5.1" 11 | end 12 | 13 | appraise "rails_5_2" do 14 | gem "rails", "5.2" 15 | end 16 | 17 | appraise "rails_latest" do 18 | gem "rails" 19 | end 20 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Code of Conduct 2 | 3 | As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities. 4 | 5 | We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion. 6 | 7 | Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct. 8 | 9 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team. 10 | 11 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers. 12 | 13 | This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/) 14 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in hamlit-rails.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Meng Fung 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Hamlit-rails 2 | ============ 3 | 4 | [![Build Status](https://travis-ci.org/mfung/hamlit-rails.svg)](https://travis-ci.org/mfung/hamlit-rails) [![Gem Version](https://badge.fury.io/rb/hamlit-rails.svg)](http://badge.fury.io/rb/hamlit-rails) 5 | 6 | Provides hamlit generators for Rails. It also enables hamlit as the templating 7 | engine and "hamlit:erb2haml" rake task that converts erb files to haml. 8 | 9 | ## Installation 10 | 11 | Run: 12 | 13 | ```sh 14 | bundle add hamlit-rails 15 | ``` 16 | 17 | Or install it yourself as: 18 | 19 | $ gem install hamlit-rails 20 | 21 | ## Usage 22 | 23 | By installing this gem, Haml templates are generated on `rails g`. 24 | 25 | Also, use the following rake task to convert .erb to .haml: 26 | 27 | ``` 28 | bundle exec rake hamlit:erb2haml 29 | ``` 30 | 31 | ## Contributing 32 | 33 | 1. Fork it ( https://github.com/mfung/hamlit-rails/fork ) 34 | 2. Create your feature branch (`git checkout -b my-new-feature`) 35 | 3. Commit your changes (`git commit -am 'Add some feature'`) 36 | 4. Push to the branch (`git push origin my-new-feature`) 37 | 5. Create a new Pull Request 38 | 39 | ## Acknowledgement 40 | This code is heavily influenced by [haml-rails](https://github.com/indirect/haml-rails). 41 | And is written because of an issue created in that repo. 42 | 43 | ## License 44 | 45 | MIT license 46 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | 3 | require 'rake/testtask' 4 | Rake::TestTask.new(:test) do |test| 5 | test.libs << 'lib' << 'test' 6 | test.pattern = 'test/**/*_test.rb' 7 | test.verbose = true 8 | end 9 | 10 | task :default => :test -------------------------------------------------------------------------------- /bin/console: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require "bundler/setup" 4 | require "hamlit/rails" 5 | 6 | # You can add fixtures and/or initialization code here to make experimenting 7 | # with your gem easier. You can also use a different console, if you like. 8 | 9 | # (If you use this, don't forget to add pry to your Gemfile!) 10 | # require "pry" 11 | # Pry.start 12 | 13 | require "irb" 14 | IRB.start 15 | -------------------------------------------------------------------------------- /bin/setup: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -euo pipefail 3 | IFS=$'\n\t' 4 | 5 | bundle install 6 | 7 | # Do any other automated setup that you need to do here 8 | -------------------------------------------------------------------------------- /gemfiles/rails_4_2.gemfile: -------------------------------------------------------------------------------- 1 | # This file was generated by Appraisal 2 | 3 | source "https://rubygems.org" 4 | 5 | gem "rails", "4.2" 6 | 7 | gemspec :path => "../" 8 | -------------------------------------------------------------------------------- /gemfiles/rails_5_0.gemfile: -------------------------------------------------------------------------------- 1 | # This file was generated by Appraisal 2 | 3 | source "https://rubygems.org" 4 | 5 | gem "rails", "5.0" 6 | 7 | gemspec :path => "../" 8 | -------------------------------------------------------------------------------- /gemfiles/rails_5_1.gemfile: -------------------------------------------------------------------------------- 1 | # This file was generated by Appraisal 2 | 3 | source "https://rubygems.org" 4 | 5 | gem "rails", "5.1" 6 | 7 | gemspec :path => "../" 8 | -------------------------------------------------------------------------------- /gemfiles/rails_5_2.gemfile: -------------------------------------------------------------------------------- 1 | # This file was generated by Appraisal 2 | 3 | source "https://rubygems.org" 4 | 5 | gem "rails", "5.2" 6 | 7 | gemspec :path => "../" 8 | -------------------------------------------------------------------------------- /gemfiles/rails_latest.gemfile: -------------------------------------------------------------------------------- 1 | # This file was generated by Appraisal 2 | 3 | source "https://rubygems.org" 4 | 5 | gem "rails" 6 | 7 | gemspec :path => "../" 8 | -------------------------------------------------------------------------------- /hamlit-rails.gemspec: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require 'hamlit-rails/version' 5 | 6 | Gem::Specification.new do |spec| 7 | spec.name = "hamlit-rails" 8 | spec.version = Hamlit::Rails::VERSION 9 | spec.authors = ["Meng Fung"] 10 | spec.email = ["meng.fung@gmail.com"] 11 | 12 | spec.summary = %q{hamlit and rails} 13 | spec.description = %q{hamlit-rails provides generators for Rails 4.} 14 | spec.homepage = "https://github.com/mfung/hamlit-rails" 15 | spec.license = "MIT" 16 | 17 | spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } 18 | spec.bindir = "exe" 19 | spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } 20 | spec.require_paths = ["lib"] 21 | 22 | spec.add_runtime_dependency "hamlit", ">= 1.2.0" 23 | spec.add_runtime_dependency "activesupport", ">= 4.0.1" 24 | spec.add_runtime_dependency "actionpack", ">= 4.0.1" 25 | spec.add_runtime_dependency "railties", ">= 4.0.1" 26 | 27 | spec.add_development_dependency "bundler" 28 | spec.add_development_dependency "html2haml", ">= 2.0.0" 29 | spec.add_development_dependency "rake", "~> 10.0" 30 | spec.add_development_dependency "rails", ">= 4.0.1" 31 | spec.add_development_dependency "appraisal", "~> 1.0" 32 | end 33 | -------------------------------------------------------------------------------- /lib/generators/haml/controller/controller_generator.rb: -------------------------------------------------------------------------------- 1 | require 'rails/generators/erb/controller/controller_generator' 2 | 3 | module Haml 4 | module Generators 5 | class ControllerGenerator < Erb::Generators::ControllerGenerator 6 | source_root File.expand_path("../templates", __FILE__) 7 | 8 | protected 9 | 10 | def handler 11 | :haml 12 | end 13 | 14 | end 15 | end 16 | end -------------------------------------------------------------------------------- /lib/generators/haml/controller/templates/view.html.haml: -------------------------------------------------------------------------------- 1 | %h1 <%= class_name %>#<%= @action %> 2 | %p Find me in <%= @path %> 3 | -------------------------------------------------------------------------------- /lib/generators/haml/mailer/mailer_generator.rb: -------------------------------------------------------------------------------- 1 | require 'generators/haml/controller/controller_generator' 2 | 3 | module Haml 4 | module Generators 5 | class MailerGenerator < ControllerGenerator 6 | source_root File.expand_path("../templates", __FILE__) 7 | 8 | def copy_view_files 9 | if ::Rails.version.to_s >= "4.2.0" 10 | view_base_path = File.join("app/views", class_path, file_name) 11 | empty_directory view_base_path 12 | 13 | if self.behavior == :invoke 14 | formats.each do |format| 15 | layout_path = File.join("app/views/layouts", filename_with_extensions("mailer", format)) 16 | template filename_with_extensions(:layout, format), layout_path 17 | end 18 | end 19 | 20 | actions.each do |action| 21 | @action = action 22 | 23 | formats.each do |format| 24 | @path = File.join(view_base_path, filename_with_extensions(action, format)) 25 | template filename_with_extensions(:view, format), @path 26 | end 27 | end 28 | else 29 | super 30 | end 31 | end 32 | 33 | protected 34 | 35 | def format 36 | :text 37 | end 38 | 39 | def formats 40 | [:text, :html] 41 | end 42 | end 43 | end 44 | end -------------------------------------------------------------------------------- /lib/generators/haml/mailer/templates/layout.html.haml: -------------------------------------------------------------------------------- 1 | %hmtl 2 | %body 3 | = yield -------------------------------------------------------------------------------- /lib/generators/haml/mailer/templates/layout.text.haml: -------------------------------------------------------------------------------- 1 | = yield -------------------------------------------------------------------------------- /lib/generators/haml/mailer/templates/view.html.haml: -------------------------------------------------------------------------------- 1 | %h1= class_name + "#" + @action 2 | 3 | %p 4 | = @greeting + ", find me in <%= @path %>" -------------------------------------------------------------------------------- /lib/generators/haml/mailer/templates/view.text.haml: -------------------------------------------------------------------------------- 1 | <%= class_name %>#<%= @action %> 2 | 3 | = @greeting + ", find me in <%= @path %>" 4 | -------------------------------------------------------------------------------- /lib/generators/haml/scaffold/scaffold_generator.rb: -------------------------------------------------------------------------------- 1 | require 'rails/generators/erb/scaffold/scaffold_generator' 2 | 3 | module Haml 4 | module Generators 5 | class ScaffoldGenerator < Erb::Generators::ScaffoldGenerator 6 | source_root File.expand_path("../templates", __FILE__) 7 | 8 | def copy_view_files 9 | available_views.each do |view| 10 | filename = filename_with_extensions(view) 11 | template "#{view}.html.haml", File.join("app/views", controller_file_path, filename) 12 | end 13 | end 14 | 15 | hook_for :form_builder, :as => :scaffold 16 | 17 | def copy_form_file 18 | if options[:form_builder].nil? 19 | filename = filename_with_extensions("_form") 20 | template "_form.html.haml", File.join("app/views", controller_file_path, filename) 21 | end 22 | end 23 | 24 | protected 25 | 26 | def available_views 27 | %w(index edit show new) 28 | end 29 | 30 | def handler 31 | :haml 32 | end 33 | 34 | end 35 | end 36 | end -------------------------------------------------------------------------------- /lib/generators/haml/scaffold/templates/_form.html.haml: -------------------------------------------------------------------------------- 1 | = form_for @<%= singular_table_name %> do |f| 2 | - if @<%= singular_table_name %>.errors.any? 3 | #error_explanation 4 | %h2= "#{pluralize(@<%= singular_table_name %>.errors.count, "error")} prohibited this <%= singular_table_name %> from being saved:" 5 | %ul 6 | - @<%= singular_table_name %>.errors.full_messages.each do |msg| 7 | %li= msg 8 | 9 | <% for attribute in attributes -%> 10 | .field 11 | = f.label :<%= attribute.name %> 12 | = f.<%= attribute.field_type %> :<%= attribute.name %> 13 | <% end -%> 14 | .actions 15 | = f.submit 'Save' 16 | -------------------------------------------------------------------------------- /lib/generators/haml/scaffold/templates/edit.html.haml: -------------------------------------------------------------------------------- 1 | %h1 Editing <%= singular_table_name %> 2 | 3 | = render 'form' 4 | 5 | = link_to 'Show', @<%= singular_table_name %> 6 | \| 7 | = link_to 'Back', <%= index_helper %>_path 8 | -------------------------------------------------------------------------------- /lib/generators/haml/scaffold/templates/index.html.haml: -------------------------------------------------------------------------------- 1 | %h1 Listing <%= plural_table_name %> 2 | 3 | %table 4 | %thead 5 | %tr 6 | <% for attribute in attributes -%> 7 | %th <%= attribute.human_name %> 8 | <% end -%> 9 | %th 10 | %th 11 | %th 12 | 13 | %tbody 14 | - @<%= plural_table_name %>.each do |<%= singular_table_name %>| 15 | %tr 16 | <% for attribute in attributes -%> 17 | %td= <%= singular_table_name %>.<%= attribute.name %> 18 | <% end -%> 19 | %td= link_to 'Show', <%= singular_table_name %> 20 | %td= link_to 'Edit', edit_<%= singular_table_name %>_path(<%= singular_table_name %>) 21 | %td= link_to 'Destroy', <%= singular_table_name %>, :method => :delete, :data => { :confirm => 'Are you sure?' } 22 | 23 | %br 24 | 25 | = link_to 'New <%= human_name %>', new_<%= singular_table_name %>_path 26 | -------------------------------------------------------------------------------- /lib/generators/haml/scaffold/templates/new.html.haml: -------------------------------------------------------------------------------- 1 | %h1 New <%= singular_table_name %> 2 | 3 | = render 'form' 4 | 5 | = link_to 'Back', <%= index_helper %>_path 6 | -------------------------------------------------------------------------------- /lib/generators/haml/scaffold/templates/show.html.haml: -------------------------------------------------------------------------------- 1 | %p#notice= notice 2 | 3 | <% for attribute in attributes -%> 4 | %p 5 | %b <%= attribute.human_name %>: 6 | = @<%= singular_table_name %>.<%= attribute.name %> 7 | <% end -%> 8 | 9 | = link_to 'Edit', edit_<%= singular_table_name %>_path(@<%= singular_table_name %>) 10 | \| 11 | = link_to 'Back', <%= index_helper %>_path 12 | -------------------------------------------------------------------------------- /lib/hamlit-rails.rb: -------------------------------------------------------------------------------- 1 | require "hamlit-rails/version" 2 | require "rails" 3 | require "hamlit" 4 | require "hamlit/railtie" 5 | 6 | module Haml 7 | module Rails 8 | class Engine < ::Rails::Engine 9 | config.app_generators do |g| 10 | g.template_engine :haml 11 | end 12 | end 13 | class Railtie < ::Rails::Railtie 14 | config.app_generators.template_engine :haml 15 | 16 | initializer 'hamlit_rails.configure_template_digestor' do 17 | 18 | ActiveSupport.on_load(:action_view) do 19 | ActiveSupport.on_load(:after_initialize) do 20 | begin 21 | if defined?(CacheDigests::DependencyTracker) 22 | # 'cache_digests' gem being used (overrides Rails 4 implementation) 23 | CacheDigests::DependencyTracker.register_tracker :haml, CacheDigests::DependencyTracker::ERBTracker 24 | 25 | if ::Rails.env.development? 26 | # recalculate cache digest keys for each request 27 | CacheDigests::TemplateDigestor.cache = ActiveSupport::Cache::NullStore.new 28 | end 29 | elsif ::Rails.version.to_s >= '4.0' 30 | # will only apply if Rails 4, which includes 'action_view/dependency_tracker' 31 | require 'action_view/dependency_tracker' 32 | ActionView::DependencyTracker.register_tracker :haml, ActionView::DependencyTracker::ERBTracker 33 | ActionView::Base.cache_template_loading = false if ::Rails.env.development? 34 | end 35 | rescue 36 | # likely this version of Rails doesn't support dependency tracking 37 | end 38 | end 39 | end 40 | end 41 | 42 | # Configure source annotation on haml files (support for HAML was 43 | # provided directly by railties 3.2..4.1 but was dropped in 4.2. 44 | if Gem::Requirement.new(">= 4.2").satisfied_by?(Gem::Version.new(::Rails.version)) 45 | initializer 'hamlit_rails.configure_source_annotation' do 46 | if ::Rails::VERSION::MAJOR >= 6 47 | annotation_class = ::Rails::SourceAnnotationExtractor::Annotation 48 | else 49 | annotation_class = SourceAnnotationExtractor::Annotation 50 | end 51 | 52 | annotation_class.register_extensions('haml') do |tag| 53 | /\s*-#\s*(#{tag}):?\s*(.*)/ 54 | end 55 | end 56 | end 57 | 58 | rake_tasks do 59 | load 'hamlit-rails/erb2haml.rake' 60 | end 61 | end 62 | end 63 | end 64 | -------------------------------------------------------------------------------- /lib/hamlit-rails/erb2haml.rake: -------------------------------------------------------------------------------- 1 | namespace :hamlit do 2 | desc 'Convert erb to haml in app/views' 3 | task :erb2haml do 4 | begin 5 | gem 'html2haml' 6 | rescue LoadError 7 | puts "html2haml gem is not part of the bundle." 8 | puts "`rake hamlit:erb2haml` requires html2haml gem to convert erb files." 9 | puts 10 | puts "Please add html2haml gem temporarily and run `rake hamlit:erb2haml` again." 11 | puts "(You can remove html2haml gem after the conversion.)" 12 | exit 1 13 | end 14 | 15 | erb_files = Dir.glob('app/views/**/*.erb').select(&File.method(:file?)) 16 | if erb_files.empty? 17 | puts 'No .erb files found. Skipping.' 18 | next 19 | end 20 | 21 | haml_files_in_erb = Dir.glob('app/views/**/*.haml').select(&File.method(:file?)).map do |name| 22 | name.sub(/\.haml\z/, '.erb') 23 | end 24 | existing_files = erb_files.select(&haml_files_in_erb.method(:include?)) 25 | 26 | if existing_files.any? 27 | puts 'Some of your .erb files seem to have .haml equivalents:' 28 | existing_files.map { |f| puts " #{f}" } 29 | puts 30 | 31 | begin 32 | print 'Do you want to overwrite them? (y/n): ' 33 | answer = STDIN.gets.strip.downcase[0] 34 | end until ['y', 'n'].include?(answer) 35 | 36 | if answer == 'n' 37 | if (erb_files - existing_files).empty? 38 | puts 'No .erb files to convert. Skipping.' 39 | next 40 | end 41 | else 42 | existing_files.each { |file| File.delete(file.sub(/\.erb\z/, '.haml')) } 43 | end 44 | end 45 | puts 46 | 47 | erb_files.each do |file| 48 | puts "Generating .haml for #{file}..." 49 | unless system("html2haml #{file} #{file.sub(/\.erb\z/, '.haml')}") 50 | abort "Failed to execute `html2haml #{file} #{file.sub(/\.erb\z/, '.haml')}`!" 51 | end 52 | end 53 | puts 54 | 55 | begin 56 | print 'Do you want to delete original .erb files? (y/n): ' 57 | answer = STDIN.gets.strip.downcase[0] 58 | end until ['y', 'n'].include?(answer) 59 | 60 | if answer == 'y' 61 | puts 'Deleting original .erb files...' 62 | File.delete(*erb_files) 63 | erb_files.each { |file| puts " #{file}" } 64 | end 65 | puts 66 | 67 | puts 'Finished to convert erb files.' 68 | end 69 | end 70 | -------------------------------------------------------------------------------- /lib/hamlit-rails/version.rb: -------------------------------------------------------------------------------- 1 | module Hamlit 2 | module Rails 3 | VERSION = "0.2.3" 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /test/fixtures/routes.rb: -------------------------------------------------------------------------------- 1 | TestApp.routes.draw do |map| 2 | 3 | end -------------------------------------------------------------------------------- /test/lib/generators/haml/controller_generator_test.rb: -------------------------------------------------------------------------------- 1 | require 'minitest_helper' 2 | 3 | class Haml::Generators::ControllerGeneratorTest < Rails::Generators::TestCase 4 | destination Rails.root 5 | tests Rails::Generators::ControllerGenerator 6 | 7 | setup :prepare_destination 8 | setup :copy_routes 9 | 10 | arguments %w(Account foo bar) 11 | 12 | test "should invoke template engine" do 13 | run_generator 14 | assert_file "app/views/account/foo.html.haml" 15 | assert_file "app/views/account/bar.html.haml" 16 | end 17 | 18 | test "should revoke template engine" do 19 | run_generator 20 | run_generator ["account"], :behavior => :revoke 21 | 22 | assert_no_file "app/views/account" 23 | assert_no_file "app/views/account/foo.html.haml" 24 | assert_no_file "app/views/account/bar.html.haml" 25 | end 26 | end -------------------------------------------------------------------------------- /test/lib/generators/haml/mailer_generator_test.rb: -------------------------------------------------------------------------------- 1 | require 'minitest_helper' 2 | 3 | class Haml::Generators::MailerGeneratorTest < Rails::Generators::TestCase 4 | destination File.join(Rails.root) 5 | tests Rails::Generators::MailerGenerator 6 | 7 | setup :prepare_destination 8 | setup :copy_routes 9 | 10 | arguments %w(notifier foo bar) 11 | 12 | test "should invoke template engine" do 13 | run_generator 14 | 15 | if ::Rails.version.to_s >= '4.2' 16 | 17 | assert_file "app/views/layouts/mailer.text.haml" do |view| 18 | assert_match /\= yield/, view 19 | end 20 | 21 | assert_file "app/views/layouts/mailer.html.haml" do |view| 22 | assert_match /\= yield/, view 23 | end 24 | 25 | assert_file "app/views/notifier/foo.html.haml" do |view| 26 | assert_match %r(app/views/notifier/foo\.html\.haml), view 27 | assert_match /\= @greeting/, view 28 | end 29 | 30 | assert_file "app/views/notifier/bar.html.haml" do |view| 31 | assert_match %r(app/views/notifier/bar\.html\.haml), view 32 | assert_match /\= @greeting/, view 33 | end 34 | end 35 | 36 | assert_file "app/views/notifier/foo.text.haml" do |view| 37 | assert_match %r(app/views/notifier/foo\.text\.haml), view 38 | assert_match /\= @greeting/, view 39 | end 40 | 41 | assert_file "app/views/notifier/bar.text.haml" do |view| 42 | assert_match %r(app/views/notifier/bar\.text\.haml), view 43 | assert_match /\= @greeting/, view 44 | end 45 | end 46 | end -------------------------------------------------------------------------------- /test/lib/generators/haml/scaffold_generator_test.rb: -------------------------------------------------------------------------------- 1 | require 'minitest_helper' 2 | 3 | class Haml::Generators::ScaffoldGeneratorTest < Rails::Generators::TestCase 4 | destination Rails.root 5 | tests Rails::Generators::ScaffoldGenerator 6 | 7 | setup :prepare_destination 8 | setup :copy_routes 9 | 10 | arguments %w(product_line title:string price:integer) 11 | 12 | test "should invoke template engine" do 13 | run_generator 14 | 15 | %w(index edit new show _form).each { |view| assert_file "app/views/product_lines/#{view}.html.haml" } 16 | assert_no_file "app/views/layouts/product_lines.html.haml" 17 | end 18 | 19 | test "should revoke template engine" do 20 | run_generator 21 | run_generator ["product_line"], :behavior => :revoke 22 | 23 | assert_no_file "app/views/product_lines" 24 | assert_no_file "app/views/layouts/product_lines.html.haml" 25 | end 26 | 27 | end -------------------------------------------------------------------------------- /test/minitest_helper.rb: -------------------------------------------------------------------------------- 1 | $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) 2 | 3 | require 'hamlit-rails' 4 | require 'minitest/autorun' 5 | require 'rails/all' 6 | require 'rails/generators' 7 | require 'rails/generators/test_case' 8 | 9 | class TestApp < Rails::Application 10 | config.root = File.dirname(__FILE__) 11 | end 12 | 13 | module Rails 14 | def self.root 15 | @root ||= File.expand_path(File.join(File.dirname(__FILE__), '..', 'tmp', 'rails')) 16 | end 17 | end 18 | 19 | # Call configure to load the settings from 20 | # Rails.application.config.generators to Rails::Generators 21 | Rails.application.load_generators 22 | 23 | Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f} 24 | 25 | def copy_routes 26 | routes = File.join(File.dirname(__FILE__), 'fixtures', 'routes.rb') 27 | destination = File.join(Rails.root, "config") 28 | FileUtils.mkdir_p(destination) 29 | FileUtils.cp File.expand_path(routes), File.expand_path(destination) 30 | end 31 | 32 | # Asserts the given class exists in the given content. When a block is given, 33 | # it yields the content of the class. 34 | # 35 | # assert_file "test/functional/accounts_controller_test.rb" do |controller_test| 36 | # assert_class "AccountsControllerTest", controller_test do |klass| 37 | # assert_match /context "index action"/, klass 38 | # end 39 | # end 40 | # 41 | def assert_class(klass, content) 42 | assert content =~ /class #{klass}(\(.+\))?(.*?)\nend/m, "Expected to have class #{klass}" 43 | yield $2.strip if block_given? 44 | end 45 | 46 | def generator_list 47 | { 48 | :rails => ['scaffold', 'controller', 'mailer'], 49 | :haml => ['scaffold', 'controller', 'mailer'] 50 | } 51 | end 52 | 53 | def path_prefix(name) 54 | case name 55 | when :rails 56 | 'rails/generators' 57 | else 58 | 'generators' 59 | end 60 | end 61 | 62 | def require_generators(generator_list) 63 | generator_list.each do |name, generators| 64 | generators.each do |generator_name| 65 | if name.to_s == 'rails' && generator_name.to_s == 'mailer' 66 | require File.join(path_prefix(name), generator_name.to_s, "#{generator_name}_generator") 67 | else 68 | require File.join(path_prefix(name), name.to_s, generator_name.to_s, "#{generator_name}_generator") 69 | end 70 | end 71 | end 72 | end 73 | alias :require_generator :require_generators 74 | 75 | require_generators generator_list 76 | 77 | # Remove tmp directory when test suite is completed 78 | 79 | def delete_tmp_directory 80 | tmp_dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'tmp')) 81 | FileUtils.rm_r(tmp_dir) 82 | end 83 | 84 | if Minitest.respond_to? :after_run 85 | Minitest.after_run { delete_tmp_directory } 86 | else 87 | MiniTest::Unit.after_tests { delete_tmp_directory } 88 | end 89 | --------------------------------------------------------------------------------