admin_view 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +1 -0
- data/README.md +8 -3
- data/admin_view.gemspec +3 -3
- data/lib/admin_view/version.rb +1 -1
- data/lib/generators/admin_view_generator.rb +27 -6
- data/lib/generators/templates/base_helper.rb +10 -0
- data/lib/generators/templates/controller.rb +5 -1
- data/lib/generators/templates/controller_spec.rb +12 -8
- data/lib/generators/templates/views/index.html.erb +6 -0
- data/test/admin_view_generator_test.rb +50 -3
- metadata +87 -60
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# admin_view
|
2
2
|
|
3
|
-
Rails 3 generator of CRUD admin controllers, views and specs for existing models.
|
3
|
+
Rails 3 generator of CRUD admin controllers, views and specs for existing ActiveRecord models.
|
4
4
|
|
5
|
-
It uses [meta_search](https://github.com/ernie/meta_search) for search and [kaminari](https://github.com/amatsuda/kaminari) for pagination.
|
5
|
+
It uses [meta_search](https://github.com/ernie/meta_search) for search and [kaminari](https://github.com/amatsuda/kaminari) for pagination. It also assumes that you have [dynamic_form](https://github.com/joelmoss/dynamic_form) plugin installed.
|
6
6
|
|
7
7
|
Screenshot:
|
8
8
|
|
@@ -16,10 +16,15 @@ Include it in your Gemfile:
|
|
16
16
|
|
17
17
|
Run the generator:
|
18
18
|
|
19
|
-
$ bundle exec admin_view User --search_by name_or_email
|
19
|
+
$ bundle exec rails g admin_view User --search_by name_or_email
|
20
20
|
|
21
21
|
The `--search_by` option is not required - if you don't pass it, the form template will come out commented out. However, it's useful most of the time, and you can use any meta_search-compatible expression to search in string or text fields.
|
22
22
|
|
23
|
+
Other options available are:
|
24
|
+
|
25
|
+
* `--no_create` - omit functionality to create a new record
|
26
|
+
* `--read_only` - omit create, edit and update functionality
|
27
|
+
|
23
28
|
## Contributing
|
24
29
|
|
25
30
|
Yes! Feel free to fork the repo, make your changes in a topic branch and send us a pull request.
|
data/admin_view.gemspec
CHANGED
@@ -20,7 +20,7 @@ Gem::Specification.new do |s|
|
|
20
20
|
s.require_paths = ["lib"]
|
21
21
|
|
22
22
|
s.add_dependency("rails", ">= 3.0")
|
23
|
-
s.add_dependency("
|
24
|
-
s.add_dependency("
|
25
|
-
s.
|
23
|
+
s.add_dependency("meta_search", ">= 1.0")
|
24
|
+
s.add_dependency("kaminari", ">= 0.12")
|
25
|
+
s.add_development_dependency("rspec-rails", ">= 2.4.0")
|
26
26
|
end
|
data/lib/admin_view/version.rb
CHANGED
@@ -9,6 +9,10 @@ class AdminViewGenerator < Rails::Generators::NamedBase
|
|
9
9
|
|
10
10
|
class_option :search_by, :type => :string, :desc => "The field or criteria to meta_search by (not required, but without a doubt recommended)"
|
11
11
|
|
12
|
+
class_option :no_create, :type => :boolean, :default => false, :desc => "Omit functionality to create a new record."
|
13
|
+
|
14
|
+
class_option :read_only, :type => :boolean, :default => false, :desc => "Omit create, edit and update functionality."
|
15
|
+
|
12
16
|
def create_base_controller
|
13
17
|
empty_directory "app/controllers/admin"
|
14
18
|
path = File.join("app/controllers/admin", "base_controller.rb")
|
@@ -29,13 +33,16 @@ class AdminViewGenerator < Rails::Generators::NamedBase
|
|
29
33
|
template "controller_spec.rb", File.join("spec/controllers/admin", "#{controller_file_name}_controller_spec.rb")
|
30
34
|
end
|
31
35
|
|
36
|
+
def create_helper
|
37
|
+
empty_directory "app/helpers/admin"
|
38
|
+
template "base_helper.rb", File.join("app/helpers/admin", "base_helper.rb")
|
39
|
+
end
|
40
|
+
|
32
41
|
def create_views
|
33
42
|
empty_directory "app/views/admin/#{controller_file_name}"
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
template "views/#{view}.html.erb", File.join("app/views/admin", controller_file_name, "#{view}.html.erb")
|
38
|
-
end
|
43
|
+
@attributes = get_model_columns
|
44
|
+
available_views.each do |view|
|
45
|
+
template "views/#{view}.html.erb", File.join("app/views/admin", controller_file_name, "#{view}.html.erb")
|
39
46
|
end
|
40
47
|
end
|
41
48
|
|
@@ -50,7 +57,13 @@ class AdminViewGenerator < Rails::Generators::NamedBase
|
|
50
57
|
protected
|
51
58
|
|
52
59
|
def available_views
|
53
|
-
["index", "new", "show", "edit", "_form"]
|
60
|
+
views = ["index", "new", "show", "edit", "_form"]
|
61
|
+
|
62
|
+
views.delete("new") if options[:no_create]
|
63
|
+
|
64
|
+
["new", "edit", "_form"].each { |v| views.delete(v) } if options[:read_only]
|
65
|
+
|
66
|
+
views
|
54
67
|
end
|
55
68
|
|
56
69
|
def model_exists?(klass_name)
|
@@ -62,4 +75,12 @@ class AdminViewGenerator < Rails::Generators::NamedBase
|
|
62
75
|
end
|
63
76
|
end
|
64
77
|
|
78
|
+
def get_model_columns
|
79
|
+
if model_exists?(class_name)
|
80
|
+
class_name.constantize.send(:columns)
|
81
|
+
else
|
82
|
+
[] # allow user (and test) to generate the view files
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
65
86
|
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module Admin::BaseHelper
|
2
|
+
|
3
|
+
def sortable(column, title = nil)
|
4
|
+
title ||= column.titleize
|
5
|
+
css_class = (column == sort_column) ? "current #{sort_direction}" : nil
|
6
|
+
direction = (column == sort_column && sort_direction == "asc") ? "desc" : "asc"
|
7
|
+
link_to title, { :sort => column, :direction => direction }, { :class => css_class }
|
8
|
+
end
|
9
|
+
|
10
|
+
end
|
@@ -2,13 +2,14 @@ class Admin::<%= controller_class_name %>Controller < Admin::BaseController
|
|
2
2
|
|
3
3
|
helper_method :sort_column, :sort_direction, :search_params
|
4
4
|
|
5
|
-
before_filter :find_<%= singular_table_name %>, :only => [:edit, :update
|
5
|
+
before_filter :find_<%= singular_table_name %>, :only => [<% unless options[:read_only] %>:edit, :update,<% end %> :show, :destroy]
|
6
6
|
|
7
7
|
def index
|
8
8
|
@search = <%= class_name %>.search(params[:search])
|
9
9
|
@<%= plural_table_name %> = find_<%= plural_table_name %>
|
10
10
|
end
|
11
11
|
|
12
|
+
<% unless options[:no_create] or options[:read_only] %>
|
12
13
|
def new
|
13
14
|
@<%= singular_table_name %> = <%= class_name %>.new
|
14
15
|
end
|
@@ -21,10 +22,12 @@ class Admin::<%= controller_class_name %>Controller < Admin::BaseController
|
|
21
22
|
render :new
|
22
23
|
end
|
23
24
|
end
|
25
|
+
<% end -%>
|
24
26
|
|
25
27
|
def show
|
26
28
|
end
|
27
29
|
|
30
|
+
<% unless options[:read_only] %>
|
28
31
|
def edit
|
29
32
|
end
|
30
33
|
|
@@ -35,6 +38,7 @@ class Admin::<%= controller_class_name %>Controller < Admin::BaseController
|
|
35
38
|
render :edit
|
36
39
|
end
|
37
40
|
end
|
41
|
+
<% end -%>
|
38
42
|
|
39
43
|
def destroy
|
40
44
|
@<%= singular_table_name %>.destroy
|
@@ -26,6 +26,7 @@ describe Admin::<%= controller_class_name %>Controller do
|
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
+
<% unless options[:no_create] or options[:read_only] %>
|
29
30
|
describe "GET new" do
|
30
31
|
it "assigns a new <%= singular_table_name %> as @<%= singular_table_name %>" do
|
31
32
|
<%= class_name %>.stub(:new) { mock_<%= singular_table_name %> }
|
@@ -34,14 +35,6 @@ describe Admin::<%= controller_class_name %>Controller do
|
|
34
35
|
end
|
35
36
|
end
|
36
37
|
|
37
|
-
describe "GET edit" do
|
38
|
-
it "assigns the requested <%= singular_table_name %> as @<%= singular_table_name %>" do
|
39
|
-
<%= class_name %>.stub(:find).with("37") { mock_<%= singular_table_name %> }
|
40
|
-
get :edit, :id => "37"
|
41
|
-
assigns(:<%= singular_table_name %>).should be(mock_<%= singular_table_name %>)
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
38
|
describe "POST create" do
|
46
39
|
|
47
40
|
describe "with valid params" do
|
@@ -73,6 +66,16 @@ describe Admin::<%= controller_class_name %>Controller do
|
|
73
66
|
end
|
74
67
|
|
75
68
|
end
|
69
|
+
<% end -%>
|
70
|
+
|
71
|
+
<% unless options[:read_only] %>
|
72
|
+
describe "GET edit" do
|
73
|
+
it "assigns the requested <%= singular_table_name %> as @<%= singular_table_name %>" do
|
74
|
+
<%= class_name %>.stub(:find).with("37") { mock_<%= singular_table_name %> }
|
75
|
+
get :edit, :id => "37"
|
76
|
+
assigns(:<%= singular_table_name %>).should be(mock_<%= singular_table_name %>)
|
77
|
+
end
|
78
|
+
end
|
76
79
|
|
77
80
|
describe "PUT update" do
|
78
81
|
|
@@ -111,6 +114,7 @@ describe Admin::<%= controller_class_name %>Controller do
|
|
111
114
|
end
|
112
115
|
|
113
116
|
end
|
117
|
+
<% end -%>
|
114
118
|
|
115
119
|
describe "DELETE destroy" do
|
116
120
|
it "destroys the requested <%= singular_table_name %>" do
|
@@ -13,7 +13,9 @@
|
|
13
13
|
<% end %>
|
14
14
|
<%% end %>
|
15
15
|
|
16
|
+
<% unless options[:no_create] or options[:read_only] %>
|
16
17
|
<p><%%= link_to("Create a new <%= human_name %>", new_admin_<%= singular_table_name %>_path) %></p>
|
18
|
+
<% end -%>
|
17
19
|
|
18
20
|
<table class="sortable">
|
19
21
|
<thead>
|
@@ -21,7 +23,9 @@
|
|
21
23
|
<% @attributes.each do |attribute| -%>
|
22
24
|
<th><%%= sortable "<%= attribute.name %>" %></th>
|
23
25
|
<% end -%>
|
26
|
+
<% unless options[:read_only] %>
|
24
27
|
<th>Edit</th>
|
28
|
+
<% end -%>
|
25
29
|
<th>Delete</th>
|
26
30
|
</tr>
|
27
31
|
</thead>
|
@@ -33,7 +37,9 @@
|
|
33
37
|
<% next if attribute.name == "id" -%>
|
34
38
|
<td><%%= <%= singular_table_name %>.<%= attribute.name %> %></td>
|
35
39
|
<% end -%>
|
40
|
+
<% unless options[:read_only] %>
|
36
41
|
<td><%%= link_to('Edit', edit_admin_<%= singular_table_name %>_path(<%= singular_table_name %>)) %></td>
|
42
|
+
<% end -%>
|
37
43
|
<td><%%= link_to('Delete', admin_<%= singular_table_name %>_path(<%= singular_table_name %>), :confirm => "Are you sure?", :method => :delete) %></td>
|
38
44
|
</tr>
|
39
45
|
<%% end %>
|
@@ -1,13 +1,13 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), 'test_helper')
|
2
|
-
|
2
|
+
|
3
3
|
require 'generators/admin_view_generator'
|
4
|
-
|
4
|
+
|
5
5
|
class AdminViewGeneratorTest < Rails::Generators::TestCase
|
6
6
|
|
7
7
|
destination File.expand_path("../tmp", File.dirname(__FILE__))
|
8
8
|
setup :prepare_destination
|
9
9
|
tests AdminViewGenerator
|
10
|
-
|
10
|
+
|
11
11
|
test "create the controllers and specs" do
|
12
12
|
run_generator %w(User --search_by email)
|
13
13
|
|
@@ -15,5 +15,52 @@ class AdminViewGeneratorTest < Rails::Generators::TestCase
|
|
15
15
|
assert_file "app/controllers/admin/users_controller.rb", /class Admin::UsersController < Admin::BaseController/
|
16
16
|
assert_file "spec/controllers/admin/base_controller_spec.rb", /describe Admin::BaseController do/
|
17
17
|
assert_file "spec/controllers/admin/users_controller_spec.rb", /describe Admin::UsersController do/
|
18
|
+
assert_file "app/views/admin/users/index.html.erb"
|
19
|
+
assert_file "app/views/admin/users/show.html.erb"
|
20
|
+
assert_file "app/views/admin/users/new.html.erb"
|
21
|
+
assert_file "app/views/admin/users/edit.html.erb"
|
22
|
+
assert_file "app/views/admin/users/_form.html.erb"
|
23
|
+
assert_file "app/helpers/admin/base_helper.rb", /def sortable/
|
24
|
+
end
|
25
|
+
|
26
|
+
test "--no-create option skips assets to create new record" do
|
27
|
+
run_generator %w(User --no_create)
|
28
|
+
|
29
|
+
assert_no_file "app/views/admin/users/new.html.erb"
|
30
|
+
|
31
|
+
content = File.read("tmp/app/views/admin/users/index.html.erb")
|
32
|
+
assert content !~ /Create a new/
|
33
|
+
|
34
|
+
content = File.read("tmp/app/controllers/admin/users_controller.rb")
|
35
|
+
assert content !~ /def new/
|
36
|
+
assert content !~ /def create/
|
37
|
+
|
38
|
+
controller_spec_content = File.read("tmp/spec/controllers/admin/users_controller_spec.rb")
|
39
|
+
assert controller_spec_content !~ /GET new/
|
40
|
+
assert controller_spec_content !~ /POST create/
|
41
|
+
end
|
42
|
+
|
43
|
+
test "--read_only skips create, edit and update" do
|
44
|
+
run_generator %w(User --read_only)
|
45
|
+
|
46
|
+
assert_no_file "app/views/admin/users/new.html.erb"
|
47
|
+
assert_no_file "app/views/admin/users/edit.html.erb"
|
48
|
+
assert_no_file "app/views/admin/users/_form.html.erb"
|
49
|
+
|
50
|
+
index_content = File.read("tmp/app/views/admin/users/index.html.erb")
|
51
|
+
assert index_content !~ /Create a new/
|
52
|
+
assert index_content !~ /Edit/
|
53
|
+
|
54
|
+
controller_content = File.read("tmp/app/controllers/admin/users_controller.rb")
|
55
|
+
assert controller_content !~ /def new/
|
56
|
+
assert controller_content !~ /def create/
|
57
|
+
assert controller_content !~ /def edit/
|
58
|
+
assert controller_content !~ /def update/
|
59
|
+
|
60
|
+
controller_spec_content = File.read("tmp/spec/controllers/admin/users_controller_spec.rb")
|
61
|
+
assert controller_spec_content !~ /GET new/
|
62
|
+
assert controller_spec_content !~ /POST create/
|
63
|
+
assert controller_spec_content !~ /GET edit/
|
64
|
+
assert controller_spec_content !~ /PUT update/
|
18
65
|
end
|
19
66
|
end
|
metadata
CHANGED
@@ -1,68 +1,86 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: admin_view
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 2
|
8
|
+
- 0
|
9
|
+
version: 0.2.0
|
6
10
|
platform: ruby
|
7
|
-
authors:
|
11
|
+
authors:
|
8
12
|
- Marko Anastasov
|
9
13
|
- Darko Fabijan
|
10
14
|
autorequire:
|
11
15
|
bindir: bin
|
12
16
|
cert_chain: []
|
13
|
-
|
14
|
-
|
15
|
-
|
17
|
+
|
18
|
+
date: 2011-10-14 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
16
22
|
name: rails
|
17
|
-
requirement: &17270240 !ruby/object:Gem::Requirement
|
18
|
-
none: false
|
19
|
-
requirements:
|
20
|
-
- - ! '>='
|
21
|
-
- !ruby/object:Gem::Version
|
22
|
-
version: '3.0'
|
23
|
-
type: :runtime
|
24
23
|
prerelease: false
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
version: 2.4.0
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 3
|
30
|
+
- 0
|
31
|
+
version: "3.0"
|
34
32
|
type: :runtime
|
35
|
-
|
36
|
-
|
37
|
-
- !ruby/object:Gem::Dependency
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
38
35
|
name: meta_search
|
39
|
-
requirement: &17269360 !ruby/object:Gem::Requirement
|
40
|
-
none: false
|
41
|
-
requirements:
|
42
|
-
- - ! '>='
|
43
|
-
- !ruby/object:Gem::Version
|
44
|
-
version: '0'
|
45
|
-
type: :runtime
|
46
36
|
prerelease: false
|
47
|
-
|
48
|
-
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 1
|
43
|
+
- 0
|
44
|
+
version: "1.0"
|
45
|
+
type: :runtime
|
46
|
+
version_requirements: *id002
|
47
|
+
- !ruby/object:Gem::Dependency
|
49
48
|
name: kaminari
|
50
|
-
|
51
|
-
|
52
|
-
requirements:
|
53
|
-
- -
|
54
|
-
- !ruby/object:Gem::Version
|
55
|
-
|
49
|
+
prerelease: false
|
50
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
- 12
|
57
|
+
version: "0.12"
|
56
58
|
type: :runtime
|
59
|
+
version_requirements: *id003
|
60
|
+
- !ruby/object:Gem::Dependency
|
61
|
+
name: rspec-rails
|
57
62
|
prerelease: false
|
58
|
-
|
63
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
segments:
|
68
|
+
- 2
|
69
|
+
- 4
|
70
|
+
- 0
|
71
|
+
version: 2.4.0
|
72
|
+
type: :development
|
73
|
+
version_requirements: *id004
|
59
74
|
description: Rails 3 generator of admin views and controllers for existing models.
|
60
|
-
email:
|
75
|
+
email:
|
61
76
|
- devs@renderedtext.com
|
62
77
|
executables: []
|
78
|
+
|
63
79
|
extensions: []
|
80
|
+
|
64
81
|
extra_rdoc_files: []
|
65
|
-
|
82
|
+
|
83
|
+
files:
|
66
84
|
- .gitignore
|
67
85
|
- Gemfile
|
68
86
|
- MIT-LICENSE
|
@@ -74,6 +92,7 @@ files:
|
|
74
92
|
- lib/generators/admin_view_generator.rb
|
75
93
|
- lib/generators/templates/base_controller.rb
|
76
94
|
- lib/generators/templates/base_controller_spec.rb
|
95
|
+
- lib/generators/templates/base_helper.rb
|
77
96
|
- lib/generators/templates/controller.rb
|
78
97
|
- lib/generators/templates/controller_spec.rb
|
79
98
|
- lib/generators/templates/views/_form.html.erb
|
@@ -83,28 +102,36 @@ files:
|
|
83
102
|
- lib/generators/templates/views/show.html.erb
|
84
103
|
- test/admin_view_generator_test.rb
|
85
104
|
- test/test_helper.rb
|
105
|
+
has_rdoc: true
|
86
106
|
homepage: http://github.com/renderedtext/admin_view
|
87
107
|
licenses: []
|
108
|
+
|
88
109
|
post_install_message:
|
89
110
|
rdoc_options: []
|
90
|
-
|
111
|
+
|
112
|
+
require_paths:
|
91
113
|
- lib
|
92
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
requirements:
|
101
|
-
- -
|
102
|
-
- !ruby/object:Gem::Version
|
103
|
-
|
114
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
segments:
|
119
|
+
- 0
|
120
|
+
version: "0"
|
121
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
segments:
|
126
|
+
- 0
|
127
|
+
version: "0"
|
104
128
|
requirements: []
|
129
|
+
|
105
130
|
rubyforge_project: admin_view
|
106
|
-
rubygems_version: 1.
|
131
|
+
rubygems_version: 1.3.6
|
107
132
|
signing_key:
|
108
133
|
specification_version: 3
|
109
134
|
summary: Rails 3 generator of admin views and controllers for existing models.
|
110
|
-
test_files:
|
135
|
+
test_files:
|
136
|
+
- test/admin_view_generator_test.rb
|
137
|
+
- test/test_helper.rb
|