sharp_admin 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +3 -0
- data/Rakefile +34 -0
- data/lib/sharp_admin.rb +2 -0
- data/lib/sharp_admin/sharp_admin_generator.rb +87 -0
- data/lib/sharp_admin/templates/base_controller.rb +18 -0
- data/lib/sharp_admin/templates/base_controller_spec.rb +47 -0
- data/lib/sharp_admin/templates/base_helper.rb +10 -0
- data/lib/sharp_admin/templates/controller.rb +76 -0
- data/lib/sharp_admin/templates/controller_spec.rb +136 -0
- data/lib/sharp_admin/templates/views/_form.html.erb +17 -0
- data/lib/sharp_admin/templates/views/edit.html.erb +7 -0
- data/lib/sharp_admin/templates/views/index.html.erb +44 -0
- data/lib/sharp_admin/templates/views/new.html.erb +6 -0
- data/lib/sharp_admin/templates/views/show.html.erb +20 -0
- data/lib/sharp_admin/version.rb +3 -0
- data/lib/tasks/easy_admin_tasks.rake +4 -0
- data/test/easy_admin_test.rb +67 -0
- data/test/test_helper.rb +8 -0
- metadata +107 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d833990187e75b9fedc524e9bd1fab0b8dcab152
|
4
|
+
data.tar.gz: f0a52fe45bc1dd7ca08380d38aa8a3973a6e3bd5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2188637259156f62c06a2ace1c46b502d4cd14de4ea690bf16152f6515c5032f501d9817c09b4f279e486f9048f1a2742190ae5c5cdeb6a4fd05ee144b3245dc
|
7
|
+
data.tar.gz: 53863881c2c65db47d478160c6ed31a78053347ad281785d9707cf2c64dbf222135d6ff4bd68a2b0bc00ced12ca6887a66497d9d323dc990e1d15111cfe261b5
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2015 sharp
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'SharpAdmin'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
Bundler::GemHelper.install_tasks
|
23
|
+
|
24
|
+
require 'rake/testtask'
|
25
|
+
|
26
|
+
Rake::TestTask.new(:test) do |t|
|
27
|
+
t.libs << 'lib'
|
28
|
+
t.libs << 'test'
|
29
|
+
t.pattern = 'test/**/*_test.rb'
|
30
|
+
t.verbose = false
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
task default: :test
|
data/lib/sharp_admin.rb
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'rails/generators/resource_helpers'
|
2
|
+
require 'rails/generators/named_base'
|
3
|
+
|
4
|
+
class SharpAdminGenerator < Rails::Generators::NamedBase
|
5
|
+
|
6
|
+
include Rails::Generators::ResourceHelpers
|
7
|
+
|
8
|
+
source_root File.expand_path('../templates', __FILE__)
|
9
|
+
|
10
|
+
class_option :search_by, :type => :string, :desc => "The field or criteria to meta_search by (not required, but without a doubt recommended)"
|
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
|
+
|
16
|
+
def create_base_controller
|
17
|
+
empty_directory "app/controllers/admin"
|
18
|
+
path = File.join("app/controllers/admin", "base_controller.rb")
|
19
|
+
template("base_controller.rb", path) unless File.exists?(path)
|
20
|
+
end
|
21
|
+
|
22
|
+
def create_base_controller_spec
|
23
|
+
empty_directory "spec/controllers/admin"
|
24
|
+
path = File.join("spec/controllers/admin", "base_controller_spec.rb")
|
25
|
+
template("base_controller_spec.rb", path) unless File.exists?(path)
|
26
|
+
end
|
27
|
+
|
28
|
+
def create_controller
|
29
|
+
@attributes_symbols = get_model_columns.dup.delete_if {|attribute| ['id', 'created_at', 'updated_at'].include? attribute.name }.collect {|attribute| ":#{attribute.name}" }
|
30
|
+
template "controller.rb", File.join("app/controllers/admin", "#{controller_file_name}_controller.rb")
|
31
|
+
end
|
32
|
+
|
33
|
+
def create_controller_rspec
|
34
|
+
template "controller_spec.rb", File.join("spec/controllers/admin", "#{controller_file_name}_controller_spec.rb")
|
35
|
+
end
|
36
|
+
|
37
|
+
def create_helper
|
38
|
+
empty_directory "app/helpers/admin"
|
39
|
+
template "base_helper.rb", File.join("app/helpers/admin", "base_helper.rb")
|
40
|
+
end
|
41
|
+
|
42
|
+
def create_views
|
43
|
+
empty_directory "app/views/admin/#{controller_file_name}"
|
44
|
+
@attributes = get_model_columns
|
45
|
+
available_views.each do |view|
|
46
|
+
template "views/#{view}.html.erb", File.join("app/views/admin", controller_file_name, "#{view}.html.haml")
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def add_resource_route
|
51
|
+
return if not File.exists?("config/routes.rb")
|
52
|
+
route_config = "namespace :admin do "
|
53
|
+
route_config << "resources :#{file_name.pluralize}"
|
54
|
+
route_config << " end"
|
55
|
+
route route_config
|
56
|
+
end
|
57
|
+
|
58
|
+
protected
|
59
|
+
|
60
|
+
def available_views
|
61
|
+
views = ["index", "new", "show", "edit", "_form"]
|
62
|
+
|
63
|
+
views.delete("new") if options[:no_create]
|
64
|
+
|
65
|
+
["new", "edit", "_form"].each { |v| views.delete(v) } if options[:read_only]
|
66
|
+
|
67
|
+
views
|
68
|
+
end
|
69
|
+
|
70
|
+
def model_exists?(klass_name)
|
71
|
+
begin
|
72
|
+
klass = Module.const_get(klass_name)
|
73
|
+
return klass.superclass == ActiveRecord::Base
|
74
|
+
rescue NameError
|
75
|
+
return false
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def get_model_columns
|
80
|
+
if model_exists?(class_name)
|
81
|
+
class_name.constantize.send(:columns)
|
82
|
+
else
|
83
|
+
[] # allow user (and test) to generate the view files
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class Admin::BaseController < ApplicationController
|
2
|
+
|
3
|
+
before_filter :require_admin
|
4
|
+
|
5
|
+
protected
|
6
|
+
|
7
|
+
def require_admin
|
8
|
+
unless current_user.try(:admin?)
|
9
|
+
render404 and return false
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def render404
|
14
|
+
render :file => File.join(Rails.root, 'public', '404.html'), :status => 404
|
15
|
+
return true
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Admin::BaseController do
|
4
|
+
|
5
|
+
describe "GET 'index'" do
|
6
|
+
|
7
|
+
context "logged in as admin" do
|
8
|
+
|
9
|
+
it "renders index page" do
|
10
|
+
controller.stub_chain(:current_user, :admin?).and_return(true)
|
11
|
+
|
12
|
+
get :index
|
13
|
+
|
14
|
+
expect(response).to render_template('index')
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
context "logged in as user" do
|
20
|
+
|
21
|
+
it "renders 404" do
|
22
|
+
controller.stub_chain(:current_user, :admin?).and_return(false)
|
23
|
+
|
24
|
+
get :index
|
25
|
+
|
26
|
+
expect(response).to render_template('404.html')
|
27
|
+
expect(response.response_code).to eql(404)
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
context "not logged in" do
|
33
|
+
|
34
|
+
it "renders 404" do
|
35
|
+
allow(controller).to receive(:current_user).and_return(nil)
|
36
|
+
|
37
|
+
get :index
|
38
|
+
|
39
|
+
expect(response).to render_template('404.html')
|
40
|
+
expect(response.response_code).to eql(404)
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
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
|
@@ -0,0 +1,76 @@
|
|
1
|
+
class Admin::<%= controller_class_name %>Controller < Admin::BaseController
|
2
|
+
|
3
|
+
helper_method :sort_column, :sort_direction
|
4
|
+
|
5
|
+
before_filter :find_<%= singular_table_name %>, :only => [<% unless options[:read_only] %>:edit, :update,<% end %> :show, :destroy]
|
6
|
+
|
7
|
+
def index
|
8
|
+
@q = <%= class_name %>.search(params[:q])
|
9
|
+
@<%= plural_table_name %> = find_<%= plural_table_name %>
|
10
|
+
end
|
11
|
+
|
12
|
+
<% unless options[:no_create] || options[:read_only] %>
|
13
|
+
def new
|
14
|
+
@<%= singular_table_name %> = <%= class_name %>.new
|
15
|
+
end
|
16
|
+
|
17
|
+
def create
|
18
|
+
@<%= singular_table_name %> = <%= class_name %>.new(<%= singular_table_name %>_params)
|
19
|
+
if @<%= singular_table_name %>.save
|
20
|
+
redirect_to admin_<%= plural_table_name %>_path, :notice => "Successfully created <%= human_name.downcase %>."
|
21
|
+
else
|
22
|
+
render :new
|
23
|
+
end
|
24
|
+
end
|
25
|
+
<% end -%>
|
26
|
+
|
27
|
+
def show
|
28
|
+
end
|
29
|
+
|
30
|
+
<% unless options[:read_only] -%>
|
31
|
+
def edit
|
32
|
+
end
|
33
|
+
|
34
|
+
def update
|
35
|
+
if @<%= singular_table_name %>.update_attributes(<%= singular_table_name %>_params)
|
36
|
+
redirect_to admin_<%= plural_table_name %>_path, :notice => "Successfully updated <%= human_name.downcase %>."
|
37
|
+
else
|
38
|
+
render :edit
|
39
|
+
end
|
40
|
+
end
|
41
|
+
<% end -%>
|
42
|
+
|
43
|
+
def destroy
|
44
|
+
@<%= singular_table_name %>.destroy
|
45
|
+
redirect_to admin_<%= plural_table_name %>_path, :notice => "<%= human_name %> deleted."
|
46
|
+
end
|
47
|
+
|
48
|
+
protected
|
49
|
+
|
50
|
+
def find_<%= singular_table_name %>
|
51
|
+
@<%= singular_table_name %> = <%= class_name %>.find(params[:id])
|
52
|
+
end
|
53
|
+
|
54
|
+
def find_<%= plural_table_name %>
|
55
|
+
search_relation = @q.result
|
56
|
+
@<%= plural_table_name %> = search_relation.order(sort_column + " " + sort_direction).references(:<%= singular_table_name %>).page params[:page]
|
57
|
+
end
|
58
|
+
|
59
|
+
def sort_column
|
60
|
+
<%= class_name %>.column_names.include?(params[:sort]) ? params[:sort] : "created_at"
|
61
|
+
end
|
62
|
+
|
63
|
+
def sort_direction
|
64
|
+
%w[asc desc].include?(params[:direction]) ? params[:direction] : "desc"
|
65
|
+
end
|
66
|
+
|
67
|
+
<% unless options[:read_only] -%>
|
68
|
+
private
|
69
|
+
|
70
|
+
# Never trust parameters from the scary internet, only allow the white list through.
|
71
|
+
def <%= singular_table_name %>_params
|
72
|
+
params.require(:<%= singular_table_name %>).permit(<%= @attributes_symbols.join(", ") %>)
|
73
|
+
end
|
74
|
+
<% end -%>
|
75
|
+
|
76
|
+
end
|
@@ -0,0 +1,136 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Admin::<%= controller_class_name %>Controller do
|
4
|
+
|
5
|
+
def mock_<%= singular_table_name %>(stubs={})
|
6
|
+
(@mock_<%= singular_table_name %> ||= mock_model(<%= class_name %>).as_null_object).tap do |<%= singular_table_name %>|
|
7
|
+
stubs.each_key do |method_name|
|
8
|
+
allow(<%= singular_table_name %>).to receive(method_name).and_return(stubs[method_name])
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
before { allow(controller).to receive(:require_admin) }
|
14
|
+
|
15
|
+
describe "GET index" do
|
16
|
+
it "assigns all <%= plural_table_name %> as @<%= plural_table_name %>" do
|
17
|
+
allow(controller).to receive(:find_<%= plural_table_name %>).and_return([mock_<%= singular_table_name %>])
|
18
|
+
get :index
|
19
|
+
expect(assigns(:<%= plural_table_name %>)).to eq([mock_<%= singular_table_name %>])
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "GET show" do
|
24
|
+
it "assigns the requested <%= singular_table_name %> as @<%=singular_table_name %>" do
|
25
|
+
allow(<%= class_name %>).to receive(:find).with("37") { mock_<%= singular_table_name %> }
|
26
|
+
get :show, :id => "37"
|
27
|
+
expect(assigns(:<%= singular_table_name %>)).to be(mock_<%= singular_table_name %>)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
<% unless options[:no_create] or options[:read_only] %>
|
32
|
+
describe "GET new" do
|
33
|
+
it "assigns a new <%= singular_table_name %> as @<%= singular_table_name %>" do
|
34
|
+
allow(<%= class_name %>).to receive(:new) { mock_<%= singular_table_name %> }
|
35
|
+
get :new
|
36
|
+
expect(assigns(:<%= singular_table_name %>)).to be(mock_<%= singular_table_name %>)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "POST create" do
|
41
|
+
|
42
|
+
describe "with valid params" do
|
43
|
+
it "assigns a newly created <%= singular_table_name %> as @<%= singular_table_name %>" do
|
44
|
+
allow(<%= class_name %>).to receive(:new).with({'these' => 'params'}) { mock_<%= singular_table_name %>(:save => true) }
|
45
|
+
post :create, :<%= singular_table_name %> => {'these' => 'params'}
|
46
|
+
expect(assigns(:<%= singular_table_name %>)).to be(mock_<%= singular_table_name %>)
|
47
|
+
end
|
48
|
+
|
49
|
+
it "redirects to the created <%= singular_table_name %>" do
|
50
|
+
allow(<%= class_name %>).to receive(:new) { mock_<%= singular_table_name %>(:save => true) }
|
51
|
+
post :create, :<%= singular_table_name %> => {}
|
52
|
+
expect(response).to redirect_to(admin_<%= plural_table_name %>_url)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "with invalid params" do
|
57
|
+
it "assigns a newly created but unsaved <%= singular_table_name %> as @<%= singular_table_name %>" do
|
58
|
+
allow(<%= class_name %>).to receive(:new).with({'these' => 'params'}) { mock_<%= singular_table_name %>(:save => false) }
|
59
|
+
post :create, :<%= singular_table_name %> => {'these' => 'params'}
|
60
|
+
expect(assigns(:<%= singular_table_name %>)).to be(mock_<%= singular_table_name %>)
|
61
|
+
end
|
62
|
+
|
63
|
+
it "re-renders the 'new' template" do
|
64
|
+
allow(<%= class_name %>).to receive(:new) { mock_<%= singular_table_name %>(:save => false) }
|
65
|
+
post :create, :<%= singular_table_name %> => {}
|
66
|
+
expect(response).to render_template("new")
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
<% end -%>
|
72
|
+
|
73
|
+
<% unless options[:read_only] %>
|
74
|
+
describe "GET edit" do
|
75
|
+
it "assigns the requested <%= singular_table_name %> as @<%= singular_table_name %>" do
|
76
|
+
allow(<%= class_name %>).to receive(:find).with("37") { mock_<%= singular_table_name %> }
|
77
|
+
get :edit, :id => "37"
|
78
|
+
expect(assigns(:<%= singular_table_name %>)).to be(mock_<%= singular_table_name %>)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
describe "PUT update" do
|
83
|
+
|
84
|
+
describe "with valid params" do
|
85
|
+
it "updates the requested <%= singular_table_name %>" do
|
86
|
+
expect(<%= class_name %>).to receive(:find).with("37") { mock_<%= singular_table_name %> }
|
87
|
+
expect(mock_<%= singular_table_name %>).to receive(:update_attributes).with({'these' => 'params'})
|
88
|
+
put :update, :id => "37", :<%= singular_table_name %> => {'these' => 'params'}
|
89
|
+
end
|
90
|
+
|
91
|
+
it "assigns the requested <%= singular_table_name %> as @<%= singular_table_name %>" do
|
92
|
+
allow(<%= class_name %>).to receive(:find) { mock_<%= singular_table_name %>(:update_attributes => true) }
|
93
|
+
put :update, :id => "1"
|
94
|
+
expect(assigns(:<%= singular_table_name %>)).to be(mock_<%= singular_table_name %>)
|
95
|
+
end
|
96
|
+
|
97
|
+
it "redirects to the <%= singular_table_name %>" do
|
98
|
+
allow(<%= class_name %>).to receive(:find) { mock_<%= singular_table_name %>(:update_attributes => true) }
|
99
|
+
put :update, :id => "1"
|
100
|
+
expect(response).to redirect_to(admin_<%= plural_table_name %>_url)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
describe "with invalid params" do
|
105
|
+
it "assigns the <%= singular_table_name %> as @<%= singular_table_name %>" do
|
106
|
+
allow(<%= class_name %>).to receive(:find) { mock_<%= singular_table_name %>(:update_attributes => false) }
|
107
|
+
put :update, :id => "1"
|
108
|
+
expect(assigns(:<%= singular_table_name %>)).to be(mock_<%= singular_table_name %>)
|
109
|
+
end
|
110
|
+
|
111
|
+
it "re-renders the 'edit' template" do
|
112
|
+
allow(<%= class_name %>).to receive(:find) { mock_<%= singular_table_name %>(:update_attributes => false) }
|
113
|
+
put :update, :id => "1"
|
114
|
+
expect(response).to render_template("edit")
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
119
|
+
<% end -%>
|
120
|
+
|
121
|
+
describe "DELETE destroy" do
|
122
|
+
it "destroys the requested <%= singular_table_name %>" do
|
123
|
+
expect(<%= class_name %>).to receive(:find).with("37") { mock_<%= singular_table_name %> }
|
124
|
+
expect(mock_<%= singular_table_name %>).to receive(:destroy)
|
125
|
+
delete :destroy, :id => "37"
|
126
|
+
end
|
127
|
+
|
128
|
+
it "redirects to the <%= plural_table_name %> list" do
|
129
|
+
allow(<%= class_name %>).to receive(:find) { mock_<%= singular_table_name %> }
|
130
|
+
delete :destroy, :id => "1"
|
131
|
+
expect(response).to redirect_to(admin_<%= plural_table_name %>_url)
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
end
|
136
|
+
|
@@ -0,0 +1,17 @@
|
|
1
|
+
- if @<%= singular_table_name %>.errors.any?
|
2
|
+
%ul.errors
|
3
|
+
@<%= singular_table_name %>.errors.full_messages.each do |msg|
|
4
|
+
%li= msg
|
5
|
+
|
6
|
+
<% @attributes.each do |attribute| -%>
|
7
|
+
<% next if ["id", "created_at", "updated_at"].include?(attribute.name) -%>
|
8
|
+
.form-group
|
9
|
+
= f.label :<%= attribute.name %>, :class => "col-sm-2 control-label"
|
10
|
+
.col-sm-4
|
11
|
+
= f.text_field :<%= attribute.name %>, :class => "form-control", :placeholder => "<%= attribute.name.capitalize %>"
|
12
|
+
<% end -%>
|
13
|
+
|
14
|
+
- label = @<%= singular_table_name %>.new_record? ? "Create" : "Update"
|
15
|
+
.form-group
|
16
|
+
.col-sm-offset-2 col-sm-4
|
17
|
+
= f.submit label, class: 'btn btn-default'
|
@@ -0,0 +1,7 @@
|
|
1
|
+
.page-header
|
2
|
+
%h3
|
3
|
+
Editing
|
4
|
+
= link_to "<%= human_name.downcase %> #{@<%= singular_table_name %>.id}", admin_<%= singular_table_name %>_path(@<%= singular_table_name %>) %>
|
5
|
+
|
6
|
+
= form_for [:admin, @<%= singular_table_name %>], :html => { :class => "form-horizontal", :role => "form" } do |f|
|
7
|
+
= render "form", { :f => f }
|
@@ -0,0 +1,44 @@
|
|
1
|
+
.page-header
|
2
|
+
%h3
|
3
|
+
<%= human_name.pluralize %>
|
4
|
+
|
5
|
+
<% if options[:search_by].present? -%>
|
6
|
+
= search_form_for @q, :url => admin_<%= plural_table_name %>_path, :html => { :method => :get, :class => "well form-inline" } do |f|
|
7
|
+
.class="form-group
|
8
|
+
= f.text_field :<%= options[:search_by] %>_cont, :type => "search", :class => "form-control", :placeholder => "Search by <%= options[:search_by] %>"
|
9
|
+
|
10
|
+
= f.submit "Search", :class => "btn btn-default"
|
11
|
+
<% end %>
|
12
|
+
|
13
|
+
<% unless options[:no_create] || options[:read_only] %>
|
14
|
+
<p>
|
15
|
+
= link_to("Create a new <%= human_name %>", new_admin_<%= singular_table_name %>_path)
|
16
|
+
<% end -%>
|
17
|
+
|
18
|
+
%table.sortable.table.table-striped.table-bordered.table-condensed
|
19
|
+
%thead
|
20
|
+
%tr
|
21
|
+
<% @attributes.each do |attribute| -%>
|
22
|
+
<th>
|
23
|
+
= sortable "<%= attribute.name %>"
|
24
|
+
<% end -%>
|
25
|
+
<% unless options[:read_only] -%>
|
26
|
+
<th> Edit
|
27
|
+
<th> Delete
|
28
|
+
<% end -%>
|
29
|
+
%tbody
|
30
|
+
- @<%= plural_table_name %>.each do |<%= singular_table_name %>| %>
|
31
|
+
%tr.<%= singular_table_name %>
|
32
|
+
%td
|
33
|
+
= link_to(<%= singular_table_name %>.id, admin_<%= singular_table_name %>_path(<%= singular_table_name %>)) %>
|
34
|
+
<% @attributes.each do |attribute| -%>
|
35
|
+
<% next if attribute.name == "id" -%>
|
36
|
+
%td
|
37
|
+
= <%= singular_table_name %>.<%= attribute.name %> %>
|
38
|
+
<% end -%>
|
39
|
+
<% unless options[:read_only] -%>
|
40
|
+
%td= link_to('Edit', edit_admin_<%= singular_table_name %>_path(<%= singular_table_name %>))
|
41
|
+
%td= link_to('Delete', admin_<%= singular_table_name %>_path(<%= singular_table_name %>), :confirm => "Are you sure?", :method => :delete)
|
42
|
+
<% end -%>
|
43
|
+
|
44
|
+
= paginate @<%= plural_table_name %>
|
@@ -0,0 +1,20 @@
|
|
1
|
+
.page-header
|
2
|
+
%h3
|
3
|
+
<%= human_name.capitalize %> <%%= @<%= singular_table_name %>.id %>
|
4
|
+
|
5
|
+
.table.table-bordered
|
6
|
+
<% @attributes.each do |attribute| -%>
|
7
|
+
<tr>
|
8
|
+
<td>= attribute.human_name :</td>
|
9
|
+
<td><%%= @<%= singular_table_name %>.<%= attribute.name %> %></td>
|
10
|
+
</tr>
|
11
|
+
<% end -%>
|
12
|
+
|
13
|
+
<% unless options[:read_only] -%>
|
14
|
+
<tr>
|
15
|
+
<td>
|
16
|
+
Actions
|
17
|
+
<td>
|
18
|
+
= link_to "Edit", edit_admin_<%= singular_table_name %>_path(@<%= singular_table_name %>), :class => "btn btn-default"
|
19
|
+
= link_to("Delete", admin_<%= singular_table_name %>_path(@<%= singular_table_name %>), :data => { :confirm => "Are you sure?" }, :method => :delete, :class => "btn btn-danger")
|
20
|
+
<% end -%>
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
|
4
|
+
require 'sharp_admin/sharp_admin_generator'
|
5
|
+
|
6
|
+
class SharpAdminGeneratorTest < Rails::Generators::TestCase
|
7
|
+
|
8
|
+
destination File.expand_path("../tmp", File.dirname(__FILE__))
|
9
|
+
setup :prepare_destination
|
10
|
+
tests SharpAdminGenerator
|
11
|
+
|
12
|
+
test "create the controllers and specs" do
|
13
|
+
run_generator %w(User --search_by email)
|
14
|
+
|
15
|
+
assert_file "app/controllers/admin/base_controller.rb", /class Admin::BaseController < ApplicationController/
|
16
|
+
assert_file "app/controllers/admin/users_controller.rb", /class Admin::UsersController < Admin::BaseController/
|
17
|
+
assert_file "spec/controllers/admin/base_controller_spec.rb", /describe Admin::BaseController do/
|
18
|
+
assert_file "spec/controllers/admin/users_controller_spec.rb", /describe Admin::UsersController do/
|
19
|
+
assert_file "app/views/admin/users/index.html.haml"
|
20
|
+
assert_file "app/views/admin/users/show.html.haml"
|
21
|
+
assert_file "app/views/admin/users/new.html.haml"
|
22
|
+
assert_file "app/views/admin/users/edit.html.haml"
|
23
|
+
assert_file "app/views/admin/users/_form.html.haml"
|
24
|
+
assert_file "app/helpers/admin/base_helper.rb", /def sortable/
|
25
|
+
end
|
26
|
+
|
27
|
+
test "--no-create option skips assets to create new record" do
|
28
|
+
run_generator %w(User --no_create)
|
29
|
+
|
30
|
+
assert_no_file "app/views/admin/users/new.html.haml"
|
31
|
+
|
32
|
+
content = File.read("tmp/app/views/admin/users/index.html.haml")
|
33
|
+
assert content !~ /Create a new/
|
34
|
+
|
35
|
+
content = File.read("tmp/app/controllers/admin/users_controller.rb")
|
36
|
+
assert content !~ /def new/
|
37
|
+
assert content !~ /def create/
|
38
|
+
|
39
|
+
controller_spec_content = File.read("tmp/spec/controllers/admin/users_controller_spec.rb")
|
40
|
+
assert controller_spec_content !~ /GET new/
|
41
|
+
assert controller_spec_content !~ /POST create/
|
42
|
+
end
|
43
|
+
|
44
|
+
test "--read_only skips create, edit and update" do
|
45
|
+
run_generator %w(User --read_only)
|
46
|
+
|
47
|
+
assert_no_file "app/views/admin/users/new.html.haml"
|
48
|
+
assert_no_file "app/views/admin/users/edit.html.haml"
|
49
|
+
assert_no_file "app/views/admin/users/_form.html.haml"
|
50
|
+
|
51
|
+
index_content = File.read("tmp/app/views/admin/users/index.html.haml")
|
52
|
+
assert index_content !~ /Create a new/
|
53
|
+
assert index_content !~ /Edit/
|
54
|
+
|
55
|
+
controller_content = File.read("tmp/app/controllers/admin/users_controller.rb")
|
56
|
+
assert controller_content !~ /def new/
|
57
|
+
assert controller_content !~ /def create/
|
58
|
+
assert controller_content !~ /def edit/
|
59
|
+
assert controller_content !~ /def update/
|
60
|
+
|
61
|
+
controller_spec_content = File.read("tmp/spec/controllers/admin/users_controller_spec.rb")
|
62
|
+
assert controller_spec_content !~ /GET new/
|
63
|
+
assert controller_spec_content !~ /POST create/
|
64
|
+
assert controller_spec_content !~ /GET edit/
|
65
|
+
assert controller_spec_content !~ /PUT update/
|
66
|
+
end
|
67
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sharp_admin
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- sharp
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-10-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.2.4
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.2.4
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: haml
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: sqlite3
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: generator for your admin.
|
56
|
+
email:
|
57
|
+
- liuqiang_0701@163.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- MIT-LICENSE
|
63
|
+
- README.rdoc
|
64
|
+
- Rakefile
|
65
|
+
- lib/sharp_admin.rb
|
66
|
+
- lib/sharp_admin/sharp_admin_generator.rb
|
67
|
+
- lib/sharp_admin/templates/base_controller.rb
|
68
|
+
- lib/sharp_admin/templates/base_controller_spec.rb
|
69
|
+
- lib/sharp_admin/templates/base_helper.rb
|
70
|
+
- lib/sharp_admin/templates/controller.rb
|
71
|
+
- lib/sharp_admin/templates/controller_spec.rb
|
72
|
+
- lib/sharp_admin/templates/views/_form.html.erb
|
73
|
+
- lib/sharp_admin/templates/views/edit.html.erb
|
74
|
+
- lib/sharp_admin/templates/views/index.html.erb
|
75
|
+
- lib/sharp_admin/templates/views/new.html.erb
|
76
|
+
- lib/sharp_admin/templates/views/show.html.erb
|
77
|
+
- lib/sharp_admin/version.rb
|
78
|
+
- lib/tasks/easy_admin_tasks.rake
|
79
|
+
- test/easy_admin_test.rb
|
80
|
+
- test/test_helper.rb
|
81
|
+
homepage: https://github.com/sharp/sharp_admin
|
82
|
+
licenses:
|
83
|
+
- MIT
|
84
|
+
metadata: {}
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options: []
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
requirements: []
|
100
|
+
rubyforge_project:
|
101
|
+
rubygems_version: 2.2.2
|
102
|
+
signing_key:
|
103
|
+
specification_version: 4
|
104
|
+
summary: generator for your admin.
|
105
|
+
test_files:
|
106
|
+
- test/easy_admin_test.rb
|
107
|
+
- test/test_helper.rb
|