├── .gitignore ├── .ruby-version ├── .travis.yml ├── Gemfile ├── Gemfile.lock ├── LICENSE ├── README.md ├── Rakefile ├── gemfiles ├── rails_4_0.gemfile └── rails_4_1.gemfile ├── lib ├── generators │ └── admin │ │ └── scaffold_controller │ │ ├── USAGE │ │ ├── scaffold_controller_generator.rb │ │ └── templates │ │ ├── controllers │ │ ├── jbuilder │ │ │ └── controller.rb.erb │ │ └── railties │ │ │ └── controller.rb.erb │ │ ├── tests │ │ └── test_unit │ │ │ └── functional_test.rb.erb │ │ └── views │ │ ├── erb │ │ ├── _form.html.erb.erb │ │ ├── edit.html.erb.erb │ │ ├── index.html.erb.erb │ │ ├── new.html.erb.erb │ │ └── show.html.erb.erb │ │ ├── erb_bootstrap │ │ ├── _form.html.erb.erb │ │ ├── edit.html.erb.erb │ │ ├── index.html.erb.erb │ │ ├── new.html.erb.erb │ │ └── show.html.erb.erb │ │ ├── haml │ │ ├── _form.html.haml.erb │ │ ├── edit.html.haml.erb │ │ ├── index.html.haml.erb │ │ ├── new.html.haml.erb │ │ └── show.html.haml.erb │ │ ├── haml_bootstrap │ │ ├── _form.html.haml.erb │ │ ├── edit.html.haml.erb │ │ ├── index.html.haml.erb │ │ ├── new.html.haml.erb │ │ └── show.html.haml.erb │ │ └── jbuilder │ │ ├── index.json.jbuilder.erb │ │ └── show.json.jbuilder.erb ├── rails-admin-scaffold │ └── version.rb └── rails_admin_scaffold.rb ├── rails-admin-scaffold.gemspec └── test ├── fixtures └── routes.rb ├── lib └── generators │ └── admin │ ├── scaffold_controller_generator_test.rb │ └── testing_helper.rb └── test_helper.rb /.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore gem-related folders 2 | *.gem 3 | .bundle/ 4 | 5 | # Ignore rails temp directory for tests 6 | tmp/ 7 | 8 | # Ignore project settings 9 | .idea/ 10 | *.iml 11 | -------------------------------------------------------------------------------- /.ruby-version: -------------------------------------------------------------------------------- 1 | ruby-2.1.5 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | 3 | rvm: 4 | - 1.9.3 5 | - 2.0.0 6 | - 2.1.0 7 | - 2.1.5 8 | - ruby-head 9 | 10 | gemfile: 11 | - gemfiles/rails_4_0.gemfile 12 | - gemfiles/rails_4_1.gemfile 13 | 14 | matrix: 15 | allow_failures: 16 | - rvm: ruby-head 17 | 18 | notifications: 19 | email: false -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | 3 | # Declare your gem's dependencies in rails_admin_scaffold.gemspec. 4 | # Bundler will treat runtime dependencies like base dependencies, and 5 | # development dependencies will be added by default to the :development group. 6 | gemspec 7 | 8 | # Declare any dependencies that are still in development here instead of in 9 | # your gemspec. These might include edge Rails or gems from your path or 10 | # Git. Remember to move these dependencies to your gemspec before releasing 11 | # your gem to rubygems.org. 12 | 13 | # To use debugger 14 | # gem 'debugger' 15 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | PATH 2 | remote: . 3 | specs: 4 | rails-admin-scaffold (0.1.0) 5 | rails (>= 4.0) 6 | 7 | GEM 8 | remote: https://rubygems.org/ 9 | specs: 10 | actionmailer (4.1.8) 11 | actionpack (= 4.1.8) 12 | actionview (= 4.1.8) 13 | mail (~> 2.5, >= 2.5.4) 14 | actionpack (4.1.8) 15 | actionview (= 4.1.8) 16 | activesupport (= 4.1.8) 17 | rack (~> 1.5.2) 18 | rack-test (~> 0.6.2) 19 | actionview (4.1.8) 20 | activesupport (= 4.1.8) 21 | builder (~> 3.1) 22 | erubis (~> 2.7.0) 23 | activemodel (4.1.8) 24 | activesupport (= 4.1.8) 25 | builder (~> 3.1) 26 | activerecord (4.1.8) 27 | activemodel (= 4.1.8) 28 | activesupport (= 4.1.8) 29 | arel (~> 5.0.0) 30 | activesupport (4.1.8) 31 | i18n (~> 0.6, >= 0.6.9) 32 | json (~> 1.7, >= 1.7.7) 33 | minitest (~> 5.1) 34 | thread_safe (~> 0.1) 35 | tzinfo (~> 1.1) 36 | arel (5.0.1.20140414130214) 37 | builder (3.2.2) 38 | erubis (2.7.0) 39 | hike (1.2.3) 40 | i18n (0.6.11) 41 | json (1.8.1) 42 | mail (2.6.3) 43 | mime-types (>= 1.16, < 3) 44 | mime-types (2.4.3) 45 | minitest (5.4.3) 46 | multi_json (1.10.1) 47 | rack (1.5.2) 48 | rack-test (0.6.2) 49 | rack (>= 1.0) 50 | rails (4.1.8) 51 | actionmailer (= 4.1.8) 52 | actionpack (= 4.1.8) 53 | actionview (= 4.1.8) 54 | activemodel (= 4.1.8) 55 | activerecord (= 4.1.8) 56 | activesupport (= 4.1.8) 57 | bundler (>= 1.3.0, < 2.0) 58 | railties (= 4.1.8) 59 | sprockets-rails (~> 2.0) 60 | railties (4.1.8) 61 | actionpack (= 4.1.8) 62 | activesupport (= 4.1.8) 63 | rake (>= 0.8.7) 64 | thor (>= 0.18.1, < 2.0) 65 | rake (10.1.0) 66 | sprockets (2.12.3) 67 | hike (~> 1.2) 68 | multi_json (~> 1.0) 69 | rack (~> 1.0) 70 | tilt (~> 1.1, != 1.3.0) 71 | sprockets-rails (2.2.0) 72 | actionpack (>= 3.0) 73 | activesupport (>= 3.0) 74 | sprockets (>= 2.8, < 4.0) 75 | thor (0.19.1) 76 | thread_safe (0.3.4) 77 | tilt (1.4.1) 78 | tzinfo (1.2.2) 79 | thread_safe (~> 0.1) 80 | 81 | PLATFORMS 82 | ruby 83 | 84 | DEPENDENCIES 85 | bundler (~> 1.2) 86 | rails-admin-scaffold! 87 | rake 88 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Kirill Kalachev (aka dhampik) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | 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, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | rails-admin-scaffold [![Build Status](https://travis-ci.org/dhampik/rails-admin-scaffold.png?branch=master)](https://travis-ci.org/dhampik/rails-admin-scaffold) 2 | ==================== 3 | 4 | Rails 4 generator which allows to scaffold admin controllers, views with proper (non-namespaced) models, helpers, tests and routes 5 | 6 | How to use 7 | ----------- 8 | * Add ```gem 'rails-admin-scaffold', 'x.x.x'``` to your Gemfile 9 | * Run ```bundle install``` 10 | * Create admin scaffold with e. g. ```bin/rails g admin:scaffold_controller Post title:string content:text published:boolean``` 11 | * And don't forget to generate model itself ```bin/rails g model Post title:string content:text published:boolean``` and apply migrations ```bin/rake db:migrate``` 12 | 13 | #### How to customize your generator templates 14 | 15 | Put the '_form.html.erb.erb', 'edit.html.erb.erb', 'index.html.erb.erb', 'new.html.erb.erb', 'show.html.erb.erb' into '/lib/templates/admin/scaffold_controller/views/erb/' 16 | 17 | Then the generator will use those templates to generate scaffold. 18 | 19 | #### How to customize your prefix name 20 | 21 | Type your command with option --prefix_name=xxxx. 22 | 23 | If you want change the prefix name 'admin' to 'manager' for example you can do something like this: 24 | 25 | ```bin/rails g admin:scaffold_controller Post title:string content:text published:boolean --prefix_name=manager``` 26 | 27 | This will generate `class Manager::PostsController < ApplicationController` 28 | 29 | #### How to customize your parent controller 30 | 31 | If you want all your Admin controllers to inherit from another 32 | controller like `AdminController` you can specify the 33 | --parent_controller option: 34 | 35 | ```bin/rails g admin:scaffold_controller Post title:string content:text published:boolean --parent_controller=admin``` 36 | 37 | This will generate `class Admin::PostsController < AdminController` 38 | 39 | #### How to generate bootstrap-friendly views 40 | 41 | If you want to generate bootstrap friendly views, use the `--bootstrap` option (`-b` for short) 42 | 43 | ```bin/rails g admin:scaffold_controller Post title:string content:text published:boolean --bootstrap``` 44 | 45 | Supports 46 | -------- 47 | * Rails 4+ 48 | * Rails default generators (erb, test::unit) 49 | * Haml (if haml is used for views generation) 50 | * Twitter bootstrap support 51 | * Jbuilder (if jbuilder is installed for the project) 52 | 53 | Plans 54 | ------ 55 | * add haml suppurt 56 | * add jbuilder support 57 | * improve tests 58 | * use travis for ci 59 | * ability to specify parent controller name (e.g. AdminController instead of ApplicationController) 60 | * add minitest support 61 | * add rspec support 62 | * split controller_scaffold into several separate generators 63 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'bundler' 2 | Bundler::GemHelper.install_tasks 3 | 4 | require 'rake/testtask' 5 | Rake::TestTask.new(:test) do |test| 6 | test.libs << 'lib' << 'test' 7 | test.pattern = 'test/**/*_test.rb' 8 | test.verbose = true 9 | end 10 | 11 | task :default => :test 12 | -------------------------------------------------------------------------------- /gemfiles/rails_4_0.gemfile: -------------------------------------------------------------------------------- 1 | # This file was generated by Appraisal 2 | 3 | source "https://rubygems.org" 4 | 5 | gem "rails", "~> 4.0.0" 6 | 7 | gemspec :path => "../" -------------------------------------------------------------------------------- /gemfiles/rails_4_1.gemfile: -------------------------------------------------------------------------------- 1 | # This file was generated by Appraisal 2 | 3 | source "https://rubygems.org" 4 | 5 | gem "rails", "~> 4.1.0" 6 | 7 | gemspec :path => "../" -------------------------------------------------------------------------------- /lib/generators/admin/scaffold_controller/USAGE: -------------------------------------------------------------------------------- 1 | Description: 2 | Stubs out a scaffolded controller, its seven RESTful actions and related 3 | views. Pass the model name, either CamelCased or under_scored. The 4 | controller name is retrieved as a pluralized version of the model name 5 | namespaces within Admin namespaced. 6 | 7 | This generates a controller class in app/controllers/admin and invokes helper, 8 | template engine and test framework generators. 9 | It uses non-namespaced model in app/models 10 | 11 | If you want change the prefix name 'admin' to something else you can pass --prefix_name option 12 | 13 | Example: 14 | `bin/rails generate admin:scaffold_controller Post` 15 | `bin/rails g admin:scaffold_controller Post title:string content:text published:boolean --prefix_name=manager` 16 | -------------------------------------------------------------------------------- /lib/generators/admin/scaffold_controller/scaffold_controller_generator.rb: -------------------------------------------------------------------------------- 1 | require 'rubygems/specification' 2 | require 'rails/generators/named_base' 3 | require 'rails/generators/resource_helpers' 4 | 5 | module Admin 6 | module Generators 7 | class ScaffoldControllerGenerator < Rails::Generators::NamedBase 8 | include Rails::Generators::ResourceHelpers 9 | 10 | source_root File.expand_path('../templates', __FILE__) 11 | 12 | class_option :template_engine, desc: 'Template engine to be invoked (erb or haml).' 13 | 14 | check_class_collision suffix: "Controller" 15 | 16 | check_class_collision suffix: "ControllerTest" 17 | 18 | check_class_collision suffix: "Helper" 19 | 20 | class_option :orm, banner: "NAME", type: :string, required: true, 21 | desc: "ORM to generate the controller for" 22 | 23 | class_option :html, type: :boolean, default: true, 24 | desc: "Generate a scaffold with HTML output" 25 | 26 | class_option :prefix_name, banner: "admin", type: :string, default: "admin", 27 | desc: "Define the prefix of controller" 28 | 29 | class_option :parent_controller, banner: "admin", type: :string, default: "application", 30 | desc: "Define the parent controller" 31 | 32 | class_option :bootstrap, required: false, default: nil, aliases: '-b', 33 | desc: "Use bootstrap for templates" 34 | 35 | argument :attributes, type: :array, default: [], banner: "field:type field:type" 36 | 37 | def initialize(args, *options) #:nodoc: 38 | super 39 | end 40 | 41 | hook_for :resource_route, in: :rails do |resource_route| 42 | invoke resource_route, [prefixed_class_name] 43 | end 44 | 45 | def create_controller_files 46 | # I think there should be a better way to detect if jbuilder is in use 47 | # If you know it, please let me know 48 | if Gem::Specification.find_all_by_name('jbuilder').length >= 1 49 | template "controllers/jbuilder/controller.rb.erb", File.join('app/controllers', prefix, class_path, "#{controller_file_name}_controller.rb") 50 | else 51 | template "controllers/railties/controller.rb.erb", File.join('app/controllers', prefix, class_path, "#{controller_file_name}_controller.rb") 52 | end 53 | end 54 | 55 | def create_test_files 56 | template "tests/test_unit/functional_test.rb.erb", File.join("test/controllers", prefix, controller_class_path, "#{controller_file_name}_controller_test.rb") 57 | end 58 | 59 | hook_for :helper, in: :rails do |helper| 60 | invoke helper, [prefixed_controller_class_name] 61 | end 62 | 63 | def create_root_folder 64 | empty_directory File.join("app/views", prefix, controller_file_path) 65 | end 66 | 67 | def copy_view_files 68 | available_views.each do |view| 69 | filename = filename_with_extensions(view) 70 | if bootstrap 71 | template_path = "views/#{handler}_bootstrap/#{filename}.erb" 72 | else 73 | template_path = "views/#{handler}/#{filename}.erb" 74 | end 75 | template template_path, File.join("app/views", prefix, controller_file_path, filename) 76 | end 77 | 78 | # I think there should be a better way to detect if jbuilder is in use 79 | if Gem::Specification.find_all_by_name('jbuilder').length >= 1 80 | %w(index show).each do |view| 81 | template "views/jbuilder/#{view}.json.jbuilder.erb", File.join("app/views", prefix, controller_file_path, "#{view}.json.jbuilder") 82 | end 83 | end 84 | end 85 | 86 | hook_for :assets, in: :rails do |assets| 87 | invoke assets, [prefixed_class_name] 88 | end 89 | 90 | protected 91 | 92 | def bootstrap 93 | options[:bootstrap] 94 | end 95 | 96 | def prefix 97 | options[:prefix_name] 98 | end 99 | 100 | def prefixed_class_name 101 | "#{prefix.capitalize}::#{class_name}" 102 | end 103 | 104 | def prefixed_controller_class_name 105 | "#{prefix.capitalize}::#{controller_class_name}" 106 | end 107 | 108 | def parent_controller_class_name 109 | options[:parent_controller].capitalize 110 | end 111 | 112 | def prefixed_route_url 113 | "/#{prefix}#{route_url}" 114 | end 115 | 116 | def prefixed_plain_model_url 117 | "#{prefix}_#{singular_table_name}" 118 | end 119 | 120 | def prefixed_index_helper 121 | "#{prefix}_#{index_helper}" 122 | end 123 | 124 | def available_views 125 | %w(index edit show new _form) 126 | end 127 | 128 | def format 129 | :html 130 | end 131 | 132 | def handler 133 | options[:template_engine] 134 | end 135 | 136 | def filename_with_extensions(name) 137 | [name, format, handler].compact.join(".") 138 | end 139 | 140 | # Add a class collisions name to be checked on class initialization. You 141 | # can supply a hash with a :prefix or :suffix to be tested. 142 | # 143 | # ==== Examples 144 | # 145 | # check_class_collision suffix: "Decorator" 146 | # 147 | # If the generator is invoked with class name Admin, it will check for 148 | # the presence of "AdminDecorator". 149 | # 150 | def self.check_class_collision(options={}) 151 | define_method :check_class_collision do 152 | name = if self.respond_to?(:prefixed_controller_class_name) # for ScaffoldBase 153 | prefixed_controller_class_name 154 | elsif self.respond_to?(:prefixed_controller_class_name) # for ScaffoldBase 155 | controller_class_name 156 | else 157 | class_name 158 | end 159 | 160 | class_collisions "#{options[:prefix]}#{name}#{options[:suffix]}" 161 | end 162 | end 163 | 164 | def attributes_hash 165 | return if attributes_names.empty? 166 | 167 | attributes_names.map do |name| 168 | if %w(password password_confirmation).include?(name) && attributes.any?(&:password_digest?) 169 | "#{name}: 'secret'" 170 | else 171 | "#{name}: @#{singular_table_name}.#{name}" 172 | end 173 | end.sort.join(', ') 174 | end 175 | 176 | def attributes_list_with_timestamps 177 | attributes_list(attributes_names + %w(created_at updated_at)) 178 | end 179 | 180 | def attributes_list(attributes = attributes_names) 181 | if self.attributes.any? {|attr| attr.name == 'password' && attr.type == :digest} 182 | attributes = attributes.reject {|name| %w(password password_confirmation).include? name} 183 | end 184 | 185 | attributes.map { |a| ":#{a}"} * ', ' 186 | end 187 | 188 | end 189 | end 190 | end 191 | -------------------------------------------------------------------------------- /lib/generators/admin/scaffold_controller/templates/controllers/jbuilder/controller.rb.erb: -------------------------------------------------------------------------------- 1 | <% if namespaced? -%> 2 | require_dependency "<%= namespaced_file_path %>/application_controller" 3 | 4 | <% end -%> 5 | <% module_namespacing do -%> 6 | class <%= prefixed_controller_class_name %>Controller < <%= parent_controller_class_name %>Controller 7 | before_action :set_<%= singular_table_name %>, only: [:show, :edit, :update, :destroy] 8 | 9 | # GET <%= prefixed_route_url %> 10 | # GET <%= prefixed_route_url %>.json 11 | def index 12 | @<%= plural_table_name %> = <%= orm_class.all(class_name) %> 13 | end 14 | 15 | # GET <%= prefixed_route_url %>/1 16 | # GET <%= prefixed_route_url %>/1.json 17 | def show 18 | end 19 | 20 | # GET <%= prefixed_route_url %>/new 21 | def new 22 | @<%= singular_table_name %> = <%= orm_class.build(class_name) %> 23 | end 24 | 25 | # GET <%= prefixed_route_url %>/1/edit 26 | def edit 27 | end 28 | 29 | # POST <%= prefixed_route_url %> 30 | # POST <%= prefixed_route_url %>.json 31 | def create 32 | @<%= singular_table_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %> 33 | 34 | respond_to do |format| 35 | if @<%= orm_instance.save %> 36 | format.html { redirect_to <%= "[:#{prefix}, @#{singular_table_name}]" %>, notice: <%= "'#{human_name} was successfully created.'" %> } 37 | format.json { render action: 'show', status: :created, location: <%= "@#{singular_table_name}" %> } 38 | else 39 | format.html { render action: 'new' } 40 | format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity } 41 | end 42 | end 43 | end 44 | 45 | # PATCH/PUT <%= prefixed_route_url %>/1 46 | # PATCH/PUT <%= prefixed_route_url %>/1.json 47 | def update 48 | respond_to do |format| 49 | if @<%= orm_instance.update("#{singular_table_name}_params") %> 50 | format.html { redirect_to <%= "[:#{prefix}, @#{singular_table_name}]" %>, notice: <%= "'#{human_name} was successfully updated.'" %> } 51 | format.json { head :no_content } 52 | else 53 | format.html { render action: 'edit' } 54 | format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity } 55 | end 56 | end 57 | end 58 | 59 | # DELETE <%= prefixed_route_url %>/1 60 | # DELETE <%= prefixed_route_url %>/1.json 61 | def destroy 62 | @<%= orm_instance.destroy %> 63 | respond_to do |format| 64 | format.html { redirect_to <%= prefixed_index_helper %>_url, notice: <%= "'#{human_name} was successfully destroyed.'" %> } 65 | format.json { head :no_content } 66 | end 67 | end 68 | 69 | private 70 | # Use callbacks to share common setup or constraints between actions. 71 | def set_<%= singular_table_name %> 72 | @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %> 73 | end 74 | 75 | # Never trust parameters from the scary internet, only allow the white list through. 76 | def <%= "#{singular_table_name}_params" %> 77 | <%- if attributes_names.empty? -%> 78 | params[<%= ":#{singular_table_name}" %>] 79 | <%- else -%> 80 | params.require(<%= ":#{singular_table_name}" %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>) 81 | <%- end -%> 82 | end 83 | end 84 | <% end -%> 85 | -------------------------------------------------------------------------------- /lib/generators/admin/scaffold_controller/templates/controllers/railties/controller.rb.erb: -------------------------------------------------------------------------------- 1 | <% if namespaced? -%> 2 | require_dependency "<%= namespaced_file_path %>/application_controller" 3 | 4 | <% end -%> 5 | <% module_namespacing do -%> 6 | class <%= prefixed_controller_class_name %>Controller < <%= parent_controller_class_name %>Controller 7 | before_action :set_<%= singular_table_name %>, only: [:show, :edit, :update, :destroy] 8 | 9 | # GET <%= prefixed_route_url %> 10 | def index 11 | @<%= plural_table_name %> = <%= orm_class.all(class_name) %> 12 | end 13 | 14 | # GET <%= prefixed_route_url %>/1 15 | def show 16 | end 17 | 18 | # GET <%= prefixed_route_url %>/new 19 | def new 20 | @<%= singular_table_name %> = <%= orm_class.build(class_name) %> 21 | end 22 | 23 | # GET <%= prefixed_route_url %>/1/edit 24 | def edit 25 | end 26 | 27 | # POST <%= prefixed_route_url %> 28 | def create 29 | @<%= singular_table_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %> 30 | 31 | if @<%= orm_instance.save %> 32 | redirect_to <%= "[:#{prefix}, @#{singular_table_name}]" %>, notice: <%= "'#{human_name} was successfully created.'" %> 33 | else 34 | render action: 'new' 35 | end 36 | end 37 | 38 | # PATCH/PUT <%= prefixed_route_url %>/1 39 | def update 40 | if @<%= orm_instance.update("#{singular_table_name}_params") %> 41 | redirect_to <%= "[:#{prefix}, @#{singular_table_name}]" %>, notice: <%= "'#{human_name} was successfully updated.'" %> 42 | else 43 | render action: 'edit' 44 | end 45 | end 46 | 47 | # DELETE <%= prefixed_route_url %>/1 48 | def destroy 49 | @<%= orm_instance.destroy %> 50 | redirect_to <%= prefixed_index_helper %>_url, notice: <%= "'#{human_name} was successfully destroyed.'" %> 51 | end 52 | 53 | private 54 | # Use callbacks to share common setup or constraints between actions. 55 | def set_<%= singular_table_name %> 56 | @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %> 57 | end 58 | 59 | # Only allow a trusted parameter "white list" through. 60 | def <%= "#{singular_table_name}_params" %> 61 | <%- if attributes_names.empty? -%> 62 | params[<%= ":#{singular_table_name}" %>] 63 | <%- else -%> 64 | params.require(<%= ":#{singular_table_name}" %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>) 65 | <%- end -%> 66 | end 67 | end 68 | <% end -%> 69 | -------------------------------------------------------------------------------- /lib/generators/admin/scaffold_controller/templates/tests/test_unit/functional_test.rb.erb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | <% module_namespacing do -%> 4 | class <%= prefixed_controller_class_name %>ControllerTest < ActionController::TestCase 5 | setup do 6 | @<%= singular_table_name %> = <%= table_name %>(:one) 7 | end 8 | 9 | test "should get index" do 10 | get :index 11 | assert_response :success 12 | assert_not_nil assigns(:<%= table_name %>) 13 | end 14 | 15 | test "should get new" do 16 | get :new 17 | assert_response :success 18 | end 19 | 20 | test "should create <%= singular_table_name %>" do 21 | assert_difference('<%= class_name %>.count') do 22 | post :create, <%= "#{singular_table_name}: { #{attributes_hash} }" %> 23 | end 24 | 25 | assert_redirected_to <%= prefixed_plain_model_url %>_path(assigns(:<%= singular_table_name %>)) 26 | end 27 | 28 | test "should show <%= singular_table_name %>" do 29 | get :show, id: <%= "@#{singular_table_name}" %> 30 | assert_response :success 31 | end 32 | 33 | test "should get edit" do 34 | get :edit, id: <%= "@#{singular_table_name}" %> 35 | assert_response :success 36 | end 37 | 38 | test "should update <%= singular_table_name %>" do 39 | patch :update, id: <%= "@#{singular_table_name}" %>, <%= "#{singular_table_name}: { #{attributes_hash} }" %> 40 | assert_redirected_to <%= prefixed_plain_model_url %>_path(assigns(:<%= singular_table_name %>)) 41 | end 42 | 43 | test "should destroy <%= singular_table_name %>" do 44 | assert_difference('<%= class_name %>.count', -1) do 45 | delete :destroy, id: <%= "@#{singular_table_name}" %> 46 | end 47 | 48 | assert_redirected_to <%= prefixed_index_helper %>_path 49 | end 50 | end 51 | <% end -%> 52 | -------------------------------------------------------------------------------- /lib/generators/admin/scaffold_controller/templates/views/erb/_form.html.erb.erb: -------------------------------------------------------------------------------- 1 | <%%= form_for(<%= "[:#{prefix}, @#{singular_table_name}]" %>) do |f| %> 2 | <%% if @<%= singular_table_name %>.errors.any? %> 3 |
4 |

