admin_help 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module AdminHelp
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -15,6 +15,15 @@ module Admin
15
15
  class_option :parent_controller, banner: "admin", type: :string, default: "application",
16
16
  desc: "Define the parent controller"
17
17
 
18
+ class_option :orm, banner: "NAME", type: :string, required: true,
19
+ desc: "ORM to generate the controller for"
20
+
21
+ argument :attributes, type: :array, default: [], banner: "field:type field:type"
22
+
23
+ hook_for :resource_route, in: :rails do |resource_route|
24
+ invoke resource_route, [prefixed_class_name]
25
+ end
26
+
18
27
  def create_controller_files
19
28
  # I think there should be a better way to detect if jbuilder is in use
20
29
  # If you know it, please let me know
@@ -22,20 +31,67 @@ module Admin
22
31
  # create_file "app/controllers/a.rb", "# Add initialization content here"
23
32
  end
24
33
 
34
+ def copy_view_files
35
+ available_views.each do |view|
36
+ template_path = "views/erb/#{view}.html.erb.erb"
37
+ template template_path, File.join("app/views", prefix, controller_file_path, "#{view}.html.erb")
38
+ end
39
+ end
40
+
41
+ def create_test_files
42
+ template "tests/test_unit/functional_test.rb.erb", File.join("test/controllers", prefix, controller_class_path, "#{controller_file_name}_controller_test.rb")
43
+ end
44
+
45
+ def attributes_hash
46
+ return if attributes_names.empty?
47
+
48
+ attributes_names.map do |name|
49
+ if %w(password password_confirmation).include?(name) && attributes.any?(&:password_digest?)
50
+ "#{name}: 'secret'"
51
+ else
52
+ "#{name}: @#{singular_table_name}.#{name}"
53
+ end
54
+ end.sort.join(', ')
55
+ end
56
+
25
57
  def prefix
26
58
  options[:prefix_name]
27
59
  end
28
60
 
61
+ def prefixed_controller_class_name
62
+ "#{prefix.camelcase}::#{controller_class_name}"
63
+ end
64
+
65
+ def parent_controller_class_name
66
+ options[:parent_controller].camelcase
67
+ end
68
+
29
69
  def prefixed_class_name
30
70
  "#{prefix.capitalize}::#{class_name}"
31
71
  end
32
72
 
33
- def prefixed_controller_class_name
34
- "#{prefix.capitalize}::#{controller_class_name}"
73
+ def prefixed_route_url
74
+ "/#{prefix}#{route_url}"
35
75
  end
36
76
 
37
- def parent_controller_class_name
38
- options[:parent_controller].capitalize
77
+ def prefixed_plain_model_url
78
+ "#{prefix}_#{singular_table_name}"
79
+ end
80
+
81
+ def prefixed_index_helper
82
+ "#{prefix}_#{index_helper}"
83
+ end
84
+
85
+ def available_views
86
+ %w(index edit show new _form)
87
+ end
88
+
89
+ def plural_table_name_camelcase
90
+ plural_table_name.camelcase
91
+ end
92
+
93
+ def singular_table_name_camelcase
94
+ singular_table_name.camelcase
39
95
  end
40
96
 
41
97
  end
@@ -4,6 +4,65 @@ require_dependency "<%= namespaced_file_path %>/application_controller"
4
4
  <% end -%>
5
5
  <% module_namespacing do -%>
6
6
  class <%= prefixed_controller_class_name %>Controller < <%= parent_controller_class_name %>Controller
7
+ before_action :set_<%= singular_table_name %>, only: [:show, :edit, :update, :destroy]
7
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 :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 :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
8
67
  end
9
68
  <% end -%>
