admin_views 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Changelog ADDED
@@ -0,0 +1,2 @@
1
+ * 0.1
2
+ - Initial version
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2010 Conor Hunt <conor.hunt@gmail.com>
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ Admin Views
2
+ =================
3
+
4
+ A Rails generator to create admin scaffolding for a model.
5
+
6
+ The generator is pretty opinionated, because I wrote this after I became tired of writing the same
7
+ code over and over. It expects that your admin controllers will reside under app/controllers/admin and
8
+ use namespaced restful routes. It also expects a base admin controller named Admin::AdminController, but
9
+ this is a single edit in the generated controller if you do not use that.
10
+
11
+ Install
12
+ -------
13
+
14
+ gem install admin_views
15
+
16
+ This library uses the will\_paginate and formtastic gems.
17
+
18
+ Usage
19
+ -----
20
+
21
+ Create the model that you need, make sure the db table is generated too. Then:
22
+
23
+ script/generate admin_views User
24
+
25
+ This will generate:
26
+
27
+ app/controllers/admin/users_controller.rb
28
+ app/views/admin/users/_form.html.erb
29
+ app/views/admin/users/_user.html.erb
30
+ app/views/admin/users/edit.html.erb
31
+ app/views/admin/users/index.html.erb
32
+ app/views/admin/users/new.html.erb
33
+ app/views/admin/users/show.html.erb
34
+
35
+ Add the model resource route to your routes.rb. Example:
36
+
37
+ map.namespace :admin do |admin|
38
+ admin.resources :users
39
+ admin.root :controller => 'users'
40
+ end
41
+
42
+ Edit the generated index.html.erb and \_user.html.erb to only display the fields that you need.
43
+ By default they show all fields for the model. Same with the \_form.html.erb and show.html.erb.
44
+
45
+ COPYRIGHT
46
+ ---------
47
+
48
+ Copyright (c) 2010 Conor Hunt <conor.hunt@gmail.com>
49
+ Released under the MIT license
@@ -0,0 +1,31 @@
1
+ require 'active_support/inflector'
2
+
3
+ class AdminViewsGenerator < Rails::Generator::NamedBase
4
+ include ActiveSupport::Inflector
5
+
6
+ attr_reader :model_class, :model_name, :model_plural, :columns
7
+
8
+ def initialize(runtime_args, runtime_options = {})
9
+ super
10
+
11
+ @model_class = name
12
+ @model_name = underscore(@model_class)
13
+ @model_plural = underscore(pluralize(@model_class))
14
+ @columns = Object.const_get(@model_class).columns
15
+ end
16
+
17
+ def manifest
18
+ record do |m|
19
+ m.directory("app/controllers/admin")
20
+ m.template('controllers/controller.rb', "app/controllers/admin/#{model_plural}_controller.rb")
21
+
22
+ m.directory("app/views/admin/#{model_plural}")
23
+ m.template('views/_form.html.erb', "app/views/admin/#{model_plural}/_form.html.erb")
24
+ m.template('views/_model.html.erb', "app/views/admin/#{model_plural}/_#{model_name}.html.erb")
25
+ m.template('views/edit.html.erb', "app/views/admin/#{model_plural}/edit.html.erb")
26
+ m.template('views/index.html.erb', "app/views/admin/#{model_plural}/index.html.erb")
27
+ m.template('views/new.html.erb', "app/views/admin/#{model_plural}/new.html.erb")
28
+ m.template('views/show.html.erb', "app/views/admin/#{model_plural}/show.html.erb")
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,56 @@
1
+ class Admin::<%= pluralize(model_class) %>Controller < Admin::AdminController
2
+ # GET /admin/<%= model_plural %>
3
+ def index
4
+ if params[:search]
5
+ # TODO: By default search all text fields
6
+ @<%= model_plural %> = <%= model_class %>.paginate(:all, :page => params[:page])
7
+ else
8
+ @<%= model_plural %> = <%= model_class %>.paginate(:all, :page => params[:page])
9
+ end
10
+ end
11
+
12
+ # GET /admin/<%= model_plural %>/1
13
+ def show
14
+ @<%= model_name %> = <%= model_class %>.find(params[:id])
15
+ end
16
+
17
+ # GET /admin/<%= model_plural %>/new
18
+ def new
19
+ @<%= model_name %> = <%= model_class %>.new
20
+ end
21
+
22
+ # GET /admin/<%= model_plural %>/1/edit
23
+ def edit
24
+ @<%= model_name %> = <%= model_class %>.find(params[:id])
25
+ end
26
+
27
+ # POST /admin/<%= model_plural %>
28
+ def create
29
+ @<%= model_name %> = <%= model_class %>.new(params[:<%= model_name %>])
30
+
31
+ if @<%= model_name %>.save
32
+ redirect_to(@<%= model_name %>, :notice => '<%= class_name %> was successfully created.')
33
+ else
34
+ render :action => "new"
35
+ end
36
+ end
37
+
38
+ # PUT /admin/<%= model_plural %>/1
39
+ def update
40
+ @<%= model_name %> = <%= model_class %>.find(params[:id])
41
+
42
+ if @<%= model_name %>.update_attributes(params[:<%= model_name %>])
43
+ redirect_to(admin_<%= model_plural %>_url(@<%= model_name %>), :notice => '<%= model_class %> was successfully updated.')
44
+ else
45
+ render :action => "edit"
46
+ end
47
+ end
48
+
49
+ # DELETE /admin/<%= model_plural %>/1
50
+ def destroy
51
+ @<%= model_name %> = <%= model_class %>.find(params[:id])
52
+ @<%= model_name %>.destroy
53
+
54
+ redirect_to(admin_<%= model_plural %>_url)
55
+ end
56
+ end
@@ -0,0 +1,8 @@
1
+ <%% semantic_form_for [:admin, @<%= model_name %>] do |form| %>
2
+ <% # http://github.com/justinfrench/formtastic %>
3
+ <% for col in columns -%>
4
+ <%%= form.input :<%= col.name %> -%>
5
+ <% end -%>
6
+
7
+ <%%= form.buttons :commit %>
8
+ <%% end %>
@@ -0,0 +1,9 @@
1
+ <tr id="<%= model_name %>_<%%= <%= model_name %>.id %>">
2
+ <% for col in columns -%>
3
+ <td><%%= h <%= model_name %>.<%= col.name %> %></td>
4
+ <% end -%>
5
+ <td>
6
+ <%%= link_to "Edit", edit_admin_<%= model_name %>_path(<%= model_name %>) %>
7
+ <%%= link_to "Delete", admin_<%= model_name %>_path(<%= model_name %>), :method => :delete, :confirm => "Are you sure?" %>
8
+ </td>
9
+ </tr>
@@ -0,0 +1,3 @@
1
+ <h2>Edit <%= model_class %></h2>
2
+
3
+ <%%= render :partial => "form" %>
@@ -0,0 +1,20 @@
1
+ <div id="create-link" style="float: right; font-size: 1.2em; font-weight: bold">
2
+ <%%= link_to "Create New <%= model_class %>", new_admin_<%= model_name %>_path %>
3
+ </div>
4
+
5
+ <div id="search">
6
+
7
+ </div>
8
+
9
+ <h2><%= model_class %></h2>
10
+ <table>
11
+ <tr>
12
+ <% for col in columns -%>
13
+ <th><%= col.human_name %></th>
14
+ <% end -%>
15
+ <th></th>
16
+ </tr>
17
+ <%%= render @<%= model_plural %> %>
18
+ </table>
19
+
20
+ <%%= will_paginate @<%= model_plural %> %>
@@ -0,0 +1,3 @@
1
+ <h2>Create New <%= model_class %></h2>
2
+
3
+ <%%= render :partial => "form" %>
@@ -0,0 +1,11 @@
1
+ <h2><%= model_class %> <%%= @<%= model_name %>.id %></h2>
2
+ <div id="create-link" style="float: right; font-size: 1.2em; font-weight: bold">
3
+ <%%= link_to "Show All <%= pluralize(model_class) %>", admin_<%= model_plural %>_path %>
4
+ <%%= link_to "Edit <%= model_class %>", edit_admin_<%= model_name %>_path(@<%= model_name %>) %>
5
+ </div>
6
+
7
+ <table>
8
+ <% for col in columns -%>
9
+ <tr><td><%= col.human_name %></td><td><%%= h @<%= model_name %>.<%= col.name %> %></td></tr>
10
+ <% end -%>
11
+ </table>
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: admin_views
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - Conor Hunt
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-22 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: will_paginate
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.3.12
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: formtastic
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.9.7
34
+ version:
35
+ description: Rails generator to create admin views for models
36
+ email: conor.hunt@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - README.md
45
+ - LICENSE
46
+ - Changelog
47
+ - generators/admin_views/admin_views_generator.rb
48
+ - generators/admin_views/templates/controllers/controller.rb
49
+ - generators/admin_views/templates/views/_form.html.erb
50
+ - generators/admin_views/templates/views/_model.html.erb
51
+ - generators/admin_views/templates/views/edit.html.erb
52
+ - generators/admin_views/templates/views/index.html.erb
53
+ - generators/admin_views/templates/views/new.html.erb
54
+ - generators/admin_views/templates/views/show.html.erb
55
+ has_rdoc: true
56
+ homepage: http://github.com/conorh/admin_views
57
+ licenses: []
58
+
59
+ post_install_message:
60
+ rdoc_options: []
61
+
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: "0"
75
+ version:
76
+ requirements: []
77
+
78
+ rubyforge_project:
79
+ rubygems_version: 1.3.5
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: Rails generator to create admin views for models
83
+ test_files: []
84
+