<%%= pluralize(@<%= singular_table_name %>.errors.count, "error") %> prohibited this <%= singular_table_name %> from being saved:

5 | 6 | 11 |
12 | <%% end %> 13 | 14 | <% attributes.each do |attribute| -%> 15 |
16 | <% if attribute.password_digest? -%> 17 | <%%= f.label :password %>
18 | <%%= f.password_field :password %> 19 |
20 |
21 | <%%= f.label :password_confirmation %>
22 | <%%= f.password_field :password_confirmation %> 23 | <% else -%> 24 | <% if attribute.reference? -%> 25 | <%%= f.label :<%= attribute.column_name %> %>
26 | <%%= f.<%= attribute.field_type %> :<%= attribute.column_name %> %> 27 | <% else -%> 28 | <%%= f.label :<%= attribute.name %> %>
29 | <%%= f.<%= attribute.field_type %> :<%= attribute.name %> %> 30 | <% end -%> 31 | <% end -%> 32 |
33 | <% end -%> 34 |
35 | <%%= f.submit %> 36 |
37 | <%% end %> 38 | -------------------------------------------------------------------------------- /lib/generators/admin/scaffold_controller/templates/views/erb/edit.html.erb.erb: -------------------------------------------------------------------------------- 1 |

Editing <%= singular_table_name %>