@@ -0,0 +1,50 @@
1
+ require 'test_helper'
2
+
3
+ <% module_namespacing do -%>
4
+ class <%= prefixed_controller_class_name %>ControllerTest < ActionDispatch::IntegrationTest
5
+ setup do
6
+ @<%= singular_table_name %> = <%= table_name %>(:one)
7
+ end
8
+
9
+ test "should get index" do
10
+ get <%= prefixed_index_helper %>_url
11
+ assert_response :success
12
+ end
13
+
14
+ test "should get new" do
15
+ get new_<%= prefixed_plain_model_url %>_url
16
+ assert_response :success
17
+ end
18
+
19
+ test "should create <%= singular_table_name %>" do
20
+ assert_difference('<%= class_name %>.count') do
21
+ post <%= prefixed_index_helper %>_url, params: { <%= "#{singular_table_name}: { #{attributes_hash} }" %> }
22
+ end
23
+
24
+ assert_redirected_to <%= prefixed_plain_model_url %>_url(<%= singular_table_name_camelcase %>.last)
25
+ end
26
+
27
+ test "should show <%= singular_table_name %>" do
28
+ get <%= prefixed_plain_model_url %>_url(@<%= singular_table_name %>)
29
+ assert_response :success
30
+ end
31
+
32
+ test "should get edit" do
33
+ get edit_<%= prefixed_plain_model_url %>_url(@<%= singular_table_name %>)
34
+ assert_response :success
35
+ end
36
+
37
+ test "should update <%= singular_table_name %>" do
38
+ patch <%= prefixed_plain_model_url %>_url(@<%= singular_table_name %>), params: { <%= "#{singular_table_name}: { #{attributes_hash} }" %> }
39
+ assert_redirected_to <%= prefixed_plain_model_url %>_url(<%= singular_table_name_camelcase %>.last)
40
+ end
41
+
42
+ test "should destroy <%= singular_table_name %>" do
43
+ assert_difference('<%= class_name %>.count', -1) do
44
+ delete <%= prefixed_plain_model_url %>_url(@<%= singular_table_name %>)
45
+ end
46
+
47
+ assert_redirected_to <%= prefixed_index_helper %>_url
48
+ end
49
+ end
50
+ <% end -%>
@@ -0,0 +1,38 @@
1
+ <%%= form_with(model: <%= "[:#{prefix}, #{singular_table_name}]" %>, local: true) do |form| %>
2
+ <%% if @<%= singular_table_name %>.errors.any? %>
3
+ <div id="error_explanation">
4
+ <h2><%%= pluralize(@<%= singular_table_name %>.errors.count, "error") %> prohibited this <%= singular_table_name %> from being saved:</h2>
5
+
6
+ <ul>
7
+ <%% @<%= singular_table_name %>.errors.full_messages.each do |message| %>
8
+ <li><%%= message %></li>
9
+ <%% end %>
10
+ </ul>
11
+ </div>
12
+ <%% end %>
13
+
14
+ <% attributes.each do |attribute| -%>
15
+ <div class="field">
16
+ <% if attribute.password_digest? -%>
17
+ <%%= form.label :password %>
18
+ <%%= form.password_field :password %>
19
+ </div>
20
+ <div>
21
+ <%%= form.label :password_confirmation %>
22
+ <%%= form.password_field :password_confirmation %>
23
+ <% else -%>
24
+ <% if attribute.reference? -%>
25
+ <%%= form.label :<%= attribute.column_name %> %>
26
+ <%%= form.<%= attribute.field_type %> :<%= attribute.column_name %> %>
27
+ <% else -%>
28
+ <%%= form.label :<%= attribute.name %> %>
29
+ <%%= form.<%= attribute.field_type %> :<%= attribute.name %> %>
30
+ <% end -%>
31
+ <% end -%>
32
+ </div>
33
+
34
+ <% end -%>
35
+ <div class="actions">
36
+ <%%= form.submit %>
37
+ </div>
38
+ <%% end %>
@@ -0,0 +1,6 @@
1
+ <h1>Editing <%= singular_table_name %></h1>
2
+
3
+ <%%= render 'form', <%= singular_table_name %>: @<%= singular_table_name %> %>
4
+
5
+ <%%= link_to 'Show', <%= "[:#{prefix}, @#{singular_table_name}]" %> %> |
6
+ <%%= link_to 'Back', <%= prefixed_index_helper %>_path %>
@@ -0,0 +1,31 @@
1
+ <p id="notice"><%%= notice %></p>
2
+
3
+ <h1><%= plural_table_name_camelcase %></h1>
4
+
5
+ <table>
6
+ <thead>
7
+ <tr>
8
+ <% attributes.reject(&:password_digest?).each do |attribute| -%>
9
+ <th><%= attribute.human_name %></th>
10
+ <% end -%>
11
+ <th colspan="3"></th>
12
+ </tr>
13
+ </thead>
14
+
15
+ <tbody>
16
+ <%% @<%= plural_table_name %>.each do |<%= singular_table_name %>| %>
17
+ <tr>
18
+ <% attributes.reject(&:password_digest?).each do |attribute| -%>
19
+ <td><%%= <%= singular_table_name %>.<%= attribute.name %> %></td>
20
+ <% end -%>
21
+ <td><%%= link_to 'Show', <%= "[:#{prefix}, #{singular_table_name}]" %> %></td>
22
+ <td><%%= link_to 'Edit', edit_<%= prefixed_plain_model_url %>_path(<%= singular_table_name %>) %></td>
23
+ <td><%%= link_to 'Destroy', <%= "[:#{prefix}, #{singular_table_name}]" %>, method: :delete, data: { confirm: 'Are you sure?' } %></td>
24
+ </tr>
25
+ <%% end %>
26
+ </tbody>
27
+ </table>
28
+
29
+ <br>
30
+
31
+ <%%= link_to 'New <%= human_name %>', new_<%= prefixed_plain_model_url %>_path %>
@@ -0,0 +1,5 @@
1
+ <h1>New <%= singular_table_name_camelcase %></h1>
2
+
3
+ <%%= render 'form', <%= singular_table_name %>: @<%= singular_table_name %> %>
4
+
5
+ <%%= link_to 'Back', <%= prefixed_index_helper %>_path %>
@@ -0,0 +1,11 @@
1
+ <p id="notice"><%%= notice %></p>
2
+
3
+ <% attributes.reject(&:password_digest?).each do |attribute| -%>
4
+ <p>
5
+ <strong><%= attribute.human_name %>:</strong>
6
+ <%%= @<%= singular_table_name %>.<%= attribute.name %> %>
7
+ </p>
8
+
9
+ <% end -%>
10
+ <%%= link_to 'Edit', edit_<%= prefixed_plain_model_url %>_path(@<%= singular_table_name %>) %> |
11
+ <%%= link_to 'Back', <%= prefixed_index_helper %>_path %>
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: admin_help
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - aiyuhang
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-08-06 00:00:00.000000000 Z
11
+ date: 2019-08-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,11 +52,25 @@ files:
52
52
  - README.md
