├── .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 [](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 |
<%= attribute.human_name %> | 8 | <% end -%> 9 |10 | | 11 | | 12 | |
---|---|---|---|
<%%= <%= singular_table_name %>.<%= attribute.name %> %> | 20 | <% end -%> 21 |<%%= link_to 'Show', <%= "[:#{prefix}, #{singular_table_name}]" %> %> | 22 |<%%= link_to 'Edit', edit_<%= prefixed_plain_model_url %>_path(<%= singular_table_name %>) %> | 23 |<%%= link_to 'Destroy', <%= "[:#{prefix}, #{singular_table_name}]" %>, method: :delete, data: { confirm: 'Are you sure?' } %> | 24 |
<%%= 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 |<%= attribute.human_name %> | 10 | <% end -%> 11 |12 | | 13 | | 14 | |
---|---|---|---|
<%%= <%= singular_table_name %>.<%= attribute.name %> %> | 22 | <% end -%> 23 |<%%= link_to 'Show', <%= "[:#{prefix}, #{singular_table_name}]" %>, class: 'btn btn-default btn-xs' %> | 24 |<%%= link_to 'Edit', edit_<%= prefixed_plain_model_url %>_path(<%= singular_table_name %>), class: 'btn btn-default btn-xs' %> | 25 |<%%= link_to 'Destroy', <%= "[:#{prefix}, #{singular_table_name}]" %>, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-xs btn-danger' %> | 26 |
<%%= notice %>
2 | 3 | <% attributes.reject(&:password_digest?).each do |attribute| -%> 4 |