2 | 3 | <%%= render 'form' %> 4 | 5 | <%%= link_to 'Show', <%= "[:#{prefix}, @#{singular_table_name}]" %> %> | 6 | <%%= link_to 'Back', <%= prefixed_index_helper %>_path %> 7 | -------------------------------------------------------------------------------- /lib/generators/admin/scaffold_controller/templates/views/erb/index.html.erb.erb: -------------------------------------------------------------------------------- 1 |

Listing <%= plural_table_name %>

2 | 3 | 4 | 5 | 6 | <% attributes.reject(&:password_digest?).each do |attribute| -%> 7 | 8 | <% end -%> 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | <%% @<%= plural_table_name %>.each do |<%= singular_table_name %>| %> 17 | 18 | <% attributes.reject(&:password_digest?).each do |attribute| -%> 19 | 20 | <% end -%> 21 | 22 | 23 | 24 | 25 | <%% end %> 26 | 27 |
<%= attribute.human_name %>
<%%= <%= singular_table_name %>.<%= attribute.name %> %><%%= link_to 'Show', <%= "[:#{prefix}, #{singular_table_name}]" %> %><%%= link_to 'Edit', edit_<%= prefixed_plain_model_url %>_path(<%= singular_table_name %>) %><%%= link_to 'Destroy', <%= "[:#{prefix}, #{singular_table_name}]" %>, method: :delete, data: { confirm: 'Are you sure?' } %>
28 | 29 |
30 | 31 | <%%= link_to 'New <%= human_name %>', new_<%= prefixed_plain_model_url %>_path %> 32 | -------------------------------------------------------------------------------- /lib/generators/admin/scaffold_controller/templates/views/erb/new.html.erb.erb: -------------------------------------------------------------------------------- 1 |