53
53
  - Rakefile
54
54
  - admin_help.gemspec
55
+ - app/assets/fonts/FontAwesome.otf
56
+ - app/assets/fonts/fontawesome-webfont.eot
57
+ - app/assets/fonts/fontawesome-webfont.svg
58
+ - app/assets/fonts/fontawesome-webfont.ttf
59
+ - app/assets/fonts/fontawesome-webfont.woff
60
+ - app/assets/fonts/fontawesome-webfont.woff2
61
+ - app/assets/stylesheets/admin_help.css
62
+ - app/assets/stylesheets/font-awesome.css.erb
55
63
  - lib/admin_help.rb
56
64
  - lib/admin_help/version.rb
57
65
  - lib/generators/admin/scaffold_controller/USAGE
58
66
  - lib/generators/admin/scaffold_controller/scaffold_controller_generator.rb
59
67
  - lib/generators/admin/scaffold_controller/templates/controllers/controller.rb.erb
68
+ - lib/generators/admin/scaffold_controller/templates/tests/test_unit/functional_test.rb.erb
69
+ - lib/generators/admin/scaffold_controller/templates/views/erb/_form.html.erb.erb
70
+ - lib/generators/admin/scaffold_controller/templates/views/erb/edit.html.erb.erb
71
+ - lib/generators/admin/scaffold_controller/templates/views/erb/index.html.erb.erb
72
+ - lib/generators/admin/scaffold_controller/templates/views/erb/new.html.erb.erb
73
+ - lib/generators/admin/scaffold_controller/templates/views/erb/show.html.erb.erb
60
74
  homepage: https://witcan.com
61
75
  licenses:
62
76
  - MIT