New <%= singular_table_name %>

2 | 3 | <%%= render 'form' %> 4 | 5 | <%%= link_to 'Back', <%= prefixed_index_helper %>_path %> 6 | -------------------------------------------------------------------------------- /lib/generators/admin/scaffold_controller/templates/views/erb/show.html.erb.erb: -------------------------------------------------------------------------------- 1 |

<%%= notice %>

2 | 3 | <% attributes.reject(&:password_digest?).each do |attribute| -%> 4 |

5 | <%= attribute.human_name %>: 6 | <%%= @<%= singular_table_name %>.<%= attribute.name %> %> 7 |

8 | 9 | <% end -%> 10 | <%%= link_to 'Edit', edit_<%= prefixed_plain_model_url %>_path(@<%= singular_table_name %>) %> | 11 | <%%= link_to 'Back', <%= prefixed_index_helper %>_path %> 12 | -------------------------------------------------------------------------------- /lib/generators/admin/scaffold_controller/templates/views/erb_bootstrap/_form.html.erb.erb: -------------------------------------------------------------------------------- 1 | <%%= form_for(<%= "[:#{prefix}, @#{singular_table_name}]" %>, html: { class: 'form-horizontal <%= singular_table_name %>' }) do |f| %> 2 | <%% if @<%= singular_table_name %>.errors.any? %> 3 |
4 |
5 |

<%%= pluralize(@<%= singular_table_name %>.errors.count, "error") %> prohibited this <%= singular_table_name %> from being saved:

6 |
7 |
8 | 13 |
14 |
15 | <%% end %> 16 | 17 | <% attributes.each do |attribute| -%> 18 |
19 | <%- if attribute.field_type != :check_box -%> 20 | <%- if attribute.password_digest? -%> 21 | <%%= f.label :password, class: 'control-label col-md-2' %> 22 |
23 | <%%= f.password_field :password, class: 'form-control' %> 24 |
25 |
26 |
27 | <%%= f.label :password_confirmation, class: 'control-label col-md-2' %> 28 |
29 | <%%= f.password_field :password_confirmation, class: 'form-control' %> 30 |
31 | <%- else -%> 32 | <%- if attribute.reference? -%> 33 | <%%= f.label :<%= attribute.column_name %>, class: 'control-label col-md-2' %> 34 |
35 | <%%= f.<%= attribute.field_type %> :<%= attribute.column_name %>, class: 'form-control' %> 36 |
37 | <%- else -%> 38 | <%%= f.label :<%= attribute.name %>, class: 'control-label col-md-2' %> 39 |
40 | <%%= f.<%= attribute.field_type %> :<%= attribute.name %>, class: 'form-control' %> 41 |
42 | <%- end -%> 43 | <%- end -%> 44 | <%- else -%> 45 |
46 | <%%= f.label :<%= attribute.name %>, for: nil do %> 47 | <%%= f.<%= attribute.field_type %> :<%= attribute.name %> %> 48 | <%%= <%= class_name %>.human_attribute_name(:<%= attribute.name %>) %> 49 | <%% end %> 50 |
51 | <%- end -%> 52 |
53 | <% end -%> 54 |
55 |
56 | <%%= f.submit nil, class: 'btn btn-primary' %> 57 |
58 |
59 | <%% end %> 60 | -------------------------------------------------------------------------------- /lib/generators/admin/scaffold_controller/templates/views/erb_bootstrap/edit.html.erb.erb: -------------------------------------------------------------------------------- 1 | 4 | 5 | <%%= render 'form' %> 6 | 7 | <%%= link_to 'Show', <%= "[:#{prefix}, @#{singular_table_name}]" %>, class: 'btn btn-default' %> 8 | <%%= link_to 'Back', <%= prefixed_index_helper %>_path, class: 'btn btn-default' %> 9 | -------------------------------------------------------------------------------- /lib/generators/admin/scaffold_controller/templates/views/erb_bootstrap/index.html.erb.erb: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | 7 | 8 | <% attributes.reject(&:password_digest?).each do |attribute| -%> 9 | 10 | <% end -%> 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | <%% @<%= plural_table_name %>.each do |<%= singular_table_name %>| %> 19 | 20 | <% attributes.reject(&:password_digest?).each do |attribute| -%> 21 | 22 | <% end -%> 23 | 24 | 25 | 26 | 27 | <%% end %> 28 | 29 |
<%= attribute.human_name %>
<%%= <%= singular_table_name %>.<%= attribute.name %> %><%%= link_to 'Show', <%= "[:#{prefix}, #{singular_table_name}]" %>, class: 'btn btn-default btn-xs' %><%%= link_to 'Edit', edit_<%= prefixed_plain_model_url %>_path(<%= singular_table_name %>), class: 'btn btn-default btn-xs' %><%%= link_to 'Destroy', <%= "[:#{prefix}, #{singular_table_name}]" %>, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-xs btn-danger' %>
30 | 31 |
32 | 33 | <%%= link_to 'New <%= human_name %>', new_<%= prefixed_plain_model_url %>_path, class: 'btn btn-primary' %> 34 | -------------------------------------------------------------------------------- /lib/generators/admin/scaffold_controller/templates/views/erb_bootstrap/new.html.erb.erb: -------------------------------------------------------------------------------- 1 | 4 | 5 | <%%= render 'form' %> 6 | 7 | <%%= link_to 'Back', <%= prefixed_index_helper %>_path, class: 'btn btn-default' %> 8 | -------------------------------------------------------------------------------- /lib/generators/admin/scaffold_controller/templates/views/erb_bootstrap/show.html.erb.erb: -------------------------------------------------------------------------------- 1 |

<%%= notice %>

2 | 3 | <% attributes.reject(&:password_digest?).each do |attribute| -%> 4 |
5 |
<%= attribute.human_name %>:
6 |
<%%= @<%= singular_table_name %>.<%= attribute.name %> %>
7 |
8 | 9 | <% end -%> 10 | <%%= link_to 'Edit', edit_<%= prefixed_plain_model_url %>_path(@<%= singular_table_name %>), class: 'btn btn-default' %> 11 | <%%= link_to 'Back', <%= prefixed_index_helper %>_path, class: 'btn btn-default' %> 12 | -------------------------------------------------------------------------------- /lib/generators/admin/scaffold_controller/templates/views/haml/_form.html.haml.erb: -------------------------------------------------------------------------------- 1 | = form_for(<%= "[:#{prefix}, @#{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/admin/scaffold_controller/templates/views/haml/edit.html.haml.erb: -------------------------------------------------------------------------------- 1 | %h1 Editing <%= singular_table_name %> 2 | 3 | = render 'form' 4 | 5 | = link_to 'Show', <%= "[:#{prefix}, @#{singular_table_name}]" %> 6 | \| 7 | = link_to 'Back', <%= prefixed_index_helper %>_path 8 | -------------------------------------------------------------------------------- /lib/generators/admin/scaffold_controller/templates/views/haml/index.html.haml.erb: -------------------------------------------------------------------------------- 1 | %h1 Listing <%= plural_table_name %> 2 | 3 | %table 4 | %tr 5 | <% for attribute in attributes -%> 6 | %th <%= attribute.human_name %> 7 | <% end -%> 8 | %th 9 | %th 10 | %th 11 | 12 | - @<%= plural_table_name %>.each do |<%= singular_table_name %>| 13 | %tr 14 | <% for attribute in attributes -%> 15 | %td= <%= singular_table_name %>.<%= attribute.name %> 16 | <% end -%> 17 | %td= link_to 'Show', <%= "[:#{prefix}, #{singular_table_name}]" %> 18 | %td= link_to 'Edit', edit_<%= prefixed_plain_model_url %>_path(<%= singular_table_name %>) 19 | %td= link_to 'Destroy', <%= "[:#{prefix}, #{singular_table_name}]" %>, :method => :delete, :data => { :confirm => 'Are you sure?' } 20 | 21 | %br 22 | 23 | = link_to 'New <%= human_name %>', new_<%= prefixed_plain_model_url %>_path 24 | -------------------------------------------------------------------------------- /lib/generators/admin/scaffold_controller/templates/views/haml/new.html.haml.erb: -------------------------------------------------------------------------------- 1 | %h1 New <%= singular_table_name %> 2 | 3 | = render 'form' 4 | 5 | = link_to 'Back', <%= prefixed_index_helper %>_path 6 | -------------------------------------------------------------------------------- /lib/generators/admin/scaffold_controller/templates/views/haml/show.html.haml.erb: -------------------------------------------------------------------------------- 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_<%= prefixed_plain_model_url %>_path(@<%= singular_table_name %>) 10 | \| 11 | = link_to 'Back', <%= prefixed_index_helper %>_path 12 | -------------------------------------------------------------------------------- /lib/generators/admin/scaffold_controller/templates/views/haml_bootstrap/_form.html.haml.erb: -------------------------------------------------------------------------------- 1 | = form_for(<%= "[:#{prefix}, @#{singular_table_name}], html: { class: 'form-horizontal #{singular_table_name}'} " %>) do |f| 2 | - if @<%= singular_table_name %>.errors.any? 3 | #error_explanation.panel.panel-danger 4 | .panel-heading 5 | %h2.panel-title= "#{pluralize(@<%= singular_table_name %>.errors.count, "error")} prohibited this <%= singular_table_name %> from being saved:" 6 | .panel-body 7 | %ul 8 | - @<%= singular_table_name %>.errors.full_messages.each do |msg| 9 | %li= msg 10 | 11 | <% for attribute in attributes -%> 12 | <%- if attribute.field_type != :check_box -%> 13 | .form-group 14 | = f.label :<%= attribute.name %>, class: 'control-label col-md-2' 15 | .col-md-10= f.<%= attribute.field_type %> :<%= attribute.name %>, class: 'form-control' 16 | <%- else -%> 17 | .col-md-offset-2.col-md-10 18 | = f.label :<%= attribute.name %>, for: nil do 19 | = f.<%= attribute.field_type %> :<%= attribute.name %> 20 | = <%= class_name %>.human_attribute_name(:<%= attribute.name %>) 21 | <%- end -%> 22 | <% end -%> 23 | .form-group 24 | .col-md-offset-2.col-md-10 25 | = f.submit nil, class: 'btn btn-primary' 26 | -------------------------------------------------------------------------------- /lib/generators/admin/scaffold_controller/templates/views/haml_bootstrap/edit.html.haml.erb: -------------------------------------------------------------------------------- 1 | .page-header 2 | %h1 Editing <%= singular_table_name %> 3 | 4 | = render 'form' 5 | 6 | = link_to 'Show', <%= "[:#{prefix}, @#{singular_table_name}]" %>, class: 'btn btn-default' 7 | = link_to 'Back', <%= prefixed_index_helper %>_path, class: 'btn btn-default' 8 | -------------------------------------------------------------------------------- /lib/generators/admin/scaffold_controller/templates/views/haml_bootstrap/index.html.haml.erb: -------------------------------------------------------------------------------- 1 | .page-header 2 | %h1 Listing <%= plural_table_name %> 3 | 4 | %table.table.table-stripped 5 | %tr 6 | <% for attribute in attributes -%> 7 | %th <%= attribute.human_name %> 8 | <% end -%> 9 | %th 10 | %th 11 | %th 12 | 13 | - @<%= plural_table_name %>.each do |<%= singular_table_name %>| 14 | %tr 15 | <% for attribute in attributes -%> 16 | %td= <%= singular_table_name %>.<%= attribute.name %> 17 | <% end -%> 18 | %td= link_to 'Show', <%= "[:#{prefix}, #{singular_table_name}]" %>, class: 'btn btn-default btn-xs' 19 | %td= link_to 'Edit', edit_<%= prefixed_plain_model_url %>_path(<%= singular_table_name %>), class: 'btn btn-default btn-xs' 20 | %td= link_to 'Destroy', <%= "[:#{prefix}, #{singular_table_name}]" %>, :method => :delete, :data => { :confirm => 'Are you sure?' }, class: 'btn btn-danger btn-xs' 21 | 22 | %br 23 | 24 | = link_to 'New <%= human_name %>', new_<%= prefixed_plain_model_url %>_path, class: 'btn btn-primary' 25 | -------------------------------------------------------------------------------- /lib/generators/admin/scaffold_controller/templates/views/haml_bootstrap/new.html.haml.erb: -------------------------------------------------------------------------------- 1 | .page-header 2 | %h1 New <%= singular_table_name %> 3 | 4 | = render 'form' 5 | 6 | = link_to 'Back', <%= prefixed_index_helper %>_path, class: 'btn btn-default' 7 | -------------------------------------------------------------------------------- /lib/generators/admin/scaffold_controller/templates/views/haml_bootstrap/show.html.haml.erb: -------------------------------------------------------------------------------- 1 | %p#notice= notice 2 | 3 | <% for attribute in attributes -%> 4 | %dl.dl-horizontal 5 | %dt 6 | %strong <%= attribute.human_name %>: 7 | %dd= @<%= singular_table_name %>.<%= attribute.name %> 8 | <% end -%> 9 | 10 | = link_to 'Edit', edit_<%= prefixed_plain_model_url %>_path(@<%= singular_table_name %>), class: 'btn btn-default' 11 | = link_to 'Back', <%= prefixed_index_helper %>_path, class: 'btn btn-default' 12 | -------------------------------------------------------------------------------- /lib/generators/admin/scaffold_controller/templates/views/jbuilder/index.json.jbuilder.erb: -------------------------------------------------------------------------------- 1 | json.array!(@<%= plural_table_name %>) do |<%= singular_table_name %>| 2 | json.extract! <%= singular_table_name %>, <%= attributes_list %> 3 | json.url <%= singular_table_name %>_url(<%= singular_table_name %>, format: :json) 4 | end -------------------------------------------------------------------------------- /lib/generators/admin/scaffold_controller/templates/views/jbuilder/show.json.jbuilder.erb: -------------------------------------------------------------------------------- 1 | json.extract! @<%= singular_table_name %>, <%= attributes_list_with_timestamps %> -------------------------------------------------------------------------------- /lib/rails-admin-scaffold/version.rb: -------------------------------------------------------------------------------- 1 | module RailsAdminScaffold 2 | VERSION = "0.1.0" 3 | end 4 | -------------------------------------------------------------------------------- /lib/rails_admin_scaffold.rb: -------------------------------------------------------------------------------- 1 | require "rails-admin-scaffold/version" -------------------------------------------------------------------------------- /rails-admin-scaffold.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | $:.push File.expand_path("../lib", __FILE__) 3 | 4 | # Maintain your gem's version: 5 | require "rails-admin-scaffold/version" 6 | 7 | # Describe your gem and declare its dependencies: 8 | Gem::Specification.new do |s| 9 | s.name = "rails-admin-scaffold" 10 | s.version = RailsAdminScaffold::VERSION 11 | s.platform = Gem::Platform::RUBY 12 | s.authors = ["Kirill Kalachev"] 13 | s.email = ["dhampik@gmail.com"] 14 | s.homepage = "http://github.com/dhampik/rails-admin-scaffold" 15 | s.summary = "Rails admin controllers scaffolding generator" 16 | s.description = "Rails generator which allows to scaffold admin controllers, views with proper (non-namespaced) models, helpers, tests and routes" 17 | s.licenses = ["MIT"] 18 | 19 | s.rubyforge_project = "rails-admin-scaffold" 20 | s.required_rubygems_version = ">= 1.3.6" 21 | 22 | s.add_dependency "rails", [">= 4.0"] 23 | 24 | s.add_development_dependency "bundler", "~> 1.2" 25 | s.add_development_dependency "rake" 26 | 27 | s.files = Dir['LICENSE', 'README.md', 'lib/**/*'] 28 | s.test_files = Dir['test/**/*.rb'] 29 | s.require_path = 'lib' 30 | end 31 | -------------------------------------------------------------------------------- /test/fixtures/routes.rb: -------------------------------------------------------------------------------- 1 | TestApp.routes.draw do |map| 2 | end 3 | -------------------------------------------------------------------------------- /test/lib/generators/admin/scaffold_controller_generator_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | require 'lib/generators/admin/testing_helper' 3 | 4 | class Admin::Generators::ScaffoldControllerGeneratorTest < Rails::Generators::TestCase 5 | destination File.join(Rails.root) 6 | tests Admin::Generators::ScaffoldControllerGenerator 7 | arguments %w(User name:string age:integer) 8 | 9 | setup :prepare_destination 10 | setup :copy_routes 11 | 12 | def test_controller_skeleton_is_created 13 | run_generator 14 | 15 | assert_file "app/controllers/admin/users_controller.rb" do |content| 16 | assert_match(/class Admin::UsersController < ApplicationController/, content) 17 | 18 | assert_instance_method :index, content do |m| 19 | assert_match(/@users = User\.all/, m) 20 | end 21 | 22 | assert_instance_method :show, content 23 | 24 | assert_instance_method :new, content do |m| 25 | assert_match(/@user = User\.new/, m) 26 | end 27 | 28 | assert_instance_method :edit, content 29 | 30 | assert_instance_method :create, content do |m| 31 | assert_match(/@user = User\.new\(user_params\)/, m) 32 | assert_match(/@user\.save/, m) 33 | end 34 | 35 | assert_instance_method :update, content do |m| 36 | assert_match(/@user\.update\(user_params\)/, m) 37 | end 38 | 39 | assert_instance_method :destroy, content do |m| 40 | assert_match(/@user\.destroy/, m) 41 | assert_match(/User was successfully destroyed/, m) 42 | end 43 | 44 | assert_instance_method :set_user, content do |m| 45 | assert_match(/@user = User\.find\(params\[:id\]\)/, m) 46 | end 47 | 48 | assert_match(/def user_params/, content) 49 | assert_match(/params\.require\(:user\)\.permit\(:name, :age\)/, content) 50 | end 51 | end 52 | 53 | def test_dont_use_require_or_permit_if_there_are_no_attributes 54 | run_generator ["User"] 55 | 56 | assert_file "app/controllers/admin/users_controller.rb" do |content| 57 | assert_match(/def user_params/, content) 58 | assert_match(/params\[:user\]/, content) 59 | end 60 | end 61 | 62 | def test_helper_are_invoked_with_a_pluralized_name 63 | run_generator 64 | assert_file "app/helpers/admin/users_helper.rb", /module Admin::UsersHelper/ 65 | assert_file "test/helpers/admin/users_helper_test.rb", /class Admin::UsersHelperTest < ActionView::TestCase/ 66 | end 67 | 68 | def test_erb_views_are_generated 69 | run_generator 70 | 71 | %w(index edit new show).each do |view| 72 | assert_file "app/views/admin/users/#{view}.html.erb" 73 | end 74 | 75 | # Ensure we're not using the bootstrap templates (which will have a class inside the table tag) 76 | assert_file "app/views/admin/users/index.html.erb", // 77 | end 78 | 79 | def test_erb_bootstrap_views_are_generated 80 | run_generator ['user', '-b'] 81 | 82 | %w(index edit new show).each do |view| 83 | assert_file "app/views/admin/users/#{view}.html.erb" 84 | end 85 | 86 | # Ensure we're not using the regular erb templates 87 | assert_file "app/views/admin/users/index.html.erb", /
['scaffold_controller'] 2 | -------------------------------------------------------------------------------- /test/test_helper.rb: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | require 'minitest/autorun' 3 | require 'rails/all' 4 | require 'rails/generators' 5 | require 'rails/generators/test_case' 6 | 7 | class TestApp < Rails::Application 8 | config.root = File.dirname(__FILE__) 9 | end 10 | Rails.application = TestApp 11 | 12 | module Rails 13 | def self.root 14 | @root ||= File.expand_path(File.join(File.dirname(__FILE__), '..', 'tmp', 'rails')) 15 | end 16 | end 17 | Rails.application.config.root = Rails.root 18 | 19 | # Call configure to load the settings from 20 | # Rails.application.config.generators to Rails::Generators 21 | Rails::Generators.configure! Rails.application.config.generators 22 | 23 | Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f} 24 | 25 | def copy_routes 26 | routes = File.expand_path(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), 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 | :admin => ['scaffold_controller'] 49 | } 50 | end 51 | 52 | def path_prefix(name) 53 | case name 54 | when :rails 55 | 'rails/generators' 56 | else 57 | 'generators' 58 | end 59 | end 60 | 61 | def require_generators(generator_list) 62 | generator_list.each do |name, generators| 63 | generators.each do |generator_name| 64 | if name.to_s == 'rails' && generator_name.to_s == 'mailer' 65 | require File.join(path_prefix(name), generator_name.to_s, "#{generator_name}_generator") 66 | else 67 | require File.join(path_prefix(name), name.to_s, generator_name.to_s, "#{generator_name}_generator") 68 | end 69 | end 70 | end 71 | end 72 | alias :require_generator :require_generators 73 | 74 | require_generators generator_list 75 | --------------------------------------------------------------------------------